Skip to content

Commit

Permalink
use spawn instead of exec
Browse files Browse the repository at this point in the history
  • Loading branch information
sottar committed Feb 19, 2020
1 parent e7f3c18 commit 81d6a41
Showing 1 changed file with 29 additions and 38 deletions.
67 changes: 29 additions & 38 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import fs from 'fs';
import chalk from 'chalk';
import inquirer from 'inquirer';

import { ExecException } from 'child_process';

const exec = require('child_process').exec;
import childProcess from 'child_process';

const run = (command: string): Promise<void> => {
const spawn = childProcess.spawn;
return new Promise(resolve => {
const child = spawn(command, { shell: true });
child.stdout.on('data', (data: Buffer) => {
let output = data.toString();
if (output.slice(-1) === '\n') {
output = output.substring(0, output.length - 1);
}
console.log(chalk(output));
});
child.stderr.on('data', (data: Buffer) => {
let output = data.toString();
if (output.slice(-1) === '\n') {
output = output.substring(0, output.length - 1);
}
console.log(chalk(output));
});
child.on('close', () => {
resolve();
});
});
};

const convertCommandToYarn = (command: string, options: Array<string>): string => {
// nyarm
Expand Down Expand Up @@ -69,31 +90,14 @@ const initInstall = async (): Promise<void> => {
if (answer2.manager === 'npm') {
console.log('');
console.log(chalk.gray('> npm install'));
await exec('npm install', (err: ExecException, stdout: string, stderr: string) => {
if (err) {
console.log(err.message);
}
console.log(stderr);
console.log(stdout);

console.log('');
console.log(chalk.green('Done.'));
});
await run('npm install');
}

if (answer2.manager === 'yarn') {
console.log('');
console.log(chalk.gray('> yarn install'));

exec(`yarn install`, (err: ExecException, stdout: string, stderr: string) => {
if (err) {
console.log(err.message);
}
console.log(stderr);
console.log(stdout);
console.log('');
console.log(chalk.green('Done.'));
});
await run('yarn install');
}
};

Expand Down Expand Up @@ -158,14 +162,7 @@ you can use just nyarm command instead of npm or yarn command.`),

console.log('');
console.log(chalk.gray(`> npm ${npmCommand} ${options.join(' ')}`));

exec(`npm ${npmCommand} ${options.join(' ')}`, (err: ExecException, stdout: string, stderr: string) => {
if (err) {
console.log(err.message);
}
console.log(stderr);
console.log(stdout);
});
await run(`npm ${npmCommand} ${options.join(' ')}`);
}
if (isYarnExisted) {
console.log(chalk.green('yarn.lock is found, use yarn...'));
Expand All @@ -174,12 +171,6 @@ you can use just nyarm command instead of npm or yarn command.`),
console.log('');
console.log(chalk.green(`> yarn ${yarnCommand} ${options.join(' ')}`));

exec(`yarn ${yarnCommand} ${options.join(' ')}`, (err: ExecException, stdout: string, stderr: string) => {
if (err) {
console.log(err.message);
}
console.log(stderr);
console.log(stdout);
});
await run(`yarn ${yarnCommand} ${options.join(' ')}`);
}
})();

0 comments on commit 81d6a41

Please sign in to comment.