maishu-scripts 1.3.0 → 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.
- package/out/index.d.ts +9 -0
- package/out/index.js +12 -4
- package/out/modules/actions/babel-compile.d.ts +3 -0
- package/out/modules/actions/babel-compile.js +185 -0
- package/out/modules/actions/copy-file.js +18 -8
- package/out/modules/actions/scss-compile.js +17 -7
- package/out/modules/actions/ts-compile.d.ts +2 -7
- package/out/modules/actions/ts-compile.js +35 -13
- package/out/modules/compile.d.ts +16 -3
- package/out/modules/compile.js +102 -46
- package/out/modules/configs.d.ts +7 -0
- package/out/modules/configs.js +27 -0
- package/out/modules/import-path-rewrite.js +17 -7
- package/out/modules/project-compiler.js +17 -7
- package/out/types.d.ts +1 -0
- package/out/types.js +2 -0
- package/package.json +3 -2
- package/src/index.ts +7 -3
- package/src/modules/actions/{biel-compile.ts → babel-compile.ts} +37 -11
- package/src/modules/actions/copy-file.ts +1 -1
- package/src/modules/actions/ts-compile.test.ts +30 -2
- package/src/modules/actions/ts-compile.ts +22 -7
- package/src/modules/compile.test.ts +158 -3
- package/src/modules/compile.ts +111 -41
- package/src/modules/configs.test.ts +32 -0
- package/src/modules/configs.ts +29 -0
- /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 (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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"));
|
|
@@ -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 (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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.ProjectCompiler = void 0;
|
|
27
37
|
const errors_1 = require("./errors");
|
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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "maishu-scripts",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "用于对 node 项目进行代码生成,打包,运行",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@babel/core": "^7.24.3",
|
|
@@ -8,13 +8,14 @@
|
|
|
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",
|
|
12
13
|
"less": "^4.5.1",
|
|
13
14
|
"maishu-toolkit": "^1.12.6",
|
|
14
15
|
"node-watch": "^0.7.4",
|
|
15
16
|
"nodemon": "^3.1.4",
|
|
16
17
|
"sass": "^1.97.1",
|
|
17
|
-
"typescript": "^5.
|
|
18
|
+
"typescript": "^5.9.3"
|
|
18
19
|
},
|
|
19
20
|
"devDependencies": {
|
|
20
21
|
"@babel/preset-env": "^7.24.3",
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
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)) {
|
|
@@ -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
|
|
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
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { create as createCompiler, findTypeScriptConfigPath, getTypescriptConfig } from './ts-compile';
|
|
2
2
|
import * as path from 'path';
|
|
3
3
|
import * as fs from 'fs';
|
|
4
|
+
import ts from 'typescript';
|
|
4
5
|
|
|
5
6
|
describe('ts-compile', () => {
|
|
6
7
|
|
|
@@ -18,7 +19,8 @@ describe('ts-compile', () => {
|
|
|
18
19
|
fs.rmSync(jsFile, { recursive: true, force: true });
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
const compileFile = createCompiler();
|
|
23
|
+
compileFile(tsFile, outDir, projectPath);
|
|
22
24
|
expect(fs.existsSync(jsFile));
|
|
23
25
|
})
|
|
24
26
|
|
|
@@ -36,4 +38,30 @@ describe('ts-compile', () => {
|
|
|
36
38
|
expect(scriptPath).toBe(findTypeScriptConfigPath(projectPath, "src/static"));
|
|
37
39
|
|
|
38
40
|
})
|
|
41
|
+
|
|
42
|
+
test("should get typescript config", function () {
|
|
43
|
+
let config = getTypescriptConfig(projectPath, "src/static/design");
|
|
44
|
+
expect(config).not.toBeNull();
|
|
45
|
+
expect(config).not.toBeUndefined();
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test("create 带 compilerOptions 参数", function () {
|
|
49
|
+
const outDir = "dist";
|
|
50
|
+
let compilerOptions: ts.CompilerOptions = {
|
|
51
|
+
target: ts.ScriptTarget.ESNext,
|
|
52
|
+
module: ts.ModuleKind.ESNext,
|
|
53
|
+
};
|
|
54
|
+
let compileFile = createCompiler(compilerOptions);
|
|
55
|
+
expect(compileFile).not.toBeNull();
|
|
56
|
+
expect(compileFile).not.toBeUndefined();
|
|
57
|
+
|
|
58
|
+
let tsFile = path.join(__dirname, '..', '..', '..', 'test', 'src', 'main.ts');
|
|
59
|
+
const fileOutPath = path.join(projectPath, outDir, "main.js");
|
|
60
|
+
fs.rmSync(fileOutPath, { force: true });
|
|
61
|
+
|
|
62
|
+
expect(fs.existsSync(tsFile)).toBeTruthy();
|
|
63
|
+
compileFile(tsFile, outDir, projectPath);
|
|
64
|
+
|
|
65
|
+
expect(fs.existsSync(fileOutPath)).toBeTruthy();
|
|
66
|
+
})
|
|
39
67
|
});
|
|
@@ -3,6 +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
7
|
|
|
7
8
|
const TS_CONFIG_FILE = 'tsconfig.json';
|
|
8
9
|
|
|
@@ -10,13 +11,24 @@ type TypeScriptProjectConfig = {
|
|
|
10
11
|
compilerOptions: ts.CompilerOptions;
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
export function create(compilerOptions?: ts.CompilerOptions): FileAction {
|
|
15
|
+
const action: FileAction = async (filePath: string, outDir: string, projectPath: string) => {
|
|
16
|
+
if (!compilerOptions) {
|
|
17
|
+
compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
|
|
18
|
+
}
|
|
19
|
+
return compileFile(filePath, outDir, projectPath, compilerOptions)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return action;
|
|
23
|
+
}
|
|
24
|
+
|
|
13
25
|
/**
|
|
14
26
|
* 编译特定文件,并生成到制定目录
|
|
15
27
|
* @param filePath 源文件路径,绝对路径
|
|
16
28
|
* @param outDir 输出目录,相对于项目路径
|
|
17
29
|
* @param projectPath 项目路径,绝对路径
|
|
18
30
|
* */
|
|
19
|
-
|
|
31
|
+
async function compileFile(filePath: string, outDir: string, projectPath: string, compilerOptions: ts.CompilerOptions) {
|
|
20
32
|
if (!filePath)
|
|
21
33
|
throw errors.argumentNull('sourcePath');
|
|
22
34
|
if (!outDir)
|
|
@@ -24,16 +36,11 @@ export async function compileFile(filePath: string, outDir: string, projectPath:
|
|
|
24
36
|
if (!projectPath)
|
|
25
37
|
throw errors.argumentNull('projectPath');
|
|
26
38
|
|
|
27
|
-
if (!path.isAbsolute(filePath))
|
|
28
|
-
throw errors.notAbsolutePath(filePath);
|
|
29
|
-
|
|
30
39
|
|
|
31
40
|
let content = fs.readFileSync(filePath, 'utf-8');
|
|
32
|
-
let compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
|
|
41
|
+
// let compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
|
|
33
42
|
let result = ts.transpileModule(content, { compilerOptions });
|
|
34
43
|
|
|
35
|
-
console.log(result.outputText);
|
|
36
|
-
|
|
37
44
|
let ext = path.extname(filePath);
|
|
38
45
|
let outExt = ProjectCompiler.tsOutExt;
|
|
39
46
|
let targetPath = path.join(projectPath, outDir, path.basename(filePath).replace(ext, outExt));
|
|
@@ -43,6 +50,14 @@ export async function compileFile(filePath: string, outDir: string, projectPath:
|
|
|
43
50
|
fs.mkdirSync(outDirPath, { recursive: true });
|
|
44
51
|
|
|
45
52
|
fs.writeFileSync(targetPath, result.outputText);
|
|
53
|
+
|
|
54
|
+
// 编译声明文件
|
|
55
|
+
if (compilerOptions.declaration && result.outputText) {
|
|
56
|
+
const d = ts.transpileDeclaration(content, { compilerOptions });
|
|
57
|
+
const declPath = targetPath.replace(outExt, '.d.ts');
|
|
58
|
+
fs.writeFileSync(declPath, d.outputText);
|
|
59
|
+
}
|
|
60
|
+
|
|
46
61
|
}
|
|
47
62
|
|
|
48
63
|
export function getTypescriptConfig(projectPath: string, directoryPath: string): ts.CompilerOptions {
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { generateCode, isIgnoredFile } from "./compile";
|
|
1
|
+
import { generateCode, isIgnoredFile, watchDirectory } from "./compile";
|
|
2
2
|
import * as path from "path";
|
|
3
3
|
import * as fs from "fs";
|
|
4
|
-
import
|
|
4
|
+
import { create as createTsCompiler } from "../modules/actions/ts-compile";
|
|
5
|
+
import ts from "typescript";
|
|
6
|
+
import { FileAction } from "../types";
|
|
5
7
|
|
|
6
8
|
describe("Compile Test", function () {
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
test("copyFile test", function () {
|
|
9
11
|
|
|
10
12
|
let srcPath = "test/src";
|
|
11
13
|
let srcExists = fs.existsSync(srcPath);
|
|
@@ -24,10 +26,163 @@ describe("Compile Test", function () {
|
|
|
24
26
|
generateCode(srcPath, destPath, projectPath);
|
|
25
27
|
})
|
|
26
28
|
|
|
29
|
+
test("generateCode with callback test", function () {
|
|
30
|
+
|
|
31
|
+
let srcPath = "test/src";
|
|
32
|
+
let destPath = "test/out";
|
|
33
|
+
if (fs.existsSync(destPath)) {
|
|
34
|
+
fs.rmdirSync(destPath, { recursive: true });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let projectPath = path.join(__dirname, "..", "..");
|
|
38
|
+
const callback = jest.fn();
|
|
39
|
+
generateCode(srcPath, destPath, projectPath, callback);
|
|
40
|
+
expect(callback.mock.calls.length).toBeGreaterThan(0);
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test("generateCode with skipFiles test", function () {
|
|
44
|
+
|
|
45
|
+
let srcPath = "test/src";
|
|
46
|
+
let destPath = "test/out";
|
|
47
|
+
if (fs.existsSync(destPath)) {
|
|
48
|
+
fs.rmdirSync(destPath, { recursive: true });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let projectPath = path.join(__dirname, "..", "..");
|
|
52
|
+
let executedPaths: string[] = [];
|
|
53
|
+
generateCode(srcPath, destPath, projectPath, (filePath: string) => {
|
|
54
|
+
executedPaths.push(filePath);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// 'test\\src\\controllers\\proxy.ts'
|
|
58
|
+
expect(executedPaths.some(p => p.includes("controllers")));
|
|
59
|
+
|
|
60
|
+
// 忽略掉 controllers 目录
|
|
61
|
+
const skipFiles = [new RegExp("controllers")];
|
|
62
|
+
executedPaths = [];
|
|
63
|
+
generateCode({
|
|
64
|
+
sourceDir: srcPath, outDir: destPath, projectPath, skipFiles,
|
|
65
|
+
callback(filePath: string) {
|
|
66
|
+
executedPaths.push(filePath);
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
expect(executedPaths.some(p => p.includes("controllers"))).toBe(false);
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test("generateCode 根据文件夹使用不同的 TS 编译器", function () {
|
|
73
|
+
|
|
74
|
+
let srcPath = "test/src";
|
|
75
|
+
let destPath = "test/out";
|
|
76
|
+
if (fs.existsSync(destPath)) {
|
|
77
|
+
fs.rmdirSync(destPath, { recursive: true });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const srcCompiler = createTsCompiler({
|
|
81
|
+
target: ts.ScriptTarget.ES2015,
|
|
82
|
+
module: ts.ModuleKind.CommonJS,
|
|
83
|
+
declaration: true,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const staticCompiler = createTsCompiler({
|
|
87
|
+
target: ts.ScriptTarget.ES5,
|
|
88
|
+
module: ts.ModuleKind.AMD,
|
|
89
|
+
declaration: false,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
let projectPath = path.join(__dirname, "..", "..");
|
|
93
|
+
const mainFile = "main.ts";
|
|
94
|
+
const staticFile = "static/index.ts";
|
|
95
|
+
const mainFilePath = path.resolve(srcPath, mainFile);
|
|
96
|
+
expect(fs.existsSync(mainFilePath)).toBeTruthy();
|
|
97
|
+
const staticFilePath = path.resolve(srcPath, staticFile);
|
|
98
|
+
expect(fs.existsSync(staticFilePath)).toBeTruthy();
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
generateCode({
|
|
102
|
+
sourceDir: srcPath, outDir: destPath, projectPath,
|
|
103
|
+
fileAction(filePath: string) {
|
|
104
|
+
const ext = path.extname(filePath);
|
|
105
|
+
if (ext !== ".ts" && ext !== ".tsx") {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
filePath = filePath.replace(/\\/g, "/");
|
|
109
|
+
if (filePath.startsWith("test/src/static/")) {
|
|
110
|
+
return staticCompiler;
|
|
111
|
+
}
|
|
112
|
+
return srcCompiler;
|
|
113
|
+
},
|
|
114
|
+
callback(filePath: string) {
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
const mainOutFile = "main.js";
|
|
119
|
+
const staticOutFile = "static/index.js";
|
|
120
|
+
const mainOutFilePath = path.resolve(destPath, mainOutFile);
|
|
121
|
+
const staticOutFilePath = path.resolve(destPath, staticOutFile);
|
|
122
|
+
expect(fs.existsSync(mainOutFilePath)).toBeTruthy();
|
|
123
|
+
expect(fs.existsSync(staticOutFilePath)).toBeTruthy();
|
|
124
|
+
|
|
125
|
+
const mainFileContent = fs.readFileSync(mainOutFilePath, "utf-8");
|
|
126
|
+
const staticFileContent = fs.readFileSync(staticOutFilePath, "utf-8");
|
|
127
|
+
|
|
128
|
+
expect(mainFileContent.includes("Object.defineProperty(exports, \"__esModule\"")).toBe(true);
|
|
129
|
+
expect(staticFileContent.includes("define([")).toBe(true);
|
|
130
|
+
|
|
131
|
+
})
|
|
132
|
+
|
|
27
133
|
test("isIgnored test", function () {
|
|
28
134
|
let filePath = "D:\\startdard-process\\plm-projects\\tc-api-proxy\\src\\app.controller.spec.ts";
|
|
29
135
|
let ignored = isIgnoredFile(filePath);
|
|
30
136
|
expect(ignored).toBe(true);
|
|
31
137
|
})
|
|
32
138
|
|
|
139
|
+
test("watchDirectory test, 修改文件应该触发回调, 删除文不应该触发回调", async function () {
|
|
140
|
+
|
|
141
|
+
let srcPath = "test/src";
|
|
142
|
+
let destPath = "test/out";
|
|
143
|
+
|
|
144
|
+
// 在 srcPath 中创建文件
|
|
145
|
+
let filePath = path.join(srcPath, "test.txt");
|
|
146
|
+
fs.writeFileSync(filePath, "test");
|
|
147
|
+
const fileAction = jest.fn();
|
|
148
|
+
|
|
149
|
+
setTimeout(() => {
|
|
150
|
+
// 监听 srcPath
|
|
151
|
+
let projectPath = path.join(__dirname, "..", "..");
|
|
152
|
+
let executedPaths: string[] = [];
|
|
153
|
+
// watchDirectory(srcPath, destPath, projectPath, (filePath: string) => {
|
|
154
|
+
// executedPaths.push(filePath);
|
|
155
|
+
// });
|
|
156
|
+
watchDirectory({
|
|
157
|
+
sourceDir: srcPath, outDir: destPath, projectPath,
|
|
158
|
+
fileAction: () => {
|
|
159
|
+
return fileAction;
|
|
160
|
+
},
|
|
161
|
+
callback(filePath: string) {
|
|
162
|
+
executedPaths.push(filePath);
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
}, 0);
|
|
166
|
+
|
|
167
|
+
await new Promise(resolve => setTimeout(resolve, 1000 * 1));
|
|
168
|
+
// 修改文件
|
|
169
|
+
fs.writeFileSync(filePath, "test2");
|
|
170
|
+
|
|
171
|
+
await new Promise(resolve => setTimeout(resolve, 1000 * 1));
|
|
172
|
+
expect(fileAction.mock.calls.length).toBe(1);
|
|
173
|
+
expect(fileAction.mock.calls[0][0]).toBe(filePath);
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
const calltimes = fileAction.mock.calls.length;
|
|
177
|
+
setTimeout(() => {
|
|
178
|
+
// 删除文件
|
|
179
|
+
fs.unlinkSync(filePath);
|
|
180
|
+
}, 1000 * 1);
|
|
181
|
+
|
|
182
|
+
await new Promise(resolve => setTimeout(resolve, 1000 * 1));
|
|
183
|
+
expect(fileAction.mock.calls.length).toBe(calltimes);
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
})
|
|
187
|
+
|
|
33
188
|
})
|