Skip to content

Commit bb6ee51

Browse files
Deric Cainbajtos
authored andcommitted
feat: use dependency instead of keyword to check loopback projects
1 parent ce74b80 commit bb6ee51

File tree

6 files changed

+44
-36
lines changed

6 files changed

+44
-36
lines changed

packages/cli/lib/base-generator.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,14 @@ module.exports = class BaseGenerator extends Generator {
305305

306306
/**
307307
* Checks if current directory is a LoopBack project by checking for
308-
* keyword 'loopback' under 'keywords' attribute in package.json.
309-
* 'keywords' is an array
308+
* "@loopback/core" package in the dependencies section of the
309+
* package.json.
310310
*/
311311
async checkLoopBackProject() {
312312
debug('Checking for loopback project');
313313
if (this.shouldExit()) return false;
314314
const pkg = this.fs.readJSON(this.destinationPath('package.json'));
315-
const key = 'loopback';
315+
316316
if (!pkg) {
317317
const err = new Error(
318318
'No package.json found in ' +
@@ -323,20 +323,25 @@ module.exports = class BaseGenerator extends Generator {
323323
this.exit(err);
324324
return;
325325
}
326-
if (!pkg.keywords || !pkg.keywords.includes(key)) {
326+
327+
this.packageJson = pkg;
328+
329+
const projectDeps = pkg.dependencies || {};
330+
const projectDevDeps = pkg.devDependencies || {};
331+
332+
const dependentPackage = '@loopback/core';
333+
const projectDepsNames = Object.keys(projectDeps);
334+
335+
if (!projectDepsNames.includes(dependentPackage)) {
327336
const err = new Error(
328-
'No `loopback` keyword found in ' +
337+
'No `@loopback/core` package found in the "dependencies" section of ' +
329338
this.destinationPath('package.json') +
330339
'. ' +
331340
'The command must be run in a LoopBack project.',
332341
);
333342
this.exit(err);
334343
return;
335344
}
336-
this.packageJson = pkg;
337-
338-
const projectDeps = pkg.dependencies || {};
339-
const projectDevDeps = pkg.devDependencies || {};
340345

341346
const cliPkg = require('../package.json');
342347
const templateDeps = cliPkg.config.templateDependencies;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ describe('lb4 controller', () => {
5656
).to.be.rejectedWith(/No package.json found in/);
5757
});
5858

59-
it('does not run without the loopback keyword', () => {
59+
it('does not run without "@loopback/core" as a dependency', () => {
6060
return expect(
6161
testUtils
6262
.executeGenerator(generator)
6363
.inDir(SANDBOX_PATH, () =>
64-
testUtils.givenLBProject(SANDBOX_PATH, {excludeKeyword: true}),
64+
testUtils.givenLBProject(SANDBOX_PATH, {excludeLoopbackCore: true}),
6565
)
6666
.withPrompts(basicCLIInput),
67-
).to.be.rejectedWith(/No `loopback` keyword found in/);
67+
).to.be.rejectedWith(/No `@loopback\/core` package found/);
6868
});
6969

7070
describe('basic controller', () => {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ describe('lb4 datasource integration', () => {
8686
).to.be.rejectedWith(/No package.json found in/);
8787
});
8888

89-
it('does not run without the loopback keyword', () => {
89+
it('does not run without the "@loopback/core" dependency', () => {
9090
return expect(
9191
testUtils
9292
.executeGenerator(generator)
9393
.inDir(SANDBOX_PATH, () =>
94-
testUtils.givenLBProject(SANDBOX_PATH, {excludeKeyword: true}),
94+
testUtils.givenLBProject(SANDBOX_PATH, {excludeLoopbackCore: true}),
9595
)
9696
.withPrompts(basicCLIInput),
97-
).to.be.rejectedWith(/No `loopback` keyword found in/);
97+
).to.be.rejectedWith(/No `@loopback\/core` package found/);
9898
});
9999

100100
describe('basic datasource', () => {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,23 @@ describe('lb4 model integration', () => {
4949
).to.be.rejectedWith(/No package.json found in/);
5050
});
5151

52-
it('does not run without the loopback keyword', () => {
52+
it('does not run without the "@loopback/core" dependency', () => {
5353
return expect(
5454
testUtils
5555
.executeGenerator(generator)
5656
.inDir(SANDBOX_PATH, () =>
57-
testUtils.givenLBProject(SANDBOX_PATH, {excludeKeyword: true}),
57+
testUtils.givenLBProject(SANDBOX_PATH, {excludeLoopbackCore: true}),
5858
)
5959
.withPrompts(basicCLIInput),
60-
).to.be.rejectedWith(/No `loopback` keyword found in/);
60+
).to.be.rejectedWith(/No `@loopback\/core` package found/);
6161
});
6262

6363
it('does not run if passed an invalid model from command line', () => {
6464
return expect(
6565
testUtils
6666
.executeGenerator(generator)
6767
.inDir(SANDBOX_PATH, () =>
68-
testUtils.givenLBProject(SANDBOX_PATH, {excludeKeyword: false}),
68+
testUtils.givenLBProject(SANDBOX_PATH, {excludeLoopbackCore: false}),
6969
)
7070
.withArguments('myNewModel --base InvalidModel'),
7171
).to.be.rejectedWith(/Model was not found in/);

packages/cli/test/integration/lib/artifact-generator.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,27 @@ module.exports = function(artiGenerator) {
7474
undefined,
7575
/No package.json found/,
7676
);
77+
7778
testCheckLoopBack(
78-
'throws an error if "keywords" key does not exist',
79-
{foobar: 'test'},
80-
/No `loopback` keyword found/,
81-
);
82-
testCheckLoopBack(
83-
'throws an error if "keywords" key does not map to an array with "loopback" as a member',
84-
{keywords: ['foobar', 'test']},
85-
/No `loopback` keyword found/,
79+
'throws an error if "@loopback/core" is not a dependency',
80+
{dependencies: {}},
81+
/No `@loopback\/core` package found/,
8682
);
8783

8884
testCheckLoopBack(
8985
'throws an error if dependencies have incompatible versions',
9086
{
91-
keywords: ['loopback'],
92-
dependencies: {'@loopback/context': '^0.0.0'},
87+
dependencies: {
88+
'@loopback/context': '^0.0.0',
89+
'@loopback/core': '^0.0.0',
90+
},
9391
},
9492
/Incompatible dependencies/,
9593
);
9694

9795
testCheckLoopBack(
9896
'allows */x/X for version range',
9997
{
100-
keywords: ['loopback'],
10198
devDependencies: {'@types/node': '*'},
10299
dependencies: {
103100
'@loopback/context': 'x.x',
@@ -107,8 +104,7 @@ module.exports = function(artiGenerator) {
107104
// No expected error here
108105
);
109106

110-
it('passes if "keywords" maps to "loopback"', async () => {
111-
gen.fs.readJSON.returns({keywords: ['test', 'loopback']});
107+
it('passes if "@loopback/core" is a dependency', async () => {
112108
await gen.checkLoopBackProject();
113109
});
114110

packages/cli/test/test-utils.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ exports.executeGenerator = function(GeneratorOrNamespace, settings) {
5454
*
5555
* @param {string} rootDir Root directory in which to create the project
5656
* @param {Object} options
57-
* @property {boolean} excludeKeyword Excludes the 'loopback' keyword in package.json
57+
* @property {boolean} excludeLoopbackCore Excludes the '@loopback/core' dependency in package.json
5858
* @property {boolean} excludePackageJSON Excludes package.json
5959
* @property {boolean} excludeYoRcJSON Excludes .yo-rc.json
6060
* @property {boolean} excludeControllersDir Excludes the controllers directory
@@ -70,9 +70,16 @@ exports.givenLBProject = function(rootDir, options) {
7070
options = options || {};
7171
const sandBoxFiles = options.additionalFiles || [];
7272

73-
const content = {};
74-
if (!options.excludeKeyword) {
75-
content.keywords = ['loopback'];
73+
const content = {
74+
dependencies: {
75+
'@loopback/core': '*',
76+
},
77+
};
78+
79+
// We infer if a project is loopback by checking whether its dependencies includes @loopback/core or not.
80+
// This flag is created for testing invalid loopback projects.
81+
if (options.excludeLoopbackCore) {
82+
delete content.dependencies['@loopback/core'];
7683
}
7784

7885
if (!options.excludePackageJSON) {

0 commit comments

Comments
 (0)