jsir 1.0.7 → 1.1.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.
Files changed (6) hide show
  1. package/cmd/ooa.js +254 -191
  2. package/ethWeb.js +114 -54
  3. package/index.js +23 -11
  4. package/package.json +1 -1
  5. package/sol.js +2 -1
  6. package/util.js +159 -65
package/util.js CHANGED
@@ -17,7 +17,7 @@ const globalDirectories = require('global-dirs');
17
17
  let md5 = require('md5');
18
18
  const emptyFn = ()=>{}
19
19
  const dayJs = require('dayjs')
20
- let _globalLog = createLimitLogger2(`${setting.name}.log`)
20
+ let _globalLog = createLimitLogger2(`${setting.name}.log`, null, false)
21
21
  global.$log = str => {
22
22
  _globalLog(`[${timeStr()}] ${str}`)
23
23
  }
@@ -151,67 +151,101 @@ function appendLog(fileName, text) {
151
151
  fs.appendFile(logPath, text + '\n', emptyFn)
152
152
  }
153
153
 
154
- function createLimitLogger(fileName, maxChars) {
154
+ function createLimitLogger(fileName, maxChars, logInfo = true) {
155
155
  let logDir = getLibDataDir() + "/log"
156
156
  mkdir(logDir)
157
157
  let logPath = logDir + "/" + fileName
158
158
  fileCleaner(logPath, maxChars)
159
+
160
+ if (logInfo) {
161
+ info(`log: ${logPath}`)
162
+ }
163
+
159
164
  return (text) => {
160
165
  fs.appendFile(logPath, String(text) + '\n', emptyFn)
161
166
  }
162
167
  }
163
168
 
164
- function createLimitLogger2(fileName, maxChars) {
169
+ function createLimitLogger2(fileName, maxChars, logInfo = true) {
165
170
  let logDir = getLibDataDir() + "/log"
166
171
  mkdir(logDir)
167
172
  let logPath = logDir + "/" + fileName
168
173
  cleanFile(logPath, maxChars)
174
+
175
+ if (logInfo) {
176
+ info(`log: ${logPath}`)
177
+ }
178
+
179
+ let minNum = (Date.now()/(1000 * 60 * 10)).toFixed(0)
169
180
  return (text) => {
170
- if (Date.now()%(1000 * 60 * 10) === 0) {
181
+ let _minNum = (Date.now()/(1000 * 60 * 10)).toFixed(0)
182
+ if (_minNum !== minNum) {
183
+ minNum = _minNum
171
184
  cleanFile(logPath, maxChars)
172
185
  }
173
186
  fs.appendFile(logPath, String(text) + '\n', emptyFn)
174
187
  }
175
188
  }
176
189
 
177
- function objDataFile(fileName, fn, fmt) {
190
+ function textDataFile(fileName, fn) {
178
191
  let dataDir = getLibDataDir() + "/data"
179
192
  let path = dataDir + "/" + fileName
180
193
  if (!fs.existsSync(path)) {
181
194
  mkdir(dataDir)
182
- fs.writeFileSync(path, "{}")
195
+ fs.writeFileSync(path, "")
183
196
  }
184
197
 
185
- let obj = JSON.parse(String(fs.readFileSync(path)))
198
+ let text = String(fs.readFileSync(path))
186
199
  if (!fn) {
187
- return obj
200
+ return text
188
201
  }
189
- let val = fn(obj)
190
- if (val && typeof val === 'object') {
191
- obj = val
202
+ let val = fn(text)
203
+ if (vl(val) && val.constructor.name === 'Promise') {
204
+ return val.then(result => {
205
+ fileLock(fileName, () => fs.writeFileSync(path, result))
206
+ return result
207
+ })
208
+ } else {
209
+ fileLock(fileName, () => fs.writeFileSync(path, val))
210
+ return val
192
211
  }
193
- fileLock(fileName, () => fs.writeFileSync(path, JSON.stringify(obj, null, fmt ? 2:null)))
194
- return obj
195
212
  }
196
213
 
197
- async function objDataFileAsync(fileName, fn, fmt) {
214
+ function objDataFile(fileName, fn, fmt) {
215
+ return dataFile(fileName, fn, fmt, {})
216
+ }
217
+ function arrayDataFile(fileName, fn, fmt) {
218
+ return dataFile(fileName, fn, fmt, [])
219
+ }
220
+ function dataFile(fileName, fn, fmt, defaultObj = {}) {
198
221
  let dataDir = getLibDataDir() + "/data"
199
222
  let path = dataDir + "/" + fileName
200
223
  if (!fs.existsSync(path)) {
201
224
  mkdir(dataDir)
202
- fs.writeFileSync(path, "{}")
225
+ fs.writeFileSync(path, JSON.stringify(defaultObj, null, fmt ? 2:null))
203
226
  }
204
227
 
205
228
  let obj = JSON.parse(String(fs.readFileSync(path)))
206
229
  if (!fn) {
207
230
  return obj
208
231
  }
209
- let val = await fn(obj)
210
- if (val && typeof val === 'object') {
211
- obj = val
232
+ let val = fn(obj)
233
+ if (vl(val) && val.constructor.name === 'Promise') {
234
+ return new Promise(async resolve => {
235
+ let result = await val
236
+ if (result && typeof result === 'object') {
237
+ obj = result
238
+ }
239
+ fileLock(fileName, () => fs.writeFileSync(path, JSON.stringify(obj, null, fmt ? 2:null)))
240
+ resolve(obj)
241
+ })
242
+ } else {
243
+ if (val && typeof val === 'object') {
244
+ obj = val
245
+ }
246
+ fileLock(fileName, () => fs.writeFileSync(path, JSON.stringify(obj, null, fmt ? 2:null)))
247
+ return obj
212
248
  }
213
- fileLock(fileName, () => fs.writeFileSync(path, JSON.stringify(obj, null, fmt ? 2:null)))
214
- return obj
215
249
  }
216
250
 
217
251
  function requireG(module){
@@ -398,33 +432,24 @@ function fileLock(key, fn) {
398
432
  } catch (e) {
399
433
  return;
400
434
  }
401
- try {
402
- fn()
403
- } finally {
404
- rmDir(file)
405
- }
406
- }
407
435
 
408
- async function fileLockAsync(key, fn) {
409
- if (!(fn && key)) {
410
- return;
411
- }
412
- key = md5(trim(key))
413
- let lockDir = getLibDataDir() + "/lock"
414
- mkdir(lockDir)
415
- let file = lockDir + "/" + key
416
- if (fs.existsSync(file)) {
417
- return;
418
- }
436
+ let obj
419
437
  try {
420
- fs.mkdirSync(file)
438
+ obj = fn()
421
439
  } catch (e) {
422
- return;
440
+ rmDir(file)
423
441
  }
424
- try {
425
- await fn()
426
- } finally {
442
+ if (obj && obj.constructor.name === 'Promise') {
443
+ return obj.then(result => {
444
+ rmDir(file)
445
+ return result
446
+ }).catch(e => {
447
+ rmDir(file)
448
+ throw e
449
+ })
450
+ } else {
427
451
  rmDir(file)
452
+ return obj
428
453
  }
429
454
  }
430
455
 
@@ -565,33 +590,34 @@ function linuxAskAndKill(...keys){
565
590
  }
566
591
 
567
592
  function timeLoop(fn, timestamp, lastIds = []){
593
+ let val
568
594
  try {
569
- if (fn()) {
570
- return;
571
- }
595
+ val = fn()
572
596
  } catch (e) {
573
597
  console.error(e.toString().split("\n")[0])
574
598
  $log(e.stack)
575
599
  }
576
- if (lastIds[0] !== false) {
577
- lastIds[0] = setTimeout(() => timeLoop(fn, timestamp, lastIds), timestamp)
578
- }
579
- return lastIds
580
- }
600
+ if (vl(val) && val.constructor.name === 'Promise') {
601
+ return val.then(result => {
602
+ if (lastIds[0] !== false && result !== true) {
603
+ lastIds[0] = setTimeout(() => timeLoop(fn, timestamp, lastIds), timestamp)
604
+ }
605
+ return lastIds
606
+ }).catch(e => {
607
+ console.error(e.toString().split("\n")[0])
608
+ $log(e.stack)
581
609
 
582
- async function timeLoopAsync(fn, timestamp, lastIds = []){
583
- try {
584
- if (await fn()) {
585
- return;
610
+ if (lastIds[0] !== false) {
611
+ lastIds[0] = setTimeout(() => timeLoop(fn, timestamp, lastIds), timestamp)
612
+ }
613
+ return lastIds
614
+ })
615
+ } else {
616
+ if (lastIds[0] !== false && val !== true) {
617
+ lastIds[0] = setTimeout(() => timeLoop(fn, timestamp, lastIds), timestamp)
586
618
  }
587
- } catch (e) {
588
- console.error(e.toString().split("\n")[0])
589
- $log(e.stack)
590
- }
591
- if(lastIds[0] !== false) {
592
- lastIds[0] = setTimeout(() => timeLoopAsync(fn, timestamp, lastIds), timestamp)
619
+ return lastIds
593
620
  }
594
- return lastIds
595
621
  }
596
622
 
597
623
  function _linuxAskAndKill(rl, keys){
@@ -732,6 +758,68 @@ function getOrFn(obj, key, defaultValFn) {
732
758
  return obj[key]
733
759
  }
734
760
 
761
+ function strEq(a, b) {
762
+ if (a === b) {
763
+ return true
764
+ }
765
+ if (!(vl(b) && vl(b))) {
766
+ return false
767
+ }
768
+ return String(a).toLowerCase() === String(b).toLowerCase()
769
+ }
770
+
771
+ function wrapRows(rows) {
772
+ let newRows = []
773
+ for(let row of rows) {
774
+ let temp = []
775
+ for(let key of Object.keys(row)) {
776
+ let val = row[key]
777
+ let items = String(val).split('\n')
778
+ for(let i = 0; i<items.length; i++) {
779
+ let r = getOr(temp, i, {})
780
+ r[key] = items[i]
781
+ }
782
+ }
783
+ newRows.push(...temp)
784
+ }
785
+ return newRows
786
+ }
787
+
788
+ function info(msg) {
789
+ process.stdout.write('\x1B[32m')
790
+ process.stdout.write('[info] ')
791
+ process.stdout.write('\x1B[39m')
792
+ console.log(msg)
793
+ }
794
+
795
+ function warn(msg) {
796
+ process.stdout.write('\x1B[33m')
797
+ process.stdout.write('[warn] ')
798
+ process.stdout.write('\x1B[39m')
799
+ console.log(msg)
800
+ }
801
+
802
+ function error(e, msg) {
803
+ process.stdout.write('\x1B[35m')
804
+ process.stdout.write(`[${msg || 'error'}] `)
805
+ process.stdout.write('\x1B[39m')
806
+ console.log(e)
807
+ }
808
+
809
+ function parseSteps(str) {
810
+ let items = str.split(',').map(trim).filter(i => i)
811
+ let result = []
812
+ for (let item of items) {
813
+ let ss = item.split('-').map(trim).filter(i => i)
814
+ if (ss.length > 1) {
815
+ result.push(...range(Number(ss[0]), Number(ss[1])))
816
+ } else {
817
+ result.push(Number(ss[0]))
818
+ }
819
+ }
820
+ return result
821
+ }
822
+
735
823
  module.exports = {
736
824
  run,
737
825
  reget,
@@ -762,7 +850,6 @@ module.exports = {
762
850
  requireG,
763
851
  toBigNum,
764
852
  objDataFile,
765
- objDataFileAsync,
766
853
  appendLog,
767
854
  createLimitLogger,
768
855
  clearConsole,
@@ -770,7 +857,6 @@ module.exports = {
770
857
  globalLock,
771
858
  globalUnLock,
772
859
  rmDir,
773
- fileLockAsync,
774
860
  eFn,
775
861
  removeFirst,
776
862
  sleep,
@@ -786,7 +872,6 @@ module.exports = {
786
872
  timeStr,
787
873
  splitArray,
788
874
  BigNumber,
789
- timeLoopAsync,
790
875
  range,
791
876
  Range,
792
877
  getOr,
@@ -797,5 +882,14 @@ module.exports = {
797
882
  getOrFn,
798
883
  emptyFn,
799
884
  setConfig,
800
- axios
885
+ axios,
886
+ textDataFile,
887
+ arrayDataFile,
888
+ dataFile,
889
+ strEq,
890
+ wrapRows,
891
+ info,
892
+ warn,
893
+ error,
894
+ parseSteps
801
895
  }