Skip to content

Commit 461265a

Browse files
authored
Create base.js
1 parent 0fbf6b1 commit 461265a

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

src/generators/base.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
'use strict';
2+
import path from 'path';
3+
import {exec} from 'child_process';
4+
import _ from 'lodash';
5+
import s from 'underscore.string';
6+
import Promise from 'bluebird';
7+
import semver from 'semver';
8+
import {appName} from './util.js';
9+
import {Base} from 'yeoman-generator';
10+
11+
// extend lodash with underscore.string
12+
_.mixin(s.exports());
13+
14+
/**
15+
* Run the given command in a child process
16+
* @param {string} cmd - command to run
17+
* @returns {Promise}
18+
*/
19+
function runCmd(cmd) {
20+
return new Promise((resolve, reject) => {
21+
exec(cmd, {}, function(err, stdout) {
22+
if(err) {
23+
console.error(stdout);
24+
return reject(err);
25+
}
26+
27+
return resolve(stdout);
28+
});
29+
});
30+
}
31+
32+
export class BaseGenerator extends Base {
33+
initializing() {
34+
this.argument('name', { type: String, required: false });
35+
36+
this.lodash = _;
37+
38+
var yoCheckPromise;
39+
40+
// CI won't have yo installed
41+
if(!process.env.CI) {
42+
yoCheckPromise = runCmd('yo --version').then(stdout => {
43+
if(!semver.satisfies(semver.clean(stdout), '>= 1.7.1')) {
44+
throw new Error('ERROR: You need to update yo to at least 1.7.1 (npm i -g yo)');
45+
}
46+
});
47+
} else {
48+
yoCheckPromise = Promise.resolve();
49+
}
50+
51+
try {
52+
this.appname = require(path.join(process.cwd(), 'bower.json')).name;
53+
} catch(err) {
54+
this.appname = path.basename(process.cwd());
55+
}
56+
this.appname = _.slugify(_.humanize(this.appname));
57+
this.scriptAppName = this.config.get('moduleName') || _.camelize(this.appname) + appName(this);
58+
59+
this.cameledName = _.camelize(this.name);
60+
this.classedName = _.classify(this.name);
61+
this.kebabName = _.kebabCase(this.name);
62+
63+
this.filters = this.config.get('filters');
64+
this.extensions = this.config.get('extensions');
65+
this.hasFilter = filter => this.filters.indexOf(filter) !== -1;
66+
this.hasExtension = ext => this.extensions.indexOf(ext) !== -1;
67+
68+
this.scriptExt = this.hasExtension('ts') ? 'ts' : 'js';
69+
this.templateExt = this.hasExtension('pug') ? 'pug' : 'html';
70+
this.styleExt = this.hasExtension('sass') ? 'scss' :
71+
this.hasExtension('less') ? 'less' :
72+
this.hasExtension('stylus') ? 'styl' :
73+
'css';
74+
75+
// dynamic assertion statements
76+
this.expect = () => this.hasFilter('expect') ? 'expect(' : '';
77+
this.to = () => this.hasFilter('expect') ? ').to' : '.should';
78+
79+
if(typeof this.env.options.appPath === 'undefined') {
80+
try {
81+
this.env.options.appPath = require(path.join(process.cwd(), 'bower.json')).appPath;
82+
} catch(err) {}
83+
this.env.options.appPath = this.env.options.appPath || 'app';
84+
}
85+
86+
this.sourceRoot(path.join(__dirname, '..', '/templates'));
87+
88+
return yoCheckPromise;
89+
}
90+
}

0 commit comments

Comments
 (0)