maishu-scripts 1.4.0 → 1.4.1

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.
@@ -1,3 +1,3 @@
1
1
  import * as babel from "@babel/core";
2
- import { FileAction } from "../../types";
3
- export declare function create(babelOptions?: babel.TransformOptions): FileAction;
2
+ import { FileAction, JavaScriptExtension } from "../../types";
3
+ export declare function create(defaultBabelOptions?: babel.TransformOptions, outExit?: JavaScriptExtension): FileAction;
@@ -39,26 +39,30 @@ const fs = __importStar(require("fs"));
39
39
  const path = __importStar(require("path"));
40
40
  const errors_1 = require("../errors");
41
41
  const project_compiler_1 = require("../project-compiler");
42
- const outExt = project_compiler_1.ProjectCompiler.tsOutExt;
43
- function create(babelOptions) {
42
+ // const outExt = ProjectCompiler.tsOutExt;
43
+ function create(defaultBabelOptions, outExit) {
44
44
  const action = (sourcePath, outputPath, projectPath) => {
45
- if (!babelOptions) {
46
- let bablePath;
47
- let sourceDir = path.dirname(sourcePath);
48
- if (projectPath) {
49
- let c = project_compiler_1.ProjectCompiler.getBabelConfig(projectPath, sourceDir);
50
- bablePath = c.path;
51
- babelOptions = c.options;
52
- }
53
- else {
54
- babelOptions = project_compiler_1.ProjectCompiler.getDefaultBabelConfig();
55
- bablePath = '';
56
- }
57
- babelOptions.filename = sourcePath;
58
- babelOptions.code = false;
59
- babelOptions.ast = true;
45
+ outExit = outExit || ".js";
46
+ if (defaultBabelOptions) {
47
+ return compileFile(sourcePath, outputPath, projectPath, defaultBabelOptions, outExit);
60
48
  }
61
- return compileFile(sourcePath, outputPath, projectPath, babelOptions);
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;
57
+ }
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);
62
66
  };
63
67
  return action;
64
68
  }
