maishu-scripts 1.3.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.
- 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 +191 -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 +38 -15
- package/out/modules/compile.d.ts +16 -3
- package/out/modules/compile.js +102 -48
- 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 +2 -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} +57 -25
- 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 +26 -8
- package/src/modules/compile.test.ts +159 -3
- package/src/modules/compile.ts +112 -43
- package/src/modules/configs.test.ts +32 -0
- package/src/modules/configs.ts +29 -0
- package/src/types.ts +2 -0
- package/src/types.d.ts +0 -1
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,191 @@
|
|
|
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 = ProjectCompiler.tsOutExt;
|
|
43
|
+
function create(defaultBabelOptions, outExit) {
|
|
44
|
+
const action = (sourcePath, outputPath, projectPath) => {
|
|
45
|
+
outExit = outExit || ".js";
|
|
46
|
+
if (defaultBabelOptions) {
|
|
47
|
+
return compileFile(sourcePath, outputPath, projectPath, defaultBabelOptions, outExit);
|
|
48
|
+
}
|
|
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);
|
|
66
|
+
};
|
|
67
|
+
return action;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 编译特定文件,并生成到制定目录
|
|
71
|
+
* @param sourcePath 源文件路径
|
|
72
|
+
* @param outputPath 输出目录
|
|
73
|
+
* @param projectPath 项目路径
|
|
74
|
+
* */
|
|
75
|
+
async function compileFile(sourcePath, outputPath, projectPath, babelOptions, outExt) {
|
|
76
|
+
if (!sourcePath)
|
|
77
|
+
throw errors_1.errors.argumentNull("sourcePath");
|
|
78
|
+
if (!outputPath)
|
|
79
|
+
throw errors_1.errors.argumentNull("outputPath");
|
|
80
|
+
if (!projectPath)
|
|
81
|
+
throw errors_1.errors.argumentNull("projectPath");
|
|
82
|
+
// if (!fs.existsSync(sourcePath)) throw errors.pathNotExists(sourcePath);
|
|
83
|
+
if (!fs.existsSync(sourcePath)) {
|
|
84
|
+
console.warn(`Path not exists: ${sourcePath}`);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
let sourceDir = path.dirname(sourcePath);
|
|
88
|
+
// let babelOptions: babel.TransformOptions;
|
|
89
|
+
let bablePath;
|
|
90
|
+
//= projectPath ?
|
|
91
|
+
// ProjectCompiler.getBabelConfig(projectPath, sourceDir) : ProjectCompiler.getDefaultBabelConfig();
|
|
92
|
+
if (projectPath) {
|
|
93
|
+
// let c = ProjectCompiler.getBabelConfig(projectPath, sourceDir);
|
|
94
|
+
// bablePath = c.path;
|
|
95
|
+
// babelOptions = c.options;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
// babelOptions = ProjectCompiler.getDefaultBabelConfig();
|
|
99
|
+
// bablePath = '';
|
|
100
|
+
}
|
|
101
|
+
// babelOptions.filename = sourcePath;
|
|
102
|
+
// babelOptions.code = false;
|
|
103
|
+
// babelOptions.ast = true;
|
|
104
|
+
// let fileResult = babel.transformFileSync(sourcePath, {
|
|
105
|
+
// filename: sourcePath, code: false, ast: true, plugins,
|
|
106
|
+
// presets
|
|
107
|
+
// });
|
|
108
|
+
let fileResult = babel.transformFileSync(sourcePath, babelOptions);
|
|
109
|
+
if (!fileResult)
|
|
110
|
+
throw errors_1.errors.compileError(sourcePath);
|
|
111
|
+
let ast = fileResult.ast;
|
|
112
|
+
if (!ast)
|
|
113
|
+
throw errors_1.errors.compileError(sourcePath);
|
|
114
|
+
new ImportPathRewrite(sourcePath, ast, outExt);
|
|
115
|
+
let r = babel.transformFromAstSync(ast, undefined, {
|
|
116
|
+
filename: sourcePath, plugins: babelOptions.plugins,
|
|
117
|
+
presets: babelOptions.presets, sourceMaps: babelOptions.sourceMaps
|
|
118
|
+
});
|
|
119
|
+
if (!r || r.code == null)
|
|
120
|
+
throw errors_1.errors.compileError(sourcePath);
|
|
121
|
+
let ext = extname(sourcePath);
|
|
122
|
+
//let outExt = fileOutExt(sourcePath);
|
|
123
|
+
let targetPath = path.join(outputPath, path.basename(sourcePath).replace(ext, outExt)); //sourcePath.replace(new RegExp(ext + "$"), outExt).replace(path.dirname(sourcePath), outDir);
|
|
124
|
+
let outDirPath = path.resolve(targetPath, "..");
|
|
125
|
+
let mapPath = targetPath + ".map";
|
|
126
|
+
if (r.map) {
|
|
127
|
+
r.map.file = path.basename(targetPath);
|
|
128
|
+
r.map.sources = [path.relative(outputPath, sourcePath)];
|
|
129
|
+
if (!fs.existsSync(outDirPath))
|
|
130
|
+
fs.mkdirSync(outDirPath, { recursive: true });
|
|
131
|
+
fs.writeFileSync(mapPath, JSON.stringify(r.map));
|
|
132
|
+
// r.code += `\n//babel file path = ${bablePath}`;
|
|
133
|
+
r.code += "\n//# sourceMappingURL=" + path.basename(mapPath);
|
|
134
|
+
}
|
|
135
|
+
else if (fs.existsSync(mapPath)) {
|
|
136
|
+
fs.rmSync(targetPath);
|
|
137
|
+
}
|
|
138
|
+
if (!fs.existsSync(outDirPath))
|
|
139
|
+
fs.mkdirSync(outDirPath, { recursive: true });
|
|
140
|
+
fs.writeFileSync(targetPath, r.code);
|
|
141
|
+
}
|
|
142
|
+
function extname(file) {
|
|
143
|
+
// let ext = /\.[a-zA-Z]+/.exec(file)?.[0] || '';
|
|
144
|
+
let ext = path.extname(file);
|
|
145
|
+
return ext;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* 获取源文件所对应生成文件的扩展名
|
|
149
|
+
* @param file 源文件名
|
|
150
|
+
* */
|
|
151
|
+
// function fileOutExt(file: string) {
|
|
152
|
+
// let ext = extname(file);
|
|
153
|
+
// if (ext === ".ts")
|
|
154
|
+
// return outExt;
|
|
155
|
+
// if (ext === ".tsx")
|
|
156
|
+
// return outExt;
|
|
157
|
+
// return ext;
|
|
158
|
+
// }
|
|
159
|
+
class ImportPathRewrite {
|
|
160
|
+
filePath;
|
|
161
|
+
outExt;
|
|
162
|
+
constructor(filePath, node, outExt) {
|
|
163
|
+
this.filePath = filePath;
|
|
164
|
+
this.outExt = outExt;
|
|
165
|
+
this.traverse(node);
|
|
166
|
+
}
|
|
167
|
+
traverse(node) {
|
|
168
|
+
switch (node.type) {
|
|
169
|
+
case "Program":
|
|
170
|
+
node.body.forEach(c => this.traverse(c));
|
|
171
|
+
break;
|
|
172
|
+
case "File":
|
|
173
|
+
this.traverse(node.program);
|
|
174
|
+
break;
|
|
175
|
+
case "ImportDeclaration":
|
|
176
|
+
if (node.source.type === "StringLiteral" && node.source.value.startsWith(".")) {
|
|
177
|
+
let ext = extname(node.source.value);
|
|
178
|
+
if (ext != this.outExt) {
|
|
179
|
+
let dir = path.dirname(this.filePath);
|
|
180
|
+
let fullPath = path.join(dir, node.source.value);
|
|
181
|
+
console.log(`ImportDeclaration: ${fullPath} -> ${ext}`);
|
|
182
|
+
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
|
|
183
|
+
node.source.value = node.source.value + "/index";
|
|
184
|
+
}
|
|
185
|
+
node.source.value += this.outExt;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -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, JavaScriptExtension } from '../../types';
|
|
3
|
+
export declare function create(defaultCompilerOptions?: ts.CompilerOptions, outExt?: JavaScriptExtension): 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,49 +15,72 @@ 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"));
|
|
30
40
|
const errors_1 = require("../errors");
|
|
31
41
|
const fs = __importStar(require("fs"));
|
|
32
42
|
const path = __importStar(require("path"));
|
|
33
|
-
const project_compiler_1 = require("../project-compiler");
|
|
34
43
|
const TS_CONFIG_FILE = 'tsconfig.json';
|
|
44
|
+
function create(defaultCompilerOptions, outExt) {
|
|
45
|
+
outExt = outExt || ".js";
|
|
46
|
+
const action = async (filePath, outDir, projectPath) => {
|
|
47
|
+
if (defaultCompilerOptions) {
|
|
48
|
+
return compileFile(filePath, outDir, projectPath, defaultCompilerOptions, outExt);
|
|
49
|
+
}
|
|
50
|
+
// 如果没有指定默认编译选项,则尝试从项目目录查找 tsconfig.json 文件,并使用其中的配置进行编译
|
|
51
|
+
const compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
|
|
52
|
+
return compileFile(filePath, outDir, projectPath, compilerOptions, outExt);
|
|
53
|
+
};
|
|
54
|
+
return action;
|
|
55
|
+
}
|
|
35
56
|
/**
|
|
36
57
|
* 编译特定文件,并生成到制定目录
|
|
37
58
|
* @param filePath 源文件路径,绝对路径
|
|
38
59
|
* @param outDir 输出目录,相对于项目路径
|
|
39
60
|
* @param projectPath 项目路径,绝对路径
|
|
40
61
|
* */
|
|
41
|
-
async function compileFile(filePath, outDir, projectPath) {
|
|
62
|
+
async function compileFile(filePath, outDir, projectPath, compilerOptions, outExt) {
|
|
42
63
|
if (!filePath)
|
|
43
64
|
throw errors_1.errors.argumentNull('sourcePath');
|
|
44
65
|
if (!outDir)
|
|
45
66
|
throw errors_1.errors.argumentNull('outDir');
|
|
46
67
|
if (!projectPath)
|
|
47
68
|
throw errors_1.errors.argumentNull('projectPath');
|
|
48
|
-
if (!path.isAbsolute(filePath))
|
|
49
|
-
throw errors_1.errors.notAbsolutePath(filePath);
|
|
50
69
|
let content = fs.readFileSync(filePath, 'utf-8');
|
|
51
|
-
let compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
|
|
70
|
+
// let compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
|
|
52
71
|
let result = ts.transpileModule(content, { compilerOptions });
|
|
53
|
-
console.log(result.outputText);
|
|
54
72
|
let ext = path.extname(filePath);
|
|
55
|
-
let outExt = project_compiler_1.ProjectCompiler.tsOutExt;
|
|
56
73
|
let targetPath = path.join(projectPath, outDir, path.basename(filePath).replace(ext, outExt));
|
|
57
74
|
let outDirPath = path.resolve(targetPath, "..");
|
|
58
75
|
if (!fs.existsSync(outDirPath))
|
|
59
76
|
fs.mkdirSync(outDirPath, { recursive: true });
|
|
60
77
|
fs.writeFileSync(targetPath, result.outputText);
|
|
78
|
+
// 编译声明文件
|
|
79
|
+
if (compilerOptions.declaration && result.outputText) {
|
|
80
|
+
const d = ts.transpileDeclaration(content, { compilerOptions });
|
|
81
|
+
const declPath = targetPath.replace(outExt, '.d.ts');
|
|
82
|
+
fs.writeFileSync(declPath, d.outputText);
|
|
83
|
+
}
|
|
61
84
|
}
|
|
62
85
|
function getTypescriptConfig(projectPath, directoryPath) {
|
|
63
86
|
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,14 @@ 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
|
-
let
|
|
71
|
-
let action = fileActions[ext];
|
|
129
|
+
let action = fileActions?.(filePath) || configs_1.default.fileActions[ext];
|
|
72
130
|
if (action) {
|
|
73
131
|
action(filePath, outDir, projectPath);
|
|
74
132
|
if (callback) {
|
|
@@ -81,42 +139,38 @@ function generateCode(sourceDir, outDir, projectPath, callback) {
|
|
|
81
139
|
let fullPath = path.join(sourceDir, dir);
|
|
82
140
|
let outDirPath = path.join(outDir, dir);
|
|
83
141
|
if (fs.statSync(fullPath).isDirectory()) {
|
|
84
|
-
|
|
142
|
+
generateCodeByOptions({ sourceDir: fullPath, outDir: outDirPath, projectPath: projectPath, skipFiles, fileAction: fileActions, callback });
|
|
85
143
|
}
|
|
86
144
|
}
|
|
87
145
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
146
|
+
function watchDirectoryByOptions(options) {
|
|
147
|
+
let { sourceDir, outDir, projectPath, skipFiles, fileAction: fileActions, callback } = options;
|
|
148
|
+
(0, node_watch_1.default)(sourceDir, { recursive: true }, async (evt, name) => {
|
|
149
|
+
const filePath = name.replace(/\\/g, "/");
|
|
150
|
+
sourceDir = sourceDir.replace(/\\/g, "/");
|
|
151
|
+
outDir = outDir.replace(/\\/g, "/");
|
|
152
|
+
const ext = extname(filePath);
|
|
153
|
+
skipFiles = skipFiles || configs_1.default.skipFiles[ext] || [];
|
|
154
|
+
let isSkip = skipFiles.some(pattern => pattern.test(filePath)); //isIgnoredFile(filePath);//skipFiles.some(pattern => new RegExp(pattern).test(filePath));
|
|
155
|
+
if (isSkip) {
|
|
156
|
+
console.log(`Skip ${filePath}`);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (evt === "remove") {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
let action = fileActions?.(filePath) || configs_1.default.fileActions[ext];
|
|
99
163
|
if (!action) {
|
|
100
164
|
return;
|
|
101
165
|
}
|
|
102
166
|
try {
|
|
103
|
-
|
|
167
|
+
let outPath = path.dirname(filePath).replace(sourceDir, outDir);
|
|
168
|
+
action(filePath, outPath, projectPath);
|
|
104
169
|
if (callback)
|
|
105
|
-
callback(
|
|
170
|
+
callback(filePath, outPath, projectPath);
|
|
106
171
|
}
|
|
107
172
|
catch (e) {
|
|
108
173
|
console.error(e);
|
|
109
174
|
}
|
|
110
175
|
});
|
|
111
176
|
}
|
|
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];
|