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
6 changes: 3 additions & 3 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"eqnull": true,
"immed": true,
"indent": 2,
"latedef": true,
"latedef": "nofunc",
"newcap": true,
"nonew": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true,
"maxlen": 80
}
40 changes: 32 additions & 8 deletions acl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
var yeoman = require('yeoman-generator');
var async = require('async');

var workspace = require('loopback-workspace');
var AclDefinition = workspace.models.AclDefinition;
var wsModels = require('loopback-workspace').models;
var ModelAccessControl = wsModels.ModelAccessControl;

var actions = require('../lib/actions');
var helpers = require('../lib/helpers');
Expand All @@ -18,6 +18,30 @@ module.exports = yeoman.generators.Base.extend({

loadModels: actions.loadModels,

loadAccessTypeValues: function() {
var done = this.async();
ModelAccessControl.getAccessTypes(function(err, list) {
this.accessTypeValues = list;
done(err);
}.bind(this));
},

loadRoleValues: function() {
var done = this.async();
ModelAccessControl.getBuiltinRoles(function(err, list) {
this.roleValues = list;
done(err);
}.bind(this));
},

loadPermissionValues: function() {
var done = this.async();
ModelAccessControl.getPermissionTypes(function(err, list) {
this.permissionValues = list;
done(err);
}.bind(this));
},

askForModel: function() {
var done = this.async();

Expand Down Expand Up @@ -81,27 +105,27 @@ module.exports = yeoman.generators.Base.extend({
message: 'Select the access type:',
type: 'list',
default: 'all',
choices: AclDefinition.accessTypeValues,
choices: this.accessTypeValues,
},
{
name: 'role',
message: 'Select the role',
type: 'list',
default: '$everyone',
choices: AclDefinition.builtinRoleValues,
choices: this.roleValues,
},
{
name: 'permission',
message: 'Select the permission to apply',
type: 'list',
choices: AclDefinition.permissionValues,
choices: this.permissionValues,
}
];
this.prompt(prompts, function(answers) {
this.aclDef = {
property: answers.property,
accessType: answers.accessType,
principalType: AclDefinition.ROLE,
principalType: 'ROLE', // TODO support all principal types
principalId: answers.role,
permission: answers.permission
};
Expand All @@ -117,14 +141,14 @@ module.exports = yeoman.generators.Base.extend({
{ where: { name: this.modelName }, limit: 1 } :
true /* all models, force refresh */;

this.project.models(filter, function(err, models) {
wsModels.ModelDefinition.find(filter, function(err, models) {
if (err) {
return done(err);
}

var firstError = true;
async.each(models, function(model, cb) {
model.permissions.create(aclDef, function(err) {
model.accessControls.create(aclDef, function(err) {
if (err && firstError) {
helpers.reportValidationError(err);
firstError = false;
Expand Down
48 changes: 33 additions & 15 deletions app/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';
var util = require('util');
var yeoman = require('yeoman-generator');
var yosay = require('yosay');
var chalk = require('chalk');
var workspace = require('loopback-workspace');
var Project = workspace.models.Project;
var Workspace = workspace.models.Workspace;

module.exports = yeoman.generators.Base.extend({
constructor: function() {
Expand All @@ -23,6 +22,7 @@ module.exports = yeoman.generators.Base.extend({
},

injectProjectWriteFile: function() {
/* TODO inject `this.directory` to replace `ncp` in workspace
// Modify Project.writeFile use yeoman's write
var _projectWriteFile = Project.writeFile;
Project.writeFile = function(filepath, content, encoding, cb) {
Expand All @@ -34,10 +34,29 @@ module.exports = yeoman.generators.Base.extend({
this.on('end', function() {
Project.writeFile = _projectWriteFile;
});
*/
},

init: function() {
this.pkg = require('../package.json');
initWorkspace: function() {
process.env.WORKSPACE_DIR = this.destinationRoot();
},

loadTemplates: function() {
var done = this.async();

Workspace.getAvailableTemplates(function(err, list) {
if (err) return done(err);
this.templates = list.map(function(t) {
return {
// TODO - workspace does not provide template details yet
// name: util.format('%s (%s)', t.name, t.description),
// value: t.name
name: t,
value: t
};
});
done();
}.bind(this));
},

askForParameters: function() {
Expand All @@ -53,23 +72,23 @@ module.exports = yeoman.generators.Base.extend({
message: 'What\'s the name of your application?',
default: name
},
/*
TODO: not all templates are projects, some of them are mere components
The only functional project template is 'api-server' at the moment
{
name: 'template',
message: 'What kind of application do you have in mind?',
type: 'list',
default: 'mobile',
choices: Project.listTemplates().map(function(t) {
return {
name: util.format('%s (%s)', t.name, t.description),
value: t.name
};
}),
default: 'api-server',
choices: this.templates
}
*/
];

this.prompt(prompts, function(props) {
this.appname = props.appname;
this.template = props.template;
//this.template = props.template;

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.

Might want to comment this or get rid of it

this.template = 'api-server';

done();
}.bind(this));
Expand All @@ -78,10 +97,9 @@ module.exports = yeoman.generators.Base.extend({
project: function() {
var done = this.async();

Project.createFromTemplate(
this.destinationRoot(),
this.appname,
Workspace.createFromTemplate(
this.template,
this.appname,
done
);
},
Expand Down
10 changes: 5 additions & 5 deletions datasource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
var chalk = require('chalk');
var yeoman = require('yeoman-generator');

var workspace = require('loopback-workspace');
var Project = workspace.models.Project;
var wsModels = require('loopback-workspace').models;

var actions = require('../lib/actions');
var helpers = require('../lib/helpers');
Expand All @@ -18,7 +17,7 @@ module.exports = yeoman.generators.NamedBase.extend({

loadConnectors: function() {
var done = this.async();
Project.listAvailableConnectors(function(err, list) {
wsModels.Workspace.listAvailableConnectors(function(err, list) {
if (err) {
return done(err);
}
Expand Down Expand Up @@ -75,10 +74,11 @@ module.exports = yeoman.generators.NamedBase.extend({
var done = this.async();
var config = {
name: this.name,
connector: this.connector
connector: this.connector,
componentName: 'rest' // hard-coded for now
};

this.project.dataSources.create(config, function(err) {
wsModels.DataSourceDefinition.create(config, function(err) {
helpers.reportValidationError(err, this.log);
return done(err);
}.bind(this));
Expand Down
34 changes: 11 additions & 23 deletions lib/actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';
var util = require('util');
var workspace = require('loopback-workspace');
var Project = workspace.models.Project;

var actions = exports;

Expand All @@ -11,20 +9,21 @@ var actions = exports;

/**
* Load the project in `this.destinationRoo()`.
* Set `this.projectDir` and `this.project`.
* Set `this.projectDir`.
* @async
*/
actions.loadProject = function() {
if (this.options.nested && this.options.projectDir && this.options.project) {
if (this.options.nested && this.options.projectDir) {
this._externalProject = true;
this.projectDir = this.options.projectDir;
this.project = this.options.project;
return;
}

var done = this.async();
this.projectDir = this.destinationRoot();
process.env.WORKSPACE_DIR = this.projectDir;

/* TODO - validate the project

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.

It would be nice maintain our convention of TODO($user) which makes it clear who is going to make sure it gets done or atleast we know who created the TODO.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is a short-lived item, I'll implement it in next few days.

I do follow the convention in other comments.

var done = this.async();
Project.isValidProjectDir(this.projectDir, function(err, isValid, message) {
if (err) {
return done(err);
Expand All @@ -37,41 +36,30 @@ actions.loadProject = function() {
message);
return done(new Error(msg));
}

Project.loadFromFiles(this.projectDir, function(err, project) {
if (err) {
return done(err);
}

this.project = project;
done();
}.bind(this));
}.bind(this));
*/
};

/**
* Save `this.project`, update all project files.
* Save the current project, update all project files.
*/
actions.saveProject = function() {
if (this._externalProject) {
return;
}

var done = this.async();
this.project.saveToFiles(this.projectDir, done);
// no-op in workspace 3.0

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.

Should we expose this again in workspace 3.0? Does it have value still?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Since all changes are saved automatically, it does not make sense to expose it in workspace 3.0.

I am keeping the saveProject action around in case we decided to change the loopback-workspace behaviour again.

};

/**
* Load all models of `this.project`.
* Load all models of the current project.
* `this.projectModels` will contain an array of all models (Array.<Model>)
* `this.modelNames` will contain an array of names (Array.<string>)
*/
actions.loadModels = function() {
var done = this.async();
this.project.models(function(err, results) {
if (err) {
return done(err);
}
workspace.models.ModelDefinition.all(function(err, results) {
if (err) return done(err);
this.projectModels = results;
this.modelNames = results.map(function(m) {
return m.name;
Expand Down
22 changes: 19 additions & 3 deletions model/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var chalk = require('chalk');
var yeoman = require('yeoman-generator');
var wsModels = require('loopback-workspace').models;

var actions = require('../lib/actions');
var helpers = require('../lib/helpers');
Expand All @@ -15,7 +16,8 @@ module.exports = yeoman.generators.NamedBase.extend({

loadDataSources: function() {
var done = this.async();
this.project.dataSources(function(err, results) {

wsModels.DataSourceDefinition.find(function(err, results) {
if (err) {
return done(err);
}
Expand Down Expand Up @@ -54,16 +56,30 @@ module.exports = yeoman.generators.NamedBase.extend({
}.bind(this));
},

model: function() {
modelDefinition: function() {
var done = this.async();
var config = {
properties: {},
name: this.name,
componentName: wsModels.ConfigFile.ROOT_COMPONENT,
public: this.public,
};

wsModels.ModelDefinition.create(config, function(err) {
helpers.reportValidationError(err, this.log);
return done(err);
}.bind(this));
},

modelConfiguration: function() {
var done = this.async();
var config = {
name: this.name,
componentName: 'rest', // hard-coded for now
dataSource: this.dataSource
};

this.project.models.create(config, function(err) {
wsModels.ComponentModel.create(config, function(err) {
helpers.reportValidationError(err, this.log);
return done(err);
}.bind(this));
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
"strongloop"
],
"dependencies": {
"yeoman-generator": "~0.16.0",
"loopback-workspace": "~2.5.0",
"chalk": "~0.4.0",
"yosay": "^0.1.0",
"async": "^0.9.0"
"async": "^0.9.0",
"chalk": "^0.4.0",
"loopback-workspace": "strongloop/loopback-workspace#3.0",
"yeoman-generator": "^0.16.0",
"yosay": "^0.1.0"
},
"peerDependencies": {
"yo": ">=1.0.0"
Expand Down
Loading