Files
vercel/packages/now-cli/scripts/build.ts
Steven b0ad5238f7 Create monorepo (#2812)
* Move now-cli to /packages/now-cli

* Fix .gitignore paths

* Add now-client

* Add lerna to top level

* Add scripts

* Update codeowners

* Fix `/now-cli/build.ts` script

* Fix circleci path to artifacts

* Use relative paths

* Fix path to scripts

* Add test-lint script

* Add missing return type

* Fix typo in test-lint

* Fix string match in shell scripts

* Fix path to hugo

* Add package node_modules

* Delete lock files in packages, use root yarn.lock

* Add missing b.js file

* Add test-integration-now-dev script

* Add missing test files

* Add missing integration test script

* Add missing test files

* Delete travis.yml

* Fix ts-jest in now-client

* Add support for Node 8 (ES2015 target)

* Add support for Node 8

* Add polyfill for Node 8

* Fix polyfill for Node 8

* Only run coverage for now-cli

* Add packages from now-builders

* Run integration tests for builders

* Add node_modules to cache

* Add root readme.md

* Move readme to top level

* Add yarn bootstrap

* Add bootstrap step

* Add dist to `persist_to_workspace`

* Fix 08-yarn-npm integration test

* Remove duplicate path

* Change stdio to inherit

* Add back store_artifacts

* testing - remove bootstrap step

* Add back now-build-utils

* Remove bootstrap step

* Fix test again

* Add console.log()

* Fix lint

* Use local ncc version

* Install go

* Revert changes to stdio and console.log()

* Add missing now-go test

* Add missing integration tests

* Add --runInBand flag

* Fix now-node-bridge persistence

* Add missing symlinks

* Add codeowners

* Consolidate into single run.sh function

* Run uniq

* Fix typo

* Change now-routing-utils to test-unit

* Special case test for node 8

* Add docs from builders

* Only run script for modified packages

* Add test-integration-once which only runs once

* Fix set intersection
2019-08-23 23:57:00 +00:00

118 lines
3.8 KiB
TypeScript

import cpy from 'cpy';
import tar from 'tar-fs';
import execa from 'execa';
import { join } from 'path';
import pipe from 'promisepipe';
import { createGzip } from 'zlib';
import { createWriteStream, mkdirp, remove, writeJSON } from 'fs-extra';
import { getDistTag } from '../src/util/get-dist-tag';
import pkg from '../package.json';
const dirRoot = join(__dirname, '..');
const bundledBuilders = Object.keys(pkg.devDependencies).filter(d =>
d.startsWith('@now/')
);
async function createBuildersTarball() {
const distTag = getDistTag(pkg.version);
const builders = Array.from(bundledBuilders).map(b => `${b}@${distTag}`);
console.log(`Creating builders tarball with: ${builders.join(', ')}`);
const buildersDir = join(dirRoot, '.builders');
const assetsDir = join(dirRoot, 'assets');
await mkdirp(buildersDir);
await mkdirp(assetsDir);
const buildersTarballPath = join(assetsDir, 'builders.tar.gz');
try {
const buildersPkg = join(buildersDir, 'package.json');
await writeJSON(buildersPkg, { private: true }, { flag: 'wx' });
} catch (err) {
if (err.code !== 'EEXIST') {
throw err;
}
}
const yarn = join(dirRoot, '../../node_modules/yarn/bin/yarn.js');
await execa(process.execPath, [yarn, 'add', '--no-lockfile', ...builders], {
cwd: buildersDir,
stdio: 'inherit'
});
const packer = tar.pack(buildersDir);
await pipe(
packer,
createGzip(),
createWriteStream(buildersTarballPath)
);
}
async function main() {
const isDev = process.argv[2] === '--dev';
// Create a tarball from all the `@now` scoped builders which will be bundled
// with Now CLI
await createBuildersTarball();
// `now dev` uses chokidar to watch the filesystem, but opts-out of the
// `fsevents` feature using `useFsEvents: false`, so delete the module here so
// that it is not compiled by ncc, which makes the npm package size larger
// than necessary.
await remove(join(dirRoot, '../../node_modules/fsevents'));
// Compile the `doT.js` template files for `now dev`
console.log();
await execa(process.execPath, [join(__dirname, 'compile-templates.js')], {
stdio: 'inherit'
});
// Do the initial `ncc` build
console.log();
const src = join(dirRoot, 'src');
const ncc = join(dirRoot, 'node_modules/@zeit/ncc/dist/ncc/cli.js');
const args = [ncc, 'build', '--source-map'];
if (!isDev) {
args.push('--minify');
}
args.push(src);
await execa(process.execPath, args, { stdio: 'inherit' });
// `ncc` has some issues with `@zeit/fun`'s runtime files:
// - Executable bits on the `bootstrap` files appear to be lost:
// https://github.com/zeit/ncc/pull/182
// - The `bootstrap.js` asset does not get copied into the output dir:
// https://github.com/zeit/ncc/issues/278
//
// Aside from those issues, all the same files from the `runtimes` directory
// should be copied into the output runtimes dir, specifically the `index.js`
// files (correctly) do not get copied into the output bundle because they
// get compiled into the final ncc bundle file, however, we want them to be
// present in the npm package because the contents of those files are involved
// with `fun`'s cache invalidation mechanism and they need to be shasum'd.
const runtimes = join(dirRoot, '../../node_modules/@zeit/fun/dist/src/runtimes');
const dest = join(dirRoot, 'dist/runtimes');
await cpy('**/*', dest, { parents: true, cwd: runtimes });
console.log('Finished building `now-cli`');
}
process.on('unhandledRejection', (err: any) => {
console.error('Unhandled Rejection:');
console.error(err);
process.exit(1);
});
process.on('uncaughtException', (err: any) => {
console.error('Uncaught Exception:');
console.error(err);
process.exit(1);
});
main().catch(err => {
console.error(err);
process.exit(1);
});