Skip to content

Commit

Permalink
Require Node.js 18 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 31, 2023
1 parent f37f194 commit 4fde14b
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 98 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 20
- 18
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
73 changes: 32 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,43 @@
'use strict';
const through = require('through2');
const uidNumber = require('uid-number');
const PluginError = require('plugin-error');
import process from 'node:process';
import uidNumber_ from 'uid-number';
import {gulpPlugin} from 'gulp-plugin-extras';
import pify from 'pify';

const defaultMode = 511 & (~process.umask()); // 511 = 0777
const uidNumber = pify(uidNumber_, {multiArgs: true});

const defaultMode = 0o777 & (~process.umask()); // eslint-disable-line no-bitwise
const uidCache = {};
const gidCache = {};

module.exports = (user, group) => {
let firstFile = true;
let finalUid = typeof uidCache[user] === 'number' ? uidCache[user] : (typeof user === 'number' ? user : null);
let finalGid = typeof gidCache[group] === 'number' ? gidCache[group] : (typeof group === 'number' ? group : null);

return through.obj((file, encoding, callback) => {
if (file.isNull() && !file.isDirectory()) {
callback(null, file);
return;
}
export default function gulpChown(user, group) {
let isFirstFile = true;
let finalUid = typeof uidCache[user] === 'number' ? uidCache[user] : (typeof user === 'number' ? user : undefined);
let finalGid = typeof gidCache[group] === 'number' ? gidCache[group] : (typeof group === 'number' ? group : undefined);

file.stat = file.stat || {};
file.stat.mode = file.stat.mode || defaultMode;
return gulpPlugin('gulp-chown', async file => {
file.stat = file.stat ?? {};
file.stat.mode = file.stat.mode ?? defaultMode;

function finish() {
file.stat.uid = finalUid === null ? file.stat.uid : finalUid;
file.stat.gid = finalGid === null ? file.stat.gid : finalGid;
callback(null, file);
}
if (isFirstFile && typeof user === 'string' && finalUid === undefined && finalGid === undefined) {
let result;
try {
result = await uidNumber(user, group);
} catch (error) {
throw error[0];
}

if (firstFile && typeof user === 'string' && finalUid === null && finalGid === null) {
uidNumber(user, group, (error, uid, gid) => {
if (error) {
callback(new PluginError('gulp-chown', error, {fileName: file.path}));
return;
}
finalUid = result.uid;
uidCache[user] = finalUid;

finalUid = uid;
uidCache[user] = finalUid;
finalGid = result.gid;
gidCache[group] = finalGid;

finalGid = gid;
gidCache[group] = finalGid;

finish();
});

firstFile = false;
return;
isFirstFile = false;
}

finish();
});
};
file.stat.uid = finalUid ?? file.stat.uid;
file.stat.gid = finalGid ?? file.stat.gid;

return file;
}, {supportsDirectories: true});
}
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
"description": "Change owner of Vinyl files",
"license": "MIT",
"repository": "sindresorhus/gulp-chown",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=18"
},
"scripts": {
"test": "xo && mocha"
"test": "xo && ava"
},
"files": [
"index.js"
Expand All @@ -30,14 +33,15 @@
"ownership"
],
"dependencies": {
"plugin-error": "^1.0.1",
"through2": "^3.0.1",
"gulp-plugin-extras": "^0.2.0",
"pify": "^6.1.0",
"uid-number": "0.0.6"
},
"devDependencies": {
"mocha": "^6.2.0",
"vinyl": "^2.1.0",
"xo": "^0.24.0"
"ava": "^5.3.1",
"p-event": "^6.0.0",
"vinyl": "^3.0.0",
"xo": "^0.56.0"
},
"peerDependencies": {
"gulp": ">=4"
Expand Down
33 changes: 14 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@

> [Change owner](https://en.wikipedia.org/wiki/Chown) of [Vinyl](https://github.com/gulpjs/vinyl) files

## Install

```sh
npm install --save-dev gulp-chown
```
$ npm install --save-dev gulp-chown
```


## Usage

```js
const gulp = require('gulp');
const chown = require('gulp-chown');
import gulp from 'gulp';
import chown from 'gulp-chown';

exports.default = () => (
export default () => (
gulp.src('src/app.js')
.pipe(chown('sindresorhus'))
.pipe(gulp.dest('dist'))
Expand All @@ -26,26 +24,25 @@ exports.default = () => (
or

```js
const gulp = require('gulp');
const chown = require('gulp-chown');
import gulp from 'gulp';
import chown from 'gulp-chown';

exports.default = () => (
export default () => (
gulp.src('src/app.js')
.pipe(chown(501))
.pipe(gulp.dest('dist'))
);
```


## API

### chown(userId, groupId)

Arguments must be of the same type.
The arguments must be of the same type.

#### userId

*Required*
*Required*\
Type: `string | number`

The user name or [user id](https://en.wikipedia.org/wiki/User_identifier) to change ownership to.
Expand All @@ -56,19 +53,18 @@ Type: `string | number`

The group name or [group id](https://en.wikipedia.org/wiki/Group_identifier) to change ownership to.


## Tip

Combine it with [gulp-filter](https://github.com/sindresorhus/gulp-filter) to only change ownership of a subset of the files.

```js
const gulp = require('gulp');
const gFilter = require('gulp-filter');
const chown = require('gulp-chown');
import gulp from 'gulp';
import chown from 'gulp-chown';
import gFilter from 'gulp-filter';

const filter = gFilter('src/vendor-*.js');

exports.default = () => (
export default () => (
gulp.src('src/*.js')
// Filter a subset of the files
.pipe(filter)
Expand All @@ -80,7 +76,6 @@ exports.default = () => (
);
```


## Related

- [gulp-chmod](https://github.com/sindresorhus/gulp-chmod) - Change permissions of Vinyl files
51 changes: 28 additions & 23 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
/* eslint-env mocha */
'use strict';
const assert = require('assert');
const Vinyl = require('vinyl');
const chown = require('.');
import process from 'node:process';
import {Buffer} from 'node:buffer';
import test from 'ava';
import Vinyl from 'vinyl';
import {pEvent} from 'p-event';
import chown from './index.js';

it('chown files', cb => {
test('chown files', async t => {
const stream = chown(501, 20);

stream.on('data', file => {
assert.strictEqual(file.stat.uid, 501);
assert.strictEqual(file.stat.gid, 20);
cb();
});

stream.end(new Vinyl({
stat: {
uid: 400,
gid: 10
gid: 10,
},
contents: Buffer.from('')
contents: Buffer.from(''),
}));

const file = await pEvent(stream, 'data');
t.is(file.stat.uid, 501);
t.is(file.stat.gid, 20);
});

it('chown files using a username', cb => {
const stream = chown(process.env.TRAVIS ? 'travis' : 'root');
test.failing('chown files using a username', async t => {
if ('CI' in process.env) {
t.pass();
return;
}

stream.on('data', file => {
assert.strictEqual(file.stat.uid, process.env.TRAVIS ? 2000 : 0);
assert.strictEqual(file.stat.gid, process.env.TRAVIS ? 2000 : 0);
cb();
});
const username = 'root';
const expectedUid = 0;
const expectedGid = 0;

const stream = chown(username);
stream.end(new Vinyl({
stat: {
uid: 400,
gid: 10
gid: 10,
},
contents: Buffer.from('')
contents: Buffer.from(''),
}));

const file = await pEvent(stream, 'data');
t.is(file.stat.uid, expectedUid);
t.is(file.stat.gid, expectedGid);
});

0 comments on commit 4fde14b

Please sign in to comment.