-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
executable file
·231 lines (187 loc) · 5.74 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env node
var co = require('co')
var prompt = require('co-prompt')
var Promise = require('bluebird')
var errors = require('./lib/errors')
var exec = require('./lib/exec')
var colors = require('./lib/colors')
var fs = require('fs')
var Path = require('path')
var program = require('commander')
program
.version(require('./package.json').version)
//
// The in-memory file system
//
var memFs = require('mem-fs')
var editor = require('mem-fs-editor')
var store = memFs.create()
var vfs = editor.create(store)
var util = require('mem-fs-editor/lib/util')
//
// Add a new module
//
program
.command('add [module] [moduleArgs...]')
.action(function (module, moduleArgs) {
// Wrap everything in co to easily catch errors
co(function * () {
if (!module) {
var list = require('./commands/list-modules.js')()
console.log(list)
return process.exit(0);
}
module = module.toLowerCase()
try {
fs.accessSync( Path.resolve(__dirname,`modules/${module}/config.js` ) )
}
catch (e) {
throw new errors.NonexistentModule(module)
}
var config = {
package: require( Path.resolve(process.cwd(), 'package.json')),
projectRoot: process.cwd(),
server: require( Path.resolve(process.cwd(), 'server/config/index.json')),
serverSetup: require( Path.resolve(process.cwd(), 'server/config/setup/index.json')),
}
var result = require('./commands/add-module.js')(vfs, config, module, moduleArgs)
console.log(`\nAdding the ${ colors.subject(module) } module will result in the following changes:\n`)
var base = null
store.each(function (file) {
if ( ! file.state ) return;
base = base || util.getCommonPath(config.projectRoot, file.history[0])
var shortFilePath = file.history[0].replace(base, '.')
file.isNew
? console.log(colors.fileAdd(` + ${ shortFilePath }`))
: console.log(colors.fileMod(` M ${ shortFilePath }`))
})
if ( result.installs ) {
console.log('\nand install the following packages:\n')
console.log(` ${ result.installs.join(', ') }`)
}
if ( result.serverSetup ) {
console.log(`\nIt will also re-run ${ colors.command('yarn setup') }.`)
}
var response = yield prompt('\nIs this ok? (Y/n) ')
if ( response && response.toLowerCase() !== 'y' ) {
console.log("Cancelled.")
return process.exit(0)
}
yield call(vfs, 'commit')
if ( result.installs ) { yield exec('yarn') }
if ( result.serverSetup ) { yield exec('yarn setup') }
console.log(`Added ${module}! :)`)
})
.then(exit(0), exit(1))
})
//
// List all modules
//
program
.command('ls')
.action(function () {
var config = {
projectRoot: process.cwd(),
}
return require('./commands/list-generators.js')(config)
.then(generators => {
var modules = require('./commands/list-modules.js')()
return `${modules}\n ----\n${generators}`
})
.then(console.log)
.then(exit(0))
})
//
// Generate a new project
//
program
.command('new <projectName>')
.action(function (projectName) {
var config = {
projectName: projectName,
cwd: process.cwd(),
}
var result = require('./commands/new-project.js')(vfs, config, projectName)
co(function * () {
yield call(vfs, 'commit')
// "cd" into newly generated project folder
process.chdir('./' + projectName)
// Initialize some things
yield exec(`yarn`)
yield exec(`git`, ['init'])
yield exec(`git`, ['add', '.'])
yield exec(`git`, ['commit', '-m', `"First commit"`])
yield exec(`yarn`, ['setup'])
console.log("\nYour new project is ready! `cd` into it to get started:\n")
console.log(` $ cd ${projectName}`)
console.log(` $ yarn watch`)
console.log(` $ pult add knex pg # optional; see docs for more modules\n`)
})
.then(exit(0), exit(1))
})
//
// Generators
//
program
.command('generate [generatorName] [generatorArgs...]')
.alias('g')
.action(function (generatorName, generatorArgs) {
var config = {
projectRoot: process.cwd(),
}
if (!generatorName) {
return require('./commands/list-generators.js')(config)
.then(console.log)
.then(exit(0))
}
generatorName = generatorName.toLowerCase()
co(function * () {
var result = yield require(`./commands/run-generator`)(vfs, config, generatorName, generatorArgs)
console.log(`\nGenerating this ${generatorName} will result in the following changes:\n`)
var base = null
store.each(function (file) {
if ( ! file.state ) return;
base = base || util.getCommonPath(config.projectRoot, file.history[0])
console.log(` ${ file.isNew ? '+' : 'M' } ${ file.history[0].replace(base, '.') }`)
})
var response = yield prompt('\nIs this ok? (Y/n) ')
if ( response && response.toLowerCase() !== 'y' ) {
console.log("Cancelled.")
return process.exit(0)
}
yield call(vfs, 'commit')
console.log(`Generated ${generatorName}! :)`)
})
.then(exit(0), exit(1))
})
//
// Catch-all (invalid arguments)
//
program
.command('*')
.action(function(){
exec(`pult --help`)
});
//
// No arguments
//
if(process.argv.length === 2) {
exec(`pult --help`)
}
//
// Utility
//
var call = (obj, method, ...args) => Promise.promisify(obj[method]).apply(obj, args)
var exit = (code) => (x) => {
if ( x instanceof errors.PultError ) {
console.error(colors.error("\nError:"), x.message, '\n')
}
else if ( x instanceof Error ) {
console.error(x)
}
process.exit(code)
}
//
// Begin the process
//
program.parse(process.argv)