-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 94c6331
Showing
9 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: CI | ||
on: | ||
- push | ||
- pull_request | ||
|
||
jobs: | ||
test: | ||
name: Node.js ${{ matrix.node-version }} | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
node-version: | ||
- 18 | ||
- 16 | ||
- 14 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
|
||
- run: npm install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
node_modules | ||
npm-debug.log* | ||
lerna-debug.log* | ||
package-lock.json | ||
yarn.lock | ||
.eslintcache | ||
.DS_Store | ||
.cache | ||
.vscode | ||
.npmrc | ||
|
||
*.bak | ||
*.tem | ||
*.temp | ||
#.swp | ||
*.*~ | ||
~*.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
chmod-cli | ||
=== | ||
|
||
A simple command line tool for changing file permissions | ||
|
||
## Installation | ||
|
||
```shell | ||
$ npm install chmod-cli | ||
# Or | ||
$ npm install --global chmod-cli | ||
``` | ||
|
||
## Usage | ||
|
||
```bash | ||
$ chmod --help | ||
|
||
A simple Node.js module for changing file permissions. | ||
|
||
Usage: $ chmod <path> … | ||
|
||
Options: | ||
--mode, -m The new permissions for the file or directory. | ||
This can be a numeric mode (e.g. 666), | ||
or a string mode (e.g. 'rwxr-xr-x') | ||
|
||
Examples: | ||
|
||
$ chmod test.js xxx.js -m 777 | ||
$ chmod test.js -m 777 | ||
$ chmod-cli test.js -m 777 | ||
|
||
``` | ||
|
||
## API | ||
|
||
`chmod(path, mode, callback)` | ||
|
||
Changes the permissions of the file or directory at the specified `path`. | ||
|
||
- `path` (**string**): The path to the file or directory. | ||
- `mode` (**string** or **number**): The new permissions for the file or directory. This can be a numeric mode (e.g. 666), or a string mode (e.g. 'rwxr-xr-x'). | ||
- `callback` (**function**): A callback function to call when the operation completes. The callback should take one argument, an error object, which will be null if the operation completes successfully. | ||
|
||
```javascript | ||
import chmod from 'chmod-cli'; | ||
|
||
chmod('./test.txt', '666', (err) => { | ||
if (err) throw err; | ||
console.log('File permissions have been changed.'); | ||
}); | ||
``` | ||
|
||
## License | ||
|
||
This package is licensed under the MIT License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import fs from 'node:fs'; | ||
|
||
export default function chmod(path, mode, callback) { | ||
// 检查参数 | ||
if (typeof path !== 'string') { | ||
throw new TypeError('path must be a string'); | ||
} | ||
if (typeof mode !== 'string' && typeof mode !== 'number') { | ||
throw new TypeError('mode must be a string or number'); | ||
} | ||
if (typeof callback !== 'function') { | ||
throw new TypeError('callback must be a function'); | ||
} | ||
|
||
fs.chmod(path, mode, (err) => { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
callback(null); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env node | ||
import meow from 'meow'; | ||
import chmod from './chmod.js' | ||
|
||
const cli = meow(` | ||
\x1b[37;1mUsage:\x1b[0m $\x1b[32;1m chmod\x1b[0m <path> … | ||
\x1b[37;1mOptions:\x1b[0m | ||
--version, -v Show version number | ||
--help, -h Displays help information. | ||
--mode, -m The new permissions for the file or directory. | ||
This can be a numeric mode (e.g. 666), | ||
or a string mode (e.g. 'rwxr-xr-x') | ||
\x1b[37;1mExamples:\x1b[0m | ||
$\x1b[34;1m chmod\x1b[0m test.js xxx.js -m 777 | ||
$\x1b[34;1m chmod\x1b[0m test.js -m 777 | ||
$\x1b[34;1m chmod-cli\x1b[0m test.js -m 777 | ||
`, { | ||
importMeta: import.meta, | ||
flags: { | ||
mode: { | ||
type: 'number', | ||
alias: 'm', | ||
}, | ||
}, | ||
}); | ||
|
||
if (cli.flags.h || cli.flags.help) { | ||
cli.showHelp() | ||
process.exitCode = 0; | ||
} | ||
|
||
if (cli.input.length === 0) { | ||
console.error('\n \x1b[31;1m Specify at least one path\x1b[0m\n'); | ||
process.exitCode = 1; | ||
} | ||
|
||
if (!cli.flags.mode) { | ||
console.error('\n \x1b[31;1m mode must be a string or number\x1b[0m\n'); | ||
process.exitCode = 1; | ||
} | ||
|
||
if (cli.flags.v || cli.flags.version) { | ||
cli.showVersion() | ||
process.exitCode = 0; | ||
} | ||
|
||
cli.input.forEach((pathName) => { | ||
chmod(pathName, cli.flags.mode, (err) => { | ||
if (err) { | ||
console.error(`\n \x1b[31;1m ${err.message}\x1b[0m\n`); | ||
process.exitCode = 1; | ||
return; | ||
} | ||
console.log(` \x1b[32;1m👉 ${pathName} \x1b[0m File permissions have been changed.`) | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "chmod-cli", | ||
"version": "1.0.0", | ||
"description": "A simple command line tool for changing file permissions.", | ||
"type": "module", | ||
"license": "MIT", | ||
"main": "./chmod.js", | ||
"bin": { | ||
"chmod": "./cli.js", | ||
"chmod-cli": "./cli.js" | ||
}, | ||
"author": "kenny wong <wowohoo@qq.com>", | ||
"homepage": "http://jaywcjlove.github.io/chmod-cli", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/jaywcjlove/chmod-cli.git" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"files": [ | ||
"cli.js", | ||
"chmod.js" | ||
], | ||
"keywords": [ | ||
"cli-app", | ||
"cli", | ||
"chmod", | ||
"chmod-cli", | ||
"permissions", | ||
"file", | ||
"directory", | ||
"string", | ||
"unix", | ||
"rwx", | ||
"read", | ||
"write", | ||
"execute", | ||
"linux" | ||
], | ||
"engines": { | ||
"node": ">=14.16" | ||
}, | ||
"dependencies": { | ||
"meow": "^11.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import assert from 'node:assert'; |
Empty file.