From 3345f62c65e308f5dbff8754d19a48b4485687d4 Mon Sep 17 00:00:00 2001 From: Ke Wu Date: Thu, 6 Jul 2023 19:26:21 +0800 Subject: [PATCH] fix: use execa instead of runscript (#19) * fix: use execa instead of runscript * fix: run scripts --------- Co-authored-by: tianding.wk --- integration/index.2.test.js | 6 +++--- package.json | 3 +-- packages/cli/lib/error.js | 2 +- packages/cli/lib/nydusd/fuse_mode.js | 14 +++++++------- packages/cli/lib/nydusd/index.js | 4 ++-- packages/cli/lib/nydusd/nydusd_api.js | 3 +-- packages/cli/lib/util.js | 7 ++++--- packages/cli/test/download_dependency.test.js | 4 ++-- 8 files changed, 21 insertions(+), 22 deletions(-) diff --git a/integration/index.2.test.js b/integration/index.2.test.js index 2630d27..ded169d 100644 --- a/integration/index.2.test.js +++ b/integration/index.2.test.js @@ -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, @@ -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'); }); @@ -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'); }); diff --git a/package.json b/package.json index 299b0d1..166102e 100644 --- a/package.json +++ b/package.json @@ -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", @@ -54,4 +53,4 @@ "engines": { "node": ">=14.19.1" } -} +} \ No newline at end of file diff --git a/packages/cli/lib/error.js b/packages/cli/lib/error.js index 99e409f..7d740ec 100644 --- a/packages/cli/lib/error.js +++ b/packages/cli/lib/error.js @@ -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 = ''; } diff --git a/packages/cli/lib/nydusd/fuse_mode.js b/packages/cli/lib/nydusd/fuse_mode.js index c18279c..365dd8f 100644 --- a/packages/cli/lib/nydusd/fuse_mode.js +++ b/packages/cli/lib/nydusd/fuse_mode.js @@ -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, @@ -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'); } @@ -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 }); @@ -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.'); })); } @@ -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 }); diff --git a/packages/cli/lib/nydusd/index.js b/packages/cli/lib/nydusd/index.js index db37828..8b6d06b 100644 --- a/packages/cli/lib/nydusd/index.js +++ b/packages/cli/lib/nydusd/index.js @@ -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'); @@ -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(); diff --git a/packages/cli/lib/nydusd/nydusd_api.js b/packages/cli/lib/nydusd/nydusd_api.js index 1426843..b8f9169 100644 --- a/packages/cli/lib/nydusd/nydusd_api.js +++ b/packages/cli/lib/nydusd/nydusd_api.js @@ -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'); @@ -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); diff --git a/packages/cli/lib/util.js b/packages/cli/lib/util.js index 5f36f50..81793d6 100644 --- a/packages/cli/lib/util.js +++ b/packages/cli/lib/util.js @@ -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, @@ -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) { @@ -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) { diff --git a/packages/cli/test/download_dependency.test.js b/packages/cli/test/download_dependency.test.js index f03bd28..d940677 100644 --- a/packages/cli/test/download_dependency.test.js +++ b/packages/cli/test/download_dependency.test.js @@ -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'); @@ -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 () => {