storybook/scripts/utils/cli-utils.js

61 lines
1.3 KiB
JavaScript
Raw Normal View History

const { spawn } = require('child_process');
const { join } = require('path');
const { existsSync } = require('fs');
2020-04-07 20:18:10 +02:00
const logger = console;
const checkDependencies = async () => {
const scriptsPath = join(__dirname, '..');
const codePath = join(__dirname, '..', '..', 'code');
2020-04-07 20:18:10 +02:00
const tasks = [];
if (!existsSync(join(scriptsPath, 'node_modules'))) {
tasks.push(
spawn('yarn', ['install'], {
cwd: scriptsPath,
stdio: ['inherit', 'inherit', 'inherit'],
})
);
}
if (!existsSync(join(codePath, 'node_modules'))) {
tasks.push(
spawn('yarn', ['install'], {
cwd: codePath,
stdio: ['inherit', 'inherit', 'inherit'],
})
);
2020-04-07 20:18:10 +02:00
}
if (tasks.length > 0) {
logger.log('installing dependencies');
2020-04-07 20:18:10 +02:00
await Promise.all(
tasks.map(
(t) =>
new Promise((res, rej) => {
t.on('exit', (code) => {
if (code !== 0) {
rej();
} else {
res();
}
});
})
)
).catch(() => {
tasks.forEach((t) => t.kill());
throw new Error('Failed to install dependencies');
});
2020-04-07 20:18:10 +02:00
// give the filesystem some time
await new Promise((res, rej) => {
setTimeout(res, 1000);
});
}
2020-04-07 20:18:10 +02:00
};
module.exports = {
checkDependencies,
2020-04-07 20:18:10 +02:00
};