-
Notifications
You must be signed in to change notification settings - Fork 1
/
project.js
485 lines (413 loc) · 19.3 KB
/
project.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
module.exports = (function() {
var ProjectJs = function() {
var findProjectRoot = function() {
var findup = require('findup-sync'),
path = require('path'),
_ = require('underscore');
var projectPath = findup("project.json");
if (_.isUndefined(projectPath) || _.isNull(projectPath)) {
throw new Error("Could not find a project.json file within or under the current directory!");
}
return {
'file': projectPath,
'dir': path.dirname(projectPath)
};
};
var writeProjectFile = function(projectFile, filePath) {
var fs = require('fs');
var json = projectFile.toJSON();
fs.writeFileSync(filePath, json, {'encoding':'utf8'});
};
this.getVersion = function() {
var ownVersion = require('own-version');
return ownVersion.sync();
};
var isCallbackSet = function(options) {
var _ = require('underscore');
return (_.has(options, 'callback') && _.isFunction(options.callback));
};
/**
* Creates a new project in the given or working directory.
* @param {String} namespace The project's base namespace. Required.
* @param {String} root Root path of the project. Optional.
* @param {String} src Name of or path to source folder
* @param {String} build Name of or path to build folder
* @param {Function} callback Callback function. Optional.
* @return {void}
*/
this.init = function(options) {
var fs = require('fs-extra'),
path = require('path'),
_ = require('underscore'),
ProjectJsFactory = require('./lib/factory'),
util = require('./lib/util');
var hasNs = (_.has(options, 'namespace') && !_.isEmpty(options['namespace'])),
hasCallback = isCallbackSet(options);
if (!hasNs && hasCallback) {
options.callback.call(null, new Error("Base namespace is required!"));
return;
} else if (!hasNs) {
throw new Error("Base namespace is required!");
}
// check root to see if it already has a project in it
var root = _.has(options, "root") ? options['root'] : process.cwd(),
isProject = fs.existsSync(path.join(root, './project.json'));
if (isProject && hasCallback) {
options.callback.call(null, new Error("A project already exists in this directory"));
return;
} else if (isProject) {
throw new Error("A project already exists in this directory");
}
// construct src and build folders
var args = {
'baseNs': options['namespace']
},
factory = new ProjectJsFactory();
if (_.has(options, 'src') && !_.isEmpty(options['src'])) {
if (path.isAbsolute(options['src'])) {
var srcDirPath = path.relative(root, options['src']);
args['srcDir'] = util.convertBackSlashes(srcDirPath);
} else {
args['srcDir'] = './' + util.convertBackSlashes(options['src']);
}
// make sure given source directory exists
fs.ensureDirSync(path.join(root, args['srcDir']));
}
if (_.has(options, 'build') && !_.isEmpty(options['build'])) {
if (path.isAbsolute(options['build'])) {
var buildDirPath = path.relative(root, options['build']);
args['buildDir'] = util.convertBackSlashes(buildDirPath);
} else {
args['buildDir'] = './' + util.convertBackSlashes(options['build']);
}
// make sure given build directory exists
fs.ensureDirSync(path.join(root, args['buildDir']));
}
// write project.json file
var json = factory.getProjectJson(args),
filePath = path.join(root, "project.json");
fs.writeFileSync(filePath, json, {'encoding':'utf8'});
if (hasCallback) {
options.callback.call(null, null, {'success': true});
}
};
/**
* Creates and adds a new class to the project.
* @param {String} name Name of class to add
* @param {String} path File path to class. Optional.
* @param {Function} callback Callback function.
* @return {void}
*/
this.createClass = function(options) {
var _ = require('underscore'),
ProjectJsParser = require('./lib/parser'),
ProjectJsFactory = require('./lib/factory');
var hasName = (_.has(options, 'name') && !_.isEmpty(options.name)),
hasCallback = isCallbackSet(options);
if (!hasName && hasCallback) {
options.callback.call(null, new Error("Class name is required!"));
return;
} else if (!hasName) {
throw new Error("Class name is required!");
}
var root = findProjectRoot(),
parser = new ProjectJsParser(),
projectFile = parser.loadProjectFile(root.file),
factory = new ProjectJsFactory(),
classDir = _.has(options, "path") ? options["path"] : process.cwd();
var classInfo = factory.createNewClass(options.name, classDir, projectFile);
projectFile.addClass(classInfo.name, classInfo.path);
writeProjectFile(projectFile, root.file);
if (hasCallback) {
options.callback.call(null, null, _.extend({'success':true}, classInfo));
}
};
/**
* Removes a class from the project and deletes the class file.
* @param {String} name Name of class
* @param {Boolean} retainFile Whether to delete the class file or not. Optional.
* @param {Function} callback Callback function. Optional.
* @return {void}
*/
this.removeClass = function(options) {
var fs = require('fs-extra'),
path = require('path'),
_ = require('underscore'),
ProjectJsParser = require('./lib/parser');
var hasName = (_.has(options, "name") && !_.isEmpty(options.name)),
hasCallback = isCallbackSet(options);
if (!hasName && hasCallback) {
options.callback.call(null, new Error("Class name is required!"), null);
return;
} else if (!hasName) {
throw new Error("Class name is required!");
}
var className = options.name,
root = findProjectRoot(),
parser = new ProjectJsParser(),
projectFile = parser.loadProjectFile(root.file),
registry = parser.createRegistry(projectFile);
if (registry.has(className)) {
var classFilePath = registry.resolve(className),
filePath = path.join(root.dir, classFilePath) + ".js";
if (!_.has(options, 'retainFile') || options['retainFile'] === false) {
fs.removeSync(filePath);
}
projectFile.removeClass(className);
writeProjectFile(projectFile, root.file);
if (hasCallback) {
options.callback.call(null, null, {'class': className, 'file': filePath, 'success': true});
}
} else if (hasCallback) {
options.callback.call(null, new Error("Class not found in registry!"), {'success':false});
return;
} else {
throw new Error("Class not found in registry!");
}
};
this.addDependency = function(options) {
var ProjectJsParser = require('./lib/parser'),
_ = require('underscore');
if (!_.has(options, "name") || _.isEmpty(options['name'])) {
throw new Error("Dependency name is required!");
} else if (!_.has(options, "path") || _.isEmpty(options['path'])) {
throw new Error("Path to dependency is required!");
}
var root = findProjectRoot(),
parser = new ProjectJsParser(),
projectFile = parser.loadProjectFile(root.file);
// TODO: check to make sure dependency exists
projectFile.addDependency(options['name'], options['path']);
writeProjectFile(projectFile, root.file);
console.log("Dependency %s added", options['name']);
};
/**
* Adds a shorthand alias for the given class
* @param {String} alias Alias
* @param {String} className Full name of class
* @param {Function} callback Callback function
*/
this.addAlias = function(options) {
var ProjectJsParser = require('./lib/parser'),
_ = require('underscore');
// check for required options
var hasAlias = (_.has(options, "alias") && !_.isEmpty(options['alias'])),
hasClassName = (_.has(options, "className") && !_.isEmpty(options['className'])),
hasCallback = isCallbackSet(options);
if (!hasAlias && hasCallback) {
options.callback.call(null, new Error("Alias is required!"));
return;
} else if (!hasAlias) {
throw new Error("Alias is required!");
} else if (!hasClassName && hasCallback) {
options.callback.call(null, new Error("Class name is required!"));
return;
} else if (!hasClassName) {
throw new Error("Class name is required!");
}
var root = findProjectRoot(),
parser = new ProjectJsParser(),
projectFile = parser.loadProjectFile(root.file);
// TODO: check to make sure class path exists / resolve classes relative to cwd
projectFile.addAlias(options['alias'], options['className']);
writeProjectFile(projectFile, root.file);
if (hasCallback) {
options.callback.call(null, null, {'success': true, 'alias': options['alias']});
}
};
/**
* Removes an existing alias from the project
* @param {String} alias Alias to remove
* @param {Function} callback Callback function
*/
this.removeAlias = function(options) {
var ProjectJsParser = require('./lib/parser'),
_ = require('underscore');
var hasAlias = (_.has(options, "alias") && !_.isEmpty(options.alias)),
hasCallback = isCallbackSet(options);
if (!hasAlias && hasCallback) {
options.callback.call(null, new Error("Alias is required!"), null);
} else if (!hasAlias) {
throw new Error("Alias is required!");
}
var root = findProjectRoot(),
parser = new ProjectJsParser(),
projectFile = parser.loadProjectFile(root.file);
if (projectFile.hasAlias(options.alias)) {
projectFile.removeAlias(options.alias);
writeProjectFile(projectFile, root.file);
if (hasCallback) {
options.callback.call(null, null, {'success': true});
}
} else if (hasCallback) {
options.callback.call(null, new Error("Alias not registered!"), {'success': false});
} else {
throw new Error("Alias not registered!");
}
};
/**
* Sets the project's start point to a particular script file
* @param {String} script Path to script file
* @param {Function} callback Callback function
*/
this.setStartScript = function(options) {
var _ = require('underscore'),
ProjectJsParser = require('./lib/parser');
var hasScript = (_.has(options, "script") && !_.isEmpty(options.script)),
hasCallback = isCallbackSet(options);
if (!hasScript && hasCallback) {
options.callback.call(null, null, new Error("Script parameter is required"));
} else if (!hasScript) {
throw new Error("Script parameter is required");
}
var root = findProjectRoot(),
parser = new ProjectJsParser(),
projectFile = parser.loadProjectFile(root.file);
// TODO: add more verification / check to make sure file exists / is a file path
// TODO: also make sure script path is relative to src or root folder
projectFile.setStart(options.script);
writeProjectFile(projectFile, root.file);
if (hasCallback) {
options.callback.call(null, null, {'success': true, 'filePath': options.script});
}
};
/**
* Sets the project's start point to a particular class and method
* @param {String} className Name of starting class. Required.
* @param {String} method Name of start method. Required.
* @param {String} output Name or path of script to output on build
* @param {Function} callback Callback function
*/
this.setStartClass = function(options) {
var _ = require('underscore'),
ProjectJsParser = require('./lib/parser');
var hasClassName = (_.has(options, 'className') && !_.isEmpty(options.className)),
hasMethod = (_.has(options, 'method') && !_.isEmpty(options.method)),
hasCallback = isCallbackSet(options);
if (!hasClassName && hasCallback) {
options.callback.call(null, new Error("Class name is required"));
return;
} else if (!hasClassName) {
throw new Error("Class name is required");
} else if (!hasMethod && hasCallback) {
options.callback.call(null, new Error("Method is required"));
return;
} else if (!hasMethod) {
throw new Error("Method is required");
}
var root = findProjectRoot(),
parser = new ProjectJsParser(),
projectFile = parser.loadProjectFile(root.file);
//registry = parser.createRegistry(projectFile);
// TODO: add more verification / check to make sure class
// exists in the registry / has the specified method, etc.
if (!_.has(options, "output") || _.isEmpty(options.output)) {
// default output to "start.js"
options.output = "./start.js";
}
projectFile.setStart({
"class": options["className"],
"method": options["method"],
"output": options["output"]
});
writeProjectFile(projectFile, root.file);
if (hasCallback) {
options.callback.call(null, null, {'success': true});
}
};
/**
* Builds a project.
* @param {String} projectFile Path to project file to build. Optional.
* @param {Function} callback Callback function
*/
this.build = function(options) {
var ProjectJsParser = require('./lib/parser'),
ProjectJsCompiler = require('./lib/compiler'),
_ = require('underscore');
var parser = new ProjectJsParser(),
projectFile,
registry,
hasCallback = isCallbackSet(options);
if (_.has(options, 'projectFile') && !_.isEmpty(options.projectFile)) {
try {
projectFile = parser.loadProjectFile(options.projectFile);
registry = parser.createRegistry(projectFile);
} catch (e) {
if (hasCallback) {
options.callback.call(null, {
'message': "Unable to load project file",
'error': e
});
} else {
throw e;
}
return;
}
} else {
var root = findProjectRoot();
projectFile = parser.loadProjectFile(root.file);
registry = parser.createRegistry(projectFile);
}
var compiler = new ProjectJsCompiler();
compiler.buildProject(projectFile, registry);
if (hasCallback) {
options.callback.call(null, null, {'success': true});
}
};
/**
* Runs a project.
* @param {String} projectFile Path to project file. Optional.
* @param {Function} callback Callback function
*/
this.run = function(options) {
var fs = require('fs-extra'),
path = require('path'),
_ = require('underscore'),
ProjectJsParser = require('./lib/parser'),
ProjectJsRunner = require('./lib/runner');
var hasRoot = (_.has(options, 'projectFile') && !_.isEmpty(options.projectFile)),
hasCallback = isCallbackSet(options);
var rootDir,
parser = new ProjectJsParser(),
projectFile,
registry;
// resolve project file paths
try {
if (hasRoot) {
var filePath;
try {
filePath = fs.realpathSync(options.projectFile);
} catch (notfound) {
filePath = fs.realpathSync("." + path.sep + options.projectFile);
}
rootDir = path.dirname(filePath);
projectFile = parser.loadProjectFile(filePath);
registry = parser.createRegistry(projectFile);
} else {
var root = findProjectRoot();
rootDir = root.dir;
projectFile = parser.loadProjectFile(root.file);
registry = parser.createRegistry(projectFile);
}
} catch (e) {
if (hasCallback) {
options.callback.call(null, {
'message': "Error resolving project file",
'error': e
});
return;
} else {
throw e;
}
}
// run project
var runner = new ProjectJsRunner();
if (hasCallback) {
runner.run(rootDir, projectFile, registry, options.callback);
} else {
runner.run(rootDir, projectFile, registry);
}
};
};
return ProjectJs;
})();