Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions boot-script/USAGE
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`.
65 changes: 65 additions & 0 deletions boot-script/index.js
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`):',

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raymondfeng Fixed.

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);
}
});
10 changes: 10 additions & 0 deletions boot-script/templates/async.js
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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

};
9 changes: 9 additions & 0 deletions boot-script/templates/sync.js
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.
*/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link is nice.

};
67 changes: 67 additions & 0 deletions test/boot-script.test.js
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;
}
});