From d69a2253dedfa83a5f244c0b52eeb3a641bd0dec Mon Sep 17 00:00:00 2001 From: virkt25 Date: Wed, 1 Aug 2018 13:22:48 -0400 Subject: [PATCH] fix(cli): install dependencies for clones examples --- docs/site/Download-examples.md | 6 +++--- .../Preparing-the-API-for-consumption.shelved.md | 1 - examples/hello-world/README.md | 4 ++-- examples/log-extension/README.md | 2 +- examples/rpc-server/README.md | 4 ++-- examples/todo-list/README.md | 4 ++-- examples/todo/README.md | 4 ++-- packages/cli/generators/example/index.js | 6 ++++++ packages/cli/lib/base-generator.js | 12 ++++++++++++ packages/cli/lib/project-generator.js | 9 --------- .../integration/generators/example.integration.js | 4 ++-- 11 files changed, 32 insertions(+), 24 deletions(-) diff --git a/docs/site/Download-examples.md b/docs/site/Download-examples.md index 6199b227ebed..8e25d9432b3c 100644 --- a/docs/site/Download-examples.md +++ b/docs/site/Download-examples.md @@ -36,6 +36,6 @@ The tool will prompt you for: ### Output -The example project is downloaded to a new directory. For example, when -downloading `todo` example, the tool stores the files under the newly created -`loopback4-example-todo` directory. +The example project is downloaded to a new directory and its dependencies are +installed. For example, when downloading `todo` example, the tool stores the +files under the newly created `loopback4-example-todo` directory. diff --git a/docs/site/Preparing-the-API-for-consumption.shelved.md b/docs/site/Preparing-the-API-for-consumption.shelved.md index 56e1a7a47a68..31b4d197a42f 100644 --- a/docs/site/Preparing-the-API-for-consumption.shelved.md +++ b/docs/site/Preparing-the-API-for-consumption.shelved.md @@ -24,7 +24,6 @@ application: ```sh lb4 example todo cd loopback4-example-todo -npm i npm start ``` diff --git a/examples/hello-world/README.md b/examples/hello-world/README.md index a645ebbc4e2a..9639aab0691a 100644 --- a/examples/hello-world/README.md +++ b/examples/hello-world/README.md @@ -35,10 +35,10 @@ npm i -g @loopback/cli lb4 example hello-world ``` -3. Switch to the directory and install dependencies. +3. Switch to the directory. ```sh -cd loopback4-example-hello-world && npm i +cd loopback4-example-hello-world ``` ## Use diff --git a/examples/log-extension/README.md b/examples/log-extension/README.md index e60bdff611e6..193dfc2e78bc 100644 --- a/examples/log-extension/README.md +++ b/examples/log-extension/README.md @@ -63,7 +63,7 @@ You can obtain a local clone of this project (without the rest of our monorepo) using the following command: ```sh -lb4 example todo +lb4 example log-extension ``` ## Tutorial diff --git a/examples/rpc-server/README.md b/examples/rpc-server/README.md index b58d0782c544..d7f924327af3 100644 --- a/examples/rpc-server/README.md +++ b/examples/rpc-server/README.md @@ -19,10 +19,10 @@ npm i -g @loopback/cli lb4 example rpc-server ``` -3. Switch to the directory and install dependencies. +3. Switch to the directory. ```sh -cd loopback-example-rpc-server && npm i +cd loopback-example-rpc-server ``` 4. Start the app! diff --git a/examples/todo-list/README.md b/examples/todo-list/README.md index 3c662820e7ac..bc7947aea39f 100644 --- a/examples/todo-list/README.md +++ b/examples/todo-list/README.md @@ -69,10 +69,10 @@ application, follow these steps: rpc-server: A basic RPC server using a made-up protocol. ``` -2. Jump into the directory and then install the required dependencies: +2. Switch to the directory. ```sh - cd loopback4-example-todo-list && npm i + cd loopback4-example-todo-list ``` 3. Finally, start the application! diff --git a/examples/todo/README.md b/examples/todo/README.md index f04357946758..5cd5cdb1b2cc 100644 --- a/examples/todo/README.md +++ b/examples/todo/README.md @@ -62,10 +62,10 @@ application, follow these steps: rpc-server: A basic RPC server using a made-up protocol. ``` -2. Jump into the directory and then install the required dependencies: +2. Switch to the directory. ```sh - cd loopback4-example-todo && npm i + cd loopback4-example-todo ``` 3. Finally, start the application! diff --git a/packages/cli/generators/example/index.js b/packages/cli/generators/example/index.js index 20739d3d523b..018a1a5bd0d5 100644 --- a/packages/cli/generators/example/index.js +++ b/packages/cli/generators/example/index.js @@ -93,6 +93,12 @@ module.exports = class extends BaseGenerator { this.outDir = path.relative(cwd, absOutDir); } + install() { + if (this.shouldExit()) return false; + this.destinationRoot(this.outDir); + return super.install(); + } + end() { if (!super.end()) return false; this.log(); diff --git a/packages/cli/lib/base-generator.js b/packages/cli/lib/base-generator.js index cab2a18da066..6c06cabffb13 100644 --- a/packages/cli/lib/base-generator.js +++ b/packages/cli/lib/base-generator.js @@ -257,6 +257,18 @@ module.exports = class BaseGenerator extends Generator { this.exitGeneration = reason; } + /** + * Run `npm install` in the project + */ + install() { + if (this.shouldExit()) return false; + const opts = this.options.npmInstall || {}; + const spawnOpts = Object.assign({}, this.options.spawn, { + cwd: this.destinationRoot(), + }); + this.npmInstall(null, opts, spawnOpts); + } + /** * Checks if current directory is a LoopBack project by checking for * keyword 'loopback' under 'keywords' attribute in package.json. diff --git a/packages/cli/lib/project-generator.js b/packages/cli/lib/project-generator.js index 07c08c491419..72a0973da8c8 100644 --- a/packages/cli/lib/project-generator.js +++ b/packages/cli/lib/project-generator.js @@ -246,13 +246,4 @@ module.exports = class ProjectGenerator extends BaseGenerator { this.fs.delete(this.destinationPath('.vscode')); } } - - install() { - if (this.shouldExit()) return false; - const opts = this.options.npmInstall || {}; - const spawnOpts = Object.assign({}, this.options.spawn, { - cwd: this.destinationRoot(), - }); - this.npmInstall(null, opts, spawnOpts); - } }; diff --git a/packages/cli/test/integration/generators/example.integration.js b/packages/cli/test/integration/generators/example.integration.js index 09a964f477c1..390b37e41d2e 100644 --- a/packages/cli/test/integration/generators/example.integration.js +++ b/packages/cli/test/integration/generators/example.integration.js @@ -49,7 +49,7 @@ describe('lb4 example', function() { .executeGenerator(generator) .withPrompts({name: VALID_EXAMPLE}) .then(() => { - const targetPkgFile = `loopback4-example-${VALID_EXAMPLE}/package.json`; + const targetPkgFile = 'package.json'; const originalPkgMeta = require(`../../../../../examples/${VALID_EXAMPLE}/package.json`); assert.file(targetPkgFile); assert.jsonFileContent(targetPkgFile, { @@ -64,7 +64,7 @@ describe('lb4 example', function() { .executeGenerator(generator) .withArguments([VALID_EXAMPLE]) .then(() => { - const targetPkgFile = `loopback4-example-${VALID_EXAMPLE}/package.json`; + const targetPkgFile = 'package.json'; const originalPkgMeta = require(`../../../../../examples/${VALID_EXAMPLE}/package.json`); assert.file(targetPkgFile); assert.jsonFileContent(targetPkgFile, {