From 763b9c8332eb3c71307ea09fc6e01cd0b9dfaeb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Thu, 23 Apr 2020 09:47:35 +0200 Subject: [PATCH] fix(cli): remove project references from examples MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In strongloop/loopback-next#5155, we reworked our typescript setup to leverage composite mode and project references. This breaks our example applications when they are checked out standalone, typically via `lb4 example`. ```sh $ lb4 example todo (...) $ cd loopback4-example-todo $ npm t (...) > lb-tsc error TS6053: File '/private/packages/http-caching-proxy/tsconfig.json' not found. error TS6053: File '/private/packages/testlab/tsconfig.json' not found. (...) Found 10 errors. ``` This commit fixes the problem by introducing a new step to `lb4 example` command where we remove `references` field from `tsconfig.json` file and set the field `compilerOptions.composite` to `false`. Signed-off-by: Miroslav Bajtoš --- packages/cli/generators/example/index.js | 16 ++++++++++-- .../generators/example.integration.js | 25 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/packages/cli/generators/example/index.js b/packages/cli/generators/example/index.js index 4059053121f9..ecd70092aa37 100644 --- a/packages/cli/generators/example/index.js +++ b/packages/cli/generators/example/index.js @@ -132,10 +132,22 @@ module.exports = class extends BaseGenerator { const absOutDir = await downloadAndExtractExample(this.exampleName, cwd); this.outDir = path.relative(cwd, absOutDir); const tsconfig = path.join(absOutDir, 'tsconfig.json'); + + // Support older versions of examples that are using `tsconfig.build.json` const tsBuildConfig = path.join(absOutDir, 'tsconfig.build.json'); const exists = await fs.exists(tsconfig); - if (exists) return; - return fs.rename(tsBuildConfig, tsconfig); + if (!exists) { + return fs.rename(tsBuildConfig, tsconfig); + } + + // Recent versions of examples are using project references inside monorepo, + // see https://github.com/strongloop/loopback-next/pull/5155 + // We must switch to standalone mode (no project references) when the code + // was checked out outside of our monorepo. + const tsconfigContent = await fs.readJson(tsconfig); + delete tsconfigContent.references; + tsconfigContent.compilerOptions.composite = false; + await fs.writeJson(tsconfig, tsconfigContent); } install() { diff --git a/packages/cli/test/integration/generators/example.integration.js b/packages/cli/test/integration/generators/example.integration.js index de908c331aa9..7f990533bcb2 100644 --- a/packages/cli/test/integration/generators/example.integration.js +++ b/packages/cli/test/integration/generators/example.integration.js @@ -8,6 +8,7 @@ const assert = require('yeoman-assert'); const expect = require('@loopback/testlab').expect; const path = require('path'); +const {readJsonSync} = require('fs-extra'); const generator = path.join(__dirname, '../../../generators/example'); const baseTests = require('../lib/base-generator')(generator); @@ -87,4 +88,28 @@ describe('lb4 example', function () { }, ); }); + + it('removes project references from tsconfig', () => { + return testUtils + .executeGenerator(generator) + .withPrompts({name: VALID_EXAMPLE}) + .then(() => { + const tsconfigFile = 'tsconfig.json'; + const expectedConfig = readJsonSync( + require.resolve( + `../../../../../examples/${VALID_EXAMPLE}/${tsconfigFile}`, + ), + ); + delete expectedConfig.references; + expectedConfig.compilerOptions.composite = false; + + assert.file(tsconfigFile); + + // IMPORTANT! We cannot use `assert.jsonFileContent` here + // because the helper only checks if the file contains all expected + // properties, it does not verify there is no additional data. + const actualConfig = readJsonSync(tsconfigFile); + expect(actualConfig).to.deepEqual(expectedConfig); + }); + }); });