forked from pasaran/yate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyate
executable file
·86 lines (65 loc) · 2.3 KB
/
yate
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
#!/usr/bin/env node
// --------------------------------------------------------------------------------------------------------------- //
var fs_ = require('fs');
var path_ = require('path');
// --------------------------------------------------------------------------------------------------------------- //
var yate = require('./lib/yate.js');
require('./lib/actions.js');
// --------------------------------------------------------------------------------------------------------------- //
var nopt = require('nopt');
var parsed = nopt(
{
'write-ast': Boolean,
'ast': Boolean,
'print': Boolean,
'mode': String,
'output': String,
'import': [ Array, path_ ],
'module-name': String
},
{
'a': '--ast',
'p': '--print',
'm': '--mode',
'o': '--output',
'i': '--import'
}
);
var mode = parsed.mode || '';
var template = parsed.argv.remain[0];
var data = parsed.argv.remain[1];
var externals = parsed.argv.remain[2];
if (!template) {
console.log('Usage: yate --output template.js template.yate');
console.log(' yate [ --mode mode ] template.yate data.json [ external.js ]');
console.log(' yate --ast template.yate');
console.log(' yate --print template.yate');
return 10;
}
// --------------------------------------------------------------------------------------------------------------- //
yate.modules = {};
var imports = parsed.import;
if (imports) {
for (var i = 0, l = imports.length; i < l; i++) {
var filename = imports[i];
var obj = JSON.parse( fs_.readFileSync(filename, 'utf-8') );
yate.modules[ obj.name ] = obj;
}
}
if (parsed.ast) {
console.log( yate.compile(template, parsed).ast.toString() );
} else if (parsed.print) {
var ast = yate.parse(template, parsed);
console.log( ast.yate() );
} else if (data) {
console.log( yate.run(template, parsed, { filename: data }, externals, mode) );
} else {
var js = yate.compile(template, parsed).js;
if (parsed.output) {
fs_.writeFileSync(parsed.output, js, 'utf-8');
} else {
console.log(js);
}
}
// --------------------------------------------------------------------------------------------------------------- //
// vim: set filetype=javascript: