Skip to content

Commit 5a49026

Browse files
authored
Merge pull request #3131 from skoeva/use-execfile-2
app: frontend: plugins: Modify exec and run commands
2 parents 4d05387 + d1c05bf commit 5a49026

File tree

4 files changed

+42
-31
lines changed

4 files changed

+42
-31
lines changed

app/windows/codesign.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { exec } = require('child_process');
1+
const { execFile } = require('child_process');
22
const path = require('path');
33
const os = require('os');
44
const fs = require('fs');
@@ -145,8 +145,9 @@ function sign(esrpTool, pathToSign) {
145145
const authJson = path.resolve(os.tmpdir(), 'Auth.json');
146146
fs.writeFileSync(authJson, JSON.stringify(AUTH_JSON, undefined, 2));
147147

148-
exec(
149-
`${esrpTool} Sign -a ${authJson} -p ${policyJson} -i ${signInputJson}`,
148+
execFile(
149+
esrpTool,
150+
['Sign', '-a', authJson, '-p', policyJson, '-i', signInputJson],
150151
(error, stdout, stderr) => {
151152
if (error) {
152153
console.log(`error: ${error.message}`);

frontend/src/make-env.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const os = require('os');
22
const path = require('path');
3-
const { execSync } = require('child_process');
3+
const { execFileSync } = require('child_process');
44
const { readFileSync } = require('fs');
55

66
const envFile = path.join(os.tmpdir(), 'tmpEnv');
77

88
test('Create & verify', () => {
99
const execFile = path.resolve(path.join(__dirname, '..', 'make-env.js'));
10-
execSync(`node ${execFile} ${envFile}`);
10+
execFileSync('node', [execFile, envFile]);
1111

1212
const envContents = readFileSync(envFile).toString();
1313

plugins/headlamp-plugin/bin/headlamp-plugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,10 @@ function runScriptOnPackages(packageFolder, scriptName, cmdLine, env) {
549549

550550
console.log(`"${folder}": ${scriptName}-ing, :${cmdLineToUse}:...`);
551551

552+
const [cmd, ...args] = cmdLineToUse.split(' ');
553+
552554
try {
553-
child_process.execSync(cmdLineToUse, {
555+
child_process.execFileSync(cmd, args, {
554556
stdio: 'inherit',
555557
encoding: 'utf8',
556558
env: { ...process.env, ...(env || {}) },

plugins/headlamp-plugin/test-headlamp-plugin.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,69 +13,69 @@ function testHeadlampPlugin() {
1313
cleanup();
1414

1515
// Make a package file of headlamp-plugin we can test
16-
run('npm install');
17-
run('npm run build');
18-
run('npm pack');
16+
run('npm', ['install']);
17+
run('npm', ['run', 'build']);
18+
run('npm', ['pack']);
1919

2020
const packedFile = fs
2121
.readdirSync('.')
2222
.filter(file => file.match('kinvolk-headlamp-plugin-.*gz'))[0];
2323
console.log('Packed headlamp-plugin package file:', packedFile);
2424

2525
// Use "link" to test the repo version of the headlamp-plugin tool.
26-
run('npm link');
27-
run(`node bin/headlamp-plugin.js create ${PACKAGE_NAME} --link`);
26+
run('npm', ['link']);
27+
run('node', ['bin/headlamp-plugin.js', 'create', PACKAGE_NAME, '--link']);
2828
curDir = join('.', PACKAGE_NAME);
29-
run(`npm install ${join('..', packedFile)}`);
29+
run('npm', ['install', join('..', packedFile)]);
3030

3131
// test headlamp-plugin build
32-
run(`node ${join('..', 'bin', 'headlamp-plugin.js')} build`);
32+
run('node', [join('..', 'bin', 'headlamp-plugin.js'), 'build']);
3333
checkFileExists(join(PACKAGE_NAME, 'dist', 'main.js'));
3434

3535
// test headlamp-plugin build folder
3636
curDir = '.';
3737
fs.rmSync(PACKAGE_NAME, { recursive: true });
38-
run(`node bin/headlamp-plugin.js create ${PACKAGE_NAME} --link`);
38+
run('node', ['bin/headlamp-plugin.js', 'create', PACKAGE_NAME, '--link']);
3939
curDir = PACKAGE_NAME;
40-
run(`npm install ${join('..', packedFile)}`);
40+
run('npm', ['install', join('..', packedFile)]);
4141
curDir = '.';
42-
run(`node bin/headlamp-plugin.js build ${PACKAGE_NAME}`);
42+
run('node', ['bin/headlamp-plugin.js', 'build', PACKAGE_NAME]);
4343
checkFileExists(join(PACKAGE_NAME, 'dist', 'main.js'));
4444

4545
fs.writeFileSync(join(PACKAGE_NAME, 'dist', 'extra.txt'), 'All dist/ files will be copied.');
4646

4747
// test extraction works
48-
run(`node bin/headlamp-plugin.js extract . .plugins`);
48+
run('node', ['bin/headlamp-plugin.js', 'extract', '.', '.plugins']);
4949
checkFileExists(join('.plugins', PACKAGE_NAME, 'main.js'));
5050
checkFileExists(join('.plugins', PACKAGE_NAME, 'package.json'));
5151
// make sure extra files in dist/ folder are copied too
5252
checkFileExists(join('.plugins', PACKAGE_NAME, 'extra.txt'));
5353

5454
// test packing works
5555
const tmpDir = fs.mkdtempSync('headlamp-plugin-test-');
56-
run(`node bin/headlamp-plugin.js package ${PACKAGE_NAME} ${tmpDir}`);
56+
run('node', ['bin/headlamp-plugin.js', 'package', PACKAGE_NAME, tmpDir]);
5757
checkFileExists(join(tmpDir, `${PACKAGE_NAME}-0.1.0.tar.gz`));
5858
// extract archive and check files
5959
const extractionFolder = join(tmpDir, 'dst');
6060
fs.mkdirSync(extractionFolder, { recursive: true });
61-
run(`tar -xzf ${join(tmpDir, `${PACKAGE_NAME}-0.1.0.tar.gz`)} -C ${extractionFolder}`);
61+
run('tar', ['-xzf', join(tmpDir, `${PACKAGE_NAME}-0.1.0.tar.gz`), '-C', extractionFolder]);
6262
checkFileExists(join(extractionFolder, `${PACKAGE_NAME}`, 'main.js'));
6363
checkFileExists(join(extractionFolder, `${PACKAGE_NAME}`, 'package.json'));
6464
fs.rmSync(tmpDir, { recursive: true });
6565

6666
// test format command and that default code is formatted correctly
6767
fs.rmSync(PACKAGE_NAME, { recursive: true });
68-
run(`node bin/headlamp-plugin.js create ${PACKAGE_NAME} --link`);
68+
run('node', ['bin/headlamp-plugin.js', 'create', PACKAGE_NAME, '--link']);
6969
curDir = PACKAGE_NAME;
70-
run(`npm install ${join('..', packedFile)}`);
71-
run('npm run format');
70+
run('npm', ['install', join('..', packedFile)]);
71+
run('npm', ['run', 'format']);
7272

7373
// test lint command and default code is lint free
74-
run('npm run lint');
75-
run('npm run lint-fix');
74+
run('npm', ['run', 'lint']);
75+
run('npm', ['run', 'lint-fix']);
7676

7777
// test type script error checks
78-
run('npm run tsc');
78+
run('npm', ['run', 'tsc']);
7979

8080
// test the storybook builds
8181
// TODO: Reenable after storybook is fixed
@@ -90,7 +90,7 @@ function testHeadlampPlugin() {
9090
filesToRemove.forEach(file => {
9191
fs.rmSync(join(curDir, file), { recursive: true });
9292
});
93-
run(`node ${join('..', 'bin', 'headlamp-plugin.js')} upgrade --skip-package-updates`);
93+
run('node', [join('..', 'bin', 'headlamp-plugin.js'), 'upgrade', '--skip-package-updates']);
9494
checkFileExists(join(curDir, 'tsconfig.json'));
9595
checkFileExists(join(curDir, 'src', 'headlamp-plugin.d.ts'));
9696
checkFileExists(join(curDir, '.vscode', 'extensions.json'));
@@ -110,7 +110,7 @@ function testHeadlampPlugin() {
110110
fs.writeFileSync(packageJsonPath, changedJson);
111111

112112
// test upgrade updates the package line, and the old version is not in there
113-
run(`node ${join('..', 'bin', 'headlamp-plugin.js')} upgrade`);
113+
run('node', [join('..', 'bin', 'headlamp-plugin.js'), 'upgrade']);
114114
const oldVersion = '0.4.9';
115115
if (fs.readFileSync(packageJsonPath, 'utf8').includes(oldVersion)) {
116116
exit(`Error: old version still in ${packageJsonPath}`);
@@ -143,18 +143,26 @@ function cleanup() {
143143
.forEach(folder => fs.rmSync(folder, { recursive: true }));
144144
}
145145

146-
function run(cmd) {
146+
function run(cmd, args) {
147147
console.log('');
148-
console.log(`Running cmd:${cmd} inside of cwd:${curDir} abs: "${resolve(curDir)}"`);
148+
console.log(
149+
`Running cmd:${cmd} with args:${args.join(' ')} inside of cwd:${curDir} abs: "${resolve(
150+
curDir
151+
)}"`
152+
);
149153
console.log('');
150154
try {
151-
child_process.execSync(cmd, {
155+
child_process.execFileSync(cmd, args, {
152156
stdio: 'inherit',
153157
cwd: curDir,
154158
encoding: 'utf8',
155159
});
156160
} catch (e) {
157-
exit(`Error: Problem running "${cmd}" inside of "${curDir}" abs: "${resolve(curDir)}"`);
161+
exit(
162+
`Error: Problem running "${cmd} ${args.join(' ')}" inside of "${curDir}" abs: "${resolve(
163+
curDir
164+
)}"`
165+
);
158166
}
159167
}
160168
function checkFileExists(fname) {

0 commit comments

Comments
 (0)