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
package/out/index.d.ts
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
+
import copyFile from "./modules/actions/copy-file";
|
|
1
2
|
export { generateCode, watchDirectory } from "./modules/compile";
|
|
2
3
|
export { run } from "./modules/run";
|
|
4
|
+
export declare const options: {
|
|
5
|
+
fileActions: Record<string, import("./types").FileAction>;
|
|
6
|
+
skipFiles: Record<string, RegExp[]>;
|
|
7
|
+
};
|
|
8
|
+
export { create as createTsCompiler } from "./modules/actions/ts-compile";
|
|
9
|
+
export { create as createBielCompiler } from "./modules/actions/babel-compile";
|
|
10
|
+
export { copyFile };
|
|
11
|
+
export { FileAction } from "./types";
|
package/out/index.js
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.run = exports.watchDirectory = exports.generateCode = void 0;
|
|
6
|
+
exports.copyFile = exports.createBielCompiler = exports.createTsCompiler = exports.options = exports.run = exports.watchDirectory = exports.generateCode = void 0;
|
|
7
|
+
const configs_1 = __importDefault(require("./modules/configs"));
|
|
8
|
+
const copy_file_1 = __importDefault(require("./modules/actions/copy-file"));
|
|
9
|
+
exports.copyFile = copy_file_1.default;
|
|
4
10
|
var compile_1 = require("./modules/compile");
|
|
5
11
|
Object.defineProperty(exports, "generateCode", { enumerable: true, get: function () { return compile_1.generateCode; } });
|
|
6
12
|
Object.defineProperty(exports, "watchDirectory", { enumerable: true, get: function () { return compile_1.watchDirectory; } });
|
|
7
13
|
var run_1 = require("./modules/run");
|
|
8
14
|
Object.defineProperty(exports, "run", { enumerable: true, get: function () { return run_1.run; } });
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
exports.options = configs_1.default;
|
|
16
|
+
var ts_compile_1 = require("./modules/actions/ts-compile");
|
|
17
|
+
Object.defineProperty(exports, "createTsCompiler", { enumerable: true, get: function () { return ts_compile_1.create; } });
|
|
18
|
+
var babel_compile_1 = require("./modules/actions/babel-compile");
|
|
19
|
+
Object.defineProperty(exports, "createBielCompiler", { enumerable: true, get: function () { return babel_compile_1.create; } });
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
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
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.create = create;
|
|
37
|
+
const babel = __importStar(require("@babel/core"));
|
|
38
|
+
const fs = __importStar(require("fs"));
|
|
39
|
+
const path = __importStar(require("path"));
|
|
40
|
+
const errors_1 = require("../errors");
|
|
41
|
+
const project_compiler_1 = require("../project-compiler");
|
|
42
|
+
const outExt = project_compiler_1.ProjectCompiler.tsOutExt;
|
|
43
|
+
function create(babelOptions) {
|
|
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;
|
|
60
|
+
}
|
|
61
|
+
return compileFile(sourcePath, outputPath, projectPath, babelOptions);
|
|
62
|
+
};
|
|
63
|
+
return action;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* 编译特定文件,并生成到制定目录
|
|
67
|
+
* @param sourcePath 源文件路径
|
|
68
|
+
* @param outputPath 输出目录
|
|
69
|
+
* @param projectPath 项目路径
|
|
70
|
+
* */
|
|
71
|
+
async function compileFile(sourcePath, outputPath, projectPath, babelOptions) {
|
|
72
|
+
if (!sourcePath)
|
|
73
|
+
throw errors_1.errors.argumentNull("sourcePath");
|
|
74
|
+
if (!outputPath)
|
|
75
|
+
throw errors_1.errors.argumentNull("outputPath");
|
|
76
|
+
if (!projectPath)
|
|
77
|
+
throw errors_1.errors.argumentNull("projectPath");
|
|
78
|
+
// if (!fs.existsSync(sourcePath)) throw errors.pathNotExists(sourcePath);
|
|
79
|
+
if (!fs.existsSync(sourcePath)) {
|
|
80
|
+
console.warn(`Path not exists: ${sourcePath}`);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
let sourceDir = path.dirname(sourcePath);
|
|
84
|
+
// let babelOptions: babel.TransformOptions;
|
|
85
|
+
let bablePath;
|
|
86
|
+
//= projectPath ?
|
|
87
|
+
// ProjectCompiler.getBabelConfig(projectPath, sourceDir) : ProjectCompiler.getDefaultBabelConfig();
|
|
88
|
+
if (projectPath) {
|
|
89
|
+
// let c = ProjectCompiler.getBabelConfig(projectPath, sourceDir);
|
|
90
|
+
// bablePath = c.path;
|
|
91
|
+
// babelOptions = c.options;
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
// babelOptions = ProjectCompiler.getDefaultBabelConfig();
|
|
95
|
+
// bablePath = '';
|
|
96
|
+
}
|
|
97
|
+
// babelOptions.filename = sourcePath;
|
|
98
|
+
// babelOptions.code = false;
|
|
99
|
+
// babelOptions.ast = true;
|
|
100
|
+
// let fileResult = babel.transformFileSync(sourcePath, {
|
|
101
|
+
// filename: sourcePath, code: false, ast: true, plugins,
|
|
102
|
+
// presets
|
|
103
|
+
// });
|
|
104
|
+
let fileResult = babel.transformFileSync(sourcePath, babelOptions);
|
|
105
|
+
if (!fileResult)
|
|
106
|
+
throw errors_1.errors.compileError(sourcePath);
|
|
107
|
+
let ast = fileResult.ast;
|
|
108
|
+
if (!ast)
|
|
109
|
+
throw errors_1.errors.compileError(sourcePath);
|
|
110
|
+
new ImportPathRewrite(sourcePath, ast);
|
|
111
|
+
let r = babel.transformFromAstSync(ast, undefined, {
|
|
112
|
+
filename: sourcePath, plugins: babelOptions.plugins,
|
|
113
|
+
presets: babelOptions.presets, sourceMaps: babelOptions.sourceMaps
|
|
114
|
+
});
|
|
115
|
+
if (!r || r.code == null)
|
|
116
|
+
throw errors_1.errors.compileError(sourcePath);
|
|
117
|
+
let ext = extname(sourcePath);
|
|
118
|
+
let outExt = fileOutExt(sourcePath);
|
|
119
|
+
let targetPath = path.join(outputPath, path.basename(sourcePath).replace(ext, outExt)); //sourcePath.replace(new RegExp(ext + "$"), outExt).replace(path.dirname(sourcePath), outDir);
|
|
120
|
+
let outDirPath = path.resolve(targetPath, "..");
|
|
121
|
+
let mapPath = targetPath + ".map";
|
|
122
|
+
if (r.map) {
|
|
123
|
+
r.map.file = path.basename(targetPath);
|
|
124
|
+
r.map.sources = [path.relative(outputPath, sourcePath)];
|
|
125
|
+
if (!fs.existsSync(outDirPath))
|
|
126
|
+
fs.mkdirSync(outDirPath, { recursive: true });
|
|
127
|
+
fs.writeFileSync(mapPath, JSON.stringify(r.map));
|
|
128
|
+
// r.code += `\n//babel file path = ${bablePath}`;
|
|
129
|
+
r.code += "\n//# sourceMappingURL=" + path.basename(mapPath);
|
|
130
|
+
}
|
|
131
|
+
else if (fs.existsSync(mapPath)) {
|
|
132
|
+
fs.rmSync(targetPath);
|
|
133
|
+
}
|
|
134
|
+
if (!fs.existsSync(outDirPath))
|
|
135
|
+
fs.mkdirSync(outDirPath, { recursive: true });
|
|
136
|
+
fs.writeFileSync(targetPath, r.code);
|
|
137
|
+
}
|
|
138
|
+
function extname(file) {
|
|
139
|
+
// let ext = /\.[a-zA-Z]+/.exec(file)?.[0] || '';
|
|
140
|
+
let ext = path.extname(file);
|
|
141
|
+
return ext;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 获取源文件所对应生成文件的扩展名
|
|
145
|
+
* @param file 源文件名
|
|
146
|
+
* */
|
|
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
|
+
}
|
|
155
|
+
class ImportPathRewrite {
|
|
156
|
+
filePath;
|
|
157
|
+
constructor(filePath, node) {
|
|
158
|
+
this.filePath = filePath;
|
|
159
|
+
this.traverse(node);
|
|
160
|
+
}
|
|
161
|
+
traverse(node) {
|
|
162
|
+
switch (node.type) {
|
|
163
|
+
case "Program":
|
|
164
|
+
node.body.forEach(c => this.traverse(c));
|
|
165
|
+
break;
|
|
166
|
+
case "File":
|
|
167
|
+
this.traverse(node.program);
|
|
168
|
+
break;
|
|
169
|
+
case "ImportDeclaration":
|
|
170
|
+
if (node.source.type === "StringLiteral" && node.source.value.startsWith(".")) {
|
|
171
|
+
let ext = extname(node.source.value);
|
|
172
|
+
if (ext != outExt) {
|
|
173
|
+
let dir = path.dirname(this.filePath);
|
|
174
|
+
let fullPath = path.join(dir, node.source.value);
|
|
175
|
+
console.log(`ImportDeclaration: ${fullPath} -> ${ext}`);
|
|
176
|
+
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
|
|
177
|
+
node.source.value = node.source.value + "/index";
|
|
178
|
+
}
|
|
179
|
+
node.source.value += outExt;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
@@ -15,18 +15,28 @@ 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
|
const errors_1 = require("../errors");
|
|
27
37
|
const fs = __importStar(require("fs"));
|
|
28
38
|
const path = __importStar(require("path"));
|
|
29
|
-
let copyFile = (filePath, outPath
|
|
39
|
+
let copyFile = (filePath, outPath) => {
|
|
30
40
|
if (!filePath)
|
|
31
41
|
throw errors_1.errors.argumentNull("filePath");
|
|
32
42
|
if (!outPath)
|
|
@@ -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.compileFile = void 0;
|
|
27
37
|
const sass = __importStar(require("sass"));
|
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @param filePath 源文件路径,绝对路径
|
|
5
|
-
* @param outDir 输出目录,相对于项目路径
|
|
6
|
-
* @param projectPath 项目路径,绝对路径
|
|
7
|
-
* */
|
|
8
|
-
export declare function compileFile(filePath: string, outDir: string, projectPath: string): Promise<void>;
|
|
2
|
+
import { FileAction } from '../../types';
|
|
3
|
+
export declare function create(compilerOptions?: ts.CompilerOptions): FileAction;
|
|
9
4
|
export declare function getTypescriptConfig(projectPath: string, directoryPath: string): ts.CompilerOptions;
|
|
10
5
|
export declare function findTypeScriptConfigPath(projectPath: string, directoryPath: string): string | null;
|
|
@@ -15,15 +15,25 @@ 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
|
-
exports.
|
|
36
|
+
exports.create = create;
|
|
27
37
|
exports.getTypescriptConfig = getTypescriptConfig;
|
|
28
38
|
exports.findTypeScriptConfigPath = findTypeScriptConfigPath;
|
|
29
39
|
const ts = __importStar(require("typescript"));
|
|
@@ -32,25 +42,31 @@ const fs = __importStar(require("fs"));
|
|
|
32
42
|
const path = __importStar(require("path"));
|
|
33
43
|
const project_compiler_1 = require("../project-compiler");
|
|
34
44
|
const TS_CONFIG_FILE = 'tsconfig.json';
|
|
45
|
+
function create(compilerOptions) {
|
|
46
|
+
const action = async (filePath, outDir, projectPath) => {
|
|
47
|
+
if (!compilerOptions) {
|
|
48
|
+
compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
|
|
49
|
+
}
|
|
50
|
+
return compileFile(filePath, outDir, projectPath, compilerOptions);
|
|
51
|
+
};
|
|
52
|
+
return action;
|
|
53
|
+
}
|
|
35
54
|
/**
|
|
36
55
|
* 编译特定文件,并生成到制定目录
|
|
37
56
|
* @param filePath 源文件路径,绝对路径
|
|
38
57
|
* @param outDir 输出目录,相对于项目路径
|
|
39
58
|
* @param projectPath 项目路径,绝对路径
|
|
40
59
|
* */
|
|
41
|
-
async function compileFile(filePath, outDir, projectPath) {
|
|
60
|
+
async function compileFile(filePath, outDir, projectPath, compilerOptions) {
|
|
42
61
|
if (!filePath)
|
|
43
62
|
throw errors_1.errors.argumentNull('sourcePath');
|
|
44
63
|
if (!outDir)
|
|
45
64
|
throw errors_1.errors.argumentNull('outDir');
|
|
46
65
|
if (!projectPath)
|
|
47
66
|
throw errors_1.errors.argumentNull('projectPath');
|
|
48
|
-
if (!path.isAbsolute(filePath))
|
|
49
|
-
throw errors_1.errors.notAbsolutePath(filePath);
|
|
50
67
|
let content = fs.readFileSync(filePath, 'utf-8');
|
|
51
|
-
let compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
|
|
68
|
+
// let compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
|
|
52
69
|
let result = ts.transpileModule(content, { compilerOptions });
|
|
53
|
-
console.log(result.outputText);
|
|
54
70
|
let ext = path.extname(filePath);
|
|
55
71
|
let outExt = project_compiler_1.ProjectCompiler.tsOutExt;
|
|
56
72
|
let targetPath = path.join(projectPath, outDir, path.basename(filePath).replace(ext, outExt));
|
|
@@ -58,6 +74,12 @@ async function compileFile(filePath, outDir, projectPath) {
|
|
|
58
74
|
if (!fs.existsSync(outDirPath))
|
|
59
75
|
fs.mkdirSync(outDirPath, { recursive: true });
|
|
60
76
|
fs.writeFileSync(targetPath, result.outputText);
|
|
77
|
+
// 编译声明文件
|
|
78
|
+
if (compilerOptions.declaration && result.outputText) {
|
|
79
|
+
const d = ts.transpileDeclaration(content, { compilerOptions });
|
|
80
|
+
const declPath = targetPath.replace(outExt, '.d.ts');
|
|
81
|
+
fs.writeFileSync(declPath, d.outputText);
|
|
82
|
+
}
|
|
61
83
|
}
|
|
62
84
|
function getTypescriptConfig(projectPath, directoryPath) {
|
|
63
85
|
let configPath = findTypeScriptConfigPath(projectPath, directoryPath);
|
package/out/modules/compile.d.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
|
+
import { FileAction } from "../types";
|
|
2
|
+
type Callback = (filePath: string, outDir: string, projectPath: string) => void;
|
|
1
3
|
/** 将 sourceDir 目录下所有文件生成到 outDir */
|
|
2
|
-
export declare function generateCode(sourceDir:
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
export declare function generateCode(sourceDir: Options): void;
|
|
5
|
+
export declare function generateCode(sourceDir: string, outDir: string, projectPath: string, callback?: Callback): void;
|
|
6
|
+
/** 监听 sourceDir 目录下所有文件变化,并生成到 outDir */
|
|
7
|
+
export declare function watchDirectory(sourceDir: Options): void;
|
|
8
|
+
export declare function watchDirectory(sourceDir: string, outDir: string, projectPath: string, callback?: Callback): void;
|
|
5
9
|
export declare function isIgnoredFile(filePath: string): boolean;
|
|
10
|
+
type Options = {
|
|
11
|
+
sourceDir: string;
|
|
12
|
+
outDir: string;
|
|
13
|
+
projectPath: string;
|
|
14
|
+
skipFiles?: RegExp[];
|
|
15
|
+
fileAction?: (fileName: string) => FileAction | undefined;
|
|
16
|
+
callback?: Callback;
|
|
17
|
+
};
|
|
18
|
+
export {};
|
package/out/modules/compile.js
CHANGED
|
@@ -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
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
37
|
};
|
|
@@ -33,21 +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
|
-
|
|
46
|
+
// import { compileFile as tsCompileFile } from "./actions/ts-compile";
|
|
37
47
|
const copy_file_1 = __importDefault(require("./actions/copy-file"));
|
|
38
|
-
const
|
|
39
|
-
const less_compile_1 = __importDefault(require("./actions/less-compile"));
|
|
40
|
-
const skipFiles = ["\\S+\\.d\\.tsx?$", "\\S+\\.test\\.tsx?$", "\\S+\\.spec\\.tsx?$"];
|
|
48
|
+
const configs_1 = __importDefault(require("./configs"));
|
|
41
49
|
const extCopy = [".js", ".html", ".css", ".jpg", ".png", ".gif", ".less"];
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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;
|
|
51
108
|
if (!sourceDir)
|
|
52
109
|
throw errors_1.errors.argumentNull("sourceDir");
|
|
53
110
|
if (!outDir)
|
|
@@ -62,13 +119,18 @@ function generateCode(sourceDir, outDir, projectPath, callback) {
|
|
|
62
119
|
if (!fs.statSync(filePath).isFile()) {
|
|
63
120
|
continue;
|
|
64
121
|
}
|
|
65
|
-
let
|
|
122
|
+
let ext = extname(file);
|
|
123
|
+
skipFiles = skipFiles || configs_1.default.skipFiles[ext] || [];
|
|
124
|
+
let isSkip = skipFiles.some(pattern => pattern.test(filePath));
|
|
66
125
|
if (isSkip) {
|
|
67
126
|
console.log(`Skip ${filePath}`);
|
|
68
127
|
continue;
|
|
69
128
|
}
|
|
70
|
-
|
|
71
|
-
|
|
129
|
+
fileActions = fileActions || function (fileName) {
|
|
130
|
+
const ext = path.extname(fileName);
|
|
131
|
+
return configs_1.default.fileActions[ext];
|
|
132
|
+
};
|
|
133
|
+
let action = fileActions(filePath);
|
|
72
134
|
if (action) {
|
|
73
135
|
action(filePath, outDir, projectPath);
|
|
74
136
|
if (callback) {
|
|
@@ -81,25 +143,30 @@ function generateCode(sourceDir, outDir, projectPath, callback) {
|
|
|
81
143
|
let fullPath = path.join(sourceDir, dir);
|
|
82
144
|
let outDirPath = path.join(outDir, dir);
|
|
83
145
|
if (fs.statSync(fullPath).isDirectory()) {
|
|
84
|
-
|
|
146
|
+
generateCodeByOptions({ sourceDir: fullPath, outDir: outDirPath, projectPath: projectPath, skipFiles, fileAction: fileActions, callback });
|
|
85
147
|
}
|
|
86
148
|
}
|
|
87
149
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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];
|
|
99
165
|
if (!action) {
|
|
100
166
|
return;
|
|
101
167
|
}
|
|
102
168
|
try {
|
|
169
|
+
let outPath = path.dirname(name).replace(sourceDir, outDir);
|
|
103
170
|
action(name, outPath, projectPath);
|
|
104
171
|
if (callback)
|
|
105
172
|
callback(name, outPath, projectPath);
|
|
@@ -109,14 +176,3 @@ function watchDirectory(sourceRoot, outRoot, projectPath, callback) {
|
|
|
109
176
|
}
|
|
110
177
|
});
|
|
111
178
|
}
|
|
112
|
-
function isIgnoredFile(filePath) {
|
|
113
|
-
if (!filePath)
|
|
114
|
-
throw errors_1.errors.argumentNull("filePath");
|
|
115
|
-
let isSkip = skipFiles.some(pattern => new RegExp(pattern).test(filePath));
|
|
116
|
-
return isSkip;
|
|
117
|
-
}
|
|
118
|
-
function extname(file) {
|
|
119
|
-
// let ext = /\.[a-zA-Z]+/.exec(file)?.[0] || '';
|
|
120
|
-
let ext = path.extname(file);
|
|
121
|
-
return ext;
|
|
122
|
-
}
|
|
@@ -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];
|