maishu-scripts 1.4.1 → 1.4.5

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/out/index.d.ts CHANGED
@@ -1,11 +1,9 @@
1
1
  import copyFile from "./modules/actions/copy-file";
2
2
  export { generateCode, watchDirectory } from "./modules/compile";
3
3
  export { run } from "./modules/run";
4
- export declare const options: {
5
- fileActions: Record<string, import("./types").FileAction>;
6
- skipFiles: Record<string, RegExp[]>;
7
- };
4
+ export declare const config: import("./modules/configs").Config;
8
5
  export { create as createTsCompiler } from "./modules/actions/ts-compile";
9
6
  export { create as createBielCompiler } from "./modules/actions/babel-compile";
10
7
  export { copyFile };
11
8
  export { FileAction } from "./types";
9
+ export { Config } from "./modules/configs";
package/out/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.copyFile = exports.createBielCompiler = exports.createTsCompiler = exports.options = exports.run = exports.watchDirectory = exports.generateCode = void 0;
6
+ exports.Config = exports.copyFile = exports.createBielCompiler = exports.createTsCompiler = exports.config = exports.run = exports.watchDirectory = exports.generateCode = void 0;
7
7
  const configs_1 = __importDefault(require("./modules/configs"));
8
8
  const copy_file_1 = __importDefault(require("./modules/actions/copy-file"));
9
9
  exports.copyFile = copy_file_1.default;
@@ -12,8 +12,10 @@ Object.defineProperty(exports, "generateCode", { enumerable: true, get: function
12
12
  Object.defineProperty(exports, "watchDirectory", { enumerable: true, get: function () { return compile_1.watchDirectory; } });
13
13
  var run_1 = require("./modules/run");
14
14
  Object.defineProperty(exports, "run", { enumerable: true, get: function () { return run_1.run; } });
15
- exports.options = configs_1.default;
15
+ exports.config = configs_1.default;
16
16
  var ts_compile_1 = require("./modules/actions/ts-compile");
17
17
  Object.defineProperty(exports, "createTsCompiler", { enumerable: true, get: function () { return ts_compile_1.create; } });
18
18
  var babel_compile_1 = require("./modules/actions/babel-compile");
19
19
  Object.defineProperty(exports, "createBielCompiler", { enumerable: true, get: function () { return babel_compile_1.create; } });
20
+ var configs_2 = require("./modules/configs");
21
+ Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return configs_2.Config; } });
@@ -1,3 +1,13 @@
1
1
  import * as babel from "@babel/core";
2
2
  import { FileAction, JavaScriptExtension } from "../../types";
3
- export declare function create(defaultBabelOptions?: babel.TransformOptions, outExit?: JavaScriptExtension): FileAction;
3
+ export declare function create(defaultBabelOptions?: babel.TransformOptions & {
4
+ declaration?: boolean;
5
+ }, outExt?: JavaScriptExtension): FileAction;
6
+ /**
7
+ * 生成声明文件
8
+ * @param projectPath 项目路径
9
+ * @param sourcePath 源文件
10
+ * @param targetPath 目标文件
11
+ * @param outExt 输出扩展名
12
+ */
13
+ export declare function generateDeclarationFile(projectPath: string, sourcePath: string, targetPath: string, outExt: JavaScriptExtension): Promise<void>;
@@ -34,35 +34,45 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.create = create;
37
+ exports.generateDeclarationFile = generateDeclarationFile;
37
38
  const babel = __importStar(require("@babel/core"));
38
39
  const fs = __importStar(require("fs"));
39
40
  const path = __importStar(require("path"));
41
+ const ts = __importStar(require("typescript"));
40
42
  const errors_1 = require("../errors");
41
43
  const project_compiler_1 = require("../project-compiler");
42
44
  // const outExt = ProjectCompiler.tsOutExt;
