Skip to content

Commit

Permalink
Merge pull request #191 from donejs/sbu
Browse files Browse the repository at this point in the history
Prompt for the serviceBaseURL
  • Loading branch information
matthewp committed Feb 6, 2017
2 parents ccb2fff + 5c2c657 commit 93a20cc
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 18 deletions.
3 changes: 2 additions & 1 deletion app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ module.exports = Generator.extend({
'server-production': {
renderingBaseURL: '/dist'
}
}
},
serviceBaseURL: ''
}
};

Expand Down
10 changes: 9 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ exports.toNpmInstallStrings = function(deps){
// makes sure a value is supplied
exports.validateRequired = function(value) {
return !!value;
}
};

exports.removeSlash = function(url) {
var char = url[url.length - 1];
if(char === '/') {
return url.substr(0, url.length - 1);
}
return url;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"q": "^1.4.1",
"semver": "^5.1.0",
"validate-npm-package-name": "^2.2.2",
"whatwg-url": "^4.3.0",
"yeoman-generator": "^1.1.0"
},
"devDependencies": {
Expand Down
67 changes: 54 additions & 13 deletions supermodel/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var Generator = require('yeoman-generator');
var fs = require('fs');
var path = require('path');
var _ = require('lodash');

var URL = require('whatwg-url').URL;
var utils = require('../lib/utils');
var upperFirst = require("lodash.upperfirst");
var utils = require('../lib/utils');
Expand Down Expand Up @@ -30,6 +31,10 @@ module.exports = Generator.extend({
prompting: function() {
var done = this.async();

function addToProps(props){
this.props = _.extend(this.props, props);
}

this.prompt({
name: 'name',
type: String,
Expand All @@ -38,20 +43,49 @@ module.exports = Generator.extend({
when: !this.options.name
}).then(function(first) {
var name = this.options.name = this.options.name || first.name;
var prompts = [{

var prompt = {
name: 'url',
message: 'What is the URL endpoint?',
default: '/' + name
}, {
name: 'idProp',
message: 'What is the property name of the id?',
default: 'id'
}];

this.prompt(prompts).then(function(props) {
this.props = _.extend(this.props, props);

done();
};

var promptId = function() {
var prompt = {
name: 'idProp',
message: 'What is the property name of the id?',
default: 'id'
};

return this.prompt(prompt).then(addToProps.bind(this)).then(function(){
done();
});
}.bind(this);

this.prompt(prompt).then(function(answer){
var rawUrl = answer.url.trim();
try {
var url = new URL(rawUrl);
var urlPath = url.pathname;
url.pathname = '';
var serviceBaseURL = utils.removeSlash(url.toString());

var prompt = {
name: 'useServiceBaseURL',
message: 'Is ' + serviceBaseURL + ' your service URL?',
type: Boolean
};
this.prompt(prompt).then(function(answer){
this.props.serviceBaseURL = answer.useServiceBaseURL &&
serviceBaseURL;
this.props.url = answer.useServiceBaseURL ? urlPath : rawUrl;

return promptId();
}.bind(this));
} catch(ex) {
this.props.url = answer.url;
return promptId();
}
}.bind(this));
}.bind(this));
},
Expand All @@ -69,10 +103,16 @@ module.exports = Generator.extend({
var folder = _.get(pkg, 'steal.directories.lib') || './';
var appName = _.get(pkg, 'name');

if(this.props.serviceBaseURL) {
pkg.steal.serviceBaseURL = this.props.serviceBaseURL;
var pkgPath = this.destinationPath('package.json');
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, ' '), 'utf8');
}

var options = {
className: upperFirst(_.camelCase(this.options.name)),
name: this.options.name,
url: this.props.url.trim(),
url: this.props.url,
idProp: this.props.idProp
};

Expand All @@ -98,6 +138,7 @@ module.exports = Generator.extend({
utils.addImport(modelTest, appName + '/models/' + options.name + '_test');
var fixturesFile = this.destinationPath(path.join(folder, 'models', 'fixtures', 'fixtures.js'));
utils.addImport(fixturesFile, appName + '/models/fixtures/' + _.pluralize(options.name));

done();
}
});
3 changes: 2 additions & 1 deletion supermodel/templates/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import DefineMap from 'can-define/map/';
import DefineList from 'can-define/list/';
import superMap from 'can-connect/can/super-map/';
import tag from 'can-connect/can/tag/';
import loader from '@loader';

export const <%= className %> = DefineMap.extend({
seal: false
Expand All @@ -14,7 +15,7 @@ export const <%= className %> = DefineMap.extend({
});

export const <%= name %>Connection = superMap({
url: '<%= url %>',
url: loader.serviceBaseURL + '<%= url %>',
idProp: '<%= idProp %>',
Map: <%= className %>,
List: <%= className %>.List,
Expand Down
16 changes: 15 additions & 1 deletion test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ describe('generator-donejs', function () {
});

it('fails with an invalid package name', function (done) {

helpers.run(path.join(__dirname, '../app'))
.withOptions({
packages: donejsPackage.donejs,
Expand All @@ -59,6 +58,21 @@ describe('generator-donejs', function () {
done();
});
});

it('fails if there are no packages', function(done) {
helpers.run(path.join(__dirname, '../app'))
.withOptions({
packages: null,
skipInstall: true
})
.withPrompts({
name: 'place-my-tmp'
})
.on('error', function(err){
assert(true, 'An error for not providing packages');
done();
});
})
});

describe('Absolute path support', function() {
Expand Down
57 changes: 56 additions & 1 deletion test/supermodel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ describe('generator-donejs', function () {
idProp: "id"
})
.on('end', function () {
assert( fs.existsSync( path.join( tmpDir, "src", "models", "messages.js" ) ), "bar.js exists" );
assert(fs.existsSync(path.join(tmpDir, "src", "models", "messages.js")),
"bar.js exists" );
assert.fileContent(path.join(tmpDir, 'src', 'models', 'messages.js'),
/serviceBaseURL/, "serviceBaseURL included");
done();
});
});
Expand Down Expand Up @@ -109,6 +112,58 @@ describe('generator-donejs', function () {
});
});

describe('Providing a full URL', function(){
it('Sets the package.json serviceBaseURL if answered to', function (done) {
var tmpDir;

helpers.run(path.join(__dirname, '../supermodel'))
.inTmpDir(function (dir) {
tmpDir = dir;
fs.copySync(path.join(__dirname, "tests", "basics"), dir)
})
.withOptions({
skipInstall: true
})
.withPrompts({
name: 'messages',
url: 'https://example.com/messages',
useServiceBaseURL: true,
idProp: "id"
})
.on('end', function () {
var pkg = require(path.join(tmpDir, "package.json"));
assert.equal(pkg.steal.serviceBaseURL, 'https://example.com');

done();
});
});

it('Doesn\'t set serviceBaseURL if answer that not the base url', function(done) {
var tmpDir;

helpers.run(path.join(__dirname, '../supermodel'))
.inTmpDir(function (dir) {
tmpDir = dir;
fs.copySync(path.join(__dirname, "tests", "basics"), dir)
})
.withOptions({
skipInstall: true
})
.withPrompts({
name: 'messages',
url: 'https://example.com/messages',
useServiceBaseURL: false,
idProp: "id"
})
.on('end', function () {
var pkg = require(path.join(tmpDir, "package.json"));
assert.equal(pkg.steal.serviceBaseURL, undefined);

done();
});
});
});

it('Errors when a test has an invalid name', function (done) {
var tmpDir;

Expand Down

0 comments on commit 93a20cc

Please sign in to comment.