Skip to content

Commit

Permalink
chore: add script to build typescript project references
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Aug 22, 2018
1 parent ec37f2f commit 9afd744
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 5 deletions.
93 changes: 93 additions & 0 deletions bin/update-project-refs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env node
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: loopback-next
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

/**
* This is an internal script to update TypeScript project references based on
* lerna's local package dependencies.
*
* See https://www.typescriptlang.org/docs/handbook/project-references.html
*/
'use strict';

const path = require('path');
const fs = require('fs');
const buildUtils = require('../packages/build/bin/utils');

const Project = require('@lerna/project');
const PackageGraph = require('@lerna/package-graph');

const TSCONFIG = 'tsconfig.build.json';

async function updateReferences() {
const project = new Project(process.cwd());
const packages = await project.getPackages();

const rootRefs = [];
const graph = new PackageGraph(packages);

for (const p of graph.values()) {
console.log('Package %s', p.pkg.name);
const pkgLocation = p.pkg.location;
const tsconfigFile = path.join(pkgLocation, TSCONFIG);
// Skip non-typescript packages
if (!fs.existsSync(tsconfigFile)) continue;
rootRefs.push({
path: path.join(
path.relative(project.rootPath, pkgLocation),
'tsconfig.build.json',
),
});
const tsconfig = require(tsconfigFile);
const refs = [];
for (const d of p.localDependencies.keys()) {
const depPkg = graph.get(d);
// Skip non-typescript packages
if (!fs.existsSync(path.join(depPkg.pkg.location, 'tsconfig.build.json')))
continue;
const relativePath = path.relative(pkgLocation, depPkg.pkg.location);
refs.push({path: path.join(relativePath, 'tsconfig.build.json')});
}
tsconfig.compilerOptions = tsconfig.compilerOptions || {};
tsconfig.compilerOptions.composite = true;
tsconfig.compilerOptions.target =
tsconfig.compilerOptions.target || buildUtils.getCompilationTarget();
tsconfig.compilerOptions.outDir =
tsconfig.compilerOptions.outDir ||
buildUtils.getDistribution(tsconfig.compilerOptions.target);
tsconfig.references = refs;

// Convert to JSON
const tsconfigJson = JSON.stringify(tsconfig, null, 2);

if (process.argv[2] === '-f') {
// Using `-f` to overwrite tsconfig.build.json
fs.writeFileSync(tsconfigFile, tsconfigJson + '\n', {encoding: 'utf-8'});
console.log('%s has been updated.', tsconfigFile);
} else {
// Otherwise write to console
console.log(tsconfigJson);
}
}

const rootTsconfigFile = path.join(project.rootPath, 'tsconfig.json');
const rootTsconfig = require(rootTsconfigFile);
rootTsconfig.compilerOptions = rootTsconfig.compilerOptions || {};
rootTsconfig.compilerOptions.composite = true;
rootTsconfig.references = rootRefs;
// Convert to JSON
const rootTsconfigJson = JSON.stringify(rootTsconfig, null, 2);
if (process.argv[2] === '-f') {
// Using `-f` to overwrite tsconfig.build.json
fs.writeFileSync(rootTsconfigFile, rootTsconfigJson + '\n', {
encoding: 'utf-8',
});
console.log('%s has been updated.', rootTsconfigFile);
} else {
console.log(rootTsconfigJson);
}
}

if (require.main === module) updateReferences();
31 changes: 26 additions & 5 deletions packages/build/bin/compile-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ function run(argv, options) {
const glob = require('glob');
const fse = require('fs-extra');

if (options === true) {
options = {dryRun: true};
} else {
options = options || {};
}

const packageDir = utils.getPackageDir();

const compilerOpts = argv.slice(2);
Expand All @@ -36,6 +42,11 @@ function run(argv, options) {
'--ignore-resources',
);

// Honor --dry from tsc
if (utils.isOptionSet(compilerOpts, '--dry')) {
options.dryRun = true;
}

var target;

// --ignore-resources is not a TS Compiler option so we remove it from the
Expand Down Expand Up @@ -126,7 +137,7 @@ function run(argv, options) {
// Since outDir is set, ts files are compiled into that directory.
// If ignore-resources flag is not passed, copy resources (non-ts files)
// to the same outDir as well.
if (rootDir && tsConfigFile && !isIgnoreResourcesSet) {
if (rootDir && tsConfigFile && !isIgnoreResourcesSet && !options.dryRun) {
const tsConfig = require(tsConfigFile);
const dirs = tsConfig.include
? tsConfig.include.join('|')
Expand All @@ -146,11 +157,21 @@ function run(argv, options) {

args.push(...compilerOpts);

if (options === true) {
options = {dryRun: true};
} else {
options = options || {};
// Move --build or -b as the 1st argument to avoid:
// error TS6369: Option '--build' must be the first command line argument.
const buildOptions = utils.removeOptions(args, '-b', '--build');
if (buildOptions.length) {
let projectOptions = utils.removeOptions(args, '-p', '--project');
projectOptions = projectOptions.filter(p => !p.startsWith('-'));
// Remove conflict options with '--build'
utils.removeOptions(args, '--outDir', '--target');
if (buildOptions.length === 1) {
args.unshift(...buildOptions, ...projectOptions);
} else {
args.unshift(...buildOptions);
}
}

return utils.runCLI('typescript/lib/tsc', args, {cwd, ...options});
}

Expand Down
21 changes: 21 additions & 0 deletions packages/build/bin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,26 @@ function isOptionSet(opts, ...optionNames) {
);
}

/**
* Remove options and their values from args
*/
function removeOptions(args, ...options) {
const removed = [];
for (const e of options) {
const index = args.indexOf(e);
if (index !== -1) {
const next = args[index + 1];
if (typeof next === 'string' && !next.startsWith('-')) {
// The next element is the value of the option, remove it too
removed.push(...args.splice(index, 2));
} else {
removed.push(...args.splice(index, 1));
}
}
}
return removed;
}

exports.getCompilationTarget = getCompilationTarget;
exports.getDistribution = getDistribution;
exports.getRootDir = getRootDir;
Expand All @@ -203,3 +223,4 @@ exports.resolveCLI = resolveCLI;
exports.runCLI = runCLI;
exports.runShell = runShell;
exports.isOptionSet = isOptionSet;
exports.removeOptions = removeOptions;

0 comments on commit 9afd744

Please sign in to comment.