maishu-scripts 1.2.1 → 1.4.0

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 (36) hide show
  1. package/out/index.d.ts +9 -0
  2. package/out/index.js +12 -4
  3. package/out/modules/actions/babel-compile.d.ts +3 -0
  4. package/out/modules/actions/babel-compile.js +185 -0
  5. package/out/modules/actions/copy-file.js +18 -8
  6. package/out/modules/actions/less-compile.d.ts +8 -0
  7. package/out/modules/actions/less-compile.js +33 -0
  8. package/out/modules/actions/scss-compile.d.ts +9 -0
  9. package/out/modules/actions/scss-compile.js +65 -0
  10. package/out/modules/actions/ts-compile.d.ts +2 -7
  11. package/out/modules/actions/ts-compile.js +35 -13
  12. package/out/modules/compile.d.ts +16 -3
  13. package/out/modules/compile.js +103 -51
  14. package/out/modules/configs.d.ts +7 -0
  15. package/out/modules/configs.js +27 -0
  16. package/out/modules/import-path-rewrite.js +17 -7
  17. package/out/modules/project-compiler.d.ts +0 -22
  18. package/out/modules/project-compiler.js +17 -165
  19. package/out/types.d.ts +1 -0
  20. package/out/types.js +2 -0
  21. package/package.json +6 -2
  22. package/src/index.ts +7 -3
  23. package/src/modules/actions/{biel-compile.ts → babel-compile.ts} +39 -11
  24. package/src/modules/actions/copy-file.ts +1 -1
  25. package/src/modules/actions/less-compile.test.ts +34 -0
  26. package/src/modules/actions/less-compile.ts +35 -0
  27. package/src/modules/actions/scss-compile.test.ts +38 -0
  28. package/src/modules/actions/scss-compile.ts +35 -0
  29. package/src/modules/actions/ts-compile.test.ts +30 -2
  30. package/src/modules/actions/ts-compile.ts +22 -7
  31. package/src/modules/compile.test.ts +154 -14
  32. package/src/modules/compile.ts +112 -50
  33. package/src/modules/configs.test.ts +32 -0
  34. package/src/modules/configs.ts +29 -0
  35. package/src/modules/project-compiler.ts +1 -170
  36. /package/src/{types.d.ts → types.ts} +0 -0
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
37
  };
@@ -33,17 +43,68 @@ const fs = __importStar(require("fs"));
33
43
  const node_watch_1 = __importDefault(require("node-watch"));
34
44
  const path = __importStar(require("path"));
35
45
  const errors_1 = require("./errors");
36
- const biel_compile_1 = require("./actions/biel-compile");
46
+ // import { compileFile as tsCompileFile } from "./actions/ts-compile";
37
47
  const copy_file_1 = __importDefault(require("./actions/copy-file"));
