forked from patrickpissurno/li-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lit-cli.js
executable file
·202 lines (183 loc) · 6.42 KB
/
lit-cli.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
#!/usr/bin/env node
const fs = require('fs');
const util = require('util');
const writeFile = util.promisify(fs.writeFile);
const path = require('path');
const cmd = require('commander');
const lit = require('./lit');
cmd.description('Command-line tool for pre-compiling or rendering li-template directly')
.option('-i, --input <input>', 'Input file or directory')
.option('-o, --output <output>', 'Output file or directory')
.option('-r --render', 'Tells li-tempate to look for JSON files and render them')
.option('-p --partials', 'Tells li-tempate to render partials')
.parse(process.argv)
if(cmd.input != null && cmd.output != null){
let input = cmd.input;
let output = cmd.output;
let render = cmd.render != null;
let renderPartials = render && cmd.partials != null;
let inputFiles = [];
let outputDirectory;
let outputFile;
try
{
if(fs.lstatSync(input).isDirectory())
{
fs.readdirSync(input)
.filter(x => fs.lstatSync(path.join(input, x)).isFile() && path.parse(path.join(input, x)).ext.toLowerCase() == '.lit')
.forEach(x => inputFiles.push(path.join(input, x)));
}
else if(fs.lstatSync(input).isFile())
inputFiles.push(input);
else
{
console.error('Invalid input file');
process.exit(1);
}
}
catch(ex){
console.error('The specified input file doesn\'t exist. Stack:');
console.error(ex);
process.exit(1);
}
try
{
if(!fs.lstatSync(output).isDirectory())
{
if(inputFiles.length !== 1)
{
console.error(`There are ${inputFiles.length} input file${inputFiles.length != 1 ? 's' : ''} for just 1 output file`);
process.exit(1);
}
else
outputFile = output;
}
else
outputDirectory = output;
}
catch(ex){
console.error('The specified output directory doesn\'t exist. Stack:');
console.error(ex);
process.exit(1);
}
(async() => {
let precompiled;
try
{
precompiled = await Promise.all(inputFiles.map(async x => await lit.precompileFile(x)));
}
catch(ex){
console.error('There was a problem pre-compiling your files. Stack:');
console.error(ex);
process.exit(1);
}
let rendered = [];
if(render)
{
let data;
try
{
data = inputFiles.map(x => {
try
{
return JSON.parse(fs.readFileSync(x + '.json'))
}
catch(ex)
{
return null;
}
});
if(data.filter(x => x != null).length == 0)
{
console.error('There are no .lit.json files. Stack:');
process.exit(1);
}
}
catch(ex){
console.error('There was a problem reading your .lit.json files. Stack:');
console.error(ex);
process.exit(1);
}
let compiled;
try
{
let promises = [];
for(let i = 0; i<precompiled.length; i++)
{
if(data[i] == null)
promises.push(null);
else
{
let partials = null;
if(renderPartials && data[i].partials != null)
{
let partialsDir = path.dirname(inputFiles[i]);
partials = await lit.compilePartials(data[i].partials.map(x => {
if(typeof(x) === 'string')
return path.join(partialsDir, x);
x.file = path.join(partialsDir, x.file);
return x;
}));
}
delete data[i].partials;
promises.push(lit.compile('', partials, { precompiled: precompiled[i] }));
}
}
compiled = await Promise.all(promises);
}
catch(ex){
console.error('There was a problem compiling your files to render them. Stack:');
console.error(ex);
process.exit(1);
}
try
{
for(let i = 0; i<compiled.length; i++)
{
if(compiled[i] != null)
rendered.push(compiled[i](data[i]));
}
}
catch(ex){
console.error('There was a problem rendering your template files. Stack:');
console.error(ex);
process.exit(1);
}
}
try
{
if(outputFile != null)
{
fs.writeFileSync(outputFile + '.js', precompiled[0]);
if(rendered.length === 1)
fs.writeFileSync(outputFile + '.html', rendered[0])
}
else if(outputDirectory != null)
{
let promises = [];
for(let i = 0; i<precompiled.length; i++){
let filePath = path.parse(inputFiles[i]);
promises.push(writeFile(path.join(outputDirectory, filePath.name + '.js'), precompiled[i]));
if(i < rendered.length)
promises.push(writeFile(path.join(outputDirectory, filePath.name + '.html'), rendered[i]));
}
await Promise.all(promises);
}
else
{
console.error('No valid output directory specified. This shoudln\'t be hapenning, though');
process.exit(1);
}
}
catch(ex){
console.error('There was a problem writting your files to disk. Stack:');
console.error(ex);
process.exit(1);
}
console.log(`Pre-compile${render ? ' and render' : ''} success!`);
})();
}
else
{
console.error('Missing arguments. Run li-template --help for usage help');
}