2020-05-09 11:34:50 +02:00
|
|
|
import { spawn } from 'child_process';
|
|
|
|
import { promisify } from 'util';
|
|
|
|
|
|
|
|
import { readdir as readdirRaw, readFileSync } from 'fs';
|
|
|
|
import { join } from 'path';
|
|
|
|
|
|
|
|
import { getDeployables } from './utils/list-examples';
|
2020-09-19 19:17:43 +02:00
|
|
|
import { filterDataForCurrentCircleCINode } from './utils/concurrency';
|
2020-03-13 16:11:00 +01:00
|
|
|
|
|
|
|
const readdir = promisify(readdirRaw);
|
|
|
|
|
2020-03-27 20:04:50 +01:00
|
|
|
const p = (l) => join(__dirname, '..', ...l);
|
2020-03-13 16:11:00 +01:00
|
|
|
const logger = console;
|
|
|
|
|
|
|
|
const exec = async (command, args = [], options = {}) =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const child = spawn(command, args, { ...options, stdio: 'inherit', shell: true });
|
|
|
|
|
|
|
|
child
|
2020-03-27 20:04:50 +01:00
|
|
|
.on('close', (code) => {
|
2020-03-13 16:11:00 +01:00
|
|
|
if (code) {
|
|
|
|
reject();
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
})
|
2020-03-27 20:04:50 +01:00
|
|
|
.on('error', (e) => {
|
2020-03-13 16:11:00 +01:00
|
|
|
logger.error(e);
|
|
|
|
reject();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-27 20:04:50 +01:00
|
|
|
const hasChromaticAppCode = (l) => {
|
2020-03-13 16:11:00 +01:00
|
|
|
const text = readFileSync(l, 'utf8');
|
|
|
|
const json = JSON.parse(text);
|
|
|
|
|
2020-05-01 12:37:34 +02:00
|
|
|
return !!(
|
|
|
|
json &&
|
|
|
|
json.storybook &&
|
|
|
|
json.storybook.chromatic &&
|
|
|
|
json.storybook.chromatic.projectToken
|
|
|
|
);
|
2020-03-13 16:11:00 +01:00
|
|
|
};
|
|
|
|
|
2020-03-27 20:04:50 +01:00
|
|
|
const handleExamples = async (deployables) => {
|
2020-03-13 16:11:00 +01:00
|
|
|
await deployables.reduce(async (acc, d) => {
|
|
|
|
await acc;
|
|
|
|
|
|
|
|
const out = p(['built-storybooks', d]);
|
|
|
|
const cwd = p([]);
|
|
|
|
const {
|
|
|
|
storybook: {
|
2020-05-01 12:37:34 +02:00
|
|
|
chromatic: { projectToken },
|
2020-03-13 16:11:00 +01:00
|
|
|
},
|
|
|
|
} = JSON.parse(readFileSync(p(['examples', d, 'package.json'])));
|
|
|
|
|
2020-05-01 12:37:34 +02:00
|
|
|
if (projectToken) {
|
2020-03-13 16:11:00 +01:00
|
|
|
await exec(
|
|
|
|
`yarn`,
|
|
|
|
[
|
|
|
|
'chromatic',
|
|
|
|
`--storybook-build-dir="${out}"`,
|
|
|
|
'--exit-zero-on-changes',
|
2020-05-01 12:37:34 +02:00
|
|
|
`--project-token="${projectToken}"`,
|
2020-03-13 16:11:00 +01:00
|
|
|
],
|
|
|
|
{ cwd }
|
|
|
|
);
|
|
|
|
|
|
|
|
logger.log('-------');
|
|
|
|
logger.log(`✅ ${d} ran`);
|
|
|
|
logger.log('-------');
|
|
|
|
} else {
|
|
|
|
logger.log('-------');
|
|
|
|
logger.log(`❌ ${d} skipped`);
|
|
|
|
logger.log('-------');
|
|
|
|
}
|
|
|
|
}, Promise.resolve());
|
|
|
|
};
|
|
|
|
|
|
|
|
const run = async () => {
|
|
|
|
const examples = await readdir(p(['examples']));
|
|
|
|
|
2020-09-19 19:17:43 +02:00
|
|
|
const list = filterDataForCurrentCircleCINode(examples);
|
2020-03-13 16:11:00 +01:00
|
|
|
|
2020-05-09 11:34:50 +02:00
|
|
|
const deployables = getDeployables(list, hasChromaticAppCode);
|
2020-03-13 16:11:00 +01:00
|
|
|
|
|
|
|
if (deployables.length) {
|
2020-09-19 19:17:43 +02:00
|
|
|
logger.log(`🖼 Will run chromatics for: ${deployables.join(', ')}`);
|
2020-03-13 16:11:00 +01:00
|
|
|
await handleExamples(deployables);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-27 20:04:50 +01:00
|
|
|
run().catch((e) => {
|
2020-03-13 16:11:00 +01:00
|
|
|
logger.error(e);
|
|
|
|
process.exit(1);
|
|
|
|
});
|