forked from karlsen-network/node-flow-ux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EMANATE
358 lines (308 loc) · 9.77 KB
/
EMANATE
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
"use strict";
const UglifyJS = require("uglify-es");
const _ = require("underscore");
/*
var code = `
function add(first, second) {
return first + second;
}
let a = () => b();
//let c = a?.();
`;
*/
/*
minify.js(code, {js:{}})
.then(console.log)
.catch(console.error);
*/
/*
var result = UglifyJS.minify(code);
if(result.error)
console.log(result.error); // runtime error, or `undefined` if no error
console.log(result.code);
*/
/*
const ClosureCompiler = require('google-closure-compiler').jsCompiler;
console.log('CONTRIB_PATH:', ClosureCompiler.CONTRIB_PATH);
// absolute path to the contrib folder which contains externs
const closureCompiler = new ClosureCompiler({
compilation_level: 'SIMPLE_OPTIMIZATIONS'//'ADVANCED'
});
const compilerProcess = closureCompiler.run([{
path: 'file-one.js',
src: code,
sourceMap: null, // optional input source map
markUntranspilableFeaturesAsRemoved:false
}], (exitCode, stdOut, stdErr) => {
//compilation complete
console.log("exitCode, stdOut, stdErr", exitCode, stdOut, stdErr)
});
*/
const E = new Emanator(__dirname, {
type : 'UTIL',
guid : 'c1072045-6d98-44d8-9aa5-e9be6c79bd01',
group : 'aspectron',
ident : 'flow-ux',
title : 'flow-ux',
banner : 'flow-ux',
git : '[email protected]:aspectron/flow-ux',
author : "ASPECTRON Inc.",
url : "http://aspectron.com",
archive : true,
folder : true,
standalone : true,
production: true,
DMG: false,
//nwjs : { version : '0.46.2' },
resources : 'resources/setup',
skipDirCreation:true,
debugTask:true,
manifest:manifest=>{
return manifest;
}
});
E.manifest_read_sync();
/// TODO - GENERATE ZIP FILE IN DIST!
/// TODO - GENERATE ZIP FILE IN DIST!
/// TODO - GENERATE ZIP FILE IN DIST!
const REPO = __dirname;
//let LATEST = null;
let ZIP = null;
let ROOT = path.join(REPO, 'dist','releases');
let LATEST = path.join(REPO, 'dist','latest');
let DIST = path.join(REPO, 'dist');
if(E.flags.out && E.flags.out.length){
if(path.isAbsolute(E.flags.out))
ROOT = E.flags.out;
else
ROOT = path.join(process.cwd(), E.flags.out);
if(!fs.existsSync(ROOT))
throw new Error(`Output directory (${ROOT}) dont exists.`)
ROOT = ROOT.split(/[\/\\]/);
if(ROOT[ROOT.length-1] == "")
ROOT.pop();
ROOT = ROOT.join(path.sep);
E.log("ROOT", ROOT);
DIST = path.join(ROOT,'dist');
}
else if(fs.existsSync(path.join(__dirname,'..','cdn'))) {
ROOT = path.join(__dirname,'..','cdn','releases');
LATEST = path.join(__dirname,'..','cdn','latest');
DIST = path.join(__dirname,'..','cdn','dist');
await E.mkdirp(ROOT);
}
if(LATEST)
await E.mkdirp(LATEST);
await E.mkdirp(DIST);
const FLOW_UX_STATIC_FILENAME = `flow-ux-static${E.flags['no-version']?'':'-'+E.manifest.version}${E.flags.minify?'-min':''}`;
const FLOW_UX_STATIC = path.join(ROOT, FLOW_UX_STATIC_FILENAME);
const FLOW_UX_ARCHIVE = path.join(DIST,`${FLOW_UX_STATIC_FILENAME}.zip`);
const FLOW_UX_ARCHIVE_LATEST = path.join(DIST,`flow-ux-static-latest${E.flags.minify?'-min':''}.zip`);
console.log('target:',FLOW_UX_STATIC.brightCyan);
//let FLOW_UX_STATIC = `flow-ux-static${E.flags['no-version']?'':'-'+E.manifest.version}${E.flags.minify?'-min':''}`;
const FLOW_UX_FOLDER = `flow-ux`;
const FLOW_UX = path.join(FLOW_UX_STATIC, FLOW_UX_FOLDER);
const MODULES = FLOW_UX_STATIC;//path.join(FLOW_UX_STATIC, 'node_modules');
const DEMO = path.join(ROOT, 'demo');
E.task('create-dirs', [], (callback)=>{
return Promise.all(
[
FLOW_UX_FOLDER, 'lit-element', 'lit-html', 'lit', '@lit', 'webcomponentsjs'
].map(f=>E.emptyDir(path.join(FLOW_UX_STATIC, f)))
);
//return E.mkdirp(ROOT);
});
E.task('ux-resources', ['create-dirs'], callback=>{
let uxFiles = ['resources', 'flow-ux.js', 'flow-ux.css', 'src'];
let list = uxFiles.map(file =>
E.utils.copy(path.join(REPO, file), path.join(FLOW_UX, file))
);
let node_modules = ['lit-element', 'lit-html', 'lit', '@lit'];
list = list.concat(
node_modules.map(file =>
E.utils.copy(
path.join(REPO, 'node_modules', file),
path.join(MODULES, file)
)
)
);
list.push(
E.utils.copy(
path.join(REPO, 'node_modules', '@webcomponents', 'webcomponentsjs'),
path.join(MODULES, 'webcomponentsjs')
)
)
return Promise.all(list);
});
E.task('node_modules', ['ux-resources'], callback=>{
return Promise.all(
['lit-element', 'lit-html', 'lit', '@lit']
.map(module=>{
let SRC = path.join(MODULES, module);
return E.utils.iterateFolder(SRC, /\.js$/, file=>{
//console.log("file, folder", file, folder);
return fs.writeFile(file, getFileContent(file));
});
})
)
});
E.task('flow-src', ['ux-resources'], callback=>{
let SRC = FLOW_UX;//path.join(FLOW_UX, 'src');
return E.utils.iterateFolder(SRC, /\.js$/, file=>{
//console.log("file, folder", file, folder);
return fs.writeFile(file, getFileContent(file));
});
});
E.task('cleanup', ['node_modules'], async(callback)=>{
let folders = new Map();
let list = ['lit-element', 'lit-html', 'lit', '@lit'].map(module=>{
let SRC = path.join(MODULES, module);
return E.utils.iterateFolder(SRC, /(\.ts(\.map)?|tsconfig\.json)$/, file=>{
//console.log("file, folder", file);
let map = folders;
//console.log("------------")
path.dirname(file).substring(MODULES.length+1)
.split(path.sep).forEach(f=>{
//console.log("fff", f)
let d = map.get(f);
if(!d){
d = new Map();
map.set(f, d);
}
map = d;
})
return fs.unlink(file);
});
})
let WCPATH = path.join(MODULES, 'webcomponentsjs');
['src', 'bundles'].forEach(f=>{
list.push( fs.remove(path.join(WCPATH, f)) );
})
//E.log("list", list)
await Promise.all(list);
//E.log("folders", folders);
let digest = (folders, parent)=>{
if(!folders){
E.log("folders, parent", folders, parent)
}
folders.forEach((map, folder)=>{
folder = path.join(parent, folder);
digest(map, folder)
//console.log("folder", folder, map);
if(fs.existsSync(folder)){
try{
fs.rmdirSync(folder, {maxRetries:0});
}catch(e){
if(e.code != 'ENOTEMPTY')
console.log("rmdirSync:error", folder, e.code)
}
}
});
}
digest(folders.get("lit-html"), path.join(MODULES, 'lit-html'))
digest(folders.get("lit-element"), path.join(MODULES, 'lit-element'))
digest(folders.get("lit"), path.join(MODULES, 'lit'))
digest(folders.get("@lit"), path.join(MODULES, '@lit'))
//console.log("list2", list)
//await Promise.all(list)
});
E.task('static', ['node_modules', 'flow-src', 'cleanup'], async () => {
if(LATEST) {
const TARGET = FLOW_UX_STATIC;
const SYMLINK = path.join(LATEST,`flow-ux-static${E.flags.minify?'-min':''}`);
if(fs.existsSync(SYMLINK))
fs.unlinkSync(SYMLINK);
fs.symlinkSync(TARGET,SYMLINK,E.PLATFORM == 'windows' ? 'junction' : 'dir');
// fs.symlinkSync(TARGET,SYMLINK,'dir');
}
if(E.flags.package) {
const FOLDER = FLOW_UX_STATIC;
const cwd = ROOT;
const target = FLOW_UX_STATIC_FILENAME;
const archive = FLOW_UX_ARCHIVE;
const latest = FLOW_UX_ARCHIVE_LATEST;
// console.log("FOLDER:", FOLDER);
// console.log("ARCHIVE:",ARCHIVE);
if(fs.existsSync(archive))
await E.remove(archive);
E.log(`creating dist archive for:`, target);
await E.zip(target,archive,{ cwd });
if(fs.existsSync(latest))
await E.remove(latest);
await E.copy(archive,latest);
}
});
E.task('demo-site', ['static'], async(callback)=>{
await E.emptyDir(path.join(DEMO, FLOW_UX_FOLDER));
await E.emptyDir(path.join(DEMO, 'lit-html'));
await E.emptyDir(path.join(DEMO, 'lit-element'));
await E.emptyDir(path.join(DEMO, '@lit'));
return E.utils.copy(FLOW_UX_STATIC, DEMO);
})
let getFileContent = (filePath)=>{
let filePath_ = filePath.substring(FLOW_UX_STATIC.length+1);
let prefix = "../".repeat(Math.max(filePath_.split("/").length - 1, 0));
//console.log("filePath_", filePath_, filePath, prefix)
let regExp = /(import|export)([^'"]*)from[ ]{0,}['"]([^'"]*)['"]/g;
let code = (fs.readFileSync(filePath)+"")
.replace(regExp, (a, imEx, modules, file)=>{
//'import $1 from "/node_modules/$2"'
//if(a.includes("@"))
// console.log("input:"+a+"\n imEx, modules, file\n", imEx, modules, file)
if(!/\.js$/.test(file))
file += "/"+file.split("/").pop()+".js";
if(/^\./.test(file))
return `${imEx} ${modules} from "${file}"`;
/*
let found = this.urlProxies.find(p=>{
return file.indexOf(p.proxyUrl)===0;
})
if(found)
return `${imEx} ${modules} from "${file}"`;
*/
if(!/^\/node_modules/.test(file))
file = `${prefix}${file}`;
let s = `${imEx} ${modules} from "${file}"`;
//if(a.includes("@"))
// console.log("ss:", s+"\n\n")
return s;
});
if(!E.flags.minify)
return code;
let result = UglifyJS.minify(code);
if(result.error){
E.log("UglifyJS.minify:error", filePath, result.error)
return code;
}
return result.code;
}
let start_watching = ()=>{
const FLOW_UX_FOLDER_PATH = E.flags.direct? FLOW_UX_STATIC : FLOW_UX;
const update = _.debounce((folderName, filename) => {
console.log("update running...");
fs.copyFileSync(
path.join(__dirname, folderName, filename),
path.join(FLOW_UX_FOLDER_PATH, folderName, filename)
)
}, 300, true);
[".", "src"].forEach(folderName=> {
const folderPath = path.join(__dirname, folderName);
console.log(`watching... ${folderPath}`);
fs.watch(folderPath, (type, filename)=>{
if(filename.indexOf("flow-") !== 0)
return
console.log("type, filename", type, filename);
//console.log("running update...")
update(folderName, filename);
})
})
}
if(E.flags['demo-site'])
E.run(['demo-site']);
else if(E.flags.static)
E.run(['static']);
else if(E.flags.watch)
start_watching();
else{
//E.run();
}