utiller 1.0.291 → 1.0.293

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.
@@ -91,19 +91,18 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
91
91
  /**
92
92
  * 從絕對路徑中取出 "src/" 之後的部分(包含前置 /)
93
93
  * @param {string} fullPath - 完整的絕對檔案路徑
94
- * @param {string} folder - 針對/folder/之後作為split起點
94
+ * @param {string} folder - 針對/folder/之後作為split起點(預設為 src)
95
95
  * @returns {string} - 以 / 開頭、從 src/ 之後開始的相對路徑
96
96
  */
97
97
  (0, _defineProperty2["default"])(_this, "getPathAfterSpecificFolder", function (fullPath) {
98
98
  var folder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "src";
99
- var parts = _lodash["default"].split(fullPath, _path5["default"].sep);
100
- var indexOfSrc = _lodash["default"].findLastIndex(parts, function (part) {
101
- return part === folder;
102
- });
103
- if (indexOfSrc === -1) return ""; // 找不到 src,回傳空字串
104
-
105
- var relativeParts = _lodash["default"].slice(parts, indexOfSrc + 1);
106
- return _lodash["default"].join(relativeParts, "/");
99
+ var parts = fullPath.split(path.sep);
100
+ for (var i = parts.length - 1; i >= 0; i--) {
101
+ if (parts[i] === folder) {
102
+ return "/" + parts.slice(i + 1).join("/");
103
+ }
104
+ }
105
+ return "";
107
106
  });
108
107
  /**
109
108
  * 從絕對路徑中取出 "src/" 之後的部分(包含前置 /)
@@ -120,11 +119,13 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
120
119
  key: "findSpecificFolderByPath",
121
120
  value: function findSpecificFolderByPath(path, folderName) {
122
121
  var absolute = _path5["default"].resolve(path);
123
- var splited = absolute.split("/");
124
- while (!_fs["default"].existsSync("".concat(splited.join("/"), "/").concat(folderName)) && splited.length > 0) {
125
- splited.pop();
122
+ var parts = absolute.split(_path5["default"].sep);
123
+ while (parts.length) {
124
+ var joined = _path5["default"].join.apply(_path5["default"], (0, _toConsumableArray2["default"])(parts).concat([folderName]));
125
+ if (_fs["default"].existsSync(joined)) return joined;
126
+ parts.pop();
126
127
  }
127
- return "".concat(splited.join("/"), "/.idea");
128
+ return null;
128
129
  }
129
130
 
130
131
  /** {numpages, numrender, info, text, version} */
