maishu-scripts 1.2.0 → 1.3.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.
@@ -1,133 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compileFile = void 0;
4
- const babel = require("@babel/core");
5
- const fs = require("fs");
6
- const path = require("path");
7
- const errors_1 = require("../errors");
8
- const project_compiler_1 = require("../project-compiler");
9
- const outExt = project_compiler_1.ProjectCompiler.tsOutExt;
10
- /**
11
- * 编译特定文件,并生成到制定目录
12
- * @param sourcePath 源文件路径
13
- * @param outDir 输出目录
14
- * @param projectPath 项目路径
15
- * */
16
- async function compileFile(sourcePath, outDir, projectPath) {
17
- if (!sourcePath)
18
- throw errors_1.errors.argumentNull("sourcePath");
19
- if (!outDir)
20
- throw errors_1.errors.argumentNull("outDir");
21
- if (!projectPath)
22
- throw errors_1.errors.argumentNull("projectPath");
23
- // if (!fs.existsSync(sourcePath)) throw errors.pathNotExists(sourcePath);
24
- if (!fs.existsSync(sourcePath)) {
25
- console.warn(`Path not exists: ${sourcePath}`);
26
- return;
27
- }
28
- let sourceDir = path.dirname(sourcePath);
29
- let babelOptions;
30
- let bablePath;
31
- //= projectPath ?
32
- // ProjectCompiler.getBabelConfig(projectPath, sourceDir) : ProjectCompiler.getDefaultBabelConfig();
33
- if (projectPath) {
34
- let c = project_compiler_1.ProjectCompiler.getBabelConfig(projectPath, sourceDir);
35
- bablePath = c.path;
36
- babelOptions = c.options;
37
- }
38
- else {
39
- babelOptions = project_compiler_1.ProjectCompiler.getDefaultBabelConfig();
40
- bablePath = '';
41
- }
42
- babelOptions.filename = sourcePath;
43
- babelOptions.code = false;
44
- babelOptions.ast = true;
45
- // let fileResult = babel.transformFileSync(sourcePath, {
46
- // filename: sourcePath, code: false, ast: true, plugins,
47
- // presets
48
- // });
49
- let fileResult = babel.transformFileSync(sourcePath, babelOptions);
50
- if (!fileResult)
51
- throw errors_1.errors.compileError(sourcePath);
52
- let ast = fileResult.ast;
53
- if (!ast)
54
- throw errors_1.errors.compileError(sourcePath);
55
- new ImportPathRewrite(sourcePath, ast);
56
- let r = babel.transformFromAstSync(ast, undefined, {
57
- filename: sourcePath, plugins: babelOptions.plugins,
58
- presets: babelOptions.presets, sourceMaps: true
59
- });
60
- if (!r || r.code == null)
61
- throw errors_1.errors.compileError(sourcePath);
62
- let ext = extname(sourcePath);
63
- let outExt = fileOutExt(sourcePath);
64
- let targetPath = path.join(outDir, path.basename(sourcePath).replace(ext, outExt)); //sourcePath.replace(new RegExp(ext + "$"), outExt).replace(path.dirname(sourcePath), outDir);
65
- let outDirPath = path.resolve(targetPath, "..");
66
- if (r.map) {
67
- r.map.file = path.basename(targetPath);
68
- let sources = r.map.sources || [];
69
- let sourceDir = path.dirname(sourcePath);
70
- sources.forEach((s, i) => {
71
- sources[i] = path.relative(outDirPath, path.join(sourceDir, s));
72
- });
73
- r.map.sources = sources;
74
- let mapPath = targetPath + ".map";
75
- if (!fs.existsSync(outDirPath))
76
- fs.mkdirSync(outDirPath, { recursive: true });
77
- fs.writeFileSync(mapPath, JSON.stringify(r.map));
78
- r.code += `\n//babel file path = ${bablePath}`;
79
- r.code += "\n//# sourceMappingURL=" + path.basename(mapPath);
80
- }
81
- if (!fs.existsSync(outDirPath))
82
- fs.mkdirSync(outDirPath, { recursive: true });
83
- fs.writeFileSync(targetPath, r.code);
84
- }
85
- exports.compileFile = compileFile;
86
- function extname(file) {
87
- // let ext = /\.[a-zA-Z]+/.exec(file)?.[0] || '';
88
- let ext = path.extname(file);
89
- return ext;
90
- }
91
- /**
92
- * 获取源文件所对应生成文件的扩展名
93
- * @param file 源文件名
94
- * */
95
- function fileOutExt(file) {
96
- let ext = extname(file);
97
- if (ext === ".ts")
98
- return outExt;
99
- if (ext === ".tsx")
100
- return outExt;
101
- return ext;
102
- }
103
- class ImportPathRewrite {
104
- filePath;
105
- constructor(filePath, node) {
106
- this.filePath = filePath;
107
- this.traverse(node);
108
- }
109
- traverse(node) {
110
- switch (node.type) {
111
- case "Program":
112
- node.body.forEach(c => this.traverse(c));
113
- break;
114
- case "File":
115
- this.traverse(node.program);
116
- break;
117
- case "ImportDeclaration":
118
- if (node.source.type === "StringLiteral" && node.source.value.startsWith(".")) {
119
- let ext = extname(node.source.value);
120
- if (ext != outExt) {
121
- let dir = path.dirname(this.filePath);
122
- let fullPath = path.join(dir, node.source.value);
123
- console.log(`ImportDeclaration: ${fullPath} -> ${ext}`);
124
- if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
125
- node.source.value = node.source.value + "/index";
126
- }
127
- node.source.value += outExt;
128
- }
129
- }
130
- break;
131
- }
132
- }
133
- }
@@ -1,10 +0,0 @@
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>;
9
- export declare function getTypescriptConfig(projectPath: string, directoryPath: string): ts.CompilerOptions;
10
- export declare function findTypeScriptConfigPath(projectPath: string, directoryPath: string): string | null;
@@ -1,74 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.findTypeScriptConfigPath = exports.getTypescriptConfig = exports.compileFile = void 0;
4
- const ts = require("typescript");
5
- const errors_1 = require("../errors");
6
- const fs = require("fs");
7
- const path = require("path");
8
- const project_compiler_1 = require("../project-compiler");
9
- const TS_CONFIG_FILE = 'tsconfig.json';
10
- /**
11
- * 编译特定文件,并生成到制定目录
12
- * @param filePath 源文件路径,绝对路径
13
- * @param outDir 输出目录,相对于项目路径
14
- * @param projectPath 项目路径,绝对路径
15
- * */
16
- async function compileFile(filePath, outDir, projectPath) {
17
- if (!filePath)
18
- throw errors_1.errors.argumentNull('sourcePath');
19
- if (!outDir)
20
- throw errors_1.errors.argumentNull('outDir');
21
- if (!projectPath)
22
- throw errors_1.errors.argumentNull('projectPath');
23
- if (!path.isAbsolute(filePath))
24
- throw errors_1.errors.notAbsolutePath(filePath);
25
- let content = fs.readFileSync(filePath, 'utf-8');
26
- let compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
27
- let result = ts.transpileModule(content, { compilerOptions });
28
- console.log(result.outputText);
29
- let ext = path.extname(filePath);
30
- let outExt = project_compiler_1.ProjectCompiler.tsOutExt;
31
- let targetPath = path.join(projectPath, outDir, path.basename(filePath).replace(ext, outExt));
32
- let outDirPath = path.resolve(targetPath, "..");
33
- if (!fs.existsSync(outDirPath))
34
- fs.mkdirSync(outDirPath, { recursive: true });
35
- fs.writeFileSync(targetPath, result.outputText);
36
- }
37
- exports.compileFile = compileFile;
38
- function getTypescriptConfig(projectPath, directoryPath) {
39
- let configPath = findTypeScriptConfigPath(projectPath, directoryPath);
40
- let compilerOptions;
41
- if (!configPath) {
42
- compilerOptions = defaultCompilerOptions();
43
- }
44
- else {
45
- let configContent = fs.readFileSync(configPath, 'utf-8');
46
- let config = JSON.parse(configContent);
47
- compilerOptions = config.compilerOptions;
48
- }
49
- return compilerOptions;
50
- }
51
- exports.getTypescriptConfig = getTypescriptConfig;
52
- function findTypeScriptConfigPath(projectPath, directoryPath) {
53
- if (!path.isAbsolute(directoryPath)) {
54
- directoryPath = path.join(projectPath, directoryPath);
55
- }
56
- console.assert(path.isAbsolute(projectPath), `projectPath ${projectPath} is not absolute`);
57
- console.assert(path.isAbsolute(directoryPath), `directoryPath ${directoryPath} is not absolute`);
58
- let tsConfigPath = path.join(directoryPath, TS_CONFIG_FILE);
59
- if (fs.existsSync(tsConfigPath)) {
60
- return tsConfigPath;
61
- }
62
- if (projectPath == directoryPath)
63
- return null;
64
- let parentPath = path.resolve(directoryPath, "..");
65
- return findTypeScriptConfigPath(projectPath, parentPath);
66
- }
67
- exports.findTypeScriptConfigPath = findTypeScriptConfigPath;
68
- function defaultCompilerOptions() {
69
- let r = {
70
- module: ts.ModuleKind.CommonJS,
71
- target: ts.ScriptTarget.ESNext,
72
- };
73
- return r;
74
- }
package/out/start.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/out/start.js DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const compile_1 = require("./modules/compile");
4
- const run_1 = require("./modules/run");
5
- let sourceDir = "src";
6
- let outDir = "out";
7
- (0, compile_1.generateCode)(sourceDir, outDir);
8
- (0, compile_1.watchDirectory)(sourceDir, outDir);
9
- (0, run_1.run)();