-
Notifications
You must be signed in to change notification settings - Fork 70
Add boot script generator #93
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| Description: | ||
| Creates a new boot script in the LoopBack application. | ||
|
|
||
| Example: | ||
|
|
||
| slc loopback:boot-script [your-script-name] | ||
|
|
||
| This generates a boot script in `/server/boot`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| 'use strict'; | ||
| var helpers = require('../lib/helpers'); | ||
| var path = require('path'); | ||
| var yeoman = require('yeoman-generator'); | ||
|
|
||
| module.exports = yeoman.generators.Base.extend({ | ||
| constructor: function() { | ||
| yeoman.generators.Base.apply(this, arguments); | ||
|
|
||
| this.argument('name', { | ||
| desc: 'Name of the boot script to create.', | ||
| required: false, | ||
| optional: true, | ||
| type: String | ||
| }); | ||
| }, | ||
|
|
||
| help: function() { | ||
| return helpers.customHelp(this); | ||
| }, | ||
|
|
||
| askForName: function() { | ||
| var done = this.async(); | ||
|
|
||
| if (this.name) return done(); | ||
|
|
||
| var question = { | ||
| name: 'name', | ||
| message: 'Enter the script name (without `.js`):', | ||
| default: this.name, | ||
| validate: helpers.validateName | ||
| }; | ||
|
|
||
| this.prompt(question, function(answer) { | ||
| this.name = answer.name; | ||
| done(); | ||
| }.bind(this)); | ||
| }, | ||
|
|
||
| askForType: function() { | ||
| var done = this.async(); | ||
|
|
||
| var question = { | ||
| name: 'type', | ||
| message: 'What type of boot script do you want to generate?', | ||
| type: 'list', | ||
| choices: ['async', 'sync'], | ||
| default: 'async' | ||
| }; | ||
|
|
||
| this.prompt(question, function(answer) { | ||
| this.type = answer.type; | ||
| done(); | ||
| }.bind(this)); | ||
| }, | ||
|
|
||
| generate: function() { | ||
| var source = this.templatePath(this.type + '.js'); | ||
|
|
||
| var targetPath = path.normalize('/server/boot/' + this.name + '.js'); | ||
| var target = this.destinationPath(targetPath); | ||
|
|
||
| this.copy(source, target); | ||
| } | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| module.exports = function(app, cb) { | ||
| /* | ||
| * The `app` object provides access to a variety of LoopBack resources such as | ||
| * models (e.g. `app.models.YourModelName`) or data sources (e.g. | ||
| * `app.datasources.YourDataSource`). See | ||
| * http://docs.strongloop.com/display/public/LB/Working+with+LoopBack+objects | ||
| * for more info. | ||
| */ | ||
| process.nextTick(cb); // Remove if you pass `cb` to an async function yourself | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @raymondfeng PTAL |
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| module.exports = function(app) { | ||
| /* | ||
| * The `app` object provides access to a variety of LoopBack resources such as | ||
| * models (e.g. `app.models.YourModelName`) or data sources (e.g. | ||
| * `app.datasources.YourDataSource`). See | ||
| * http://docs.strongloop.com/display/public/LB/Working+with+LoopBack+objects | ||
| * for more info. | ||
| */ | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @raymondfeng PTAL
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Link is nice. |
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /*global describe, beforeEach, it */ | ||
| 'use strict'; | ||
| var path = require('path'); | ||
| var helpers = require('yeoman-generator').test; | ||
| var SANDBOX = path.resolve(__dirname, 'sandbox'); | ||
| var fs = require('fs'); | ||
| var expect = require('chai').expect; | ||
| var common = require('./common'); | ||
|
|
||
| describe('loopback:boot-script generator', function() { | ||
| beforeEach(common.resetWorkspace); | ||
|
|
||
| beforeEach(function createSandbox(done) { | ||
| helpers.testDirectory(SANDBOX, done); | ||
| }); | ||
|
|
||
| beforeEach(function createProject(done) { | ||
| common.createDummyProject(SANDBOX, 'test-app', done); | ||
| }); | ||
|
|
||
| it('generates an async boot script properly', function(done) { | ||
| var bootGen = givenBootScriptGenerator(); | ||
| helpers.mockPrompt(bootGen, { | ||
| name: 'async-boot-script', | ||
| type: 'async' | ||
| }); | ||
|
|
||
| bootGen.run(function() { | ||
| var target = path.resolve(SANDBOX, 'server/boot/async-boot-script.js'); | ||
| expect(fs.existsSync(target), 'file exists'); | ||
|
|
||
| var targetContents = fs.readFileSync(target, 'utf8'); | ||
| var src = path.resolve(__dirname, '../boot-script/templates/async.js'); | ||
| var srcContents = fs.readFileSync(src, 'utf8'); | ||
| expect(targetContents).to.equal(srcContents); | ||
|
|
||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('generates a sync boot script properly', function(done) { | ||
| var bootGen = givenBootScriptGenerator(); | ||
| helpers.mockPrompt(bootGen, { | ||
| name: 'sync-boot-script', | ||
| type: 'sync' | ||
| }); | ||
|
|
||
| bootGen.run(function() { | ||
| var target = path.resolve(SANDBOX, 'server/boot/sync-boot-script.js'); | ||
| expect(fs.existsSync(target), 'file exists'); | ||
|
|
||
| var targetContents = fs.readFileSync(target, 'utf8'); | ||
| var src = path.resolve(__dirname, '../boot-script/templates/sync.js'); | ||
| var srcContents = fs.readFileSync(src, 'utf8'); | ||
| expect(targetContents).to.equal(srcContents); | ||
|
|
||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| function givenBootScriptGenerator(bsArgs) { | ||
| var path = '../../boot-script'; | ||
| var name = 'loopback:boot-script'; | ||
| var gen = common.createGenerator(name, path, [], bsArgs, {}); | ||
| return gen; | ||
| } | ||
| }); |
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.
@raymondfeng Fixed.