js-dev-tool 1.0.6 → 1.0.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-dev-tool",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "bin": {
5
5
  "jstool": "tools.js"
6
6
  },
@@ -0,0 +1,22 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import { fileURLToPath } from "url";
4
+
5
+ const __filename = fileURLToPath(import.meta.url);
6
+ const __dirname = path.dirname(__filename);
7
+ const commitMsgPath = path.resolve(__dirname, "..", ".codex", "commit_msg.txt");
8
+
9
+ if (!fs.existsSync(commitMsgPath)) {
10
+ process.exit(0);
11
+ }
12
+
13
+ let msg = fs.readFileSync(commitMsgPath, "utf8");
14
+
15
+ msg = msg.replace(/\r\n/g, "\n");
16
+ msg = msg
17
+ .split("\n")
18
+ .map(line => line.replace(/\s+$/u, ""))
19
+ .join("\n");
20
+ msg = msg.replace(/\n+$/u, "\n");
21
+
22
+ fs.writeFileSync(commitMsgPath, msg, "utf8");
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "1.0.6"
2
+ "version": "1.0.8"
3
3
  }
package/tool-lib/cjbm.js CHANGED
@@ -11,6 +11,8 @@
11
11
  /**
12
12
  * @file (C)onvert (J)S to (B)rowser (M)odule
13
13
  */
14
+ const fs = require("fs");
15
+ const path = require("path");
14
16
  /**
15
17
  * @import * as Regex from "../regex.d.ts";
16
18
  */
@@ -66,6 +68,33 @@ function getReplacer(ext) {
66
68
  };
67
69
  return replacer;
68
70
  }
71
+ /**
72
+ * @param {string} ext
73
+ */
74
+ const emitProcessCallback = (ext) => {
75
+ /**
76
+ * @param {string[]} sourceFiles
77
+ */
78
+ return (sourceFiles) => {
79
+ /** @type {number} */
80
+ let sourceFilesLen;
81
+ if (sourceFiles && (sourceFilesLen = sourceFiles.length)) {
82
+ for (let i = 0; i < sourceFilesLen;) {
83
+ const sourceFile = sourceFiles[i++];
84
+ const parsed = path.parse(sourceFile);
85
+ if (parsed.ext !== `.${ext}`) {
86
+ const afterPath = `${parsed.dir}/${parsed.name}.${ext}`;
87
+ fs.rename(sourceFile, afterPath, err => {
88
+ if (err) console.error(err);
89
+ else {
90
+ console.log(`renamed: ${sourceFile} => ${afterPath}`);
91
+ }
92
+ });
93
+ }
94
+ }
95
+ }
96
+ };
97
+ };
69
98
  /**
70
99
  * @returns {TJSToolEntry}
71
100
  */
