-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
455 lines (376 loc) · 11.7 KB
/
Copy pathcommon.js
File metadata and controls
455 lines (376 loc) · 11.7 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
const fs = require("fs")
const path = require('path')
const _ = require('lodash')
const assert = require('assert')
const { red, blue, green } = require('./color')
const { alloc } = require('./mem')
PATH = {
homeUser: process.env.HOME
}
class PathParser {
unix(ipath) {
// print('unix path',ipath);
var paths = ipath
var flag = false;
if(ipath.constructor.name !== 'Array'){
paths = [ipath]
flag = true
}
paths = paths.map(path => {
if (path.slice(0,1) === '~' ){
path = path.slice(1);
path = PATH.homeUser + path
return path
}
return path
})
return flag ? paths[0] : paths
}
windows(ipath) {
return ipath
}
constructor() {
this.pathParser = (ipath) => {
var system
if(ipath.constructor.name === 'Array' || ipath.constructor.name === 'String'){
system = {
unix: ipath,
windows: ipath,
macos: ipath
}
} else {
system = {
unix: ipath.unix,
windows: ipath.windows,
macos: ipath.macos
}
}
if(process.platform.match(/linux/ig) || process.platform.match(/unix/ig)){
return this.unix(system.unix)
}
if(process.platform.match(/win/ig)){
return this.windows(system.windows)
}
}
}
}
// todo: di inject ( or transfer it to global variable)
const mPathParser = new PathParser();
class CrossPath {
constructor(obj) {
this.unix = obj.unix
this.windows = obj.windows
this.macos = obj.macos
}
}
function strip(str) {
var match = /^[\s]*(.*)\b[\s]*/.exec(str)
// var match = /^[\s]*(.*)[\s]*/.exec(str)
// var match = /^[\s]*(.*)\b[\s]*$/.exec(str) // why can't find str= `if m = 'k'`
// \b 查找是从后向前查找?
if (match === null) {
return ''
}
else {
return match[1]
}
}
function split(str, spliter=/\s/) {
const result = []
let t=''
for (i in str) {
if( !str[i].match(spliter) ){
t += str[i]
} else {
if ( i !== '0' && (!str[i-1].match(spliter)) ){
result.push(t)
t = ''
}
}
}
if(t){result.push(t)}
return result
}
// 不是'\n'的位置但是可能是开头
function findUntilPreLinePos(somePosOfCur, content){
var /**character */ char = content[somePosOfCur]
var i = somePosOfCur
while(i > 0){
i--;
char = content[i]
if( char === '\n'){
i++;
break;
}
}
return i;
}
// 可能是结尾
function findUntilLastLinePos(somePosOfCur, content, includeNL=true){
const length = content.length - 1
var char = content[somePosOfCur]
var i = somePosOfCur
while(i < length-1){
i++;
char = content[i]
if( char === '\n'){
if(!includeNL){
i--;
}
break;
}
}
return i;
}
function findUntilPreLine(somePosOfCur, content) {
var /**character */ char = content[somePosOfCur]
var i = somePosOfCur
const out = []
while(i > 0){
i--;
char = content[i]
if( char === '\n'){
i++;
break;
}
out.push(char)
}
const r = alloc(out.length)
for( var i in out){
const pos = out.length - 1 - i
r.write(out[pos], pos)
}
return r.toString()
}
function findUntilLastLine(somePosOfCur, content, includeNL=true) {
const length = content.length
var char = content[somePosOfCur]
var i = somePosOfCur
const out = []
while(i < length-1){
i++;
char = content[i]
if( char === '\n'){
if(!includeNL){
i--;
} else {
out.push(char)
}
break;
}
out.push(char)
}
const r = alloc(out.length)
for( var i in out){
r.write(out[i], i)
}
return r.toString()
}
function search(str, reg, options) {
// 如何检测是否是/.*/g类型呢?
var count = 0;
while( mattached = reg.exec(str)) {
const start = findUntilPreLinePos(mattached.index, str)
const end = findUntilLastLinePos(mattached.index, str, false)
const text = str.slice(start, mattached.index) + green(mattached[0]) +
str.slice(mattached.index + mattached[0].length, end)
if(count++ === 0)
console.log( blue(options.filename))
console.log( text , mattached[1], mattached.index)
}
}
function replace( str, reg, options) {
var count = 0;
str = str.replace(reg, (...args) => {
if(count++ === 0)
console.log( blue(options.filename) )
return options.callback(str, ...args)
})
return str
}
const pathOps = (function () {
return {
nomalized: function (...lpath) {
return path.join(...lpath, '/').slice(0,-1)
}
}
}())
class FileOps {
fparseReg(regOptions) {
assert(regOptions.reg, `can't find regOptions.reg`)
var reg = regOptions.reg
const regFlag = regOptions.flag || 'g'
reg = RegExp(reg, regFlag)
return reg
}
freplace (lpath, regOptions, callback, options = {}) {
lpath = pathParser(lpath)
if(options.include) options.include = pathParser(options.include)
if(options.exclude) options.exclude = pathParser(options.exclude)
var reg = this.fparseReg(regOptions)
console.log(reg)
options.print = false
const allfile = this.findFile(lpath, options)
allfile.forEach( el => {
const f = fs.readFileSync(el)
var str = f.toString()
str = replace(str, reg, {callback, filename: el})
fs.writeFileSync(el, str)
})
}
fsearch (lpath, regOptions, options) {
lpath = pathParser(lpath)
if(options.include) options.include = pathParser(options.include)
if(options.exclude) options.exclude = pathParser(options.exclude)
var reg = this.fparseReg(regOptions)
console.log(reg)
options.print = false
const allfile = this.findFile(lpath,options)
allfile.forEach( el => {
const f = fs.readFileSync(el)
var str = f.toString()
search(str, reg, {filename: el})
})
}
findFile (lpath='.', options={}) {
lpath = pathParser(lpath)
if(options.include) options.include = pathParser(options.include)
if(options.exclude) options.exclude = pathParser(options.exclude)
if(options.print === undefined)
options.print = true
const finded = []
function exeute(lpath, options) {
lpath = pathOps.nomalized(lpath)
if(fs.lstatSync(lpath).isDirectory()){
var fd = fs.readdirSync(lpath)
fd = collecOps.addPrefix(fd, lpath, pathOps.nomalized )
fd = arrayFilter(fd, options);
// finded = _.concat(finded, fd)
// console.log()
fd.forEach(el => {
// el = path.join(lpath, el)
exeute(el, options)
})
} else {
if(fs.existsSync(lpath)){
const reg = options.reg || [/.*/]
var newMatch = false
reg.forEach(el => {
// if(! ( el1.match(el) || el1.match(el.next) || ...) ){
// match = false
// }
newMatch = newMatch || lpath.match(el)
});
if(newMatch){
if(options.print)
console.log("matched", lpath)
finded.push(lpath)
}
}
}
}
exeute(lpath, options)
return finded
}
}
const fileOps = new FileOps()
function zip(collec, callback) {
if(collec && collec.length && collec.length > 0) {
for(var i in collec[0]) {
const r0 = collec.map( x => x[i])
callback(...r0)
}
}
}
const collecOps = {
addPrefix: (array, prefix, parser) => {
if(parser){
return array.map( x => parser(prefix, x))
}
return array.map( x => prefix + x)
},
addSuffix: (array, suffix, parser) => {
if(parser){
return array.map( x => parser(suffix, x))
}
return array.map( x => x+suffix)
}
}
const addPrefix = collecOps.addPrefix
const addSuffix = collecOps.addSuffix
function arrayFilter(original, options){
const include = options.include || [/.*/] // 空字符也匹配(即匹配到0次)
const exclude = options.exclude || []
newfd = []
original.forEach( el1 => {
var match = false
include.forEach( el2 => {
if(el1.match(el2)){
match = true
}
})
exclude.forEach( el2 => {
// console.log(el1)
if(el1.match(el2)){
match = false
}
})
if(match){
newfd.push(el1)
}
})
return newfd
}
function test(){
console.log(strip(' w3w w2c '))
console.log(['spliter test', split(' 123 fff ggg')])
console.log(['spliter test', split('123 fff ggg ')])
console.log(['spliter test', split(' 123 fff ggg ')])
// viewChild("hello")
fs.copyFileSync('common.js', 'common_copy.js')
fileOps.fsearch("common_copy.js", {reg: `viewChild\\("(.*)"\\)`, flag: "g"} )
fileOps.freplace('common_copy.js', {reg: `viewChild\\("(.*)"\\)`, flag: "g"}, (str,...m) => {
const match = m[0], submatch = m[1], matchPos = m[2];
return match.slice(0, match.length-1) + ', {static: false}' + match.slice(-1)
})
}
const pathParser = mPathParser.pathParser
module.exports = {
PATH,
CrossPath,
FileOps,
collecOps,
fileOps,
strip,
split,
addPrefix,
addSuffix,
test,
pathParser,
}
// fileOps.fsearch("/home/ka/Documents/tmp1/new-employer/employer-portal-ui", {reg: `ViewChild\\([",'](.*)[",']\\)`, flag: "g"}, { exclude: [
// "/home/ka/Documents/tmp1/new-employer/employer-portal-ui/.git",
// "/home/ka/Documents/tmp1/new-employer/employer-portal-ui/node_modules",
// ]})
// fileOps.freplace("/home/ka/Documents/tmp1/new-employer/employer-portal-ui", {reg: `ViewChild\\([",'](.*)[",']\\)`, flag: "g"}, (str, ...m) => {
// const match = m[0], submatch = m[1], matchPos = m[2];
// return match.slice(0, match.length-1) + ', {static: false}' + match.slice(-1)
// }, { exclude: [
// "/home/ka/Documents/tmp1/new-employer/employer-portal-ui/.git",
// "/home/ka/Documents/tmp1/new-employer/employer-portal-ui/node_modules",
// ]})
// Array(
// "/home/ka/Documents/tmp1/new-employer/employer-portal-ui",
// "/home/ka/Documents/tmp1/compare/employer-portal-ui"
// ).forEach(basepath => {
// console.log(["basepath----",basepath])
// fileOps.findFile(basepath, {
// exclude: [
// ".*/node_modules/.*"
// ],
// reg: [".*\\.png", /Navbar\.Component\.html/ig]
// })
// })
/*
notes: RegExp(".*\\.png") 写两次'\'的原因是,第一次处理是通用字符处理,这是语言决定的,会存储为".*\.png", 然后RegExp才负责处理。
*/