38
- const skipFiles = ["\\S+\\.d\\.tsx?$", "\\S+\\.test\\.tsx?$", "\\S+\\.spec\\.tsx?$"];
39
- const extCopy = [".js", ".html", ".css", ".jpg", ".png", ".gif", ".scss", ".less"];
40
- let fileActions = {
41
- ".ts": biel_compile_1.compileFile,
42
- ".tsx": biel_compile_1.compileFile,
43
- };
44
- extCopy.forEach(ext => fileActions[ext] = copy_file_1.default);
45
- /** 将 sourceDir 目录下所有文件生成到 outDir */
46
- function generateCode(sourceDir, outDir, projectPath, callback) {
48
+ const configs_1 = __importDefault(require("./configs"));
49
+ const extCopy = [".js", ".html", ".css", ".jpg", ".png", ".gif", ".less"];
50
+ extCopy.forEach(ext => configs_1.default.fileActions[ext] = copy_file_1.default);
51
+ function generateCode(arg1, outDir, projectPath, callback) {
52
+ let options = {};
53
+ if (typeof arg1 == "object") {
54
+ options = arg1;
55
+ }
56
+ else {
57
+ options.sourceDir = arg1;
58
+ options.outDir = outDir;
59
+ options.projectPath = projectPath;
60
+ options.callback = callback;
61
+ }
62
+ if (!options.sourceDir)
63
+ throw errors_1.errors.argumentNull("sourceDir");
64
+ if (!options.outDir)
65
+ throw errors_1.errors.argumentNull("outDir");
66
+ if (!options.projectPath)
67
+ throw errors_1.errors.argumentNull("projectPath");
68
+ if (!fs.existsSync(options.sourceDir))
69
+ throw errors_1.errors.pathNotExists(options.sourceDir);
70
+ return generateCodeByOptions(options);
71
+ }
72
+ function watchDirectory(arg1, outDir, projectPath, callback) {
73
+ let options = {};
74
+ if (typeof arg1 == "object") {
75
+ options = arg1;
76
+ }
77
+ else {
78
+ options.sourceDir = arg1;
79
+ options.outDir = outDir;
80
+ options.projectPath = projectPath;
81
+ options.callback = callback;
82
+ }
83
+ if (!options.sourceDir)
84
+ throw errors_1.errors.argumentNull("sourceDir");
85
+ if (!options.outDir)
86
+ throw errors_1.errors.argumentNull("outDir");
87
+ if (!options.projectPath)
88
+ throw errors_1.errors.argumentNull("projectPath");
89
+ if (!fs.existsSync(options.sourceDir))
90
+ throw errors_1.errors.pathNotExists(options.sourceDir);
91
+ return watchDirectoryByOptions(options);
92
+ }
93
+ function isIgnoredFile(filePath) {
94
+ if (!filePath)
95
+ throw errors_1.errors.argumentNull("filePath");
96
+ const ext = extname(filePath);
97
+ const skipFiles = configs_1.default.skipFiles[ext] || [];
98
+ let isSkip = skipFiles.some(pattern => pattern.test(filePath));
99
+ return isSkip;
100
+ }
101
+ function extname(file) {
102
+ // let ext = /\.[a-zA-Z]+/.exec(file)?.[0] || '';
103
+ let ext = path.extname(file);
104
+ return ext;
105
+ }
106
+ function generateCodeByOptions(options) {
107
+ let { sourceDir, outDir, projectPath, skipFiles, fileAction: fileActions, callback } = options;
47
108
  if (!sourceDir)
48
109
  throw errors_1.errors.argumentNull("sourceDir");
49
110
  if (!outDir)
@@ -58,13 +119,18 @@ function generateCode(sourceDir, outDir, projectPath, callback) {
58
119
  if (!fs.statSync(filePath).isFile()) {
59
120
  continue;
60
121
  }
61
- let isSkip = isIgnoredFile(filePath); //skipFiles.some(pattern => new RegExp(pattern).test(filePath));
122
+ let ext = extname(file);
123
+ skipFiles = skipFiles || configs_1.default.skipFiles[ext] || [];
124
+ let isSkip = skipFiles.some(pattern => pattern.test(filePath));
62
125
  if (isSkip) {
63
126
  console.log(`Skip ${filePath}`);
64
127
  continue;
65
128
  }
66
- let ext = extname(file);
67
- let action = fileActions[ext];
129
+ fileActions = fileActions || function (fileName) {
130
+ const ext = path.extname(fileName);
131
+ return configs_1.default.fileActions[ext];
132
+ };
133
+ let action = fileActions(filePath);
68
134
  if (action) {
69
135
  action(filePath, outDir, projectPath);
70
136
  if (callback) {
@@ -77,25 +143,30 @@ function generateCode(sourceDir, outDir, projectPath, callback) {
77
143
  let fullPath = path.join(sourceDir, dir);
78
144
  let outDirPath = path.join(outDir, dir);
79
145
  if (fs.statSync(fullPath).isDirectory()) {
80
- generateCode(fullPath, outDirPath, projectPath);
146
+ generateCodeByOptions({ sourceDir: fullPath, outDir: outDirPath, projectPath: projectPath, skipFiles, fileAction: fileActions, callback });
81
147
  }
82
148
  }
83
149
  }
84
- /** 监听 sourceRoot 目录下所有文件变化,并生成到 outRoot */
85
- function watchDirectory(sourceRoot, outRoot, projectPath, callback) {
86
- if (!sourceRoot)
87
- throw errors_1.errors.argumentNull("sourceRoot");
88
- if (!outRoot)
89
- throw errors_1.errors.argumentNull("outRoot");
90
- if (!projectPath)
91
- throw errors_1.errors.argumentNull("projectPath");
92
- (0, node_watch_1.default)(sourceRoot, { recursive: true }, async (evt, name) => {
93
- let action = fileActions[extname(name)];
94
- let outPath = path.dirname(name).replace(sourceRoot, outRoot);
150
+ function watchDirectoryByOptions(options) {
151
+ let { sourceDir, outDir, projectPath, skipFiles, fileAction: fileActions, callback } = options;
152
+ (0, node_watch_1.default)(sourceDir, { recursive: true }, async (evt, name) => {
153
+ const filePath = name;
154
+ const ext = extname(filePath);
155
+ skipFiles = skipFiles || configs_1.default.skipFiles[ext] || [];
156
+ let isSkip = skipFiles.some(pattern => pattern.test(filePath)); //isIgnoredFile(filePath);//skipFiles.some(pattern => new RegExp(pattern).test(filePath));
157
+ if (isSkip) {
158
+ console.log(`Skip ${filePath}`);
159
+ return;
160
+ }
161
+ if (evt === "remove") {
162
+ return;
163
+ }
164
+ let action = fileActions?.(filePath) || configs_1.default.fileActions[ext];
95
165
  if (!action) {
96
166
  return;
97
167
  }
98
168
  try {
169
+ let outPath = path.dirname(name).replace(sourceDir, outDir);
99
170
  action(name, outPath, projectPath);
100
171
  if (callback)
101
172
  callback(name, outPath, projectPath);
@@ -105,22 +176,3 @@ function watchDirectory(sourceRoot, outRoot, projectPath, callback) {
105
176
  }
106
177
  });
107
178
  }
108
- // export function copyFile(filePath: string, outDir: string) {
109
- // if (!filePath) throw errors.argumentNull("filePath");
110
- // if (!outDir) throw errors.argumentNull("outDir");
111
- // let out = filePath.replace(path.dirname(filePath), outDir);
112
- // let outDirPath = path.resolve(out, "..");
113
- // fs.mkdirSync(outDirPath, { recursive: true });
114
- // fs.copyFileSync(filePath, out);
115
- // }
116
- function isIgnoredFile(filePath) {
117
- if (!filePath)
118
- throw errors_1.errors.argumentNull("filePath");
119
- let isSkip = skipFiles.some(pattern => new RegExp(pattern).test(filePath));
120
- return isSkip;
121
- }
122
- function extname(file) {
123
- // let ext = /\.[a-zA-Z]+/.exec(file)?.[0] || '';
124
- let ext = path.extname(file);
125
- return ext;
126
- }
@@ -0,0 +1,7 @@
1
+ import { FileAction } from "../types";
2
+ declare class Options {
3
+ fileActions: Record<string, FileAction>;
4
+ skipFiles: Record<string, RegExp[]>;
5
+ }
6
+ declare const _default: Options;
7
+ export default _default;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const scss_compile_1 = __importDefault(require("./actions/scss-compile"));
7
+ const less_compile_1 = __importDefault(require("./actions/less-compile"));
8
+ const babel_compile_1 = require("./actions/babel-compile");
9
+ // const skipFiles = ["\\S+\\.d\\.tsx?$", "\\S+\\.test\\.tsx?$", "\\S+\\.spec\\.tsx?$"]
10
+ const fileActions = {
11
+ ".ts": (0, babel_compile_1.create)(),
12
+ ".tsx": (0, babel_compile_1.create)(),
13
+ ".scss": scss_compile_1.default,
14
+ ".less": less_compile_1.default,
15
+ };
16
+ class Options {
17
+ fileActions = fileActions;
18
+ skipFiles = {
19
+ ".tsx": [/\.d\.tsx?$/, /\.test\.tsx?$/, /\.spec\.tsx?$/],
20
+ ".ts": [/\.d\.ts$/, /\.test\.ts$/, /\.spec\.ts$/],
21
+ ".scss": [],
22
+ ".less": [],
23
+ };
24
+ }
25
+ const NAME = "maishu-scripts-options";
26
+ global[NAME] = global[NAME] || new Options();
27
+ exports.default = global[NAME];
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  Object.defineProperty(exports, "__esModule", { value: true });
26
36
  exports.ImportPathRewrite = void 0;
27
37
  const path = __importStar(require("path"));
@@ -1,19 +1,7 @@
1
1
  import * as babel from "@babel/core";
2
2
  export declare class ProjectCompiler {
3
- private projectPath;
4
- private sourceDirectoryNanme;
5
- private outputDirectoryName;
6
- private fileActions;
7
3
  private skipFiles;
8
4
  static tsOutExt: string;
9
- constructor(projectPath: string, sourceDirectoryNanme: string, outputDirectoryName: string);
10
- /**
11
- * 编译特定文件,并生成到制定目录
12
- * @param sourcePath 源文件路径
13
- * @param outDir 输出目录
14
- * */
15
- compileFile(filePath: string, outDir: string): Promise<void>;
16
- private compileTypeScriptFileByBabel;
17
5
  static loadBabelConfig(configPath: string): babel.TransformOptions;
18
6
  static findBabelConfigPath(projectPath: string, directoryPath: string): string | null;
19
7
  static getBabelConfig(projectPath: string, directoryPath: string): {
@@ -21,14 +9,4 @@ export declare class ProjectCompiler {
21
9
  path: string;
22
10
  };
23
11
  static getDefaultBabelConfig(): babel.TransformOptions;
24
- /**
25
- * 获取源文件所对应生成文件的扩展名
26
- * @param file 源文件名
27
- * */
28
- private fileOutExt;
29
- private isIgnoredFile;
30
- generateCode(): void;
31
- generateCode(sourceDir: string, outDir: string): void;
32
- watchDirectory(): void;
33
- run(): void;
34
12
  }
@@ -15,111 +15,32 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
28
35
  Object.defineProperty(exports, "__esModule", { value: true });
29
36
  exports.ProjectCompiler = void 0;
30
- const nodemon_1 = __importDefault(require("nodemon"));
31
37
  const errors_1 = require("./errors");
32
38
  const fs = __importStar(require("fs"));
33
39
  const path = __importStar(require("path"));
34
- const babel = __importStar(require("@babel/core"));
35
- const import_path_rewrite_1 = require("./import-path-rewrite");
36
- const node_watch_1 = __importDefault(require("node-watch"));
37
40
  const tsOutExt = ".js";
38
41
  class ProjectCompiler {
39
- projectPath;
40
- sourceDirectoryNanme;
41
- outputDirectoryName;
42
- fileActions;
43
42
  skipFiles = ["\\S+\\.d\\.tsx?$", "\\S+\\.test\\.tsx?$", "\\S+\\.spec\\.tsx?$"];
44
43
  static tsOutExt = tsOutExt;
45
- constructor(projectPath, sourceDirectoryNanme, outputDirectoryName) {
46
- this.projectPath = projectPath;
47
- this.sourceDirectoryNanme = sourceDirectoryNanme;
48
- this.outputDirectoryName = outputDirectoryName;
49
- if (!projectPath)
50
- throw errors_1.errors.argumentNull("projectPath");
51
- if (!sourceDirectoryNanme)
52
- throw errors_1.errors.argumentNull("sourceDirectoryNanme");
53
- if (!outputDirectoryName)
54
- throw errors_1.errors.argumentNull("outputDirectoryName");
55
- if (!path.isAbsolute(projectPath))
56
- throw errors_1.errors.notAbsolutePath(projectPath);
57
- let sourcePath = path.join(projectPath, sourceDirectoryNanme);
58
- if (!fs.existsSync(sourcePath))
59
- throw errors_1.errors.pathNotExists(sourcePath);
60
- this.fileActions = {
61
- ".ts": this.compileFile.bind(this),
62
- ".tsx": this.compileFile.bind(this),
63
- };
64
- }
65
- /**
66
- * 编译特定文件,并生成到制定目录
67
- * @param sourcePath 源文件路径
68
- * @param outDir 输出目录
69
- * */
70
- async compileFile(filePath, outDir) {
71
- this.compileTypeScriptFileByBabel(filePath, outDir);
72
- }
73
- async compileTypeScriptFileByBabel(sourcePath, outDir) {
74
- if (!sourcePath)
75
- throw errors_1.errors.argumentNull("sourcePath");
76
- if (!outDir)
77
- throw errors_1.errors.argumentNull("outDir");
78
- if (!fs.existsSync(sourcePath))
79
- throw errors_1.errors.pathNotExists(sourcePath);
80
- let sourceDir = path.dirname(sourcePath);
81
- let babelConfig = ProjectCompiler.getBabelConfig(this.projectPath, sourceDir);
82
- let babelOptions = babelConfig.options;
83
- let babelPath = babelConfig.path;
84
- babelOptions.filename = sourcePath;
85
- babelOptions.code = false;
86
- babelOptions.ast = true;
87
- let fileResult = babel.transformFileSync(sourcePath, babelOptions);
88
- if (!fileResult)
89
- throw errors_1.errors.compileError(sourcePath);
90
- let ast = fileResult.ast;
91
- if (!ast)
92
- throw errors_1.errors.compileError(sourcePath);
93
- new import_path_rewrite_1.ImportPathRewrite(sourcePath, ast, tsOutExt);
94
- let r = babel.transformFromAstSync(ast, undefined, {
95
- filename: sourcePath, plugins: babelOptions.plugins,
96
- presets: babelOptions.presets, sourceMaps: true
97
- });
98
- if (!r || r.code == null)
99
- throw errors_1.errors.compileError(sourcePath);
100
- let ext = path.extname(sourcePath);
101
- let outExt = this.fileOutExt(sourcePath);
102
- let targetPath = path.join(outDir, path.basename(sourcePath).replace(ext, outExt));
103
- let outDirPath = path.resolve(targetPath, "..");
104
- if (r.map) {
105
- r.map.file = path.basename(targetPath);
106
- let sources = r.map.sources || [];
107
- let sourceDir = path.dirname(sourcePath);
108
- sources.forEach((s, i) => {
109
- sources[i] = path.relative(outDirPath, path.join(sourceDir, s));
110
- });
111
- r.map.sources = sources;
112
- let mapPath = targetPath + ".map";
113
- if (!fs.existsSync(outDirPath))
114
- fs.mkdirSync(outDirPath, { recursive: true });
115
- fs.writeFileSync(mapPath, JSON.stringify(r.map));
116
- r.code += `\n//babelPath:${babelPath}`;
117
- r.code += "\n//# sourceMappingURL=" + path.basename(sourcePath);
118
- }
119
- if (!fs.existsSync(outDirPath))
120
- fs.mkdirSync(outDirPath, { recursive: true });
121
- fs.writeFileSync(targetPath, r.code);
122
- }
123
44
  static loadBabelConfig(configPath) {
124
45
  if (!configPath)
125
46
  throw errors_1.errors.argumentNull("configPath");
@@ -182,74 +103,5 @@ class ProjectCompiler {
182
103
  let babelOptions = { plugins, presets };
183
104
  return babelOptions;
184
105
  }
185
- /**
186
- * 获取源文件所对应生成文件的扩展名
187
- * @param file 源文件名
188
- * */
189
- fileOutExt(file) {
190
- let ext = path.extname(file);
191
- if (ext === ".ts")
192
- return tsOutExt;
193
- if (ext === ".tsx")
194
- return tsOutExt;
195
- return ext;
196
- }
197
- isIgnoredFile(filePath) {
198
- if (!filePath)
199
- throw errors_1.errors.argumentNull("filePath");
200
- let isSkip = this.skipFiles.some(pattern => new RegExp(pattern).test(filePath));
201
- return isSkip;
202
- }
203
- generateCode(sourceDir, outDir) {
204
- // if (!sourceDir) throw errors.argumentNull("sourceDir");
205
- // if (!outDir) throw errors.argumentNull("outDir");
206
- if (!sourceDir)
207
- sourceDir = path.join(this.projectPath, this.sourceDirectoryNanme);
208
- if (!outDir)
209
- outDir = path.join(this.projectPath, this.outputDirectoryName);
210
- let fileActions = this.fileActions;
211
- if (!fs.existsSync(sourceDir))
212
- throw errors_1.errors.pathNotExists(sourceDir);
213
- let files = fs.readdirSync(sourceDir);
214
- for (let file of files) {
215
- let filePath = path.join(sourceDir, file);
216
- if (!fs.statSync(filePath).isFile()) {
217
- continue;
218
- }
219
- let isSkip = this.isIgnoredFile(filePath); //skipFiles.some(pattern => new RegExp(pattern).test(filePath));
220
- if (isSkip) {
221
- console.log(`Skip ${filePath}`);
222
- continue;
223
- }
224
- let ext = path.extname(file);
225
- let action = fileActions[ext];
226
- if (action) {
227
- action(filePath, outDir);
228
- }
229
- }
230
- let dirs = fs.readdirSync(sourceDir);
231
- for (let dir of dirs) {
232
- let fullPath = path.join(sourceDir, dir);
233
- let outDirPath = path.join(outDir, dir);
234
- if (fs.statSync(fullPath).isDirectory()) {
235
- this.generateCode(fullPath, outDirPath);
236
- }
237
- }
238
- }
239
- watchDirectory() {
240
- (0, node_watch_1.default)(this.sourceDirectoryNanme, { recursive: true }, async (evt, name) => {
241
- let action = this.fileActions[path.extname(name)];
242
- let outPath = path.dirname(name).replace(this.sourceDirectoryNanme, this.outputDirectoryName);
243
- if (action) {
244
- action(name, outPath);
245
- }
246
- });
247
- }
248
- run() {
249
- (0, nodemon_1.default)({
250
- script: `./${this.outputDirectoryName}/main.js`,
251
- watch: [`./${this.outputDirectoryName}/`],
252
- });
253
- }
254
106
  }
255
107
  exports.ProjectCompiler = ProjectCompiler;
package/out/types.d.ts ADDED
@@ -0,0 +1 @@
1
+ export type FileAction = (filePath: string, outPath: string, projectPath: string) => void;
package/out/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maishu-scripts",
3
- "version": "1.2.1",
3
+ "version": "1.4.0",
4
4
  "description": "用于对 node 项目进行代码生成,打包,运行",
5
5
  "dependencies": {
6
6
  "@babel/core": "^7.24.3",
@@ -8,17 +8,21 @@
8
8
  "@babel/plugin-transform-react-jsx": "^7.23.4",
9
9
  "@babel/plugin-transform-typescript": "^7.24.1",
10
10
  "@types/node": "^20.12.2",
11
+ "@types/react": "^19.2.8",
11
12
  "babel-plugin-transform-typescript-metadata": "^0.3.2",
13
+ "less": "^4.5.1",
12
14
  "maishu-toolkit": "^1.12.6",
13
15
  "node-watch": "^0.7.4",
14
16
  "nodemon": "^3.1.4",
15
- "typescript": "^5.4.5"
17
+ "sass": "^1.97.1",
18
+ "typescript": "^5.9.3"
16
19
  },
17
20
  "devDependencies": {
18
21
  "@babel/preset-env": "^7.24.3",
19
22
  "@babel/preset-typescript": "^7.24.1",
20
23
  "@types/babel__core": "^7.20.5",
21
24
  "@types/jest": "^29.5.12",
25
+ "@types/less": "^3.0.8",
22
26
  "jest": "^29.7.0"
23
27
  },
24
28
  "main": "./out/index.js",
package/src/index.ts CHANGED
@@ -1,5 +1,9 @@
1
+ import _config from "./modules/configs";
2
+ import copyFile from "./modules/actions/copy-file";
1
3
  export { generateCode, watchDirectory } from "./modules/compile";
2
4
  export { run } from "./modules/run";
3
- // let sourceDir = "src";
4
- // let outDir = "out";
5
- // generateCode(sourceDir, outDir);
5
+ export const options = _config;
6
+ export { create as createTsCompiler } from "./modules/actions/ts-compile";
7
+ export { create as createBielCompiler } from "./modules/actions/babel-compile";
8
+ export { copyFile };
9
+ export { FileAction } from "./types";
@@ -4,16 +4,42 @@ import * as fs from "fs";
4
4
  import * as path from "path";
5
5
  import { errors } from "../errors";
6
6
  import { ProjectCompiler } from "../project-compiler";
7
+ import { FileAction } from "../../types";
7
8
 
8
9
  const outExt = ProjectCompiler.tsOutExt;
9
10
 
11
+
12
+ export function create(babelOptions?: babel.TransformOptions): FileAction {
13
+ const action: FileAction = (sourcePath: string, outputPath: string, projectPath: string) => {
14
+ if (!babelOptions) {
15
+ let bablePath: string;
16
+ let sourceDir = path.dirname(sourcePath);
17
+ if (projectPath) {
18
+ let c = ProjectCompiler.getBabelConfig(projectPath, sourceDir);
19
+ bablePath = c.path;
20
+ babelOptions = c.options;
21
+ }
22
+ else {
23
+ babelOptions = ProjectCompiler.getDefaultBabelConfig();
24
+ bablePath = '';
25
+ }
26
+ babelOptions.filename = sourcePath;
27
+ babelOptions.code = false;
28
+ babelOptions.ast = true;
29
+ }
30
+
31
+ return compileFile(sourcePath, outputPath, projectPath, babelOptions);
32
+ }
33
+ return action;
34
+ }
35
+
10
36
  /**
11
37
  * 编译特定文件,并生成到制定目录
12
38
  * @param sourcePath 源文件路径
13
39
  * @param outputPath 输出目录
14
40
  * @param projectPath 项目路径
15
41
  * */
16
- export async function compileFile(sourcePath: string, outputPath: string, projectPath: string) {
42
+ async function compileFile(sourcePath: string, outputPath: string, projectPath: string, babelOptions: babel.TransformOptions) {
17
43
  if (!sourcePath) throw errors.argumentNull("sourcePath");
18
44
  if (!outputPath) throw errors.argumentNull("outputPath");
19
45
  if (!projectPath) throw errors.argumentNull("projectPath");
@@ -25,23 +51,23 @@ export async function compileFile(sourcePath: string, outputPath: string, projec
25
51
  }
26
52
 
27
53
  let sourceDir = path.dirname(sourcePath);
28
- let babelOptions: babel.TransformOptions;
54
+ // let babelOptions: babel.TransformOptions;
29
55
  let bablePath: string;
30
56
  //= projectPath ?
31
57
  // ProjectCompiler.getBabelConfig(projectPath, sourceDir) : ProjectCompiler.getDefaultBabelConfig();
32
58
  if (projectPath) {
33
- let c = ProjectCompiler.getBabelConfig(projectPath, sourceDir);
34
- bablePath = c.path;
35
- babelOptions = c.options;
59
+ // let c = ProjectCompiler.getBabelConfig(projectPath, sourceDir);
60
+ // bablePath = c.path;
61
+ // babelOptions = c.options;
36
62
  }
37
63
  else {
38
- babelOptions = ProjectCompiler.getDefaultBabelConfig();
39
- bablePath = '';
64
+ // babelOptions = ProjectCompiler.getDefaultBabelConfig();
65
+ // bablePath = '';
40
66
  }
41
67
 
42
- babelOptions.filename = sourcePath;
43
- babelOptions.code = false;
44
- babelOptions.ast = true;
68
+ // babelOptions.filename = sourcePath;
69
+ // babelOptions.code = false;
70
+ // babelOptions.ast = true;
45
71
 
46
72
 
47
73
  // let fileResult = babel.transformFileSync(sourcePath, {
@@ -78,7 +104,7 @@ export async function compileFile(sourcePath: string, outputPath: string, projec
78
104
  fs.mkdirSync(outDirPath, { recursive: true });
79
105
 
80
106
  fs.writeFileSync(mapPath, JSON.stringify(r.map));
81
- r.code += `\n//babel file path = ${bablePath}`;
107
+ // r.code += `\n//babel file path = ${bablePath}`;
82
108
  r.code += "\n//# sourceMappingURL=" + path.basename(mapPath);
83
109
  }
84
110
  else if (fs.existsSync(mapPath)) {
@@ -146,3 +172,5 @@ class ImportPathRewrite {
146
172
  }
147
173
 
148
174
  }
175
+
176
+
@@ -3,7 +3,7 @@ import { errors } from "../errors";
3
3
  import * as fs from "fs";
4
4
  import * as path from "path";
5
5
 
6
- let copyFile: FileAction = (filePath: string, outPath: string, projectPath: string) => {
6
+ let copyFile: FileAction = (filePath: string, outPath: string) => {
7
7
  if (!filePath) throw errors.argumentNull("filePath");
8
8
  if (!outPath) throw errors.argumentNull("outPath");
9
9