From 3ce547c99fd7e24821c84d60e7f1998f059acfdc Mon Sep 17 00:00:00 2001 From: Simon Ho Date: Tue, 3 Mar 2015 22:41:09 -0800 Subject: [PATCH] Add boot script generator - Add code for `slc loopback:boot script` command - Add unit tests --- boot-script/USAGE | 8 ++++ boot-script/index.js | 65 +++++++++++++++++++++++++++++++++ boot-script/templates/async.js | 10 +++++ boot-script/templates/sync.js | 9 +++++ test/boot-script.test.js | 67 ++++++++++++++++++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 boot-script/USAGE create mode 100644 boot-script/index.js create mode 100644 boot-script/templates/async.js create mode 100644 boot-script/templates/sync.js create mode 100644 test/boot-script.test.js diff --git a/boot-script/USAGE b/boot-script/USAGE new file mode 100644 index 0000000..0d8d236 --- /dev/null +++ b/boot-script/USAGE @@ -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`. diff --git a/boot-script/index.js b/boot-script/index.js new file mode 100644 index 0000000..bbd113e --- /dev/null +++ b/boot-script/index.js @@ -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); + } +}); diff --git a/boot-script/templates/async.js b/boot-script/templates/async.js new file mode 100644 index 0000000..c8c54e3 --- /dev/null +++ b/boot-script/templates/async.js @@ -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 +}; diff --git a/boot-script/templates/sync.js b/boot-script/templates/sync.js new file mode 100644 index 0000000..0f9367f --- /dev/null +++ b/boot-script/templates/sync.js @@ -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. + */ +}; diff --git a/test/boot-script.test.js b/test/boot-script.test.js new file mode 100644 index 0000000..e265b22 --- /dev/null +++ b/test/boot-script.test.js @@ -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; + } +});