@@ -593,10 +594,10 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
593
594
  }, {
594
595
  key: "getNamesOfFolderChild",
595
596
  value: function getNamesOfFolderChild(path) {
596
- return _lodash["default"].filter(this.getChildPathByPath(path), function (each) {
597
- return each.isDirectory;
598
- }).map(function (path) {
599
- return path.dirName;
597
+ return this.getChildPathByPath(path).filter(function (p) {
598
+ return p.isDirectory;
599
+ }).map(function (p) {
600
+ return p.dirName;
600
601
  });
601
602
  }
602
603
 
@@ -624,10 +625,21 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
624
625
  key: "copySingleFile",
625
626
  value: function copySingleFile(from, dest, fileName) {
626
627
  var force = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
627
- var destination = _lodash["default"].isString(fileName) && !_lodash["default"].isEmpty(fileName) ? _path5["default"].join(dest, fileName) : dest;
628
- if (_fs["default"].existsSync(destination) && !force) throw new _index2["default"](8006, destination);
628
+ var destination = fileName && fileName.trim() ? _path5["default"].join(dest, fileName) : dest;
629
+ if (_fs["default"].existsSync(destination) && !force) {
630
+ throw new _index2["default"](8006, destination);
631
+ }
629
632
  _fs["default"].copyFileSync(from, destination);
630
633
  }
634
+ }, {
635
+ key: "ensureFolderExists",
636
+ value: function ensureFolderExists(folderPath) {
637
+ var resolved = _path5["default"].resolve(folderPath);
638
+ if (_fs["default"].existsSync(resolved)) return;
639
+ _fs["default"].mkdirSync(resolved, {
640
+ recursive: true
641
+ });
642
+ }
631
643
  }, {
632
644
  key: "getNodeEnvVariable",
633
645
  value: function getNodeEnvVariable(key) {
@@ -650,7 +662,7 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
650
662
  }, {
651
663
  key: "isImageFile",
652
664
  value: function isImageFile(file) {
653
- return this.isOrEquals(file.extension, "svg", "png", "jpg", "jpeg");
665
+ return ["svg", "png", "jpg", "jpeg"].includes(file.extension);
654
666
  }
655
667
 
656
668
  /** 把內容都抹掉each 是 fileName, ex:index.js*/
@@ -985,23 +997,41 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
985
997
  _iterator7.f();
986
998
  }
987
999
  }
1000
+ }, {
1001
+ key: "isFileEmpty",
1002
+ value: function isFileEmpty(path) {
1003
+ var content = this.getFileContextInRaw(path);
1004
+ return !content || !content.trim();
1005
+ }
988
1006
 
989
1007
  /** 保守的複製檔案, 如果檔案比較舊, 或是檔案是空的, 就放棄copy行為 */
990
1008
  }, {
991
1009
  key: "copySingleFileConservative",
992
- value: function copySingleFileConservative(pathOfDestinationFolder, latestFile) {
993
- if (this.isEmptyFile(latestFile.absolute)) {
994
- this.appendInfo("".concat(latestFile.absolute, " is empty file, ignore copy behavior"));
1010
+ value: function copySingleFileConservative(destPath, latestFile) {
1011
+ var absolute = latestFile.absolute,
1012
+ lastModifiedTime = latestFile.lastModifiedTime;
1013
+
1014
+ // 先確認檔案是否為空
1015
+ if (!this.isPathExist(absolute) || this.isFileEmpty(absolute)) {
1016
+ this.appendInfo("".concat(absolute, " is empty file, ignore copy behavior"));
995
1017
  return;
996
1018
  }
997
- if (!_fs["default"].existsSync(pathOfDestinationFolder)) {
998
- this.appendInfo("".concat(pathOfDestinationFolder, " is not exist, easy to override"));
999
- } else if (_fs["default"].existsSync(pathOfDestinationFolder) && this.getFileLastModifiedTime(pathOfDestinationFolder) > latestFile.lastModifiedTime) {
1000
- this.appendInfo("".concat(pathOfDestinationFolder, " is the latest, ignore this run"));
1019
+ var destExists = _fs["default"].existsSync(destPath);
1020
+
1021
+ // 如果目的檔案存在且比來源更新,則跳過
1022
+ if (destExists && this.getFileLastModifiedTime(destPath) > lastModifiedTime) {
1023
+ this.appendInfo("".concat(destPath, " is the latest, ignore this run"));
1001
1024
  return;
1002
1025
  }
1003
- this.persistByPath(this.getFolderPathOfSpecificPath(pathOfDestinationFolder));
1004
- this.copySingleFile(latestFile.absolute, pathOfDestinationFolder, undefined, true);
1026
+ if (!destExists) {
1027
+ this.appendInfo("".concat(destPath, " does not exist, safe to copy."));
1028
+ }
1029
+
1030
+ // 確保目的路徑存在(遞迴建立)
1031
+ this.ensureFolderExists(_path5["default"].dirname(destPath));
1032
+
1033
+ // 強制複製
1034
+ this.copySingleFile(absolute, destPath, undefined, true);
1005
1035
  }
1006
1036
  }, {
1007
1037
  key: "syncDeleteFile",
@@ -1207,7 +1237,7 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1207
1237
  key: "updateVersionOfTemplate",
1208
1238
  value: (function () {
1209
1239
  var _updateVersionOfTemplate = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee14(dependency, newVersion) {
1210
- var paths, _i, _paths, path, succeedOfPersistFile, json;
1240
+ var paths, _i, _paths, _path4, succeedOfPersistFile, json;
1211
1241
  return _regenerator["default"].wrap(function _callee14$(_context15) {
1212
1242
  while (1) switch (_context15.prev = _context15.next) {
1213
1243
  case 0:
@@ -1218,13 +1248,13 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1218
1248
  _context15.next = 24;
1219
1249
  break;
1220
1250
  }
1221
- path = _paths[_i];
1222
- if (!this.isPathExist(path)) {
1251
+ _path4 = _paths[_i];
1252
+ if (!this.isPathExist(_path4)) {
1223
1253
  _context15.next = 21;
1224
1254
  break;
1225
1255
  }
1226
1256
  succeedOfPersistFile = false;
1227
- json = this.getJsonObjByFilePath(path);
1257
+ json = this.getJsonObjByFilePath(_path4);
1228
1258
  if (!(json && json.dependencies && json.dependencies[dependency])) {
1229
1259
  _context15.next = 18;
1230
1260
  break;
@@ -1232,7 +1262,7 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1232
1262
  json.dependencies[dependency] = "^".concat(newVersion);
1233
1263
  _context15.prev = 9;
1234
1264
  _context15.next = 12;
1235
- return this.writeJsonThanPrettier(path, json);
1265
+ return this.writeJsonThanPrettier(_path4, json);
1236
1266
  case 12:
1237
1267
  succeedOfPersistFile = true;
1238
1268
  _context15.next = 18;
@@ -1247,7 +1277,7 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1247
1277
  break;
1248
1278
  }
1249
1279
  _context15.next = 21;
1250
- return this.updateFileOfSpecificLine(path, function (line) {
1280
+ return this.updateFileOfSpecificLine(_path4, function (line) {
1251
1281
  return " \"".concat(dependency, "\":\"^").concat(newVersion, "\"").concat(_lodash["default"].endsWith(_lodash["default"].trim(line), "," ? "," : ""));
1252
1282
  }, function (each) {
1253
1283
  return _lodash["default"].startsWith(_lodash["default"].trim(each), "\"".concat(dependency, "\""));
@@ -1284,8 +1314,8 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1284
1314
  var contentOfUpdated,
1285
1315
  predicate,
1286
1316
  context,
1287
- split,
1288
- line,
1317
+ lines,
1318
+ index,
1289
1319
  _args16 = arguments;
1290
1320
  return _regenerator["default"].wrap(function _callee15$(_context16) {
1291
1321
  while (1) switch (_context16.prev = _context16.next) {
@@ -1297,13 +1327,18 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1297
1327
  return true;
1298
1328
  };
1299
1329
  context = this.getFileContextInRaw(pathOfFile);
1300
- split = context.split("\n");
1301
- line = _lodash["default"].find(split, function (each) {
1302
- return predicate(each);
1303
- });
1304
- this.replaceArrayByContentIndex(split, line, contentOfUpdated(line));
1305
- this.appendFile(pathOfFile, split.join("\n"), true, true);
1330
+ lines = context.split("\n");
1331
+ index = lines.findIndex(predicate);
1332
+ if (!(index === -1)) {
1333
+ _context16.next = 7;
1334
+ break;
1335
+ }
1336
+ return _context16.abrupt("return");
1306
1337
  case 7:
1338
+ // 無匹配行則略過
1339
+ lines[index] = contentOfUpdated(lines[index]);
1340
+ this.appendFile(pathOfFile, lines.join("\n"), true, true);
1341
+ case 9:
1307
1342
  case "end":
1308
1343
  return _context16.stop();
1309
1344
  }
@@ -1338,48 +1373,55 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1338
1373
  }, {
1339
1374
  key: "enrichEachPackageJson",
1340
1375
  value: (function () {
1341
- var _enrichEachPackageJson = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee17(path) {
1342
- var jsons, packages, _iterator9, _step9, _path4, json, script;
1376
+ var _enrichEachPackageJson = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee17(rootPath) {
1377
+ var validNames, jsonFiles, packageFiles, _iterator9, _step9, absolute, json;
1343
1378
  return _regenerator["default"].wrap(function _callee17$(_context18) {
1344
1379
  while (1) switch (_context18.prev = _context18.next) {
1345
1380
  case 0:
1346
- jsons = this.findFilePathByExtension(path, ["json"], "gen", "node_modules", "release");
1347
- packages = _lodash["default"].filter(jsons, function (each) {
1348
- return _lodash["default"].isEqual(each.fileName, "package") || _lodash["default"].isEqual(each.fileName, "admin.package") || _lodash["default"].isEqual(each.fileName, "web.package") || _lodash["default"].isEqual(each.fileName, "functions.package");
1381
+ validNames = new Set(["package", "admin.package", "web.package", "functions.package"]);
1382
+ jsonFiles = this.findFilePathByExtension(rootPath, ["json"], "gen", "node_modules", "release");
1383
+ packageFiles = jsonFiles.filter(function (file) {
1384
+ return validNames.has(file.fileName);
1349
1385
  });
1350
- _iterator9 = _createForOfIteratorHelper(packages);
1351
- _context18.prev = 3;
1352
- _iterator9.s();
1386
+ if (!(packageFiles.length === 0)) {
1387
+ _context18.next = 5;
1388
+ break;
1389
+ }
1390
+ return _context18.abrupt("return");
1353
1391
  case 5:
1392
+ _iterator9 = _createForOfIteratorHelper(packageFiles);
1393
+ _context18.prev = 6;
1394
+ _iterator9.s();
1395
+ case 8:
1354
1396
  if ((_step9 = _iterator9.n()).done) {
1355
- _context18.next = 14;
1397
+ _context18.next = 17;
1356
1398
  break;
1357
1399
  }
1358
- _path4 = _step9.value;
1359
- json = this.getJsonObjByFilePath(_path4.absolute);
1360
- script = json.scripts ? json.scripts : {};
1361
- script["updateConfigerer"] = "npm update configerer --save";
1362
- _context18.next = 12;
1363
- return this.writeJsonThanPrettier(_path4.absolute, json);
1364
- case 12:
1365
- _context18.next = 5;
1400
+ absolute = _step9.value.absolute;
1401
+ json = this.getJsonObjByFilePath(absolute);
1402
+ json.scripts || (json.scripts = {});
1403
+ json.scripts.updateConfigerer = "npm update configerer --save";
1404
+ _context18.next = 15;
1405
+ return this.writeJsonThanPrettier(absolute, json);
1406
+ case 15:
1407
+ _context18.next = 8;
1366
1408
  break;
1367
- case 14:
1368
- _context18.next = 19;
1409
+ case 17:
1410
+ _context18.next = 22;
1369
1411
  break;
1370
- case 16:
1371
- _context18.prev = 16;
1372
- _context18.t0 = _context18["catch"](3);
1373
- _iterator9.e(_context18.t0);
1374
1412
  case 19:
1375
1413
  _context18.prev = 19;
1376
- _iterator9.f();
1377
- return _context18.finish(19);
1414
+ _context18.t0 = _context18["catch"](6);
1415
+ _iterator9.e(_context18.t0);
1378
1416
  case 22:
1417
+ _context18.prev = 22;
1418
+ _iterator9.f();
1419
+ return _context18.finish(22);
1420
+ case 25:
1379
1421
  case "end":
1380
1422
  return _context18.stop();
1381
1423
  }
1382
- }, _callee17, this, [[3, 16, 19, 22]]);
1424
+ }, _callee17, this, [[6, 19, 22, 25]]);
1383
1425
  }));
1384
1426
  function enrichEachPackageJson(_x21) {
1385
1427
  return _enrichEachPackageJson.apply(this, arguments);
@@ -1456,8 +1498,7 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1456
1498
  * console.log(`File Data Last Modified: ${stats.mtime}`);
1457
1499
  * console.log(`File Status Last Modified: ${stats.ctime}`);
1458
1500
  */
1459
- var stats = _fs["default"].statSync(path);
1460
- return stats.mtimeMs;
1501
+ return _fs["default"].statSync(path).mtimeMs;
1461
1502
  }
1462
1503
  }, {
1463
1504
  key: "getJsonObjByFilePath",
@@ -1509,11 +1550,14 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1509
1550
  }, {
1510
1551
  key: "reWriteJsonAttribute",
1511
1552
  value: (function () {
1512
- var _reWriteJsonAttribute = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee20(path) {
1513
- var json,
1553
+ var _reWriteJsonAttribute = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee20(filePath) {
1554
+ var _this$getPathInfo,
1555
+ extension,
1514
1556
  _len9,
1515
1557
  attrs,
1516
1558
  _key9,
1559
+ invalidAttr,
1560
+ json,
1517
1561
  _i2,
1518
1562
  _attrs,
1519
1563
  attr,
@@ -1521,43 +1565,39 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1521
1565
  return _regenerator["default"].wrap(function _callee20$(_context21) {
1522
1566
  while (1) switch (_context21.prev = _context21.next) {
1523
1567
  case 0:
1524
- if (!_lodash["default"].isEqual("json", this.getPathInfo(path).extension)) {
1525
- _context21.next = 17;
1568
+ _this$getPathInfo = this.getPathInfo(filePath), extension = _this$getPathInfo.extension;
1569
+ if (!(extension !== "json")) {
1570
+ _context21.next = 3;
1526
1571
  break;
1527
1572
  }
1528
- json = this.getJsonObjByFilePath(path);
1573
+ throw new _index2["default"](9999, "reWriteJsonAttribute() => path is not package.json, which is ".concat(filePath));
1574
+ case 3:
1529
1575
  for (_len9 = _args21.length, attrs = new Array(_len9 > 1 ? _len9 - 1 : 0), _key9 = 1; _key9 < _len9; _key9++) {
1530
1576
  attrs[_key9 - 1] = _args21[_key9];
1531
1577
  }
1532
- _i2 = 0, _attrs = attrs;
1533
- case 4:
1534
- if (!(_i2 < _attrs.length)) {
1535
- _context21.next = 12;
1578
+ // 驗證所有 attr 必須是 object
1579
+ invalidAttr = attrs.find(function (attr) {
1580
+ return (0, _typeof2["default"])(attr) !== "object" || attr === null;
1581
+ });
1582
+ if (!invalidAttr) {
1583
+ _context21.next = 7;
1536
1584
  break;
1537
1585
  }
1538
- attr = _attrs[_i2];
1539
- if (_lodash["default"].isObject(attr)) {
1540
- _context21.next = 8;
1541
- break;
1586
+ throw new _index2["default"](9999, "84451515 attr is not object, which is 'type=".concat((0, _typeof2["default"])(invalidAttr), " => ").concat(invalidAttr, "'"));
1587
+ case 7:
1588
+ json = this.getJsonObjByFilePath(filePath);
1589
+ for (_i2 = 0, _attrs = attrs; _i2 < _attrs.length; _i2++) {
1590
+ attr = _attrs[_i2];
1591
+ json[this.getObjectKey(attr)] = this.getObjectValue(attr);
1542
1592
  }
1543
- throw new _index2["default"](9999, "84451515 attr is not object, which is 'type=".concat((0, _typeof2["default"])(attr), " => ").concat(attr, "'"));
1544
- case 8:
1545
- json[this.getObjectKey(attr)] = this.getObjectValue(attr);
1546
- case 9:
1547
- _i2++;
1548
- _context21.next = 4;
1549
- break;
1550
- case 12:
1551
- _context21.next = 14;
1552
- return this.writeJsonThanPrettier(path, json);
1553
- case 14:
1593
+ _context21.next = 11;
1594
+ return this.writeJsonThanPrettier(filePath, json);
1595
+ case 11:
1554
1596
  return _context21.abrupt("return", {
1555
1597
  version: json.version,
1556
1598
  moduleName: json.name
1557
1599
  });
1558
- case 17:
1559
- throw new _index2["default"](9999, "reWriteJsonAttribute() => path is not package.json, which is ".concat(path));
1560
- case 18:
1600
+ case 12:
1561
1601
  case "end":
1562
1602
  return _context21.stop();
1563
1603
  }
@@ -1760,78 +1800,85 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1760
1800
  return _getObjectFromPromptQ.apply(this, arguments);
1761
1801
  }
1762
1802
  return getObjectFromPromptQ;
1763
- }() /** 產出一個/temp,然後把/src 複製過去, 再把裡面每一個file的 if(DEBUG)給去除掉,再加上prettier */)
1803
+ }()
1804
+ /** 產出一個/temp,然後把/src 複製過去, 再把裡面每一個file的 if(DEBUG)給去除掉,再加上prettier */
1805
+ /** 找出if (configerer) 當作start */
1806
+ /** 找出 } 當作 end */
1807
+ /** 刪除掉 if(configerer.DEBUG) {...........} */
1808
+ )
1764
1809
  }, {
1765
1810
  key: "generateTempFolderWithCleanSrc",
1766
1811
  value: (function () {
1767
- var _generateTempFolderWithCleanSrc = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee24(path) {
1768
- var sourceFile, tempFolderPath, _iterator10, _step10, file, tempFilePath, stmts, indexOfStart, indexOfEnd;
1812
+ var _generateTempFolderWithCleanSrc = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee24(basePath) {
1813
+ var sourceFile, tempFolderPath, filePaths, _iterator10, _step10, tempFilePath, rawLines, trimmedLines, start, end, updatedContent;
1769
1814
  return _regenerator["default"].wrap(function _callee24$(_context26) {
1770
1815
  while (1) switch (_context26.prev = _context26.next) {
1771
1816
  case 0:
1772
- this.appendInfo("generateTempFolderWithCleanSrc", path);
1773
- sourceFile = _path5["default"].join(path, "src");
1774
- tempFolderPath = _path5["default"].join(path, "temp");
1775
- if (!_fs["default"].existsSync(sourceFile)) {
1776
- _context26.next = 32;
1817
+ this.appendInfo("generateTempFolderWithCleanSrc", basePath);
1818
+ sourceFile = _path5["default"].join(basePath, "src");
1819
+ tempFolderPath = _path5["default"].join(basePath, "temp");
1820
+ if (_fs["default"].existsSync(sourceFile)) {
1821
+ _context26.next = 5;
1777
1822
  break;
1778
1823
  }
1824
+ return _context26.abrupt("return", tempFolderPath);
1825
+ case 5:
1779
1826
  this.appendInfo("generateTempFolderWithCleanSrc", "source", sourceFile);
1780
1827
  this.persistByPath(tempFolderPath);
1781
- _context26.next = 8;
1828
+ _context26.next = 9;
1782
1829
  return this.copyFromFolderToDestFolder(sourceFile, tempFolderPath);
1783
- case 8:
1784
- _iterator10 = _createForOfIteratorHelper(this.findFilePathBy(tempFolderPath));
1785
- _context26.prev = 9;
1830
+ case 9:
1831
+ filePaths = this.findFilePathBy(tempFolderPath);
1832
+ _iterator10 = _createForOfIteratorHelper(filePaths);
1833
+ _context26.prev = 11;
1786
1834
  _iterator10.s();
1787
- case 11:
1835
+ case 13:
1788
1836
  if ((_step10 = _iterator10.n()).done) {
1789
- _context26.next = 24;
1837
+ _context26.next = 27;
1790
1838
  break;
1791
1839
  }
1792
- file = _step10.value;
1793
- tempFilePath = file.absolute;
1794
- stmts = this.getFileContextInRaw(tempFilePath).split("\n").map(function (line) {
1795
- return _lodash["default"].trim(line);
1796
- });
1797
- /** 找出if (configerer) 當作start */
1798
- indexOfStart = _lodash["default"].findIndex(stmts, function (stmt) {
1799
- return _lodash["default"].startsWith(stmt, "if (configerer.DEBUG_MODE)");
1840
+ tempFilePath = _step10.value.absolute;
1841
+ rawLines = this.getFileContextInRaw(tempFilePath).split("\n");
1842
+ trimmedLines = rawLines.map(function (line) {
1843
+ return line.trim();
1800
1844
  });
1801
- /** 找出 } 當作 end */
1802
- indexOfEnd = _lodash["default"].findLastIndex(stmts, function (stmt) {
1803
- return _lodash["default"].isEqual(stmt, "}");
1845
+ start = trimmedLines.findIndex(function (line) {
1846
+ return line.startsWith("if (configerer.DEBUG_MODE)");
1804
1847
  });
1805
- if (!(indexOfEnd > 0 && indexOfStart > 0 && indexOfEnd > indexOfStart)) {
1806
- _context26.next = 22;
1848
+ end = trimmedLines.lastIndexOf("}");
1849
+ if (!(start >= 0 && end > start)) {
1850
+ _context26.next = 25;
1807
1851
  break;
1808
1852
  }
1809
- /** 刪除掉 if(configerer.DEBUG) {...........} */
1810
- this.dropItemsByIndex(stmts, indexOfStart, indexOfEnd);
1811
- this.appendFile(tempFilePath, _lodash["default"].join(stmts, "\n"), true, true);
1812
- _context26.next = 22;
1813
- return this.executeCommandLine("cd ".concat(_path5["default"].resolve("".concat(this.getFileDirPath(tempFilePath))), " &&\nnpx prettier --write ").concat(tempFilePath));
1814
- case 22:
1815
- _context26.next = 11;
1853
+ // 移除 DEBUG 區塊
1854
+ rawLines.splice(start, end - start + 1);
1855
+ updatedContent = rawLines.join("\n");
1856
+ this.appendFile(tempFilePath, updatedContent, true, true);
1857
+
1858
+ // 美化代碼
1859
+ _context26.next = 25;
1860
+ return this.executeCommandLine("cd ".concat(_path5["default"].dirname(tempFilePath), " && npx prettier --write \"").concat(tempFilePath, "\""));
1861
+ case 25:
1862
+ _context26.next = 13;
1816
1863
  break;
1817
- case 24:
1818
- _context26.next = 29;
1864
+ case 27:
1865
+ _context26.next = 32;
1819
1866
  break;
1820
- case 26:
1821
- _context26.prev = 26;
1822
- _context26.t0 = _context26["catch"](9);
1823
- _iterator10.e(_context26.t0);
1824
1867
  case 29:
1825
1868
  _context26.prev = 29;
1826
- _iterator10.f();
1827
- return _context26.finish(29);
1869
+ _context26.t0 = _context26["catch"](11);
1870
+ _iterator10.e(_context26.t0);
1828
1871
  case 32:
1872
+ _context26.prev = 32;
1873
+ _iterator10.f();
1874
+ return _context26.finish(32);
1875
+ case 35:
1829
1876
  return _context26.abrupt("return", tempFolderPath);
1830
- case 33:
1877
+ case 36:
1831
1878
  case "end":
1832
1879
  return _context26.stop();
1833
1880
  }
1834
- }, _callee24, this, [[9, 26, 29, 32]]);
1881
+ }, _callee24, this, [[11, 29, 32, 35]]);
1835
1882
  }));
1836
1883
  function generateTempFolderWithCleanSrc(_x26) {
1837
1884
  return _generateTempFolderWithCleanSrc.apply(this, arguments);
@@ -1841,15 +1888,15 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1841
1888
  /**
1842
1889
  * from, destination
1843
1890
  *
1844
- * 讓file content清除後,在寫入資料, 避免destined file address改變*/
1891
+ * 讓file content清除後,在寫入資料, 避免to(destination) file address改變*/
1845
1892
  )
1846
1893
  }, {
1847
1894
  key: "rewriteFile2File",
1848
- value: function rewriteFile2File(from, destination) {
1895
+ value: function rewriteFile2File(from, to) {
1849
1896
  var content = this.getFileContextInRaw(from);
1850
- if (_lodash["default"].isEmpty(content)) throw new _index2["default"](9999, "".concat(from, " \u5167\u5BB9\u70BA\u7A7A\u503C, ***rewrite\u529F\u80FD\u6703\u907F\u514D\u7A7A\u503C\u8986\u84CB, \u9019\u662F\u4E00\u500B\u8A2D\u8A08"));
1851
- this.appendFile(destination, content, true, true);
1852
- this.appendInfo("rewrite from:".concat(from, " => dest:").concat(destination, " succeed"));
1897
+ if (!content.trim()) throw new _index2["default"](9999, "".concat(from, " \u70BA\u7A7A\uFF0C\u907F\u514D\u8986\u84CB"));
1898
+ this.appendFile(to, content, true, true);
1899
+ this.appendInfo("rewrite from:".concat(from, " => to:").concat(to, " \u6210\u529F"));
1853
1900
  }
1854
1901
 
1855
1902
  /** 取得file第一行statement */
@@ -1866,26 +1913,24 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1866
1913
  /** 因為code gen有很多要js file要執行 persistent, 沒有修改過{const edit = true}的index persist就不要理它 */
1867
1914
  }, {
1868
1915
  key: "isFileEditSucceed",
1869
- value: function isFileEditSucceed(path) {
1870
- if (!this.isPathExist(path)) {
1871
- return false;
1872
- }
1873
- var file = this.getPathInfo(path);
1874
- var context = this.getFileContextInRaw(path);
1875
- if (_lodash["default"].isEmpty(_lodash["default"].trim(context))) {
1916
+ value: function isFileEditSucceed(filePath) {
1917
+ if (!this.isPathExist(filePath)) return false;
1918
+ var file = this.getPathInfo(filePath);
1919
+ var context = this.getFileContextInRaw(filePath).trim();
1920
+ if (context === "") {
1876
1921
  this.appendInfo("74985465 path ".concat(file.path, " is empty file, file would not persist"));
1877
1922
  return false;
1878
1923
  }
1879
1924
  try {
1880
- var stringOfHeadLine = _lodash["default"].head(context.split("\n"));
1881
- var first = _lodash["default"].tail(stringOfHeadLine.split(" ")).join(" ");
1882
- var second = this.getNormalizedStringNotEndWith(first, ";");
1883
- var splits = second.split("=").map(function (each) {
1884
- return _lodash["default"].trim(each);
1885
- });
1886
- var result = "\"".concat(splits.shift(), "\" : ").concat(splits.pop());
1887
- var json = JSON.parse("{".concat(result, "}"));
1888
- if (_lodash["default"].isEqual(this.getObjectValue(json), true)) return true;
1925
+ var firstLine = context.split("\n")[0];
1926
+
1927
+ // 偵測形如:const edit = true;
1928
+ var match = firstLine.match(/const\s+edit\s*=\s*(true|false)\s*;?/);
1929
+ if (!match) return false;
1930
+ var editValue = match[1] === "true";
1931
+ if (editValue === true) {
1932
+ return true;
1933
+ }
1889
1934
  } catch (error) {
1890
1935
  this.appendError("66445411 ".concat(error.message));
1891
1936
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utiller",
3
- "version": "1.0.291",
3
+ "version": "1.0.293",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -11,7 +11,7 @@
11
11
  "license": "ISC",
12
12
  "dependencies": {
13
13
  "configerer": "^1.0.11",
14
- "utiller": "^1.0.290",
14
+ "utiller": "^1.0.292",
15
15
  "linepayer": "^1.0.4",
16
16
  "databazer": "^1.0.12",
17
17
  "lodash": "^4.17.20",