-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmini-ls.js
More file actions
87 lines (74 loc) · 3 KB
/
mini-ls.js
File metadata and controls
87 lines (74 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const fs = require('fs');
const path = require('path');
// Function to print usage information
const printUsage = () => {
console.log('Usage: ./mini-ls [-r] [FILE...]');
process.exit(1);
};
// Function to get file permissions in a human-readable format
const getPermissions = (stats) => {
const mode = stats.mode;
const isDirectory = stats.isDirectory() ? 'd' : '-';
const ownerRead = mode & fs.constants.S_IRUSR ? 'r' : '-';
const ownerWrite = mode & fs.constants.S_IWUSR ? 'w' : '-';
const ownerExecute = mode & fs.constants.S_IXUSR ? 'x' : '-';
const groupRead = mode & fs.constants.S_IRGRP ? 'r' : '-';
const groupWrite = mode & fs.constants.S_IWGRP ? 'w' : '-';
const groupExecute = mode & fs.constants.S_IXGRP ? 'x' : '-';
const othersRead = mode & fs.constants.S_IROTH ? 'r' : '-';
const othersWrite = mode & fs.constants.S_IWOTH ? 'w' : '-';
const othersExecute = mode & fs.constants.S_IXOTH ? 'x' : '-';
return `${isDirectory}${ownerRead}${ownerWrite}${ownerExecute}${groupRead}${groupWrite}${groupExecute}${othersRead}${othersWrite}${othersExecute}`;
};
// Function to get the owner ID
const getOwnerId = (stats) => {
return stats.uid; // Returns the user ID of the file owner
};
// Function to format the modified time
const getModifiedTime = (stats) => {
return stats.mtime.toLocaleString(); // Formats modification time
};
// Function to list information about a single file or directory
const listEntry = async (filePath, config) => {
const stats = await fs.promises.stat(filePath);
const ownerId = getOwnerId(stats);
const permissions = getPermissions(stats);
const modifiedTime = getModifiedTime(stats);
console.log(`${permissions} ${ownerId} ${modifiedTime} ${filePath}`);
// If it's a directory and recursive option is set, list its contents
if (stats.isDirectory() && config.recursive) {
await listDir(filePath, config);
}
};
// Function to read and list all entries in a directory
const listDir = async (dirPath, config) => {
const files = await fs.promises.readdir(dirPath);
for (const file of files) {
const filePath = path.join(dirPath, file);
await listEntry(filePath, config); // List each entry in the directory
}
};
// Main function to execute mini-ls
const miniLs = async () => {
const args = process.argv.slice(2);
const config = { recursive: false, files: [] };
// Parse command-line arguments
for (let i = 0; i < args.length; i++) {
if (args[i] === '-r') {
config.recursive = true; // Enable recursive option
} else {
config.files.push(args[i]); // Add file paths to the list
}
}
// If no files are specified, list the current directory
if (config.files.length === 0) {
await listDir('.', config);
} else {
// List each specified file or directory
for (const file of config.files) {
await listEntry(file, config);
}
}
};
// Execute the main function
miniLs();