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
15 changes: 5 additions & 10 deletions example/templates/server/boot/create-sample-data.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
var importer = require('../sample-data/import');

module.exports = function(app) {
module.exports = function(app, cb) {
if (app.dataSources.db.name !== 'Memory') return;

console.error('Started the import of sample data.');
app.importing = true;
console.log('Started the import of sample data.');

importer(app, function(err) {
delete app.importing;
if (err) {
console.error('Cannot import sample data - ', err);
} else {
console.error('Sample data was imported.');
}
app.emit('import done', err);
if (err) return cb(err);
console.log('Sample data was imported.');
cb();
});
};
10 changes: 3 additions & 7 deletions example/templates/server/sample-data/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ module.exports = function(app, cb) {
});
};

if (require.main === module) {
// Run the import (server runs it automatically during boot)
var app = require('../server');
app.on('import done', function(err) {
process.exit(err ? 1 : 0);
});
}
if (require.main === module)
// The import runs automatically during the boot process.
require('../server');

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.

Will this exit the process with a non-zero status code when the import fails?

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.

Yes, as long as err is returned in the callback (we throw by default in strongloop/loopback-workspace@223758f#diff-383e9ce062435fb34a4326cdf175c907R16 causing an unhandledException event causing Node to return 1 for the exit code). I did a smoke test by changing the name of the JSON file in import.js deliberately causing a missing file error then checking the exit status in bash via echo $? which returned 1.

7 changes: 3 additions & 4 deletions example/templates/server/test/rest-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ var assert = require('assert');

before(function importSampleData(done) {
this.timeout(50000);
if (app.importing) {
app.on('import done', done);
} else {
if (app.booting)
app.on('booted', done);
else
done();
}
});

function json(verb, url) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"debug": "^2.1.0",
"inflection": "^1.3.8",
"loopback-swagger": "^2.0.0",
"loopback-workspace": "^3.7.1",
"loopback-workspace": "^3.10.0",
"request": "^2.47.0",
"yeoman-generator": "^0.18.7",
"yosay": "^1.0.0"
Expand Down