43
- function create(defaultBabelOptions, outExit) {
45
+ function create(defaultBabelOptions, outExt) {
44
46
  const action = (sourcePath, outputPath, projectPath) => {
45
- outExit = outExit || ".js";
46
- if (defaultBabelOptions) {
47
- return compileFile(sourcePath, outputPath, projectPath, defaultBabelOptions, outExit);
47
+ outExt = outExt || ".js";
48
+ let babelOptions = defaultBabelOptions;
49
+ if (!babelOptions) {
50
+ // 如果没有 babelOptions,则尝试从项目目录查找 babel 配置文件,如果没有,则使用默认配置。
51
+ // let babelOptions: babel.TransformOptions;
52
+ let bablePath;
53
+ let sourceDir = path.dirname(sourcePath);
54
+ if (projectPath) {
55
+ let c = project_compiler_1.ProjectCompiler.getBabelConfig(projectPath, sourceDir);
56
+ bablePath = c.path;
57
+ babelOptions = c.options;
58
+ }
59
+ else {
60
+ babelOptions = project_compiler_1.ProjectCompiler.getDefaultBabelConfig();
61
+ bablePath = '';
62
+ }
63
+ babelOptions.filename = sourcePath;
64
+ babelOptions.code = false;
65
+ babelOptions.ast = true;
48
66
  }
49
- // 如果没有 babelOptions,则尝试从项目目录查找 babel 配置文件,如果没有,则使用默认配置。
50
- let babelOptions;
51
- let bablePath;
52
- let sourceDir = path.dirname(sourcePath);
53
- if (projectPath) {
54
- let c = project_compiler_1.ProjectCompiler.getBabelConfig(projectPath, sourceDir);
55
- bablePath = c.path;
56
- babelOptions = c.options;
67
+ const declaration = babelOptions.declaration || false;
68
+ delete babelOptions.declaration;
69
+ if (declaration) {
70
+ // 如果需要生成声明文件,则调用 ts-compile 模块的生成声明文件方法
71
+ const ext = path.extname(sourcePath);
72
+ let targetPath = path.join(outputPath, path.basename(sourcePath).replace(ext, outExt));
73
+ generateDeclarationFile(projectPath, sourcePath, targetPath, outExt);
57
74
  }
58
- else {
59
- babelOptions = project_compiler_1.ProjectCompiler.getDefaultBabelConfig();
60
- bablePath = '';
61
- }
62
- babelOptions.filename = sourcePath;
63
- babelOptions.code = false;
64
- babelOptions.ast = true;
65
- return compileFile(sourcePath, outputPath, projectPath, babelOptions, outExit);
75
+ return compileFile(sourcePath, outputPath, projectPath, babelOptions, outExt);
66
76
  };
67
77
  return action;
68
78
  }
@@ -72,39 +82,17 @@ function create(defaultBabelOptions, outExit) {
72
82
  * @param outputPath 输出目录
73
83
  * @param projectPath 项目路径
74
84
  * */
75
- async function compileFile(sourcePath, outputPath, projectPath, babelOptions, outExt) {
85
+ async function compileFile(sourcePath, outputPath, projectPath, babelOptions, outExt, generateDeclaration = false) {
76
86
  if (!sourcePath)
77
87
  throw errors_1.errors.argumentNull("sourcePath");
78
88
  if (!outputPath)
79
89
  throw errors_1.errors.argumentNull("outputPath");
80
90
  if (!projectPath)
81
91
  throw errors_1.errors.argumentNull("projectPath");
82
- // if (!fs.existsSync(sourcePath)) throw errors.pathNotExists(sourcePath);
83
92
  if (!fs.existsSync(sourcePath)) {
84
93
  console.warn(`Path not exists: ${sourcePath}`);
85
94
  return;
86
95
  }
87
- let sourceDir = path.dirname(sourcePath);
88
- // let babelOptions: babel.TransformOptions;
89
- let bablePath;
90
- //= projectPath ?
91
- // ProjectCompiler.getBabelConfig(projectPath, sourceDir) : ProjectCompiler.getDefaultBabelConfig();
92
- if (projectPath) {
93
- // let c = ProjectCompiler.getBabelConfig(projectPath, sourceDir);
94
- // bablePath = c.path;
95
- // babelOptions = c.options;
96
- }
97
- else {
98
- // babelOptions = ProjectCompiler.getDefaultBabelConfig();
99
- // bablePath = '';
100
- }
101
- // babelOptions.filename = sourcePath;
102
- // babelOptions.code = false;
103
- // babelOptions.ast = true;
104
- // let fileResult = babel.transformFileSync(sourcePath, {
105
- // filename: sourcePath, code: false, ast: true, plugins,
106
- // presets
107
- // });
108
96
  let fileResult = babel.transformFileSync(sourcePath, babelOptions);
109
97
  if (!fileResult)
110
98
  throw errors_1.errors.compileError(sourcePath);
@@ -138,24 +126,15 @@ async function compileFile(sourcePath, outputPath, projectPath, babelOptions, ou
138
126
  if (!fs.existsSync(outDirPath))
139
127
  fs.mkdirSync(outDirPath, { recursive: true });
140
128
  fs.writeFileSync(targetPath, r.code);
129
+ if (generateDeclaration) {
130
+ await generateDeclarationFile(projectPath, sourcePath, targetPath, outExt);
131
+ }
141
132
  }
142
133
  function extname(file) {
143
134
  // let ext = /\.[a-zA-Z]+/.exec(file)?.[0] || '';
144
135
  let ext = path.extname(file);
145
136
  return ext;
146
137
  }
147
- /**
148
- * 获取源文件所对应生成文件的扩展名
149
- * @param file 源文件名
150
- * */
151
- // function fileOutExt(file: string) {
152
- // let ext = extname(file);
153
- // if (ext === ".ts")
154
- // return outExt;
155
- // if (ext === ".tsx")
156
- // return outExt;
157
- // return ext;
158
- // }
159
138
  class ImportPathRewrite {
160
139
  filePath;
161
140
  outExt;
@@ -189,3 +168,45 @@ class ImportPathRewrite {
189
168
  }
190
169
  }
191
170
  }
171
+ /**
172
+ * 生成声明文件
173
+ * @param projectPath 项目路径
174
+ * @param sourcePath 源文件
175
+ * @param targetPath 目标文件
176
+ * @param outExt 输出扩展名
177
+ */
178
+ function generateDeclarationFile(projectPath, sourcePath, targetPath, outExt) {
179
+ if (!projectPath)
180
+ throw errors_1.errors.argumentNull("projectPath");
181
+ if (!sourcePath)
182
+ throw errors_1.errors.argumentNull("sourcePath");
183
+ if (!targetPath)
184
+ throw errors_1.errors.argumentNull("targetPath");
185
+ const sourceExt = path.extname(sourcePath);
186
+ if (!sourceExt || (sourceExt !== ".ts" && sourceExt !== ".tsx")) {
187
+ return Promise.resolve();
188
+ }
189
+ const declarationPath = targetPath.endsWith(outExt)
190
+ ? targetPath.slice(0, targetPath.length - outExt.length) + ".d.ts"
191
+ : targetPath.replace(path.extname(targetPath), ".d.ts");
192
+ const content = fs.readFileSync(sourcePath, "utf-8");
193
+ const result = ts.transpileDeclaration(content, {
194
+ fileName: path.basename(sourcePath),
195
+ reportDiagnostics: false,
196
+ compilerOptions: {
197
+ declaration: true,
198
+ emitDeclarationOnly: true,
199
+ skipLibCheck: true,
200
+ esModuleInterop: true,
201
+ jsx: ts.JsxEmit.React,
202
+ target: ts.ScriptTarget.ESNext,
203
+ module: ts.ModuleKind.NodeNext,
204
+ }
205
+ });
206
+ const outDir = path.dirname(declarationPath);
207
+ if (!fs.existsSync(outDir)) {
208
+ fs.mkdirSync(outDir, { recursive: true });
209
+ }
210
+ fs.writeFileSync(declarationPath, result.outputText || "");
211
+ return Promise.resolve();
212
+ }
@@ -42,11 +42,19 @@ let copyFile = (filePath, outPath) => {
42
42
  if (!outPath)
43
43
  throw errors_1.errors.argumentNull("outPath");
44
44
  if (!fs.existsSync(filePath)) {
45
+ throw errors_1.errors.pathNotExists(filePath);
46
+ }
47
+ if (!fs.statSync(filePath).isFile()) {
45
48
  return;
46
49
  }
47
- let out = filePath.replace(path.dirname(filePath), outPath);
48
- let outDirPath = path.resolve(out, "..");
49
- fs.mkdirSync(outDirPath, { recursive: true });
50
- fs.copyFileSync(filePath, out);
50
+ const destFilePath = path.join(outPath, path.basename(filePath));
51
+ const destDirPath = path.dirname(destFilePath);
52
+ fs.mkdirSync(destDirPath, { recursive: true });
53
+ try {
54
+ fs.copyFileSync(filePath, destFilePath);
55
+ }
56
+ catch (err) {
57
+ throw errors_1.errors.copyFileError(filePath, destFilePath, err);
58
+ }
51
59
  };
52
60
  exports.default = copyFile;
@@ -14,5 +14,6 @@ type Options = {
14
14
  skipFiles?: RegExp[];
15
15
  fileAction?: (fileName: string) => FileAction | undefined;
16
16
  callback?: Callback;
17
+ defaultAction?: FileAction;
17
18
  };
18
19
  export {};
@@ -104,7 +104,9 @@ function extname(file) {
104
104
  return ext;
105
105
  }
106
106
  function generateCodeByOptions(options) {
107
- let { sourceDir, outDir, projectPath, skipFiles, fileAction: fileActions, callback } = options;
107
+ let { sourceDir, outDir, projectPath, skipFiles, fileAction: fileActions, callback, defaultAction } = options;
108
+ // if (!defaultAction)
109
+ // defaultAction = copyFile;
108
110
  if (!sourceDir)
109
111
  throw errors_1.errors.argumentNull("sourceDir");
110
112
  if (!outDir)
@@ -126,7 +128,7 @@ function generateCodeByOptions(options) {
126
128
  console.log(`Skip ${filePath}`);
127
129
  continue;
128
130
  }
129
- let action = fileActions?.(filePath) || configs_1.default.fileActions[ext];
131
+ let action = fileActions?.(filePath) || configs_1.default.fileActions[ext] || defaultAction;
130
132
  if (action) {
131
133
  action(filePath, outDir, projectPath);
132
134
  if (callback) {
@@ -139,12 +141,14 @@ function generateCodeByOptions(options) {
139
141
  let fullPath = path.join(sourceDir, dir);
140
142
  let outDirPath = path.join(outDir, dir);
141
143
  if (fs.statSync(fullPath).isDirectory()) {
142
- generateCodeByOptions({ sourceDir: fullPath, outDir: outDirPath, projectPath: projectPath, skipFiles, fileAction: fileActions, callback });
144
+ generateCodeByOptions({ sourceDir: fullPath, outDir: outDirPath, projectPath: projectPath, skipFiles, fileAction: fileActions, callback, defaultAction });
143
145
  }
144
146
  }
145
147
  }
146
148
  function watchDirectoryByOptions(options) {
147
- let { sourceDir, outDir, projectPath, skipFiles, fileAction: fileActions, callback } = options;
149
+ let { sourceDir, outDir, projectPath, skipFiles, fileAction: fileActions, callback, defaultAction } = options;
150
+ if (!defaultAction)
151
+ defaultAction = copy_file_1.default;
148
152
  (0, node_watch_1.default)(sourceDir, { recursive: true }, async (evt, name) => {
149
153
  const filePath = name.replace(/\\/g, "/");
150
154
  sourceDir = sourceDir.replace(/\\/g, "/");
@@ -159,7 +163,7 @@ function watchDirectoryByOptions(options) {
159
163
  if (evt === "remove") {
160
164
  return;
161
165
  }
162
- let action = fileActions?.(filePath) || configs_1.default.fileActions[ext];
166
+ let action = fileActions?.(filePath) || configs_1.default.fileActions[ext] || defaultAction;
163
167
  if (!action) {
164
168
  return;
165
169
  }
@@ -1,7 +1,7 @@
1
1
  import { FileAction } from "../types";
2
- declare class Options {
2
+ export declare class Config {
3
3
  fileActions: Record<string, FileAction>;
4
4
  skipFiles: Record<string, RegExp[]>;
5
5
  }
6
- declare const _default: Options;
7
- export default _default;
6
+ declare const config: Config;
7
+ export default config;
@@ -3,17 +3,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Config = void 0;
6
7
  const scss_compile_1 = __importDefault(require("./actions/scss-compile"));
7
8
  const less_compile_1 = __importDefault(require("./actions/less-compile"));
8
9
  const babel_compile_1 = require("./actions/babel-compile");
10
+ const copy_file_1 = __importDefault(require("./actions/copy-file"));
9
11
  // const skipFiles = ["\\S+\\.d\\.tsx?$", "\\S+\\.test\\.tsx?$", "\\S+\\.spec\\.tsx?$"]
10
12
  const fileActions = {
11
13
  ".ts": (0, babel_compile_1.create)(),
12
14
  ".tsx": (0, babel_compile_1.create)(),
13
15
  ".scss": scss_compile_1.default,
14
16
  ".less": less_compile_1.default,
17
+ ".map": copy_file_1.default,
15
18
  };
16
- class Options {
19
+ class Config {
17
20
  fileActions = fileActions;
18
21
  skipFiles = {
19
22
  ".tsx": [/\.d\.tsx?$/, /\.test\.tsx?$/, /\.spec\.tsx?$/],
@@ -22,6 +25,6 @@ class Options {
22
25
  ".less": [],
23
26
  };
24
27
  }
25
- const NAME = "maishu-scripts-options";
26
- global[NAME] = global[NAME] || new Options();
27
- exports.default = global[NAME];
28
+ exports.Config = Config;
29
+ const config = new Config();
30
+ exports.default = config;
@@ -4,6 +4,7 @@ declare class Errors extends BaseErrors {
4
4
  pathNotExists(path: string): Error;
5
5
  notAbsolutePath(path: string): Error;
6
6
  invalidBabelConfigExtension(extension: string, configPath: string): Error;
7
+ copyFileError(sourcePath: string, destPath: string, innerError?: Error): Error;
7
8
  }
8
9
  export declare const errors: Errors;
9
10
  export {};
@@ -23,5 +23,13 @@ class Errors extends errors_1.Errors {
23
23
  error.name = Errors.prototype.invalidBabelConfigExtension.name;
24
24
  return error;
25
25
  }
26
+ copyFileError(sourcePath, destPath, innerError) {
27
+ let error = new Error(`Failed to copy file from "${sourcePath}" to "${destPath}"`);
28
+ error.name = Errors.prototype.copyFileError.name;
29
+ if (innerError) {
30
+ error.cause = innerError;
31
+ }
32
+ return error;
33
+ }
26
34
  }
27
35
  exports.errors = new Errors();
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "maishu-scripts",
3
+ "version": "1.4.4",
4
+ "description": "用于对 node 项目进行代码生成,打包,运行",
5
+ "dependencies": {
6
+ "@babel/core": "^7.24.3",
7
+ "@babel/plugin-proposal-decorators": "^7.29.0",
8
+ "@babel/plugin-transform-react-jsx": "^7.23.4",
9
+ "@babel/plugin-transform-typescript": "^7.24.1",
10
+ "@types/node": "^20.12.2",
11
+ "@types/react": "^19.2.8",
12
+ "babel-plugin-transform-typescript-metadata": "^0.3.2",
13
+ "less": "^4.5.1",
14
+ "maishu-toolkit": "^1.12.6",
15
+ "node-watch": "^0.7.4",
16
+ "nodemon": "^3.1.4",
17
+ "sass": "^1.97.1",
18
+ "typescript": "^5.9.3"
19
+ },
20
+ "devDependencies": {
21
+ "@babel/preset-env": "^7.24.3",
22
+ "@babel/preset-typescript": "^7.24.1",
23
+ "@types/babel__core": "^7.20.5",
24
+ "@types/jest": "^29.5.12",
25
+ "@types/less": "^3.0.8",
26
+ "jest": "^29.7.0"
27
+ },
28
+ "main": "./index.js",
29
+ "scripts": {
30
+ "build": "node ./scripts/build.js",
31
+ "test": "jest"
32
+ }
33
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maishu-scripts",
3
- "version": "1.4.1",
3
+ "version": "1.4.5",
4
4
  "description": "用于对 node 项目进行代码生成,打包,运行",
5
5
  "dependencies": {
6
6
  "@babel/core": "^7.24.3",
@@ -31,6 +31,7 @@
31
31
  "src"
32
32
  ],
33
33
  "scripts": {
34
- "build": "tsc"
34
+ "build": "tsc -p tsconfig.build.json",
35
+ "test": "jest"
35
36
  }
36
- }
37
+ }
package/src/index.ts CHANGED
@@ -2,8 +2,9 @@ import _config from "./modules/configs";
2
2
  import copyFile from "./modules/actions/copy-file";
3
3
  export { generateCode, watchDirectory } from "./modules/compile";
4
4
  export { run } from "./modules/run";
5
- export const options = _config;
5
+ export const config = _config;
6
6
  export { create as createTsCompiler } from "./modules/actions/ts-compile";
7
7
  export { create as createBielCompiler } from "./modules/actions/babel-compile";
8
8
  export { copyFile };
9
9
  export { FileAction } from "./types";
10
+ export { Config } from "./modules/configs";