-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(cli): lb4 example [<example-name>] #848
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| node_modules | ||
| coverage | ||
| node_modules | ||
| test/sandbox |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Copyright IBM Corp. 2018. All Rights Reserved. | ||
| // Node module: @loopback/cli | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const promisify = require('../../lib/promisify'); | ||
|
|
||
| const gunzip = require('gunzip-maybe'); | ||
| const path = require('path'); | ||
| const request = require('request'); | ||
| const tar = require('tar-fs'); | ||
|
|
||
| const GITHUB_ARCHIVE_URL = | ||
| 'https://github.com/strongloop/loopback-next/tarball/master'; | ||
|
|
||
| module.exports = function cloneExampleFromGitHub(exampleName, cwd) { | ||
| const outDir = path.join(cwd, `loopback4-example-${exampleName}`); | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| request(GITHUB_ARCHIVE_URL) | ||
| .pipe(gunzip()) | ||
| .pipe(untar(outDir, exampleName)) | ||
| .on('error', reject) | ||
| .on('finish', () => resolve(outDir)); | ||
| }); | ||
| }; | ||
|
|
||
| function untar(outDir, exampleName) { | ||
| // The top directory is in the format "{org}-{repo}-{sha1}" | ||
| // I am intentionally not matching an exact repository name, because I expect | ||
| // it will change in the future, e.g. from "loopback-next" to "loopback4". | ||
| // I am also assuming that example names never contain special RegExp | ||
| // characters. | ||
| const matchTopDir = /^strongloop-loopback[^\/]*\//; | ||
|
|
||
| const sourceDir = `packages/example-${exampleName}/`; | ||
|
|
||
| // Unfortunately the tar-fs is designed in such way that "map" is called | ||
| // before "ignore" and there is no way how "map" can mark an entry for | ||
| // skipping. | ||
| // As a workaround, we are renaming all entries we want to ignore to a file name | ||
| // containing this placeholder value. The value is crafted in such way | ||
| // that the probability of a conflict with a real file in LoopBack repo | ||
| // is minimal. | ||
| const DISCARD_THIS_ENTRY = 'IGNORE_THIS_ENTRY_1B6DAPkxt3'; | ||
|
|
||
| const DISCARD_ABSOLUTE_PATH = path.resolve(outDir, DISCARD_THIS_ENTRY); | ||
| const tarOptions = { | ||
| map: header => { | ||
| // Remove the top dir like "strongloop-loopback-next-a50405a" | ||
| let name = header.name.replace(matchTopDir, ''); | ||
|
|
||
| // Remove "packages/example-{name}" of files we want to keep, | ||
| // rename the entry to a special value for files we want to discard. | ||
| header.name = name.startsWith(sourceDir) | ||
| ? name.slice(sourceDir.length) | ||
| : DISCARD_THIS_ENTRY; | ||
|
|
||
| return header; | ||
| }, | ||
|
|
||
| // Ignore files outside of our example package | ||
| ignore: absolutePath => absolutePath === DISCARD_ABSOLUTE_PATH, | ||
| }; | ||
|
|
||
| return tar.extract(outDir, tarOptions); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Copyright IBM Corp. 2018. All Rights Reserved. | ||
| // Node module: @loopback/cli | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| ('use strict'); | ||
|
|
||
| const BaseGenerator = require('../../lib/base-generator'); | ||
| const chalk = require('chalk'); | ||
| const cloneExampleFromGitHub = require('./clone-example'); | ||
| const path = require('path'); | ||
| const utils = require('../../lib/utils'); | ||
|
|
||
| const EXAMPLES = { | ||
| codehub: 'A GitHub-like application we used to use to model LB4 API.', | ||
| }; | ||
| Object.freeze(EXAMPLES); | ||
|
|
||
| module.exports = class extends BaseGenerator { | ||
| static getAllExamples() { | ||
| return EXAMPLES; | ||
| } | ||
|
|
||
| // Note: arguments and options should be defined in the constructor. | ||
| constructor(args, opts) { | ||
| super(args, opts); | ||
| } | ||
|
|
||
| _setupGenerator() { | ||
| this.projectType = 'example'; | ||
| this.argument('example-name', { | ||
| type: String, | ||
| description: 'Name of the example to clone', | ||
| required: false, | ||
| }); | ||
|
|
||
| return super._setupGenerator(); | ||
| } | ||
|
|
||
| help() { | ||
| const examplesHelp = Object.keys(EXAMPLES) | ||
| .map(name => ` ${name}: ${EXAMPLES[name]}`) | ||
| .join('\n'); | ||
|
|
||
| return super.help() + `\nAvailable examples:\n${examplesHelp}\n`; | ||
| } | ||
|
|
||
| _describeExamples() {} | ||
|
|
||
| promptExampleName() { | ||
| if (this.options['example-name']) { | ||
| this.exampleName = this.options['example-name']; | ||
| return; | ||
| } | ||
|
|
||
| const choices = Object.keys(EXAMPLES).map(k => { | ||
| return { | ||
| name: `${k}: ${EXAMPLES[k]}`, | ||
| value: `${k}`, | ||
| short: `${k}`, | ||
| }; | ||
| }); | ||
| const prompts = [ | ||
| { | ||
| name: 'name', | ||
| message: 'What example would you like to clone?', | ||
| type: 'list', | ||
| choices, | ||
| }, | ||
| ]; | ||
| return this.prompt(prompts).then( | ||
| answers => (this.exampleName = answers.name) | ||
| ); | ||
| } | ||
|
|
||
| validateExampleName() { | ||
| if (this.exampleName in EXAMPLES) return; | ||
| this.exit( | ||
| `Invalid example name: ${this.exampleName}\n` + | ||
| 'Run "lb4 example --help" to print the list of available example names.' | ||
| ); | ||
| } | ||
|
|
||
| cloneExampleFromGitHub() { | ||
| if (this.shouldExit()) return false; | ||
| const cwd = process.cwd(); | ||
| return cloneExampleFromGitHub(this.exampleName, cwd).then(o => { | ||
| this.outDir = path.relative(cwd, o); | ||
| }); | ||
| } | ||
|
|
||
| end() { | ||
| if (!super.end()) return false; | ||
| this.log(); | ||
| this.log(`The example was cloned to ${chalk.green(this.outDir)}.`); | ||
| this.log(); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright IBM Corp. 2018. All Rights Reserved. | ||
| // Node module: @loopback/cli | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| // A temporary polyfill for util.promisify on Node.js 6.x | ||
| // Remove it as part of https://github.com/strongloop/loopback-next/issues/611 | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const nativePromisify = require('util').promisify; | ||
|
|
||
| /** | ||
| * Polyfill promisify and use `util.promisify` if available | ||
| * @param func A callback-style function | ||
| */ | ||
| module.exports = function promisify(func) { | ||
| if (nativePromisify) return nativePromisify(func); | ||
|
|
||
| // The simplest implementation of Promisify | ||
| return (...args) => { | ||
| return new Promise((resolve, reject) => { | ||
| try { | ||
| func(...args, (err, result) => { | ||
| if (err) reject(err); | ||
| else resolve(result); | ||
| }); | ||
| } catch (err) { | ||
| reject(err); | ||
| } | ||
| }); | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Copyright IBM Corp. 2018. All Rights Reserved. | ||
| // Node module: @loopback/cli | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const promisify = require('../lib/promisify'); | ||
|
|
||
| const cloneExampleFromGitHub = require('../generators/example/clone-example'); | ||
| const expect = require('@loopback/testlab').expect; | ||
| const fs = require('fs'); | ||
| const glob = promisify(require('glob')); | ||
| const path = require('path'); | ||
| const rimraf = promisify(require('rimraf')); | ||
|
|
||
| const VALID_EXAMPLE = 'codehub'; | ||
| const SANDBOX = path.resolve(__dirname, 'sandbox'); | ||
|
|
||
| describe('cloneExampleFromGitHub', function() { | ||
| this.timeout(10000); | ||
|
|
||
| beforeEach(resetSandbox); | ||
|
|
||
| it('extracts all project files', () => { | ||
| return cloneExampleFromGitHub(VALID_EXAMPLE, SANDBOX) | ||
| .then(outDir => { | ||
| return Promise.all([ | ||
| glob('**', { | ||
| cwd: path.join(__dirname, `../../example-${VALID_EXAMPLE}`), | ||
| ignore: 'node_modules/**', | ||
| }), | ||
| glob('**', { | ||
| cwd: outDir, | ||
| ignore: 'node_modules/**', | ||
| }), | ||
| ]); | ||
| }) | ||
| .then(found => { | ||
| const [expected, actual] = found; | ||
| expect(actual).to.deepEqual(expected); | ||
| }); | ||
| }); | ||
|
|
||
| function resetSandbox() { | ||
| return rimraf(SANDBOX); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Copyright IBM Corp. 2018. All Rights Reserved. | ||
| // Node module: @loopback/cli | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const assert = require('yeoman-assert'); | ||
| const fs = require('fs'); | ||
| const expect = require('@loopback/testlab').expect; | ||
| const path = require('path'); | ||
|
|
||
| const generator = path.join(__dirname, '../generators/example'); | ||
| const baseTests = require('./base-generator')(generator); | ||
| const testUtils = require('./test-utils'); | ||
|
|
||
| const ALL_EXAMPLES = require(generator).getAllExamples(); | ||
| const VALID_EXAMPLE = 'codehub'; | ||
|
|
||
| describe('lb4 example', function() { | ||
| this.timeout(10000); | ||
|
|
||
| describe('correctly extends BaseGenerator', baseTests); | ||
|
|
||
| describe('_setupGenerator', () => { | ||
| it('has name argument set up', () => { | ||
| const helpText = getHelpText(); | ||
| expect(helpText) | ||
| .to.match(/\[<example-name>\]/) | ||
| .and.match(/# Name of the example/) | ||
| .and.match(/Type: String/) | ||
| .and.match(/Required: false/); | ||
| }); | ||
|
|
||
| it('lists all example names in help', () => { | ||
| const helpText = getHelpText(); | ||
| expect(helpText).to.match( | ||
| new RegExp(Object.keys(ALL_EXAMPLES).join('.*')) | ||
| ); | ||
| }); | ||
|
|
||
| function getHelpText() { | ||
| return testUtils.testSetUpGen(generator).help(); | ||
| } | ||
| }); | ||
|
|
||
| it('accepts the example name via interactive prompt', () => { | ||
| return testUtils | ||
| .executeGenerator(generator) | ||
| .withPrompts({name: VALID_EXAMPLE}) | ||
| .then(() => { | ||
| const targetPkgFile = `loopback4-example-${VALID_EXAMPLE}/package.json`; | ||
| const originalPkgMeta = require('../../example-codehub/package.json'); | ||
| assert.file(targetPkgFile); | ||
| assert.jsonFileContent(targetPkgFile, { | ||
| name: originalPkgMeta.name, | ||
| version: originalPkgMeta.version, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('accepts the example name as a CLI argument', () => { | ||
| return testUtils | ||
| .executeGenerator(generator) | ||
| .withArguments([VALID_EXAMPLE]) | ||
| .then(() => { | ||
| const targetPkgFile = `loopback4-example-${VALID_EXAMPLE}/package.json`; | ||
| const originalPkgMeta = require('../../example-codehub/package.json'); | ||
| assert.file(targetPkgFile); | ||
| assert.jsonFileContent(targetPkgFile, { | ||
| name: originalPkgMeta.name, | ||
| version: originalPkgMeta.version, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('rejects invalid example names', () => { | ||
| return testUtils | ||
| .executeGenerator(generator) | ||
| .withArguments(['example-does-not-exist']) | ||
| .then( | ||
| () => { | ||
| throw new Error('Generator should have failed.'); | ||
| }, | ||
| err => { | ||
| expect(err).to.match(/Invalid example name/); | ||
| } | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of scope for the PR, but we probably should refactor the list of generators into a separate file, such as:
generators.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, I'd like us to discover all generators automatically by scanning
generatorsdirectory. I believe Yeoman is already doing that (or was doing that in earlier versions) for projects that are following prescribed project layout (see our generator-loopback for an example).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we really want to use Yeoman discovery, which is one of areas that caused confusions in the past.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree we probably don't want to use Yeoman discovery, I'd like us to write our own to follow the conventions we like most. But yeah, it's out of scope of this pull request.