Skip to content

Commit

Permalink
init project.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Mar 9, 2023
0 parents commit 94c6331
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
23 changes: 23 additions & 0 deletions .github/workflows/main.yml
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
17 changes: 17 additions & 0 deletions .gitignore
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
*.*~
~*.*
57 changes: 57 additions & 0 deletions README.md
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.
22 changes: 22 additions & 0 deletions chmod.js
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);
});
}
61 changes: 61 additions & 0 deletions cli.js
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.`)
})
});
47 changes: 47 additions & 0 deletions package.json
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"
}
}
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import assert from 'node:assert';
Empty file added test/test.txt
Empty file.

0 comments on commit 94c6331

Please sign in to comment.