roll-right 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/phase1.js ADDED
@@ -0,0 +1,441 @@
1
+
2
+ const fs = require('fs-extra')
3
+ const path = require('path')
4
+ const untildify = require('untildify')
5
+ const {translate_marker} = require('../lib/utils')
6
+
7
+ //$$files::header.html<<
8
+ const g_inserts_match = /\$\$files\:\:(\w|_|-|\+)+\/*(\w|_|-|\+)+\.(\w|_|-\+)+\<\</g
9
+ const g_names_inserts_match = /\$\$files\:\:name\:\:(\w|_|-|\+)+\/*(\w|_|-|\+)+\<\</g
10
+
11
+ // ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
12
+ //
13
+
14
+ function subst(fdata,key,value) {
15
+ while ( fdata.indexOf(key) >= 0 ) {
16
+ fdata = fdata.replace(key,value)
17
+ }
18
+ return fdata
19
+ }
20
+
21
+
22
+ function mapify(a1,a2,key_edit) {
23
+ let the_map = {}
24
+ let n = a1.length
25
+ if ( typeof key_edit === 'function' ) {
26
+ for ( let i = 0; i < n; i++ ) {
27
+ let ky = key_edit(a1[i])
28
+ the_map[ky] = a2[i]
29
+ }
30
+ } else {
31
+ for ( let i = 0; i < n; i++ ) {
32
+ the_map[a1[i]] = a2[i]
33
+ }
34
+ }
35
+ return the_map
36
+ }
37
+
38
+
39
+ function find_map(part_form,the_map) {
40
+
41
+ let key = part_form.substr(0,part_form.indexOf("<<")).trim()
42
+ if ( key.length === 0 ) {
43
+ //console.log(part_form)
44
+ }
45
+
46
+ let data = the_map[key]
47
+
48
+ return[key,data]
49
+ }
50
+
51
+
52
+
53
+ function key_map_sub(file_data,key_values,vars) {
54
+ let fdata = '' + file_data
55
+ for ( let key in key_values ) {
56
+ let value = key_values[key]
57
+ if ( value[0] === '>' ) {
58
+ let varname = value.substr(1)
59
+ let i = vars.indexOf(varname)
60
+ if ( i >= 0 ) {
61
+ value = vars.substr(i + varname.length + '::'.length)
62
+ if ( value.indexOf('::') > 0 ) {
63
+ value = value.substr(0,value.indexOf('::'))
64
+ }
65
+ value = value.trim()
66
+ }
67
+ }
68
+ fdata = subst(fdata,`$$${key}`,value)
69
+ }
70
+ //
71
+ return fdata
72
+ }
73
+
74
+
75
+
76
+ function windows_module(file_data) {
77
+ let lines = file_data.split('\n')
78
+ lines = lines.map(line => {
79
+ if ( /\/\/\s+MODULE\:/.test(line) ) {
80
+ return line.replace('(modularized)','(windowized)')
81
+ } else if ( /import\s+\{/.test(line) ) {
82
+ return ""
83
+ } else if ( /^import\s+\* as\s+.+\s+from/.test(line) ) {
84
+ return ""
85
+ } else if ( /^export\s+function/.test(line) ) {
86
+ return line.replace('export',"").trim()
87
+ } else if ( /^export\s+async\s+function/.test(line) ) {
88
+ return line.replace('export',"").trim()
89
+ } else if ( /\/\/windowize>>/.test(line) ) {
90
+ return line.replace('//windowize>>','')
91
+ }
92
+ return line
93
+ })
94
+
95
+ return lines.join('\n')
96
+ }
97
+
98
+
99
+
100
+ function file_transformations(transforms,file_data) {
101
+ if ( Array.isArray(transforms) ) {
102
+ for ( let trans of transforms ) {
103
+ file_data = file_transformations(trans,file_data)
104
+ }
105
+ } else if ( typeof transforms === "string" ) {
106
+ switch ( transforms ) {
107
+ case "windowize" : {
108
+ file_data = windows_module(file_data)
109
+ break;
110
+ }
111
+ default : {
112
+ break;
113
+ }
114
+ }
115
+ }
116
+
117
+ return file_data
118
+ }
119
+
120
+
121
+ function alphas_file_paths(clean_key,conf) {
122
+ console.log("alphas_file_paths: " + clean_key)
123
+ //
124
+ let the_file = ""
125
+ if ( (clean_key.indexOf('/') > 0) && (clean_key[0] !== '[') ) {
126
+ let top_dir = clean_key.substr(0,clean_key.indexOf('/'))
127
+ let tdir = conf.top_dir_location[top_dir]
128
+ tdir = tdir === undefined ? `./top_dir` : tdir
129
+ the_file = clean_key.replace(top_dir,tdir)
130
+ the_file = translate_marker(the_file,conf)
131
+ } else if ( (clean_key.indexOf('/') > 0) && (clean_key[0] === '[') ) {
132
+ //
133
+ the_file = translate_marker(clean_key,conf)
134
+ } else {
135
+ let ext = path.extname(clean_key)
136
+ let src_dir = conf.ext_default_dir[ext]
137
+ the_file = src_dir + '/' + clean_key
138
+ the_file = translate_marker(the_file,conf)
139
+ }
140
+ return the_file
141
+ }
142
+
143
+
144
+ function sub_file_replace(file_data,defs,conf) {
145
+ for ( let file in defs ) {
146
+ try {
147
+ let the_file = alphas_file_paths(file,conf)
148
+ let sub_file = fs.readFileSync(the_file).toString()
149
+ let marker = `$$file::${file}<<`
150
+ file_data = file_data.replace(marker,sub_file)
151
+ } catch(e) {
152
+ }
153
+ }
154
+ return file_data
155
+ }
156
+
157
+
158
+
159
+ function sub_file_processing(clean_key,file_def,conf) {
160
+ let the_file = alphas_file_paths(clean_key,conf)
161
+ try {
162
+ let file_data = fs.readFileSync(the_file).toString()
163
+ if ( typeof file_def === 'object' ) {
164
+ file_data = sub_file_replace(file_data,file_def,conf)
165
+ }
166
+ return file_data
167
+ } catch (e) {
168
+ console.log(e)
169
+ }
170
+ return ""
171
+ }
172
+
173
+
174
+ function file_replacement(key_string,conf,file_key) {
175
+ let file_map = conf.files
176
+ let file_defs = file_map[file_key]
177
+ let clean_key = key_string.replace("$$files::","")
178
+ clean_key = clean_key.replace("<<",'')
179
+ return sub_file_processing(clean_key,file_defs[clean_key],conf)
180
+ }
181
+
182
+
183
+ function named_replacer_replacement(key_string,conf,file_key) {
184
+ let file_map = conf.files
185
+ let file_defs = file_map[file_key]
186
+
187
+ let clean_key = key_string.replace("$$files::","")
188
+ clean_key = clean_key.replace("<<",'')
189
+ let named_file_def = file_defs[clean_key]
190
+ let ext = path.extname(named_file_def.file)
191
+ let src_dir = conf.ext_default_dir[ext]
192
+ let the_file = src_dir + '/' + named_file_def.file
193
+ //
194
+ console.log(the_file)
195
+ if ( the_file[0] === '[' ) {
196
+ the_file = translate_marker(the_file,conf)
197
+ }
198
+ try {
199
+ let file_data = fs.readFileSync(the_file).toString()
200
+ //
201
+ if ( typeof named_file_def.key_values === 'object' ) {
202
+ file_data = key_map_sub(file_data,named_file_def.key_values,clean_key)
203
+ }
204
+ //
205
+ return file_data
206
+ } catch (e) {
207
+ console.log(e)
208
+ }
209
+ return ""
210
+ }
211
+
212
+
213
+
214
+ function filter_file_data(file_data,script_filters) {
215
+ let exclusions = script_filters.exclude
216
+ let inclusions = script_filters.include
217
+ let transforms = script_filters.transforms
218
+
219
+ if ( transforms ) {
220
+ file_data = file_transformations(transforms,file_data)
221
+ }
222
+
223
+ let file_data_update = ""
224
+
225
+ let exportations = file_data.split('$$EXPORTABLE::')[1]
226
+ if ( exportations ) {
227
+ //
228
+ exportations = exportations.trim()
229
+ exportations = exportations.replace('/*','').replace('*/','').trim()
230
+ exportations = exportations.split('\n')
231
+ exportations = exportations.map(line => { return line.trim() })
232
+
233
+ //
234
+ if ( exclusions === '*' ) return ""
235
+ if ( inclusions === '*' ) return file_data
236
+ if ( exclusions ) {
237
+ exportations = exportations.filter((exprt) => { return (exclusions.indexOf(exprt) < 0) })
238
+ }
239
+ if ( inclusions ) {
240
+ exportations = exportations.filter((exprt) => { return (inclusions.indexOf(exprt) >= 0) })
241
+ }
242
+ //
243
+
244
+ let file_parts = file_data.split('//$>>')
245
+ file_data_update = file_parts.shift()
246
+ let file_parts_map = {}
247
+ for ( let part of file_parts ) {
248
+ let key = part.substr(0,part.indexOf('\n'))
249
+ key = key.trim()
250
+ file_parts_map[key] = '//$>>' + part
251
+ }
252
+
253
+ for ( let ky of exportations ) {
254
+ file_data_update += file_parts_map[ky]
255
+ }
256
+ //
257
+ } else {
258
+ file_data_update = file_data
259
+ }
260
+
261
+ return file_data_update
262
+ }
263
+
264
+
265
+
266
+ function oneof_absolutes(conf,the_key) {
267
+ if ( the_key[0] === '<' ) {
268
+ let abss = conf.absolutes
269
+ for ( let abKy in abss ) {
270
+ if ( the_key.indexOf(abKy) === 0 ) {
271
+ return abss[abKy]
272
+ }
273
+ }
274
+ }
275
+ return false
276
+ }
277
+
278
+
279
+ function load_scripts(conf,file_key) {
280
+ //
281
+ let file_map = conf.files
282
+ let file_defs = file_map[file_key]
283
+ let script_list = file_defs.script
284
+ //
285
+ // script_list
286
+ let the_file = ""
287
+ let the_map = {}
288
+ for ( let clean_key in script_list ) {
289
+ let fkeys = oneof_absolutes(conf,clean_key)
290
+ if ( fkeys ) { // check for a kind of absolute path spec
291
+ the_file = fkeys.root + '/' + fkeys.offset
292
+ } else {
293
+ if ( clean_key.indexOf('/') > 0 ) { // assume a selected directory by offset from call location
294
+ the_file = alphas_file_paths(clean_key,conf)
295
+ } else { // get a default location for types of files...
296
+ let ext = path.extname(clean_key)
297
+ let src_dir = conf.ext_default_dir[ext]
298
+ the_file = src_dir + '/' + clean_key
299
+ the_file = translate_marker(the_file,conf)
300
+ }
301
+ }
302
+ let replacers = script_list[clean_key]
303
+ if ( typeof replacers === "object" ) {
304
+ if ( typeof replacers.replace === "string" ) {
305
+ let repl = clean_key.substring(clean_key.lastIndexOf('/')+1)
306
+ the_file = the_file.replace(repl,replacers.replace)
307
+ }
308
+ }
309
+ //
310
+ console.log(the_file)
311
+ if ( the_file[0] === '[' ) {
312
+ the_file = translate_marker(the_file,conf)
313
+ }
314
+ try {
315
+ let file_data = fs.readFileSync(the_file).toString()
316
+ let m_file_key = `script::${clean_key}`
317
+ //
318
+ let script_filters = script_list[clean_key]
319
+ if ( typeof script_filters === "object" ) {
320
+ file_data = filter_file_data(file_data,script_filters)
321
+ }
322
+ //
323
+ the_map[m_file_key] = file_data
324
+ } catch (e) {
325
+ console.log(e)
326
+ }
327
+ }
328
+ //
329
+ return the_map
330
+ }
331
+
332
+ // ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
333
+
334
+
335
+ class Phase1 {
336
+
337
+ constructor(target,target_conf) {
338
+ this.tconf = target_conf
339
+ this._target = target
340
+ }
341
+
342
+ ensure_directory(out_dir,target) {
343
+ fs.ensureDirSync(`${out_dir}/${target}`)
344
+ }
345
+
346
+
347
+ config(conf,current_file_key) {
348
+ console.log(conf.business_url)
349
+ console.log(conf.pre_template)
350
+
351
+ let tmpl_file = conf.pre_template
352
+ if ( tmpl_file[0] === '[' ) {
353
+ tmpl_file = translate_marker(tmpl_file,conf)
354
+ }
355
+ try {
356
+ let html_def = fs.readFileSync(tmpl_file,'utf8').toString()
357
+ //
358
+ let replacers = html_def.match(g_inserts_match)
359
+ let named_replacers = html_def.match(g_names_inserts_match)
360
+
361
+ //
362
+ console.log("FILES file_replacement")
363
+ let key_map_replacers = {}
364
+ if ( !!replacers ) {
365
+ let replacers_content = replacers.map((key_string) => {
366
+ let repl = file_replacement(key_string,conf,current_file_key)
367
+ if ( repl === undefined ) {
368
+ return ""
369
+ }
370
+ return repl
371
+ })
372
+
373
+ key_map_replacers = mapify(replacers,replacers_content,(key) => { return key.replace('$$','').replace('<<','') } )
374
+ }
375
+
376
+
377
+ let key_map_named_replacers = {}
378
+ if ( !!named_replacers ) {
379
+ console.log("NAMED FILES named_replacer_replacement")
380
+ let named_replacers_content = named_replacers.map((key_string) => {
381
+ return named_replacer_replacement(key_string,conf,current_file_key)
382
+ })
383
+ key_map_named_replacers = mapify(named_replacers,named_replacers_content,(key) => { return key.replace('$$','').replace('<<','')} )
384
+ }
385
+
386
+ console.log("SCRIPTS load_scripts")
387
+ let script_map = load_scripts(conf,current_file_key)
388
+
389
+ let key_map_all = Object.assign({},key_map_replacers,key_map_named_replacers,script_map)
390
+
391
+ //console.dir(key_map_all)
392
+ console.dir(Object.keys(key_map_all))
393
+ //
394
+ let results = []
395
+ let leaders = html_def.split('$$')
396
+ results.push(leaders.shift())
397
+ //
398
+ //
399
+ for ( let nextL of leaders ) {
400
+ let [key,found_sub] = find_map(nextL,key_map_all)
401
+ console.log(key)
402
+ if ( typeof found_sub === "object" ) {
403
+ console.dir(found_sub)
404
+ }
405
+ let matcher = `${key}<<`
406
+ if ( found_sub !== undefined ) {
407
+ let sub_file = nextL.replace(matcher,found_sub)
408
+ results.push(sub_file)
409
+ }
410
+ }
411
+ //
412
+ let sdata = results.join('\n')
413
+
414
+ let output_file = conf.out_dir + '/' + this._target + '/' + current_file_key
415
+ console.log("WRITING OUTPUT: ",output_file)
416
+ //
417
+ fs.writeFileSync(output_file,sdata)
418
+ //
419
+ } catch(e) {
420
+ console.log("TEMPLATE: " + tmpl_file + " does not exists or does not have permissions or is not formatted correctly")
421
+ console.log(e)
422
+ }
423
+ }
424
+
425
+
426
+
427
+ run() {
428
+ //
429
+ console.log("ENSURE: ",this.tconf.out_dir)
430
+ console.log("TARGET:",this._target)
431
+ //
432
+ this.ensure_directory(this.tconf.out_dir,this._target)
433
+ for ( let file in this.tconf.files ) {
434
+ this.config(this.tconf,file)
435
+ }
436
+ }
437
+
438
+
439
+ }
440
+
441
+ module.exports = Phase1
package/lib/phase2.js ADDED
@@ -0,0 +1,77 @@
1
+ const fs = require('fs-extra')
2
+ const {spawn} = require('child_process')
3
+ const {translate_marker,gsubst} = require('../lib/utils')
4
+
5
+
6
+ // node genpage.js ${top_level}/${dir}/static/${dir}.subst ../templates/index.html ${top_level}/${dir}/index.html
7
+
8
+ class Phase2 {
9
+ // ----
10
+ constructor(target,target_conf) {
11
+ this.tconf = target_conf
12
+ this._target = target
13
+ }
14
+
15
+ ensure_directory(out_dir,target) {
16
+ fs.ensureDirSync(`${out_dir}/${target}`)
17
+ }
18
+
19
+ config(tconf,target) {
20
+ let static_artifacts = tconf.static_artifacts
21
+ let where_is_subst_file = `${static_artifacts}/${target}.subst`
22
+ where_is_subst_file = gsubst(where_is_subst_file,'$$target',target)
23
+ where_is_subst_file = translate_marker(where_is_subst_file,tconf)
24
+ //
25
+ let where_is_template_file = tconf.template
26
+ where_is_template_file = gsubst(where_is_template_file,'$$target',target)
27
+ where_is_template_file = translate_marker(where_is_template_file,tconf)
28
+ //
29
+ let derived_output_file_name = tconf.template.substr(tconf.template.lastIndexOf('/'))
30
+ //
31
+ let where_does_output_go = tconf.out_dir
32
+ where_does_output_go = gsubst(where_does_output_go,'$$target',target)
33
+ where_does_output_go = translate_marker(where_does_output_go,tconf)
34
+ where_does_output_go += derived_output_file_name
35
+ //
36
+ let where_are_scripts = tconf.scripts_dir
37
+ where_are_scripts = gsubst(where_are_scripts,'$$target',target)
38
+ where_are_scripts = translate_marker(where_are_scripts,tconf) + '/tools'
39
+
40
+ console.log("----------------")
41
+ console.log("subst: " + where_is_subst_file)
42
+ console.log("template: " + where_is_template_file)
43
+ console.log("output: " + where_does_output_go)
44
+ console.log("tools: " + where_are_scripts)
45
+
46
+ let generator_program = `${where_are_scripts}/genpage.js`
47
+ //
48
+
49
+ let spawner = spawn("node",[generator_program, where_is_subst_file, where_is_template_file, where_does_output_go])
50
+
51
+ spawner.stdout.on('data', (data) => {
52
+ console.log(`stdout: ${data}`);
53
+ });
54
+
55
+ spawner.stderr.on('data', (data) => {
56
+ console.error(`stderr: ${data}`);
57
+ });
58
+
59
+ spawner.on('close', (code) => {
60
+ console.log(`child process exited with code ${code}`);
61
+ });
62
+
63
+ }
64
+
65
+ run() {
66
+ let out_dir = translate_marker(this.tconf.out_dir,this.tconf)
67
+ this.ensure_directory(out_dir,this._target)
68
+ this.config(this.tconf,this._target,this.tconf.template)
69
+ }
70
+
71
+
72
+
73
+ }
74
+
75
+
76
+
77
+ module.exports = Phase2
package/lib/rr_utils.js CHANGED
@@ -1,27 +1,147 @@
1
1
 
