Files
vercel/utils/run.js
Nathan Rajlich ffa36c12d5 [frameworks] Convert to TypeScript (#5740)
This PR converts the `frameworks.json` file to TypeScript, and extends the values with the detection logic from `@vercel/static-build`, so that it's publicly editable. You also don't need to do the type casting downstream anymore.

As a consequence, it also makes Zola a 1st-class framework, as it was previously missing from the `frameworks.json` file, but present in the static-build frameworks. An example has been included based on their "Getting Started" tutorial.

CH-3808
CH-18771
2021-02-09 22:55:49 +00:00

114 lines
2.7 KiB
JavaScript
Vendored

const { execSync, spawn } = require('child_process');
const { join, relative, sep } = require('path');
// The order matters because we must build dependencies first
const allPackages = [
'now-routing-utils',
'frameworks',
'now-build-utils',
'now-cgi',
'now-client',
'now-node-bridge',
'now-node',
'now-go',
'now-python',
'now-ruby',
'now-cli',
];
process.chdir(join(__dirname, '..'));
async function main() {
const script = process.argv[2];
const all = process.argv[3];
let modifiedPackages = new Set(allPackages);
if (!script) {
console.error('Please provide at least one argument');
process.exit(2);
}
if (all === 'all') {
console.log(`Running script "${script}" for all packages`);
} else {
const branch =
process.env.GITHUB_HEAD_REF ||
execSync('git branch --show-current').toString().trim();
const gitPath = branch === 'master' ? 'HEAD~1' : 'origin/master...HEAD';
const diff = execSync(`git diff ${gitPath} --name-only`).toString();
const changed = diff
.split('\n')
.filter(item => Boolean(item) && item.startsWith('packages'))
.map(item => relative('packages', item).split(sep)[0])
.concat('now-cli'); // Always run tests for Now CLI
modifiedPackages = new Set(changed);
console.log(
`Running "${script}" on branch "${branch}" with the following packages:\n`
);
}
for (const pkgName of allPackages) {
if (modifiedPackages.has(pkgName)) {
console.log(` - ${pkgName}`);
}
}
for (const pkgName of allPackages) {
if (modifiedPackages.has(pkgName)) {
await runScript(pkgName, script);
}
}
}
function runScript(pkgName, script) {
return new Promise((resolve, reject) => {
const cwd = join(__dirname, '..', 'packages', pkgName);
let pkgJson = null;
try {
pkgJson = require(join(cwd, 'package.json'));
} catch (e) {
pkgJson = null;
}
if (pkgJson && pkgJson.scripts && pkgJson.scripts[script]) {
console.log(`\n[${pkgName}] Running yarn ${script}`);
const child = spawn('yarn', [script], {
cwd,
stdio: 'inherit',
shell: true,
});
child.on('error', reject);
child.on('close', (code, signal) => {
if (code === 0) {
return resolve();
}
reject(
new Error(
`[${pkgName}] Exited script "${script}" with code ${
code || signal
}.`
)
);
});
} else {
console.log(
`[${pkgName}] Skipping since script "${script}" is missing from package.json`
);
resolve();
}
});
}
main()
.then(() => {
console.log('Success!');
process.exit(0);
})
.catch(e => {
console.error(e);
process.exit(1);
});