maishu-scripts 1.4.2 → 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 +2 -4
- package/out/index.js +4 -2
- package/out/modules/actions/babel-compile.d.ts +11 -1
- package/out/modules/actions/babel-compile.js +76 -55
- package/out/modules/actions/copy-file.js +12 -4
- package/out/modules/compile.js +2 -2
- package/out/modules/configs.d.ts +3 -3
- package/out/modules/configs.js +7 -4
- package/out/modules/errors.d.ts +1 -0
- package/out/modules/errors.js +8 -0
- package/out/package.json +33 -0
- package/package.json +1 -1
- package/src/index.ts +2 -1
- package/src/modules/actions/babel-compile.test.ts +436 -0
- package/src/modules/actions/babel-compile.ts +80 -64
- package/src/modules/actions/copy-file.ts +12 -4
- package/src/modules/compile.ts +7 -7
- package/src/modules/configs.ts +5 -6
- package/src/modules/errors.ts +8 -0
- package/src/modules/project-compiler.test.ts +245 -40
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
|
|
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.
|
|
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.
|
|
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
|
|
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,
|
|
45
|
+
function create(defaultBabelOptions, outExt) {
|
|
44
46
|
const action = (sourcePath, outputPath, projectPath) => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
let
|
|
55
|
-
|
|
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
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
fs.mkdirSync(
|
|
50
|
-
|
|
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;
|
package/out/modules/compile.js
CHANGED
|
@@ -105,8 +105,8 @@ function extname(file) {
|
|
|
105
105
|
}
|
|
106
106
|
function generateCodeByOptions(options) {
|
|
107
107
|
let { sourceDir, outDir, projectPath, skipFiles, fileAction: fileActions, callback, defaultAction } = options;
|
|
108
|
-
if (!defaultAction)
|
|
109
|
-
|
|
108
|
+
// if (!defaultAction)
|
|
109
|
+
// defaultAction = copyFile;
|
|
110
110
|
if (!sourceDir)
|
|
111
111
|
throw errors_1.errors.argumentNull("sourceDir");
|
|
112
112
|
if (!outDir)
|
package/out/modules/configs.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { FileAction } from "../types";
|
|
2
|
-
declare class
|
|
2
|
+
export declare class Config {
|
|
3
3
|
fileActions: Record<string, FileAction>;
|
|
4
4
|
skipFiles: Record<string, RegExp[]>;
|
|
5
5
|
}
|
|
6
|
-
declare const
|
|
7
|
-
export default
|
|
6
|
+
declare const config: Config;
|
|
7
|
+
export default config;
|
package/out/modules/configs.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
exports.default =
|
|
28
|
+
exports.Config = Config;
|
|
29
|
+
const config = new Config();
|
|
30
|
+
exports.default = config;
|
package/out/modules/errors.d.ts
CHANGED
|
@@ -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 {};
|
package/out/modules/errors.js
CHANGED
|
@@ -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();
|
package/out/package.json
ADDED
|
@@ -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
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
|
|
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";
|