-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
78 lines (68 loc) · 2.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* eslint-disable func-names,vars-on-top */
var yeoman = require('yeoman-generator');
var R = require('ramda');
var splitKeywords = require('split-keywords');
var sortedObject = require('sorted-object');
var depsObject = require('deps-object');
var mapPrefixPreset = require('./map-babel').mapPrefixPreset;
var mapPrefixPlugin = require('./map-babel').mapPrefixPlugin;
var stringify = require('./json-fp').stringify;
var parse = require('./json-fp').parse;
// concatAll :: [Array] -> Array
var concatAll = R.reduce(R.concat, []);
module.exports = yeoman.Base.extend({
constructor: function () {
yeoman.Base.apply(this, arguments);
this.argument('presets', { type: Array, required: false,
desc: 'Presets’ list: "yo babel es2015 es2016"\n'
});
this.option('plugins', { type: String, required: false, alias: 'p',
desc: 'Plugins list: "yo babel -p add-module-exports"'
});
// helpers
this.saveDepsToPkg = function (deps) {
var pkg = this.fs.readJSON(this.destinationPath('package.json'), {});
var currentDeps = pkg.devDependencies || {};
var mergedDeps = R.merge(currentDeps, deps);
var sortedDeps = sortedObject(mergedDeps);
pkg.devDependencies = sortedDeps;
this.fs.writeJSON(this.destinationPath('package.json'), pkg);
};
},
writing: function () {
var cli = {};
if (this.presets) {
cli.presets = this.presets;
}
var plugins = this.options.plugins;
if (typeof plugins === 'boolean') {
this.log('Maybe you forgot double dash: `-plugins` instead of `--plugins`');
}
if (plugins) {
cli.plugins = (typeof plugins === 'string') ? splitKeywords(plugins) : plugins;
}
var existing = this.fs.exists(this.destinationPath('.babelrc'))
? parse(this.fs.read(this.destinationPath('.babelrc')))
: {};
var defaults = { presets: ['es2015'] };
var result = R.mergeAll([existing, defaults, cli, (this.options.config || {})]);
this.fs.write(this.destinationPath('.babelrc'), (stringify(result) + '\n'));
var deps = concatAll([
'babel-cli@6.6.5', 'babel-register@6.7.2',
mapPrefixPreset(result.presets || []),
mapPrefixPlugin(result.plugins || [])
]);
return depsObject(deps)
.then(function (devDeps) {
this.saveDepsToPkg(devDeps);
}.bind(this))
.catch(function (err) {
throw err;
});
},
install: function () {
if (!this.options['skip-install']) {
this.npmInstall();
}
}
});