-
Notifications
You must be signed in to change notification settings - Fork 3
/
bin.js
245 lines (215 loc) · 10.3 KB
/
bin.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
#! /usr/bin/env node
/**
* Main CLI script. Compiles, Initializes, Analyses the files.
* @author Q'inti [email protected]
*/
const fs = require('fs');
if (process.argv.length < 3) {
const compile = require('./src/compile').compileAll;
compile();
} else if (process.argv.length === 3 && ['init', 'help', 'watch'].includes(process.argv[2])) {
if (process.argv[2] === 'init') {
init();
} else if (process.argv[2] === 'watch') {
const compileFile = require('./src/compile').compile;
const compile = require('./src/compile').compileAll;
const tools = require('./src/tools');
let includes = {};
let reverseNames = {};
console.log('Watching the files:');
fs.readFile('.nazca', (err, content) => {
if (err) {
return console.error('No .nazca file is found');
}
content = tools.flattenNazcaConfig(content.toString());
let config = JSON.parse(content);
let sources = config.sources;
let sourceFiles = Object.keys(sources).map((key) => sources[key]);
for (let source in sources) {
reverseNames[sources[source]] = source;
}
let filesToWatch = [];
sourceFiles.forEach((file) => {
filesToWatch.push(file);
let includedFiles = tools.findIncludesRecursively(file);
filesToWatch = filesToWatch.concat(includedFiles);
includedFiles.forEach((iFile) => {
includes[iFile] = includes[iFile] || [];
includes[iFile].push(file);
});
});
for (let key in includes) {
includes[key] = includes[key].filter((value, index, self) => self.indexOf(value) === index);
}
filesToWatch = filesToWatch.filter((value, index, self) => {
return self.indexOf(value) === index;
});
filesToWatch.forEach((file) => {
console.log(file);
fs.watchFile(file, async () => {
console.log(`${file} is changed. Recompiling...`);
if (includes[file]) {
for (let i = 0, n = includes[file].length; i < n; i++) {
let include = includes[file][i];
await compileFile(include, reverseNames[include], config.out, config.beautify);
}
} else {
await compileFile(file, reverseNames[file], config.out, config.beautify);
}
});
});
console.log();
compile();
});
} else if (process.argv[2] === 'help') {
help();
}
} else if ((process.argv.length === 4 && ['analyse', 'analyze'].includes(process.argv[2]))) {
let file = process.argv[3];
if (!fs.existsSync(file)) {
require('./src/compile');
} else {
require('./src/analyse').analyseFile(file);
}
} else {
console.log('nazca usage is invalid');
help();
}
function help() {
console.log('Usage:');
console.log();
console.log('1. Compile .nazca project');
console.log('┌────────────────────────────────────────────────────────────┐');
console.log('│> cd /path/to/the/folder/with/.nazca/file │');
console.log('│> nazca │');
console.log('└────────────────────────────────────────────────────────────┘');
console.log();
console.log('2. Analyse the *.nazca file for the correct syntax');
console.log('┌────────────────────────────────────────────────────────────┐');
console.log('│> nazca analyse /path/to/file.nazca │');
console.log('└────────────────────────────────────────────────────────────┘');
console.log();
console.log('3. Generate .nasca file');
console.log('┌────────────────────────────────────────────────────────────┐');
console.log('│> cd /folder/where/you/want/to/init/the/project │');
console.log('│> nazca init │');
console.log('└────────────────────────────────────────────────────────────┘');
console.log();
console.log('4. Watch the files and compile the project if any file in .nazca config changes');
console.log('┌────────────────────────────────────────────────────────────┐');
console.log('│> cd /folder/where/you/want/to/init/the/project │');
console.log('│> nazca watch │');
console.log('└────────────────────────────────────────────────────────────┘');
console.log();
console.log('5. Show the usage info');
console.log('┌────────────────────────────────────────────────────────────┐');
console.log('│> nazca help │');
console.log('└────────────────────────────────────────────────────────────┘');
}
function init() {
// To avoid the moment.js dependency, we are doing all time calculations manually.
let currentTime = new Date();
let hours = addLeadZero(currentTime.getHours());
let minutes = addLeadZero(currentTime.getMinutes());
let seconds = addLeadZero(currentTime.getSeconds());
let day = currentTime.getDate();
const dayTermination = ['st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th',
'th', 'th', 'th', 'th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'st'];
day = `${day}${dayTermination[day]}`;
let month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
"November", "December"][currentTime.getMonth()];
let year = currentTime.getFullYear();
function addLeadZero(number) {
return `${number < 10 ? '0' : ''}${number}`;
}
let name = process.cwd().split(/\/|\\/).pop();
const defaultNazcaConfig = `
/**
* This is a sample .nazca file generated by ${process.env.USERNAME} at ${hours}:${minutes}:${seconds} on ${day} of ${month} ${year}
* It is a json file with block and a line comments
*/
{
"name": "${name}",
/*
"sources" shows what nazca files correspond to what css/html/js in form of
"fileName": "path/to/nazca/file.nazca"
It will generate fileName.html, fileName.css and fileName.js files.
*/
"sources": {
"index": "nazca/index.nazca"
},
/*
"out" shows the folders, where the css/html/js should be generated
*/
"out": {
"path": "www", // main folder path to output the files
"css": "css", // path to the css folder. All css files will be generated in 'path/css' folder
"html": ".", // path to the html folder. All html files will be generated in 'path/html' folder
"js": "js" // path to the js folder. All js files will be generated in 'path/js' folder
},
"beautify": 0 // -1 - uglify the JS, 0 - do not change, 1 - beautify the JS
}
`;
let indexNazca = `
.html {
.head {
.title {
text: Welcome to the world of Nazca;
};
};
.body {
font-size: 13pt;
.div {
width: 300px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 5px double #aaa;
padding: 10px;
text-align: center;
.img {
width: 100%;
$src: https://raw.githubusercontent.com/Qinti/nazca/master/logo/NazcaLogo.png;
};
.div {
html: The project is initialized, but is not functional yet.<br>Come back later!;
};
.div {
text-align: right;
padding-top: 35px;
font-size: 9pt;
text: Powered by;
.a {
color: #38742e;
font-weight: bold;
text: Nazca;
$href: https://github.com/Qinti/nazca;
};
};
};
};
};
`;
fs.writeFile('.nazca', defaultNazcaConfig, (err) => {
if (err) {
return console.error(err);
}
fs.mkdir('nazca', (err) => {
if (err && err.code !== 'EEXIST') {
return console.error(err);
}
fs.writeFile('.nazca', defaultNazcaConfig, (err) => {
if (err) {
return console.error(err);
}
fs.writeFile('nazca/index.nazca', indexNazca, (err) => {
if (err) {
return console.error(err);
}
console.log('.nazca is initialized');
});
});
});
});
}