Files
vercel/packages/now-cli/scripts/compile-templates.js
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

91 lines
2.5 KiB
JavaScript

const execa = require('execa');
const { join } = require('path');
const { readFile, writeFile, readdir, remove } = require('fs-extra');
async function main() {
const dirRoot = join(__dirname, '..');
// Compile the `doT.js` template files for `now dev`
const templatesDir = join(dirRoot, 'src/util/dev/templates');
const dotPacker = join(dirRoot, '../../node_modules/dot/bin/dot-packer');
await execa(process.execPath, [dotPacker], {
cwd: templatesDir,
stdio: 'inherit'
});
const files = await readdir(templatesDir);
const compiledFiles = files.filter(f => f.endsWith('.js'));
// Prettier
console.log('\nMaking the compiled template functions prettier...');
const prettier = join(dirRoot, '../../node_modules/prettier/bin-prettier.js');
await execa(
process.execPath,
[prettier, '--write', '--single-quote', ...compiledFiles],
{
cwd: templatesDir,
stdio: 'inherit'
}
);
console.log('\nConverting template functions to TypeScript');
for (const file of compiledFiles) {
const start = Date.now();
const fnPath = join(templatesDir, file);
const tsPath = fnPath.replace(/\.js$/, '.ts');
const def = await readFile(fnPath.replace(/\.js$/, '.tsdef'), 'utf8');
const interfaceName = def.match(/interface (\w+)/)[1];
const lines = require(fnPath)
.toString()
.split('\n');
let errorHtmlStart = -1;
let errorHtmlEnd = -1;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (errorHtmlStart === -1 && line.includes('encodeHTML')) {
errorHtmlStart = i;
} else if (errorHtmlEnd === -1 && line.includes(')();')) {
errorHtmlEnd = i;
}
if (/\bvar\b/.test(line)) {
lines[i] = line.replace(/\bvar\b/g, 'let');
}
}
lines.splice(errorHtmlStart, errorHtmlEnd);
lines[0] = `export default ${lines[0].replace(
'(it)',
`(it: ${interfaceName}): string`
)}`;
lines.unshift(
'import encodeHTML from \'escape-html\';',
'',
...def.split('\n')
);
await Promise.all([writeFile(tsPath, lines.join('\n')), remove(fnPath)]);
console.log(
`${file} -> ${file.replace(/\.js$/, '.ts')} (${Date.now() - start}ms)`
);
}
}
process.on('unhandledRejection', err => {
console.error('Unhandled Rejection:');
console.error(err);
process.exit(1);
});
process.on('uncaughtException', err => {
console.error('Uncaught Exception:');
console.error(err);
process.exit(1);
});
main().catch(err => {
console.error(err);
process.exit(1);
});