storybook/scripts/bootstrap.js

94 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-06-08 09:24:12 +02:00
#!/usr/bin/env babel-node
import inquirer from 'inquirer';
import program from 'commander';
import childProcess from 'child_process';
import chalk from 'chalk';
import log from 'npmlog';
log.heading = 'storybook';
const prefix = 'bootstrap';
2017-06-08 09:24:12 +02:00
const spawn = childProcess.spawnSync;
program
.version('3.0.0')
2017-06-08 09:24:12 +02:00
.option('--all', 'Run all without asking')
.option('--core', 'Bootstrap core')
2017-06-08 09:24:12 +02:00
.option('--docs', 'Bootstrap docs')
.option('--test-cra', 'Bootstrap examples/test-cra')
.option('--react-native-vanilla', 'Bootstrap examples/react-native-vanilla')
2017-06-08 09:24:12 +02:00
.parse(process.argv);
2017-08-19 22:31:09 +02:00
const bootstrapOptions = {
core: {
value: false,
name: `Core & Examples ${chalk.gray('(core)')}`,
default: true,
},
docs: {
value: false,
name: `Documentation ${chalk.gray('(docs)')}`,
default: false,
},
2017-08-19 22:31:09 +02:00
'build-packs': {
value: false,
name: `Build tarballs of packages ${chalk.gray('(build-packs)')}`,
default: false,
},
'test-cra': {
value: false,
name: `Realistic installed example ${chalk.gray('(test-cra)')}`,
default: false,
},
'react-native-vanilla': {
value: false,
name: `React-Native example ${chalk.gray('(react-native-vanilla)')}`,
default: false,
},
2017-06-08 09:24:12 +02:00
};
2017-08-19 22:31:09 +02:00
Object.keys(bootstrapOptions).forEach(key => {
bootstrapOptions[key].value = program[key] || program.all;
});
2017-06-08 09:24:12 +02:00
let selection;
2017-08-19 22:31:09 +02:00
if (!Object.keys(bootstrapOptions).map(key => bootstrapOptions[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-19 22:31:09 +02:00
choices: Object.keys(bootstrapOptions).map(key => ({
name: bootstrapOptions[key].name,
checked: bootstrapOptions[key].default,
})),
2017-06-08 09:24:12 +02:00
},
])
2017-08-19 22:31:09 +02:00
.then(answers =>
answers.todo.map(name =>
Object.keys(bootstrapOptions).find(i => bootstrapOptions[i].name === name)
)
);
2017-06-08 09:24:12 +02:00
} else {
2017-08-19 22:31:09 +02:00
selection = Promise.resolve(
Object.keys(bootstrapOptions).filter(key => bootstrapOptions[key].value === true)
);
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 => {
if (list.length > 1) {
2017-08-19 22:31:09 +02:00
log.info(prefix, `Bootstrapping: ${bootstrapOptions[key].name}`);
}
spawn('yarn', [`bootstrap:${key}`, '-s'], {
2017-06-08 09:24:12 +02:00
shell: true,
stdio: 'inherit',
});
});
}
});