2
- const { dir } = require('console')
3
2
  const fs = require('fs')
3
+ const path = require('path')
4
+ const {load_json_file,array_flatten} = require('../lib/utils')
4
5
 
5
6
 
7
+ // from stack oveflow
8
+ function isDir(path) {
9
+ try {
10
+ let stat = fs.lstatSync(path);
11
+ return stat.isDirectory();
12
+ } catch (e) {
13
+ // lstatSync throws an error if path doesn't exist
14
+ return false;
15
+ }
16
+ }
17
+
18
+ function load_package_json(pars) {
19
+ let file_path = `${pars}/package.json`
20
+ try {
21
+ let content = fs.readFileSync(file_path).toString()
22
+ return { file_path, content }
23
+ } catch(e) {
24
+ }
25
+ return { file_path, "content" : false }
26
+ }
6
27
 
28
+ const g_permit_paths = {'.js' : true, '.mjs' : true, '.json' : true }
7
29
 
8
- module.exports.browser_code_access = (pars) => {
9
- if ( pars === undefined ) {
30
+ const _browser_code_access = (pars,subdirs,topdir) => {
31
+ if ( typeof pars === "string" ) {
10
32
  try {
11
- let client_code_path = './client'
33
+ let client_code_path = (topdir === undefined) ? `${pars}/client` : `${pars}/${topdir}`
12
34
  let dirlist = fs.readdirSync(client_code_path)
13
35
  console.dir(dirlist)
14
36
  let file_content = dirlist.map(file_name => {
15
37
  let file_path = client_code_path + '/' + file_name
16
- let content = fs.readFileSync(file_path).toString()
17
- return content
38
+ let content = ""
39
+ if ( !(isDir(file_path)) ) {
40
+ if ( path.extname(file_path) in g_permit_paths ) {
41
+ content = fs.readFileSync(file_path).toString()
42
+ return { file_path, content }
43
+ }
44
+ } else if ( subdirs !== undefined ) {
45
+ if ( subdirs.indexOf(file_name) >= 0 ) {
46
+ return _browser_code_access(client_code_path,subdirs,file_name)
47
+ }
48
+ }
49
+ return { file_path, content }
18
50
  })
19
- return file_content
51
+ file_content = array_flatten(file_content)
52
+ let package_item = load_package_json(pars)
53
+ if ( package_item.content ) file_content.unshift(package_item)
54
+ return file_content.filter(data => (data.content.length > 0))
20
55
  } catch(e) {
21
56
  console.log(e)
22
57
  }
23
58
  }
24
- console.log("no files " + __dirname + " " + __filename )
59
+ console.log("_browser_code_access :: no files " + __dirname + " " + __filename )
25
60
  return []
26
61
  }
27
62
 
63
+
64
+ module.exports.browser_code_access = _browser_code_access
65
+
66
+
67
+
68
+
69
+ function check_sub_deps(dep,dependencies) {
70
+ console.log(dep)
71
+ console.log(dependencies)
72
+ if ( Array.isArray(dep) ) {
73
+ let ret_val = dep.shift()
74
+ for ( let subdep of dep ) {
75
+ if ( subdep in dependencies ) {
76
+ return ret_val
77
+ }
78
+ }
79
+ }
80
+ return false
81
+ }
82
+
83
+
84
+
85
+ // // transfer_node_module_browser_version
86
+ module.exports.transfer_node_module_browser_version = (source_spec) => {
87
+ let check_file = source_spec.file
88
+ let out_dir = source_spec.out_dir
89
+ let missing_dependencies = []
90
+ try {
91
+ // need to do this... node does not look at the current working directory
92
+ module.paths.unshift(`${process.cwd()}/node_modules`)
93
+ //
94
+ let package_file = load_json_file(check_file)
95
+ for ( let dep of source_spec.dependencies ) {
96
+ if ( ((typeof dep === 'string') && package_file.dependencies[dep]) || (dep = check_sub_deps(dep,package_file.dependencies)) ) {
97
+ console.log(dep + " :: " + package_file.dependencies[dep])
98
+ //
99
+ let depCheck = require(dep) // require ... ... ... ... ...
100
+ //
101
+ for ( let depKy in depCheck ) {
102
+ console.log(depKy + " ::: " + typeof depCheck[depKy] )
103
+ if ( typeof depCheck[depKy] === "function" ) {
104
+ if (depKy === "browser_code") {
105
+ try {
106
+ let code_list = depCheck.browser_code()
107
+ for ( let codes of code_list ) {
108
+ let { file_path, content } = codes
109
+ if ( file_path.indexOf("package.json") >= 0 ) {
110
+ let mod_pack = load_json_file(file_path)
111
+ let deps = mod_pack.dependencies
112
+ package_file.dependencies = Object.assign({},package_file.dependencies,deps)
113
+ } else {
114
+ console.log(" -------> " + file_path)
115
+ //console.log(content)
116
+ let out_file = `${out_dir}/${dep}.js`
117
+ fs.writeFileSync(out_file,content)
118
+ }
119
+ }
120
+ } catch (e) {
121
+ console.log(e)
122
+ }
123
+ }
124
+ }
125
+ }
126
+ } else {
127
+ missing_dependencies.push(dep)
128
+ }
129
+ }
130
+ //
131
+ //
132
+ } catch (e) {
133
+ console.log(e)
134
+ }
135
+ }
136
+
137
+
138
+
139
+ // // transfer_github_browser_version
140
+ module.exports.transfer_github_browser_version = (source_spec) => {
141
+
142
+ }
143
+
144
+ // // transfer_local_directory_browser_version
145
+ module.exports.transfer_local_directory_browser_version = (source_spec) => {
146
+
147
+ }