How to use child_process

Comprehensive child_process code examples:

How to use child_process.scss:

43
44
45
46
47
48
49
50
51
52
53
	Convert to HTML with Marked
*/
outputFile = require('marked')(outputFile);


/*
	Add animations from animations.scss.
*/
let animations = require('child_process').execSync("sass --style compressed ./scss/animations.scss");
/*
	Find the <code> elements and syntax-highlight their contents.

How to use child_process.default:

294
295
296
297
298
299
300
301
302
303
        }, null, 2);
        const configPath = path_1.default.join(tmpDir, "nwjs_start_config.json");
        const scriptPath = path_1.default.join(tmpDir, "nwjs_start.js");
        fs_1.default.writeFileSync(configPath, configStr, { encoding: 'utf8' });
        fs_1.default.copyFileSync(path_1.default.join(__dirname, "nwjs_start.js"), scriptPath);
        child_process_1.default.execSync(`node ./nwjs_start.js`, { cwd: tmpDir, stdio: 'inherit' });
    });
});
program
    .command('build')

How to use child_process.spawnSync:

1
2
3
4
5
6
7
8
9
10

const path = require('path');
const proc = require('child_process');
const fs = require('fs');

const findResult = proc.spawnSync('find', ['.', '-iname', '"_*.scss"'], {
  cwd: __dirname,
  shell: true,
  env: process.env,
}).stdout.toString();

How to use child_process.fork:

67
68
69
70
71
72
73
74
75
76

const start = () => {
  shell.cd(CWD);
  generateJSONFile('dev', CWD);
  generateHTMLFile('dev', CWD);
  cp.fork(path.join(__dirname, './watch.js'));
  const platform = process.platform;
  if (platform === 'win32') {
    shell.exec(`node ${['node_modules', 'gulp', 'bin', 'gulp.js'].join(path.sep)}`);
  } else {

How to use child_process.spawn:

21
22
23
24
25
26
27
28
29
30
/**
 * Build the Jekyll Site
 */
gulp.task('jekyll-build', function (done) {
    browserSync.notify(messages.jekyllBuild);
    return cp.spawn('jekyll', ['build'], {stdio: 'inherit'})
        .on('close', done);
});

/**

How to use child_process.execFile:

3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
}


Brain.prototype._powerOff = function _powerOff () {
  this._setState('powerOff')
  console.log('powering off')
  cp.execFile('poweroff', ['-d', '2'], {}, function () {
    process.exit(0)
  })
}

How to use child_process.execFileSync:

13
14
15
16
17
18
19
20
21
22
23
24
const DEV = ZIG_VERSION.dev ? `-dev.${ZIG_VERSION.dev}` : '';
const VERSION = `${ZIG_VERSION.major}.${ZIG_VERSION.minor}.${ZIG_VERSION.patch}${DEV}`;
const INDEX = 'https://ziglang.org/download/index.json';
const ROOT = path.resolve(__dirname, '..', '..');


const sh = (cmd, args) => execFileSync(cmd, args, {encoding: 'utf8', cwd: ROOT});


// This is hardly the browser fetch API, but it works for our primitive needs
const fetch = url => new Promise((resolve, reject) => {
  let buf = '';

How to use child_process.exec:

35
36
37
38
39
40
41
42
43
44
await mkdir(path.join(destination, folder));
console.log(`Successfully created folder: ${folder}`);

// Generate component
process.chdir(path.join(destination, folder));
exec(
    `ng generate component ${componentName}-${exampleName}-example --skip-import --skip-tests=true --type= --prefix= --flat`,
    (err, stdout, stderr) => {
        if (err) {
            console.error(err);

How to use child_process.execSync:

44
45
46
47
48
49
50
51
52
53
console.log(`Prepare theme ${data.color} ${data.variant}...`);
fs.mkdirSync(data.outPath, { recursive: true });
fs.mkdirSync(`${data.outPath}/assets`, { recursive: true });

// Compile sass
execSync(`sass ${data.srcSass} ${data.outCss} --no-source-map`);

// Copy thumbnail
const thumbnailFilePath = `${SRC_PATH}/screenshots/thumbnail-${data.variant}.png`;
execSync(`cp ${thumbnailFilePath} ${data.outPath}/thumbnail.png`);