@@ -68,7 +72,7 @@ function create(babelOptions) {
68
72
  * @param outputPath 输出目录
69
73
  * @param projectPath 项目路径
70
74
  * */
71
- async function compileFile(sourcePath, outputPath, projectPath, babelOptions) {
75
+ async function compileFile(sourcePath, outputPath, projectPath, babelOptions, outExt) {
72
76
  if (!sourcePath)
73
77
  throw errors_1.errors.argumentNull("sourcePath");
74
78
  if (!outputPath)
@@ -107,7 +111,7 @@ async function compileFile(sourcePath, outputPath, projectPath, babelOptions) {
107
111
  let ast = fileResult.ast;
108
112
  if (!ast)
109
113
  throw errors_1.errors.compileError(sourcePath);
110
- new ImportPathRewrite(sourcePath, ast);
114
+ new ImportPathRewrite(sourcePath, ast, outExt);
111
115
  let r = babel.transformFromAstSync(ast, undefined, {
112
116
  filename: sourcePath, plugins: babelOptions.plugins,
113
117
  presets: babelOptions.presets, sourceMaps: babelOptions.sourceMaps
@@ -115,7 +119,7 @@ async function compileFile(sourcePath, outputPath, projectPath, babelOptions) {
115
119
  if (!r || r.code == null)
116
120
  throw errors_1.errors.compileError(sourcePath);
117
121
  let ext = extname(sourcePath);
118
- let outExt = fileOutExt(sourcePath);
122
+ //let outExt = fileOutExt(sourcePath);
119
123
  let targetPath = path.join(outputPath, path.basename(sourcePath).replace(ext, outExt)); //sourcePath.replace(new RegExp(ext + "$"), outExt).replace(path.dirname(sourcePath), outDir);
120
124
  let outDirPath = path.resolve(targetPath, "..");
121
125
  let mapPath = targetPath + ".map";
@@ -144,18 +148,20 @@ function extname(file) {
144
148
  * 获取源文件所对应生成文件的扩展名
145
149
  * @param file 源文件名
146
150
  * */
147
- function fileOutExt(file) {
148
- let ext = extname(file);
149
- if (ext === ".ts")
150
- return outExt;
151
- if (ext === ".tsx")
152
- return outExt;
153
- return ext;
154
- }
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
+ // }
155
159
  class ImportPathRewrite {
156
160
  filePath;
157
- constructor(filePath, node) {
161
+ outExt;
162
+ constructor(filePath, node, outExt) {
158
163
  this.filePath = filePath;
164
+ this.outExt = outExt;
159
165
  this.traverse(node);
160
166
  }
161
167
  traverse(node) {
@@ -169,14 +175,14 @@ class ImportPathRewrite {
169
175
  case "ImportDeclaration":
170
176
  if (node.source.type === "StringLiteral" && node.source.value.startsWith(".")) {
171
177
  let ext = extname(node.source.value);
172
- if (ext != outExt) {
178
+ if (ext != this.outExt) {
173
179
  let dir = path.dirname(this.filePath);
174
180
  let fullPath = path.join(dir, node.source.value);
175
181
  console.log(`ImportDeclaration: ${fullPath} -> ${ext}`);
176
182
  if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
177
183
  node.source.value = node.source.value + "/index";
178
184
  }
179
- node.source.value += outExt;
185
+ node.source.value += this.outExt;
180
186
  }
181
187
  }
182
188
  break;
@@ -1,5 +1,5 @@
1
1
  import * as ts from 'typescript';
2
- import { FileAction } from '../../types';
3
- export declare function create(compilerOptions?: ts.CompilerOptions): FileAction;
2
+ import { FileAction, JavaScriptExtension } from '../../types';
3
+ export declare function create(defaultCompilerOptions?: ts.CompilerOptions, outExt?: JavaScriptExtension): FileAction;
4
4
  export declare function getTypescriptConfig(projectPath: string, directoryPath: string): ts.CompilerOptions;
5
5
  export declare function findTypeScriptConfigPath(projectPath: string, directoryPath: string): string | null;
@@ -40,14 +40,16 @@ const ts = __importStar(require("typescript"));
40
40
  const errors_1 = require("../errors");
41
41
  const fs = __importStar(require("fs"));
42
42
  const path = __importStar(require("path"));
43
- const project_compiler_1 = require("../project-compiler");
44
43
  const TS_CONFIG_FILE = 'tsconfig.json';
45
- function create(compilerOptions) {
44
+ function create(defaultCompilerOptions, outExt) {
45
+ outExt = outExt || ".js";
46
46
  const action = async (filePath, outDir, projectPath) => {
47
- if (!compilerOptions) {
48
- compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
47
+ if (defaultCompilerOptions) {
48
+ return compileFile(filePath, outDir, projectPath, defaultCompilerOptions, outExt);
49
49
  }
50
- return compileFile(filePath, outDir, projectPath, compilerOptions);
50
+ // 如果没有指定默认编译选项,则尝试从项目目录查找 tsconfig.json 文件,并使用其中的配置进行编译
51
+ const compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
52
+ return compileFile(filePath, outDir, projectPath, compilerOptions, outExt);
51
53
  };
52
54
  return action;
53
55
  }
@@ -57,7 +59,7 @@ function create(compilerOptions) {
57
59
  * @param outDir 输出目录,相对于项目路径
58
60
  * @param projectPath 项目路径,绝对路径
59
61
  * */
60
- async function compileFile(filePath, outDir, projectPath, compilerOptions) {
62
+ async function compileFile(filePath, outDir, projectPath, compilerOptions, outExt) {
61
63
  if (!filePath)
62
64
  throw errors_1.errors.argumentNull('sourcePath');
63
65
  if (!outDir)
@@ -68,7 +70,6 @@ async function compileFile(filePath, outDir, projectPath, compilerOptions) {
68
70
  // let compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
69
71
  let result = ts.transpileModule(content, { compilerOptions });
70
72
  let ext = path.extname(filePath);
71
- let outExt = project_compiler_1.ProjectCompiler.tsOutExt;
72
73
  let targetPath = path.join(projectPath, outDir, path.basename(filePath).replace(ext, outExt));
73
74
  let outDirPath = path.resolve(targetPath, "..");
74
75
  if (!fs.existsSync(outDirPath))
@@ -126,11 +126,7 @@ function generateCodeByOptions(options) {
126
126
  console.log(`Skip ${filePath}`);
127
127
  continue;
128
128
  }
129
- fileActions = fileActions || function (fileName) {
130
- const ext = path.extname(fileName);
131
- return configs_1.default.fileActions[ext];
132
- };
133
- let action = fileActions(filePath);
129
+ let action = fileActions?.(filePath) || configs_1.default.fileActions[ext];
134
130
  if (action) {
135
131
  action(filePath, outDir, projectPath);
136
132
  if (callback) {
@@ -150,7 +146,9 @@ function generateCodeByOptions(options) {
150
146
  function watchDirectoryByOptions(options) {
151
147
  let { sourceDir, outDir, projectPath, skipFiles, fileAction: fileActions, callback } = options;
152
148
  (0, node_watch_1.default)(sourceDir, { recursive: true }, async (evt, name) => {
153
- const filePath = name;
149
+ const filePath = name.replace(/\\/g, "/");
150
+ sourceDir = sourceDir.replace(/\\/g, "/");
151
+ outDir = outDir.replace(/\\/g, "/");
154
152
  const ext = extname(filePath);
155
153
  skipFiles = skipFiles || configs_1.default.skipFiles[ext] || [];
156
154
  let isSkip = skipFiles.some(pattern => pattern.test(filePath)); //isIgnoredFile(filePath);//skipFiles.some(pattern => new RegExp(pattern).test(filePath));
@@ -166,10 +164,10 @@ function watchDirectoryByOptions(options) {
166
164
  return;
167
165
  }
168
166
  try {
169
- let outPath = path.dirname(name).replace(sourceDir, outDir);
170
- action(name, outPath, projectPath);
167
+ let outPath = path.dirname(filePath).replace(sourceDir, outDir);
168
+ action(filePath, outPath, projectPath);
171
169
  if (callback)
172
- callback(name, outPath, projectPath);
170
+ callback(filePath, outPath, projectPath);
173
171
  }
174
172
  catch (e) {
175
173
  console.error(e);
package/out/types.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export type FileAction = (filePath: string, outPath: string, projectPath: string) => void;
2
+ export type JavaScriptExtension = ".js" | ".mjs" | ".cjs";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maishu-scripts",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "用于对 node 项目进行代码生成,打包,运行",
5
5
  "dependencies": {
6
6
  "@babel/core": "^7.24.3",
@@ -4,31 +4,37 @@ 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
+ import { FileAction, JavaScriptExtension } from "../../types";
8
8
 
9
- const outExt = ProjectCompiler.tsOutExt;
9
+ // const outExt = ProjectCompiler.tsOutExt;
10
10
 
11
11
 
12
- export function create(babelOptions?: babel.TransformOptions): FileAction {
12
+ export function create(defaultBabelOptions?: babel.TransformOptions, outExit?: JavaScriptExtension): FileAction {
13
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;
14
+ outExit = outExit || ".js";
15
+ if (defaultBabelOptions) {
16
+ return compileFile(sourcePath, outputPath, projectPath, defaultBabelOptions, outExit);
29
17
  }
30
18
 
31
- return compileFile(sourcePath, outputPath, projectPath, babelOptions);
19
+ // 如果没有 babelOptions,则尝试从项目目录查找 babel 配置文件,如果没有,则使用默认配置。
20
+ let babelOptions: babel.TransformOptions;
21
+ let bablePath: string;
22
+ let sourceDir = path.dirname(sourcePath);
23
+ if (projectPath) {
24
+ let c = ProjectCompiler.getBabelConfig(projectPath, sourceDir);
25
+ bablePath = c.path;
26
+ babelOptions = c.options;
27
+ }
28
+ else {
29
+ babelOptions = ProjectCompiler.getDefaultBabelConfig();
30
+ bablePath = '';
31
+ }
32
+
33
+ babelOptions.filename = sourcePath;
34
+ babelOptions.code = false;
35
+ babelOptions.ast = true;
36
+
37
+ return compileFile(sourcePath, outputPath, projectPath, babelOptions, outExit);
32
38
  }
33
39
  return action;
34
40
  }
@@ -39,7 +45,7 @@ export function create(babelOptions?: babel.TransformOptions): FileAction {
39
45
  * @param outputPath 输出目录
40
46
  * @param projectPath 项目路径
41
47
  * */
42
- async function compileFile(sourcePath: string, outputPath: string, projectPath: string, babelOptions: babel.TransformOptions) {
48
+ async function compileFile(sourcePath: string, outputPath: string, projectPath: string, babelOptions: babel.TransformOptions, outExt: JavaScriptExtension) {
43
49
  if (!sourcePath) throw errors.argumentNull("sourcePath");
44
50
  if (!outputPath) throw errors.argumentNull("outputPath");
45
51
  if (!projectPath) throw errors.argumentNull("projectPath");
@@ -82,7 +88,7 @@ async function compileFile(sourcePath: string, outputPath: string, projectPath:
82
88
  if (!ast)
83
89
  throw errors.compileError(sourcePath);
84
90
 
85
- new ImportPathRewrite(sourcePath, ast);
91
+ new ImportPathRewrite(sourcePath, ast, outExt);
86
92
  let r = babel.transformFromAstSync(ast, undefined, {
87
93
  filename: sourcePath, plugins: babelOptions.plugins,
88
94
  presets: babelOptions.presets, sourceMaps: babelOptions.sourceMaps
@@ -91,7 +97,7 @@ async function compileFile(sourcePath: string, outputPath: string, projectPath:
91
97
  throw errors.compileError(sourcePath);
92
98
 
93
99
  let ext = extname(sourcePath);
94
- let outExt = fileOutExt(sourcePath);
100
+ //let outExt = fileOutExt(sourcePath);
95
101
  let targetPath = path.join(outputPath, path.basename(sourcePath).replace(ext, outExt));//sourcePath.replace(new RegExp(ext + "$"), outExt).replace(path.dirname(sourcePath), outDir);
96
102
  let outDirPath = path.resolve(targetPath, "..");
97
103
 
@@ -128,20 +134,20 @@ function extname(file: string) {
128
134
  * 获取源文件所对应生成文件的扩展名
129
135
  * @param file 源文件名
130
136
  * */
131
- function fileOutExt(file: string) {
132
- let ext = extname(file);
133
- if (ext === ".ts")
134
- return outExt;
137
+ // function fileOutExt(file: string) {
138
+ // let ext = extname(file);
139
+ // if (ext === ".ts")
140
+ // return outExt;
135
141
 
136
- if (ext === ".tsx")
137
- return outExt;
142
+ // if (ext === ".tsx")
143
+ // return outExt;
138
144
 
139
- return ext;
140
- }
145
+ // return ext;
146
+ // }
141
147
 
142
148
 
143
149
  class ImportPathRewrite {
144
- constructor(private filePath: string, node: babel.types.File) {
150
+ constructor(private filePath: string, node: babel.types.File, private outExt: JavaScriptExtension) {
145
151
  this.traverse(node);
146
152
  }
147
153
 
@@ -156,14 +162,14 @@ class ImportPathRewrite {
156
162
  case "ImportDeclaration":
157
163
  if (node.source.type === "StringLiteral" && node.source.value.startsWith(".")) {
158
164
  let ext = extname(node.source.value);
159
- if (ext != outExt) {
165
+ if (ext != this.outExt) {
160
166
  let dir = path.dirname(this.filePath);
161
167
  let fullPath = path.join(dir, node.source.value);
162
168
  console.log(`ImportDeclaration: ${fullPath} -> ${ext}`)
163
169
  if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
164
170
  node.source.value = node.source.value + "/index";
165
171
  }
166
- node.source.value += outExt;
172
+ node.source.value += this.outExt;
167
173
  }
168
174
  }
169
175
  break;
@@ -3,7 +3,7 @@ import { errors } from '../errors';
3
3
  import * as fs from 'fs';
4
4
  import * as path from 'path';
5
5
  import { ProjectCompiler } from '../project-compiler';
6
- import { FileAction } from '../../types';
6
+ import { FileAction, JavaScriptExtension } from '../../types';
7
7
 
8
8
  const TS_CONFIG_FILE = 'tsconfig.json';
9
9
 
@@ -11,12 +11,16 @@ type TypeScriptProjectConfig = {
11
11
  compilerOptions: ts.CompilerOptions;
12
12
  }
13
13
 
14
- export function create(compilerOptions?: ts.CompilerOptions): FileAction {
14
+ export function create(defaultCompilerOptions?: ts.CompilerOptions, outExt?: JavaScriptExtension): FileAction {
15
+ outExt = outExt || ".js";
15
16
  const action: FileAction = async (filePath: string, outDir: string, projectPath: string) => {
16
- if (!compilerOptions) {
17
- compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
17
+ if (defaultCompilerOptions) {
18
+ return compileFile(filePath, outDir, projectPath, defaultCompilerOptions, outExt);
18
19
  }
19
- return compileFile(filePath, outDir, projectPath, compilerOptions)
20
+
21
+ // 如果没有指定默认编译选项,则尝试从项目目录查找 tsconfig.json 文件,并使用其中的配置进行编译
22
+ const compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
23
+ return compileFile(filePath, outDir, projectPath, compilerOptions, outExt);
20
24
  }
21
25
 
22
26
  return action;
@@ -28,7 +32,7 @@ export function create(compilerOptions?: ts.CompilerOptions): FileAction {
28
32
  * @param outDir 输出目录,相对于项目路径
29
33
  * @param projectPath 项目路径,绝对路径
30
34
  * */
31
- async function compileFile(filePath: string, outDir: string, projectPath: string, compilerOptions: ts.CompilerOptions) {
35
+ async function compileFile(filePath: string, outDir: string, projectPath: string, compilerOptions: ts.CompilerOptions, outExt: JavaScriptExtension) {
32
36
  if (!filePath)
33
37
  throw errors.argumentNull('sourcePath');
34
38
  if (!outDir)
@@ -42,7 +46,6 @@ async function compileFile(filePath: string, outDir: string, projectPath: string
42
46
  let result = ts.transpileModule(content, { compilerOptions });
43
47
 
44
48
  let ext = path.extname(filePath);
45
- let outExt = ProjectCompiler.tsOutExt;
46
49
  let targetPath = path.join(projectPath, outDir, path.basename(filePath).replace(ext, outExt));
47
50
  let outDirPath = path.resolve(targetPath, "..");
48
51
 
@@ -143,6 +143,7 @@ describe("Compile Test", function () {
143
143
 
144
144
  // 在 srcPath 中创建文件
145
145
  let filePath = path.join(srcPath, "test.txt");
146
+ filePath = filePath.replace(/\\/g, "/");
146
147
  fs.writeFileSync(filePath, "test");
147
148
  const fileAction = jest.fn();
148
149
 
@@ -116,11 +116,7 @@ function generateCodeByOptions(options: Options) {
116
116
  continue;
117
117
  }
118
118
 
119
- fileActions = fileActions || function (fileName: string) {
120
- const ext = path.extname(fileName);
121
- return configs.fileActions[ext];
122
- };
123
- let action = fileActions(filePath);
119
+ let action = fileActions?.(filePath) || configs.fileActions[ext];
124
120
  if (action) {
125
121
  action(filePath, outDir, projectPath);
126
122
  if (callback) {
@@ -143,7 +139,10 @@ function watchDirectoryByOptions(options: Options) {
143
139
  let { sourceDir, outDir, projectPath, skipFiles, fileAction: fileActions, callback } = options;
144
140
 
145
141
  watch(sourceDir, { recursive: true }, async (evt, name) => {
146
- const filePath = name;
142
+ const filePath = name.replace(/\\/g, "/");
143
+ sourceDir = sourceDir.replace(/\\/g, "/");
144
+ outDir = outDir.replace(/\\/g, "/");
145
+
147
146
  const ext = extname(filePath);
148
147
  skipFiles = skipFiles || configs.skipFiles[ext] || [];
149
148
  let isSkip = skipFiles.some(pattern => pattern.test(filePath));//isIgnoredFile(filePath);//skipFiles.some(pattern => new RegExp(pattern).test(filePath));
@@ -162,10 +161,10 @@ function watchDirectoryByOptions(options: Options) {
162
161
  }
163
162
 
164
163
  try {
165
- let outPath = path.dirname(name).replace(sourceDir, outDir);
166
- action(name, outPath, projectPath);
164
+ let outPath = path.dirname(filePath).replace(sourceDir, outDir);
165
+ action(filePath, outPath, projectPath);
167
166
  if (callback)
168
- callback(name, outPath, projectPath);
167
+ callback(filePath, outPath, projectPath);
169
168
  }
170
169
  catch (e) {
171
170
  console.error(e);
package/src/types.ts CHANGED
@@ -1 +1,2 @@
1
- export type FileAction = (filePath: string, outPath: string, projectPath: string) => void;
1
+ export type FileAction = (filePath: string, outPath: string, projectPath: string) => void;
2
+ export type JavaScriptExtension = ".js" | ".mjs" | ".cjs";