@@ -78,31 +107,14 @@ module.exports = () => {
78
107
  fn() {
79
108
  const bases = getBasePaths();
80
109
  const ext = params.ext || "js";
81
- /** ### regex summary
82
- * ```perl
83
- * (?:import|export) # comments
84
- * \s*
85
- * (?:
86
- * "(?=[.\/]+)| # character ["] \x22 e.g - import "../global/index"
87
- * (?:
88
- * [\w_]+\s+| # default import, e.g - import fs from "./file";
89
- * (?:[\w_]+\s*,\s*)?\{[^}]+\}\s*| # e.g - import View, { TitiledSphereMesh } from "./view";
90
- * \*\s*as\s+[\w_]+\s+| # e.g - export * as OO from "./rateLimiter";
91
- * \*\s* # e.g - export * from "./rateLimiter";
92
- * )from\s*"(?:[.\/]+) # Positive Lookahead (./|../|../../) ...
93
- * )
94
- * (?:[^"]*)
95
- * (?<!\.js) # Negative Lookbehind not(/\.(?:c|m)?js/)
96
- * (?="\s*;?)
97
- * ```
98
- * @see regex details see {@link https://regex101.com/r/EpuQLT/32 regex101.com}
99
- */
110
+ /* const sourceFiles = await */
100
111
  processSources(
101
112
  /** @type {string} */ (this.taskName), (data) => {
102
113
  reImportExportDetection.lastIndex = 0;
103
114
  return data.replace(reImportExportDetection, getReplacer(ext));
104
115
  }, {
105
116
  bases,
117
+ cb: emitProcessCallback(ext)
106
118
  },
107
119
  );
108
120
  },
package/tool-lib/ps.js CHANGED
@@ -50,7 +50,7 @@ module.exports = {
50
50
  * @param {(source: string) => Promise<string> | string} process
51
51
  * @param {TProcessSourcesOpt} [opt]
52
52
  */
53
- function processSources(taskName, process, opt = {}) {
53
+ async function processSources(taskName, process, opt = {}) {
54
54
  const {
55
55
  root = params.root,
56
56
  base = "./build",
@@ -58,6 +58,7 @@ module.exports = {
58
58
  test = /\.js$/,
59
59
  suffix = "",
60
60
  targets,
61
+ cb
61
62
  } = opt;
62
63
  /**
63
64
  * 主に javascript 系 file を push する.
@@ -89,41 +90,49 @@ module.exports = {
89
90
  DEBUG && console.log("processSources::processSources:", sourceFiles);
90
91
  let count = sourceFiles.length;
91
92
  let written = 0;
93
+ /** @type {() => void} */
94
+ let psResolve;
92
95
  /** @type {(fileName?: string) => void} */
93
96
  const done = (fileName) => {
94
97
  --count === 0 && (
95
- console.timeEnd(taskName), utils.log(`[${taskName}] file written: ${written}`)
98
+ console.timeEnd(taskName), utils.log(`[${taskName}] file written: ${written}`),
99
+ psResolve()
96
100
  );
97
101
  if (verbose && fileName) {
98
102
  utils.log(`[${taskName}] done: ${fileName}`);
99
103
  }
100
104
  };
101
- count && console.time(taskName);
102
- for (const sourceFile of sourceFiles) {
103
- fs.stat(sourceFile, (statErr, stat) => {
104
- if (statErr) {
105
- return handleError(statErr, () => done(sourceFile));
106
- }
107
- if (stat.isFile()) {
108
- fs.readFile(sourceFile, "utf8", async (readErr, code) => {
109
- if (readErr) {
110
- return handleError(readErr, () => done(sourceFile));
111
- }
112
- const result = await runProcess(code, process);
113
- const outputPath = sourceFile.replace(/(?=\.js$)/, suffix);
114
- if (result !== code) {
115
- fs.writeFile(outputPath, result, () => {
116
- written++, done(outputPath);
117
- });
118
- } else {
119
- done(outputPath);
120
- }
121
- });
122
- } else {
123
- done(sourceFile);
124
- }
125
- });
126
- }
105
+ await /** @type {Promise<void>} */(new Promise(resolve => {
106
+ psResolve = resolve;
107
+ count && console.time(taskName);
108
+ for (const sourceFile of sourceFiles) {
109
+ fs.stat(sourceFile, (statErr, stat) => {
110
+ if (statErr) {
111
+ return handleError(statErr, () => done(sourceFile));
112
+ }
113
+ if (stat.isFile()) {
114
+ fs.readFile(sourceFile, "utf8", async (readErr, code) => {
115
+ if (readErr) {
116
+ return handleError(readErr, () => done(sourceFile));
117
+ }
118
+ const result = await runProcess(code, process);
119
+ const outputPath = sourceFile.replace(/(?=\.js$)/, suffix);
120
+ if (result !== code) {
121
+ fs.writeFile(outputPath, result, () => {
122
+ written++, done(outputPath);
123
+ });
124
+ } else {
125
+ done(outputPath);
126
+ }
127
+ });
128
+ } else {
129
+ done(sourceFile);
130
+ }
131
+ });
132
+ }
133
+ }));
134
+ cb && cb(sourceFiles);
135
+ return sourceFiles;
127
136
  }
128
137
  globalThis.processSources = processSources;
129
138
  },
@@ -25,7 +25,7 @@ declare global {
25
25
  taskName: string,
26
26
  process: (source: string) => Promise<string> | string,
27
27
  options?: TProcessSourcesOpt
28
- ): void;
28
+ ): Promise<string[]>;
29
29
  /** regex replace sig */
30
30
  declare type TStringReplacer = (matchs: string, ...args: any[]) => string;
31
31
  /** use `(R)ecord(W)ebpack(S)ize` */
@@ -138,6 +138,7 @@ declare global {
138
138
  test?: RegExp;
139
139
  targets?: string[];
140
140
  suffix?: string;
141
+ cb?: (sourceFiles: string[]) => void
141
142
  }
142
143
  declare type TJSToolEntry = {
143
144
  fn: (mode?: string) => void;
package/tools.js CHANGED
@@ -49,6 +49,18 @@ const terserOptions = {
49
49
  quote_style: 3,
50
50
  },
51
51
  };
52
+ function getVersionRegex() {
53
+ const RE_SOURCE = String.raw`
54
+ "?version("\s*:\s*)?
55
+ \s"?
56
+ (\d+)\.(\d+)\.(\d+) # version section
57
+ ( # tag section
58
+ (?: -[A-Za-z0-9\-]+(?:\.[A-Za-z0-9\-]+)*)?
59
+ (?:\+[A-Za-z0-9\-]+(?:\.[A-Za-z0-9\-]+)*)?
60
+ )?"?
61
+ `;
62
+ return new RegExp(RE_SOURCE.replace(/\(\?\#[\s\S]*?(?<!\\)\)|(?<![\\])(?:\#\s*$|\#[\s\S]*?$)|\s+/gm, ""), "g");
63
+ }
52
64
  /**
53
65
  * @type {Record<string, TJSToolEntry>}
54
66
  */
@@ -96,7 +108,7 @@ const ToolFunctions = {
96
108
  let nextVersion;
97
109
  !isArray(pkgJsons) && (pkgJsons = [pkgJsons]);
98
110
  utils.fireReplace(
99
- /"?version("\s*:\s*)?\s"?(\d+)\.(\d+)\.(\d+)(-\w+)?"?/,
111
+ getVersionRegex(),
100
112
  /** @type {TStringReplacer} */($0, isJson, $major, $minor, $patch, tag) => {
101
113
  /** @type {string | number} */
102
114
  let _major = $major;
@@ -115,8 +127,8 @@ const ToolFunctions = {
115
127
  } else {
116
128
  _patch = +_patch + 1;
117
129
  }
118
- currentVersion = `${$major}.${$minor}.${$patch}${tag ? tag : ""}`;
119
- nextVersion = `${_major}.${_minor}.${_patch}${tag ? tag : ""}`;
130
+ currentVersion = `${$major}.${$minor}.${$patch}${tag || ""}`;
131
+ nextVersion = `${_major}.${_minor}.${_patch}${tag || ""}`;
120
132
  return isJson ? `"version": "${nextVersion}"` : `version ${nextVersion}`;
121
133
  },
122
134
  pkgJsons,
@@ -127,7 +139,9 @@ const ToolFunctions = {
127
139
  ? [params.extras]
128
140
  : [];
129
141
  if (paths.length) {
130
- utils.fireReplace(/(v)?(\d+\.\d+\.\d+)(?:-\w+)?/g, /** @type {TStringReplacer} */($0, $prefix, $versionStr) => {
142
+ utils.fireReplace(
143
+ /(v)?(\d+\.\d+\.\d+(?:[^\s"]+)?)/g,
144
+ /** @type {TStringReplacer} */($0, $prefix, $versionStr) => {
131
145
  if ($versionStr === currentVersion && $prefix) {
132
146
  return "v" + nextVersion;
133
147
  }