Skip to content

Commit 763b9c8

Browse files
committed
fix(cli): remove project references from examples
In #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š <[email protected]>
1 parent de3f080 commit 763b9c8

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

packages/cli/generators/example/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,22 @@ module.exports = class extends BaseGenerator {
132132
const absOutDir = await downloadAndExtractExample(this.exampleName, cwd);
133133
this.outDir = path.relative(cwd, absOutDir);
134134
const tsconfig = path.join(absOutDir, 'tsconfig.json');
135+
136+
// Support older versions of examples that are using `tsconfig.build.json`
135137
const tsBuildConfig = path.join(absOutDir, 'tsconfig.build.json');
136138
const exists = await fs.exists(tsconfig);
137-
if (exists) return;
138-
return fs.rename(tsBuildConfig, tsconfig);
139+
if (!exists) {
140+
return fs.rename(tsBuildConfig, tsconfig);
141+
}
142+
143+
// Recent versions of examples are using project references inside monorepo,
144+
// see https://github.com/strongloop/loopback-next/pull/5155
145+
// We must switch to standalone mode (no project references) when the code
146+
// was checked out outside of our monorepo.
147+
const tsconfigContent = await fs.readJson(tsconfig);
148+
delete tsconfigContent.references;
149+
tsconfigContent.compilerOptions.composite = false;
150+
await fs.writeJson(tsconfig, tsconfigContent);
139151
}
140152

141153
install() {

packages/cli/test/integration/generators/example.integration.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const assert = require('yeoman-assert');
99
const expect = require('@loopback/testlab').expect;
1010
const path = require('path');
11+
const {readJsonSync} = require('fs-extra');
1112

1213
const generator = path.join(__dirname, '../../../generators/example');
1314
const baseTests = require('../lib/base-generator')(generator);
@@ -87,4 +88,28 @@ describe('lb4 example', function () {
8788
},
8889
);
8990
});
91+
92+
it('removes project references from tsconfig', () => {
93+
return testUtils
94+
.executeGenerator(generator)
95+
.withPrompts({name: VALID_EXAMPLE})
96+
.then(() => {
97+
const tsconfigFile = 'tsconfig.json';
98+
const expectedConfig = readJsonSync(
99+
require.resolve(
100+
`../../../../../examples/${VALID_EXAMPLE}/${tsconfigFile}`,
101+
),
102+
);
103+
delete expectedConfig.references;
104+
expectedConfig.compilerOptions.composite = false;
105+
106+
assert.file(tsconfigFile);
107+
108+
// IMPORTANT! We cannot use `assert.jsonFileContent` here
109+
// because the helper only checks if the file contains all expected
110+
// properties, it does not verify there is no additional data.
111+
const actualConfig = readJsonSync(tsconfigFile);
112+
expect(actualConfig).to.deepEqual(expectedConfig);
113+
});
114+
});
90115
});

0 commit comments

Comments
 (0)