-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
f37f194
commit 4fde14b
Showing
6 changed files
with
91 additions
and
98 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
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 |
---|---|---|
@@ -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}); | ||
} |
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
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
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
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 |
---|---|---|
@@ -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); | ||
}); |