#!/usr/bin/env node
const { spawn } = require('child_process');
const { promisify } = require('util');
const {
readdir: readdirRaw,
readFile: readFileRaw,
writeFile: writeFileRaw,
statSync,
} = require('fs');
const { join } = require('path');
const readdir = promisify(readdirRaw);
const readFile = promisify(readFileRaw);
const writeFile = promisify(writeFileRaw);
const p = l => join(__dirname, '..', ...l);
const logger = console;
const exec = async (command, args = [], options = {}) =>
new Promise((resolve, reject) => {
const child = spawn(command, args, { ...options, stdio: 'inherit', shell: true });
child
.on('close', code => {
if (code) {
reject();
} else {
resolve();
}
})
.on('error', e => {
logger.error(e);
reject();
});
});
const hasBuildScript = async l => {
const text = await readFile(l, 'utf8');
const json = JSON.parse(text);
return !!json.scripts['build-storybook'];
};
const createContent = deployables => {
return `
`;
};
const handleExamples = async files => {
const deployables = files.filter(f => {
const packageJsonLocation = p(['examples', f, 'package.json']);
let stats = null;
try {
stats = statSync(packageJsonLocation);
} catch (e) {
//
}
return stats && stats.isFile() && hasBuildScript(packageJsonLocation);
});
await deployables.reduce(async (acc, d) => {
await acc;
logger.log('');
logger.log(
`-----------------${Array(d.length)
.fill('-')
.join('')}`
);
logger.log(`▶️ building: ${d}`);
logger.log(
`-----------------${Array(d.length)
.fill('-')
.join('')}`
);
const out = p(['built-storybooks', d]);
const cwd = p(['examples', d]);
await exec(`yarn`, [`build-storybook`, `--output-dir=${out}`, '--quiet'], { cwd });
logger.log('-------');
logger.log('✅ done');
logger.log('-------');
}, Promise.resolve());
logger.log('');
logger.log(`📑 creating index`);
const indexLocation = p(['built-storybooks', 'index.html']);
const indexContent = createContent(deployables);
await writeFile(indexLocation, indexContent);
logger.log('-------');
logger.log('✅ done');
logger.log('-------');
};
const run = async () => {
const examples = await readdir(p(['examples']));
const { length } = examples;
const [a, b] = [process.env.CIRCLE_NODE_INDEX || 0, process.env.CIRCLE_NODE_TOTAL || 1];
const step = Math.ceil(length / b);
const offset = step * a;
const list = examples.slice().splice(offset, step);
await handleExamples(list);
};
run().catch(e => {
logger.error(e);
process.exit(1);
});