-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add script to build typescript project references
- Loading branch information
1 parent
5cddff1
commit b060100
Showing
3 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters