#!/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';

const spawn = childProcess.spawnSync;

program
  .version('3.0.0')
  .option('--all', 'Run all without asking')
  .option('--core', 'Bootstrap core')
  .option('--docs', 'Bootstrap docs')
  .option('--test-cra', 'Bootstrap examples/test-cra')
  .option('--react-native-vanilla', 'Bootstrap examples/react-native-vanilla')
  .parse(process.argv);

const todo = {
  core: {
    value: false,
    name: `Core & Examples ${chalk.gray('(core)')}`,
    default: true,
  },
  docs: {
    value: false,
    name: `Documentation ${chalk.gray('(docs)')}`,
    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,
  },
};

Object.keys(todo).forEach(key => {
  todo[key].value = program[key] || program.all;
});

let selection;
if (!Object.keys(todo).map(key => todo[key].value).filter(Boolean).length) {
  selection = inquirer
    .prompt([
      {
        type: 'checkbox',
        message: 'Select which packages to bootstrap',
        name: 'todo',
        choices: Object.keys(todo).map(key => ({
          name: todo[key].name,
          checked: todo[key].default,
        })),
      },
    ])
    .then(answers => answers.todo.map(name => Object.keys(todo).find(i => todo[i].name === name)));
} else {
  selection = Promise.resolve(Object.keys(todo).filter(key => todo[key].value === true));
}

selection.then(list => {
  if (list.length === 0) {
    log.warn(prefix, 'Nothing to bootstrap');
  } else {
    list.forEach(key => {
      if (list.length > 1) {
        log.info(prefix, `Bootstrapping: ${todo[key].name}`);
      }
      spawn('yarn', [`bootstrap:${key}`, '-s'], {
        shell: true,
        stdio: 'inherit',
      });
    });
  }
});

@@ -590,20 +590,32 @@
 %0A%0Aconst 
-todo
+bootstrapOptions
  = %7B%0A  c
@@ -820,16 +820,149 @@
 e,%0A  %7D,%0A
+  'build-packs': %7B%0A    value: false,%0A    name: %60Build tarballs of packages $%7Bchalk.gray('(build-packs)')%7D%60,%0A    default: false,%0A  %7D,%0A
   'test-
@@ -1242,20 +1242,32 @@
 ct.keys(
-todo
+bootstrapOptions
 ).forEac
@@ -1279,20 +1279,32 @@
  =%3E %7B%0A  
-todo
+bootstrapOptions
 %5Bkey%5D.va
@@ -1379,29 +1379,53 @@
 eys(
-todo).map(key =%3E todo
+bootstrapOptions).map(key =%3E bootstrapOptions
 %5Bkey
@@ -1636,20 +1636,32 @@
 ct.keys(
-todo
+bootstrapOptions
 ).map(ke
@@ -1684,20 +1684,32 @@
   name: 
-todo
+bootstrapOptions
 %5Bkey%5D.na
@@ -1731,20 +1731,32 @@
 hecked: 
-todo
+bootstrapOptions
 %5Bkey%5D.de
@@ -1811,16 +1811,22 @@
 swers =%3E
+%0A     
  answers
@@ -1842,16 +1842,24 @@
 (name =%3E
+%0A       
  Object.
@@ -1867,28 +1867,52 @@
 eys(
-todo).find(i =%3E todo
+bootstrapOptions).find(i =%3E bootstrapOptions
 %5Bi%5D.
@@ -1925,17 +1925,29 @@
 == name)
-)
+%0A      )%0A    
 );%0A%7D els
@@ -1980,16 +1980,21 @@
 resolve(
+%0A    
 Object.k
@@ -2001,32 +2001,56 @@
 eys(
-todo).filter(key =%3E todo
+bootstrapOptions).filter(key =%3E bootstrapOptions
 %5Bkey
@@ -2066,16 +2066,19 @@
 == true)
+%0A  
 );%0A%7D%0A%0Ase
@@ -2282,20 +2282,32 @@
 ping: $%7B
-todo
+bootstrapOptions
 %5Bkey%5D.na

ADD build-packs