|
| 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 debug = require('debug')('loopback:build'); |
| 18 | +const buildUtils = require('../packages/build/bin/utils'); |
| 19 | + |
| 20 | +const Project = require('@lerna/project'); |
| 21 | +const PackageGraph = require('@lerna/package-graph'); |
| 22 | + |
| 23 | +const TSCONFIG = 'tsconfig.build.json'; |
| 24 | + |
| 25 | +async function updateReferences(options) { |
| 26 | + options = options || {}; |
| 27 | + const dryRun = options.dryRun; |
| 28 | + const project = new Project(process.cwd()); |
| 29 | + const packages = await project.getPackages(); |
| 30 | + |
| 31 | + const rootRefs = []; |
| 32 | + const graph = new PackageGraph(packages); |
| 33 | + |
| 34 | + for (const p of graph.values()) { |
| 35 | + debug('Package %s', p.pkg.name); |
| 36 | + const pkgLocation = p.pkg.location; |
| 37 | + const tsconfigFile = path.join(pkgLocation, TSCONFIG); |
| 38 | + // Skip non-typescript packages |
| 39 | + if (!fs.existsSync(tsconfigFile)) { |
| 40 | + debug('Skipping non-TS package: %s', p.pkg.name); |
| 41 | + continue; |
| 42 | + } |
| 43 | + rootRefs.push({ |
| 44 | + path: path.join(path.relative(project.rootPath, pkgLocation), TSCONFIG), |
| 45 | + }); |
| 46 | + const tsconfig = require(tsconfigFile); |
| 47 | + const refs = []; |
| 48 | + for (const d of p.localDependencies.keys()) { |
| 49 | + const depPkg = graph.get(d); |
| 50 | + // Skip non-typescript packages |
| 51 | + if (!fs.existsSync(path.join(depPkg.pkg.location, TSCONFIG))) { |
| 52 | + debug('Skipping non-TS dependency: %s', depPkg.pkg.name); |
| 53 | + continue; |
| 54 | + } |
| 55 | + const relativePath = path.relative(pkgLocation, depPkg.pkg.location); |
| 56 | + refs.push({path: path.join(relativePath, TSCONFIG)}); |
| 57 | + } |
| 58 | + tsconfig.compilerOptions = tsconfig.compilerOptions || {}; |
| 59 | + // composite must be true for project refs |
| 60 | + tsconfig.compilerOptions.composite = true; |
| 61 | + // outDir & target have to be set in tsconfig instead of CLI for tsc -b |
| 62 | + tsconfig.compilerOptions.target = |
| 63 | + tsconfig.compilerOptions.target || buildUtils.getCompilationTarget(); |
| 64 | + tsconfig.compilerOptions.outDir = |
| 65 | + tsconfig.compilerOptions.outDir || |
| 66 | + buildUtils.getDistribution(tsconfig.compilerOptions.target); |
| 67 | + tsconfig.references = refs; |
| 68 | + |
| 69 | + // Convert to JSON |
| 70 | + const tsconfigJson = JSON.stringify(tsconfig, null, 2); |
| 71 | + |
| 72 | + if (!dryRun) { |
| 73 | + // Using `-f` to overwrite tsconfig.build.json |
| 74 | + fs.writeFileSync(tsconfigFile, tsconfigJson + '\n', {encoding: 'utf-8'}); |
| 75 | + debug('%s has been updated.', tsconfigFile); |
| 76 | + } else { |
| 77 | + // Otherwise write to console |
| 78 | + debug(tsconfigJson); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + const rootTsconfigFile = path.join(project.rootPath, 'tsconfig.json'); |
| 83 | + const rootTsconfig = require(rootTsconfigFile); |
| 84 | + rootTsconfig.compilerOptions = rootTsconfig.compilerOptions || {}; |
| 85 | + rootTsconfig.compilerOptions.composite = true; |
| 86 | + rootTsconfig.references = rootRefs; |
| 87 | + // Convert to JSON |
| 88 | + const rootTsconfigJson = JSON.stringify(rootTsconfig, null, 2); |
| 89 | + if (!dryRun) { |
| 90 | + // Using `-f` to overwrite tsconfig.json |
| 91 | + fs.writeFileSync(rootTsconfigFile, rootTsconfigJson + '\n', { |
| 92 | + encoding: 'utf-8', |
| 93 | + }); |
| 94 | + debug('%s has been updated.', rootTsconfigFile); |
| 95 | + } else { |
| 96 | + debug(rootTsconfigJson); |
| 97 | + console.log( |
| 98 | + 'This is a dry-run. Please use -f option to update tsconfig files.', |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +if (require.main === module) { |
| 104 | + const dryRun = process.argv[2] !== '-f'; |
| 105 | + updateReferences({dryRun}); |
| 106 | +} |
0 commit comments