-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli
executable file
·152 lines (138 loc) · 4.77 KB
/
cli
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env node
var child_process = require('child_process')
var path = require('path')
var fs = require('fs')
var logger = require('semafor')()
var mkdirp = require('mkdirp')
function addScripts(json, scripts) {
json["scripts"] = json["scripts"] || {}
for(var key in scripts) {
json["scripts"][key] = scripts[key]
}
return json
}
function installNpmDependencies(dependencies, cb) {
var params = ['install', '--save']
dependencies.map(function(d){
params.push(d)
})
var spawn = child_process.spawn('npm', params)
spawn.stdout.on('data', function(data){
logger.log(data + " ");
});
spawn.stderr.on('data', function(data){
logger.fail(data + " ");
});
spawn.on('close', function() {
cb();
});
}
function verifyEnv(package_json) {
try {
return fs.accessSync(package_json, fs.W_ok)
}catch(e) {
return e
}
}
function mkdirSync(dir) {
mkdirp.sync(dir)
}
function copySync(src, dest) {
var source = fs.readFileSync(src)
fs.writeFileSync(dest, source)
}
function main() {
// check env has package.json
var package_json = path.join(process.cwd(), 'package.json')
var err = verifyEnv(package_json);
if(err != null) {
logger.fail("Are you in a npm environment? Make sure to run npm init before using babel-me")
return;
}
logger.ok("Checking npm environment")
var json = require(package_json)
// add build script
var scripts = {
"build" : "./node_modules/babel-cli/bin/babel.js lib -d dist",
"test" : "./node_modules/mocha/bin/mocha tests/* --compilers js:babel-core/register -t 15s",
"prepublish" : "npm run build"
}
json = addScripts(json, scripts)
// update json
json_string = JSON.stringify(json, null, " ")
fs.writeFileSync(package_json, json_string)
logger.ok("Adding npm scripts")
// create lib, dist, tests folders
mkdirSync(path.join(process.cwd(),'lib'))
logger.ok("Creating lib directory")
mkdirSync(path.join(process.cwd(),'dist'))
logger.ok("Creating dist directory")
mkdirSync(path.join(process.cwd(),'tests'))
logger.ok("Creating tests directory")
// copy index.js
try {
stats = fs.lstatSync(path.join(process.cwd(), 'lib','index.js'))
if(stats.isFile()) {
logger.warn("Avoid copying index.js, you already have a file in your lib folder")
}else{
var index_src = path.join(__dirname, 'src', 'index.js')
var index_dest = path.join(process.cwd(), 'lib', 'index.js')
copySync(index_src, index_dest)
}
}catch(e){
var index_src = path.join(__dirname, 'src', 'index.js')
var index_dest = path.join(process.cwd(), 'lib', 'index.js')
copySync(index_src, index_dest)
}
// copy test.js
try {
stats = fs.lstatSync(path.join(process.cwd(), 'tests','test.js'))
if(stats.isFile()) {
logger.warn("Avoid copying tst.js, you already have a file in your test folder")
}else{
var test_src = path.join(__dirname, 'src', 'test.js')
var test_dest = path.join(process.cwd(), 'tests', 'test.js')
copySync(test_src, test_dest)
}
}catch(e) {
var test_src = path.join(__dirname, 'src', 'test.js')
var test_dest = path.join(process.cwd(), 'tests', 'test.js')
copySync(test_src, test_dest)
}
// add .babelrc
var presets = {"presets": ["es2015"],"plugins":["transform-async-to-generator","transform-runtime"]}
try {
babelrc = fs.lstatSync(path.join(process.cwd(), '.babelrc'))
if(babelrc.isFile()) {
presets = fs.readFileSync(path.join(process.cwd(), '.babelrc')).toString()
presets = JSON.parse(presets)
var list = presets["presets"] || []
if(list.indexOf("es2015") == -1) {
list.push("es2015")
}
presets["presets"] = list
plugins = presets["plugins"] || []
if(plugins.indexOf("transform-async-to-generator") == -1) {
plugins.push("transform-async-to-generator")
}
if(plugins.indexOf("transform-runtime") == -1) {
plugins.push("transform-runtime")
}
presets["plugins"] = plugins;
var babelrc_string = JSON.stringify(presets, null, " ")
fs.writeFileSync(path.join(process.cwd(), '.babelrc'), babelrc_string)
logger.ok("Adding babel presets & plugins preset to .babelrc configuration file")
}
}catch(e) {
var babelrc_string = JSON.stringify(presets, null, " ")
fs.writeFileSync(path.join(process.cwd(), '.babelrc'), babelrc_string)
logger.ok("Creating .babelrc configuration file")
}
// install npm dependencies
var dependencies = ["babel-cli", "babel-core", "mocha", "babel-preset-es2015", "babel-plugin-transform-runtime", "babel-plugin-transform-async-to-generator"]
logger.log("Installing npm dependencies, this may take a while!")
installNpmDependencies(dependencies, function() {
logger.ok("Babel environment ready. Now start coding, you have a dog to feed!")
});
}
main();