Skip to content

Commit

Permalink
fix: use execa instead of runscript (#19)
Browse files Browse the repository at this point in the history
* fix: use execa instead of runscript

* fix: run scripts

---------

Co-authored-by: tianding.wk <[email protected]>
  • Loading branch information
gemwuu and tianding.wk authored Jul 6, 2023
1 parent 2e26387 commit 3345f62
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 22 deletions.
6 changes: 3 additions & 3 deletions integration/index.2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require('node:path');
const assert = require('node:assert');
const coffee = require('coffee');
const semver = require('semver');
const runscript = require('runscript');
const execa = require('execa');
const rapid = path.join(__dirname, '../node_modules/.bin/rapid');
const {
clean,
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('test/index.v2.test.js', () => {
.expect('code', 0)
.end();
await assert.doesNotReject(fs.stat(path.join(cwd, 'node_modules/canvas/package.json')));
const { stdout } = await runscript('mount', { stdio: 'pipe' });
const { stdout } = await execa.command('mount', { stdio: 'pipe' });
assert(stdout.indexOf(cwd) > 0);
assert(require(path.join(cwd, 'node_modules/canvas/package.json')).binary.host === 'https://cdn.npmmirror.com/binaries/canvas');
});
Expand All @@ -102,7 +102,7 @@ describe('test/index.v2.test.js', () => {
.end();

await assert.doesNotReject(fs.stat(path.join(cwd, 'node_modules/react-jsx-parser/package.json')));
const { stdout } = await runscript('mount', { stdio: 'pipe' });
const { stdout } = await execa.command('mount', { stdio: 'pipe' });
assert(stdout.indexOf(cwd) > 0);
assert(require(path.join(cwd, 'node_modules/react-jsx-parser/package.json')).version === '1.29.0');
});
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"nock": "^13.0.9",
"nyc": "^15.1.0",
"power-assert": "^1.6.1",
"runscript": "^1.5.1",
"test-exclude": "^6.0.0"
},
"homepage": "https://github.com/cnpm/rapid",
Expand All @@ -54,4 +53,4 @@
"engines": {
"node": ">=14.19.1"
}
}
}
2 changes: 1 addition & 1 deletion packages/cli/lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const util = require('node:util');

