storybook/scripts/bootstrap.js

150 lines
3.9 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
const inquirer = require('inquirer');
const program = require('commander');
const childProcess = require('child_process');
const chalk = require('chalk');
const log = require('npmlog');
2017-08-20 23:48:27 +02:00
const { lstatSync, readdirSync } = require('fs');
const { join } = require('path');
const isTgz = source => lstatSync(source).isFile() && source.match(/.tgz$/);
const getDirectories = source => readdirSync(source).map(name => join(source, name)).filter(isTgz);
log.heading = 'storybook';
const prefix = 'bootstrap';
2017-06-08 09:24:12 +02:00
2017-08-20 23:48:27 +02:00
const spawn = command =>
childProcess.spawnSync(`${command}`, {
shell: true,
stdio: 'inherit',
});
2017-06-08 09:24:12 +02:00
2017-08-20 23:48:27 +02:00
const main = program
.version('3.0.0')
2017-08-20 23:48:27 +02:00
.option('--all', `Bootstrap everything ${chalk.gray('(all)')}`);
2017-06-08 09:24:12 +02:00
2017-08-20 23:48:27 +02:00
const createTask = ({ defaultValue, option, name, check = () => true, command, pre = [] }) => ({
value: false,
defaultValue: defaultValue || false,
option: option || undefined,
name: name || 'unnamed task',
check: check || (() => true),
command: () => {
// run all pre tasks
// eslint-disable-next-line no-use-before-define
pre.map(key => tasks[key]).forEach(task => {
if (!task.check()) {
task.command();
}
});
2017-08-20 23:48:27 +02:00
log.info(prefix, name);
command();
},
2017-08-20 23:48:27 +02:00
});
const tasks = {
reset: createTask({
name: `Clean and re-install root dependencies ${chalk.red('(reset)')}`,
defaultValue: false,
2017-08-20 23:48:27 +02:00
option: '--reset',
command: () => {
log.info(prefix, 'git clean');
spawn('git clean -fdx');
log.info(prefix, 'yarn install');
spawn('yarn install --no-lockfile');
},
}),
core: createTask({
name: `Core & Examples ${chalk.gray('(core)')}`,
defaultValue: true,
option: '--core',
command: () => {
spawn('yarn bootstrap:core');
},
}),
docs: createTask({
name: `Documentation ${chalk.gray('(docs)')}`,
2017-08-20 23:48:27 +02:00
defaultValue: false,
option: '--docs',
command: () => {
spawn('yarn bootstrap:docs');
},
}),
packs: createTask({
2017-08-19 22:31:09 +02:00
name: `Build tarballs of packages ${chalk.gray('(build-packs)')}`,
defaultValue: false,
2017-08-20 23:48:27 +02:00
option: '--packs',
command: () => {
spawn('yarn build-packs');
},
check: () => getDirectories(join(__dirname, '..', 'packs')).length > 0,
}),
'react-native-vanilla': createTask({
name: `React-Native example ${chalk.gray('(react-native-vanilla)')}`,
2017-08-20 23:48:27 +02:00
defaultValue: false,
option: '--reactnative',
pre: ['packs'],
command: () => {
spawn('yarn bootstrap:react-native-vanilla');
},
}),
'crna-kitchen-sink': createTask({
name: `React-Native-App example ${chalk.gray('(crna-kitchen-sink)')}`,
defaultValue: false,
option: '--reactnativeapp',
pre: ['packs'],
command: () => {
spawn('yarn bootstrap:crna-kitchen-sink');
},
}),
2017-06-08 09:24:12 +02:00
};
2017-08-20 23:48:27 +02:00
Object.keys(tasks)
.reduce((acc, key) => acc.option(tasks[key].option, tasks[key].name), main)
.parse(process.argv);
Object.keys(tasks).forEach(key => {
tasks[key].value = program[tasks[key].option.replace('--', '')] || program.all;
});
2017-06-08 09:24:12 +02:00
let selection;
2017-08-20 23:48:27 +02:00
if (!Object.keys(tasks).map(key => tasks[key].value).filter(Boolean).length) {
2017-06-08 09:24:12 +02:00
selection = inquirer
.prompt([
{
type: 'checkbox',
message: 'Select which packages to bootstrap',
name: 'todo',
2017-08-20 23:48:27 +02:00
choices: Object.keys(tasks).map(key => ({
name: tasks[key].name,
checked: tasks[key].defaultValue,
})),
2017-06-08 09:24:12 +02:00
},
])
2017-08-19 22:31:09 +02:00
.then(answers =>
2017-08-20 23:48:27 +02:00
answers.todo.map(name => tasks[Object.keys(tasks).find(i => tasks[i].name === name)])
2017-08-19 22:31:09 +02:00
);
2017-06-08 09:24:12 +02:00
} else {
2017-08-19 22:31:09 +02:00
selection = Promise.resolve(
2017-08-20 23:48:27 +02:00
Object.keys(tasks).map(key => tasks[key]).filter(item => item.value === true)
2017-08-19 22:31:09 +02:00
);
2017-06-08 09:24:12 +02:00
}
selection.then(list => {
if (list.length === 0) {
log.warn(prefix, 'Nothing to bootstrap');
2017-06-08 09:24:12 +02:00
} else {
list.forEach(key => {
2017-08-20 23:48:27 +02:00
key.command();
2017-06-08 09:24:12 +02:00
});
2017-08-20 23:48:27 +02:00
process.stdout.write('\x07');
try {
spawn('say "Bootstrapping sequence complete"');
} catch (e) {
// discard error
}
2017-06-08 09:24:12 +02:00
}
});