Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): remove project references from examples #5204

Merged
merged 1 commit into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
tsconfigContent.compilerOptions.composite = false;
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;
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);
});
});
});