Skip to content

Commit

Permalink
fix(cli): remove project references from examples
Browse files Browse the repository at this point in the history
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` and `compilerOptions.composite`
field from `tsconfig.json` file.

Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Apr 23, 2020
1 parent de3f080 commit b63827d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/cli/generators/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
delete tsconfigContent.compilerOptions.composite;
await fs.writeJson(tsconfig, tsconfigContent);
}

install() {
Expand Down
25 changes: 25 additions & 0 deletions packages/cli/test/integration/generators/example.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
delete expectedConfig.compilerOptions.composite;

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);
});
});
});

0 comments on commit b63827d

Please sign in to comment.