|
| 1 | +#!/usr/bin/env node |
| 2 | +// Copyright IBM Corp. 2017,2018. All Rights Reserved. |
| 3 | +// Node module: loopback-next |
| 4 | +// This file is licensed under the MIT License. |
| 5 | +// License text available at https://opensource.org/licenses/MIT |
| 6 | + |
| 7 | +/** |
| 8 | + * This is an internal script to update TypeScript project references based on |
| 9 | + * lerna's local package dependencies. |
| 10 | + * |
| 11 | + * See https://www.typescriptlang.org/docs/handbook/project-references.html |
| 12 | + */ |
| 13 | +'use strict'; |
| 14 | + |
| 15 | +const path = require('path'); |
| 16 | +const fs = require('fs'); |
| 17 | +const buildUtils = require('../packages/build/bin/utils'); |
| 18 | + |
| 19 | +const Project = require('@lerna/project'); |
| 20 | +const PackageGraph = require('@lerna/package-graph'); |
| 21 | + |
| 22 | +const TSCONFIG = 'tsconfig.build.json'; |
| 23 | + |
| 24 | +async function updateReferences() { |
| 25 | + const project = new Project(process.cwd()); |
| 26 | + const packages = await project.getPackages(); |
| 27 | + |
| 28 | + const rootRefs = []; |
| 29 | + const graph = new PackageGraph(packages); |
| 30 | + |
| 31 | + for (const p of graph.values()) { |
| 32 | + console.log('Package %s', p.pkg.name); |
| 33 | + const pkgLocation = p.pkg.location; |
| 34 | + const tsconfigFile = path.join(pkgLocation, TSCONFIG); |
| 35 | + // Skip non-typescript packages |
| 36 | + if (!fs.existsSync(tsconfigFile)) continue; |
| 37 | + rootRefs.push({ |
| 38 | + path: path.join( |
| 39 | + path.relative(project.rootPath, pkgLocation), |
| 40 | + 'tsconfig.build.json', |
| 41 | + ), |
| 42 | + }); |
| 43 | + const tsconfig = require(tsconfigFile); |
| 44 | + const refs = []; |
| 45 | + for (const d of p.localDependencies.keys()) { |
| 46 | + const depPkg = graph.get(d); |
| 47 | + // Skip non-typescript packages |
| 48 | + if (!fs.existsSync(path.join(depPkg.pkg.location, 'tsconfig.build.json'))) |
| 49 | + continue; |
| 50 | + const relativePath = path.relative(pkgLocation, depPkg.pkg.location); |
| 51 | + refs.push({path: path.join(relativePath, 'tsconfig.build.json')}); |
| 52 | + } |
| 53 | + tsconfig.compilerOptions = tsconfig.compilerOptions || {}; |
| 54 | + tsconfig.compilerOptions.composite = true; |
| 55 | + tsconfig.compilerOptions.target = |
| 56 | + tsconfig.compilerOptions.target || buildUtils.getCompilationTarget(); |
| 57 | + tsconfig.compilerOptions.outDir = |
| 58 | + tsconfig.compilerOptions.outDir || |
| 59 | + buildUtils.getDistribution(tsconfig.compilerOptions.target); |
| 60 | + tsconfig.references = refs; |
| 61 | + |
| 62 | + // Convert to JSON |
| 63 | + const tsconfigJson = JSON.stringify(tsconfig, null, 2); |
| 64 | + |
| 65 | + if (process.argv[2] === '-f') { |
| 66 | + // Using `-f` to overwrite tsconfig.build.json |
| 67 | + fs.writeFileSync(tsconfigFile, tsconfigJson + '\n', {encoding: 'utf-8'}); |
| 68 | + console.log('%s has been updated.', tsconfigFile); |
| 69 | + } else { |
| 70 | + // Otherwise write to console |
| 71 | + console.log(tsconfigJson); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + const rootTsconfigFile = path.join(project.rootPath, 'tsconfig.json'); |
| 76 | + const rootTsconfig = require(rootTsconfigFile); |
| 77 | + rootTsconfig.compilerOptions = rootTsconfig.compilerOptions || {}; |
| 78 | + rootTsconfig.compilerOptions.composite = true; |
| 79 | + rootTsconfig.references = rootRefs; |
| 80 | + // Convert to JSON |
| 81 | + const rootTsconfigJson = JSON.stringify(rootTsconfig, null, 2); |
| 82 | + if (process.argv[2] === '-f') { |
| 83 | + // Using `-f` to overwrite tsconfig.build.json |
| 84 | + fs.writeFileSync(rootTsconfigFile, rootTsconfigJson + '\n', { |
| 85 | + encoding: 'utf-8', |
| 86 | + }); |
| 87 | + console.log('%s has been updated.', rootTsconfigFile); |
| 88 | + } else { |
| 89 | + console.log(rootTsconfigJson); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +if (require.main === module) updateReferences(); |
0 commit comments