-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchmod.js
More file actions
134 lines (119 loc) · 3.62 KB
/
chmod.js
File metadata and controls
134 lines (119 loc) · 3.62 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const fs = require('node:fs');
const path = require('node:path');
const fileSuffixList = ['.h', '.hpp', '.c', '.cpp', '.cs'];
function setFilePermissions(filename, isReadOnly)
{
const mode = isReadOnly ? 0o444 : 0o644; // 只读为 444,可读写为 644
fs.chmodSync(filename, mode);
}
function isTargetFile(filename)
{
const index = filename.lastIndexOf('.');
const fileSuffix = filename.slice(index);
return fileSuffixList.includes(fileSuffix);
}
function processDirectory(dirPath, isReadOnly)
{
if (fs.existsSync(dirPath))
{
const list = fs.readdirSync(dirPath, { withFileTypes: true });
list.forEach((dirent) =>
{
const subPath = path.join(dirPath, dirent.name);
if (dirent.isDirectory())
{
processDirectory(subPath, isReadOnly);
}
else if (isTargetFile(dirent.name))
{
setFilePermissions(subPath, isReadOnly);
console.log(`---> Processed file: ${subPath} to ${isReadOnly ? 'Read-Only' : 'Read-Write'}`);
}
});
}
else
{
console.error(`---> Target path does not exist: ${dirPath}`);
}
}
async function confirmAction(dirPath, fileType)
{
return new Promise((resolve) =>
{
process.stdout.write(`Target Path: ${dirPath}\nTarget File Types: [${fileType}]\n`);
process.stdout.write(`Are you sure you want to proceed with changing permissions? (Y/N): `);
process.stdin.once('data', (data) =>
{
const input = data.toString().trim().toUpperCase(); // 转为大写以简化比较
if (input === 'Y')
{
resolve(true);
}
else if (input === 'N')
{
console.log("Operation canceled.");
process.exit(0); // 直接退出程序
}
else
{
console.log("Invalid input. Please enter Y or N.");
confirmAction(dirPath, fileType).then(resolve); // 继续提问
}
});
});
}
function showHelp()
{
console.log
(
`Usage: node chmod.js [options] [path] => node chmod.js -R [path]
Options:
-R Set files to Read-Only.
-RW Set files to Read-Write.
-path Specify the target path to apply permissions. If no path is specified, the current working directory will be used.
-help Show this help message.`
);
}
async function main()
{
const args = process.argv.slice(2);
let isReadOnly = null;
let dirPath = process.cwd(); // 默认当前工作目录
if (args.length === 0)
{
console.log("Please provide an argument: -R for Read-Only, -RW for Read-Write, or specify a path.");
showHelp();
return;
}
if (args[0] === '-R')
{
isReadOnly = true;
}
else if (args[0] === '-RW')
{
isReadOnly = false;
}
else if (args[0] === '-help')
{
showHelp();
return;
}
else
{
console.log("Invalid argument. Use -R for Read-Only, -RW for Read-Write, or -help for help.");
return;
}
if (args[1])
{
dirPath = args[1];
}
const confirmed = await confirmAction(dirPath, fileSuffixList.join(', '));
if (!confirmed)
{
return; // 已在 confirmAction 中处理退出
}
processDirectory(dirPath, isReadOnly);
console.log("---> Operation completed!");
process.exit(0);
}
main();