Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kaelzhang/fs-chmod

A drop-in replacement of `fs.chmod` with `+x` support.
https://github.com/kaelzhang/fs-chmod

chmod

Last synced: 17 days ago
JSON representation

A drop-in replacement of `fs.chmod` with `+x` support.

Awesome Lists containing this project

README

        

[![Build Status](https://travis-ci.org/kaelzhang/fs-chmod.svg?branch=master)](https://travis-ci.org/kaelzhang/fs-chmod)
[![Coverage](https://codecov.io/gh/kaelzhang/fs-chmod/branch/master/graph/badge.svg)](https://codecov.io/gh/kaelzhang/fs-chmod)

# fs-chmod

A drop-in replacement of [`fs.chmod`][chmod] with `+x` support.

- supports **finer-grained [symbolic modes]**, such as `+x`, `ug+rw`, and etc.
- supports **mode object** to make the mode better described.

## Install

```sh
$ npm i fs-chmod
```

## Usage

```js
const {
chmod,
chmodSync,
parse
} = require('fs-chmod')

chmod('/path/to/file.js', '+x').then(() => {
console.log('done')
})

chmodSync.sync('/path/to/file.js', 'a+x')
```

### chmod(path, mode): Promise
### chmod(path, mode, callback): void
### chmodSync(path, mode): void

- **path** `string | Buffer | URL` the same as vanilla [`fs.chmod`][chmod]
- **mode** `integer | Mode | string`
- **callback** `Function(error?)`

Changes the permissions of a file.

### parse(str): Mode

- **str** `string` Symbolic notation string of file system permissions

Parses the symbolic notation string, such as `+x`, `ug+rwx` into an object of the interface `Mode`(see below)

```js
const mode = parse('u+x')

console.log(mode.owner.execute) // true
console.log(mode.owner.read) // false
console.log(mode.group) // undefined
```

## mode

### mode `integer`

The same as the the second parameter of vanilla [`fs.chmod`][chmod].

**PAY ATTENTION** that `mode` should be an **octal** number.

```js
chmodSync('/path/to/file', 0o777) // ✅ Correct~
chmodSync('/path/to/file', 777) // ❌ WRONG!
```

### mode `object`

```ts
interface Permission {
read?: boolean
write?: boolean
execute?: boolean
}

interface Mode {
owner?: Permission
group?: Permission
others?: Permission
setuid?: boolean
setgid?: boolean
sticky?: boolean
}
```

For details, see [symbolic modes]

```sh
# bash
chmod ug+rst /path/to/file
```

is equivalent to

```js
chmodSync('path/to/file', 'ug+rst')

// or
chmodSync('path/to/file', {
owner: {
read: true
},
group: {
read: true
},
setuid: true,
setgid: true,
sticky: true
})
```

### mode `string`

```
[references][operator][modes]
```

- Supported references: `u`, `g`, `o`, `a`
- Supported operators: `+`, `=`, `-`
- Supported modes:
- `r`, `w`, `x`

- `s`: setuid/setgid
- `t`: sticky

## License

[MIT](LICENSE)

[chmod]: https://nodejs.org/dist/latest/docs/api/fs.html#fs_fs_chmod_path_mode_callback
[symbolic modes]: https://en.wikipedia.org/wiki/Chmod#Symbolic_modes