class NotSupportedError extends Error {
constructor(message) {
super(util.format('Rapid mode not supported on Current OS(%s)%s', os.type()), message ? `, ${message}` : '');
super(util.format('Rapid mode not supported on Current OS(%s)', os.type()), message ? `, ${message}` : '');
this.name = 'NOT_SUPPORTED';
this.stack = '';
}
Expand Down
14 changes: 7 additions & 7 deletions packages/cli/lib/nydusd/fuse_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const fs = require('node:fs/promises');
const os = require('node:os');
const path = require('node:path');
const runscript = require('runscript');
const execa = require('execa');
const {
tarBucketsDir,
unionfs,
Expand Down Expand Up @@ -35,7 +35,7 @@ async function generateBootstrapFile(cwd, pkg) {
await Promise.all(allPkgs.map(async pkgPath => {
const { bootstrap, tarIndex } = await getWorkdir(cwd, pkgPath);
await fs.mkdir(path.dirname(bootstrap), { recursive: true });
await runscript(`${BOOTSTRAP_BIN} --stargz-config-path=${tarIndex} --stargz-dir=${tarBucketsDir} --bootstrap=${bootstrap}`);
await execa.command(`${BOOTSTRAP_BIN} --stargz-config-path=${tarIndex} --stargz-dir=${tarBucketsDir} --bootstrap=${bootstrap}`);
}));
console.timeEnd('[rapid] generate bootstrap');
}
Expand Down Expand Up @@ -65,9 +65,9 @@ async function mountOverlay(cwd, pkg) {
await fs.mkdir(overlay, { recursive: true });
await fs.mkdir(mnt, { recursive: true });
if (os.type() === 'Linux') {
await runscript(wrapSudo(`mount -t tmpfs tmpfs ${overlay}`));
await execa.command(wrapSudo(`mount -t tmpfs tmpfs ${overlay}`));
} else if (os.type() === 'Darwin') {
await runscript(wrapSudo(`mount_tmpfs -o union -e ${overlay}`));
await execa.command(wrapSudo(`mount_tmpfs -o union -e ${overlay}`));
}
await fs.mkdir(upper, { recursive: true });
await fs.mkdir(workdir, { recursive: true });
Expand All @@ -85,7 +85,7 @@ ${upper}=RW:${mnt}=RO \
${nodeModulesDir}`;
}
console.info('[rapid] mountOverlay: `%s`', shScript);
await runscript(shScript);
await execa.command(shScript);
console.info('[rapid] overlay mounted.');
}));
}
Expand All @@ -100,8 +100,8 @@ async function endNydusFs(cwd, pkg) {
baseDir,
nodeModulesDir,
} = await getWorkdir(cwd, pkgPath);
await runscript(wrapSudo(`umount ${nodeModulesDir}`));
await runscript(wrapSudo(`umount ${overlay}`));
await execa.command(wrapSudo(`umount ${nodeModulesDir}`));
await execa.command(wrapSudo(`umount ${overlay}`));
await nydusdApi.umount(`/${dirname}`);
// 清除 nydus 相关目录
await fs.rm(baseDir, { recursive: true, force: true });
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/lib/nydusd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const assert = require('node:assert');
const path = require('node:path');
const fs = require('node:fs/promises');
const debug = require('node:util').debuglog('nydusd');
const runscript = require('runscript');
const execa = require('execa');
const util = require('../util');
const fuseMode = require('./fuse_mode');

Expand Down Expand Up @@ -64,7 +64,7 @@ exports.getNydusInstallMode = async function(cwd) {
} catch (_) {
return null;
}
const stdio = await runscript('mount', {
const stdio = await execa.command('mount', {
stdio: 'pipe',
});
const stdout = stdio.stdout.toString();
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/lib/nydusd/nydusd_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const debug = require('node:util').debuglog('rapid:nydusd_api');
const fs = require('node:fs/promises');
const urllib = require('urllib');
const execa = require('execa');
const runscript = require('runscript');
const awaitEvent = require('await-event');
const util = require('../util');

Expand Down Expand Up @@ -124,7 +123,7 @@ async function checkDaemon() {
debug('mount error: ', error);
// linux 下需要用 sudo 启动,如果没有权限,这里
if (error.code === 'EACCES' && process.platform === 'linux') {
await runscript(wrapSudo(`chmod 777 ${socketPath}`));
await execa.command(wrapSudo(`chmod 777 ${socketPath}`));
}
if (Date.now() - startTime <= maxWaitDuration) {
await util.sleep(100);
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {
NotSupportedError,
FuseDeviceError,
} = require('./error');
const runscript = require('runscript');
const execa = require('execa');
const normalize = require('npm-normalize-package-bin');
const {
tarBucketsDir,
Expand Down Expand Up @@ -80,7 +80,7 @@ async function shouldFuseSupport() {
console.info(`[rapid] detect /dev/fuse: ${sh}`);

try {
await runscript(sh, {
await execa.command(sh, {
stdio: 'ignore',
});
} catch (e) {
Expand Down Expand Up @@ -479,10 +479,11 @@ exports.runScript = async (pkgDir, script, options) => {
}

try {
const res = await runscript(script, {
const res = await execa.command(script, {
cwd: pkgDir,
env,
stdio: 'inherit',
shell: true,
});
return res.stdout;
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/test/download_dependency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const path = require('node:path');
const assert = require('node:assert');
const fs = require('node:fs/promises');
const runscript = require('runscript');
const execa = require('execa');
const httpclient = require('../lib/httpclient');
const Scripts = require('../lib/scripts').Scripts;
const mm = require('mm');
Expand All @@ -29,7 +29,7 @@ describe('test/download_dependency.test.js', () => {

afterEach(async () => {
mm.restore();
await runscript(`rm -f ${tarBucketsDir}/*`);
await execa.command(`rm -f ${tarBucketsDir}/*`);
});

it('should work', async () => {
Expand Down

0 comments on commit 3345f62

Please sign in to comment.