maishu-scripts 1.0.2 → 1.2.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/main.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const core_1 = require("@nestjs/core");
4
+ const app_module_1 = require("./app.module");
5
+ async function bootstrap() {
6
+ const app = await core_1.NestFactory.create(app_module_1.AppModule);
7
+ await app.listen(3000);
8
+ }
9
+ bootstrap();
@@ -0,0 +1,7 @@
1
+ /**
2
+ * 编译特定文件,并生成到制定目录
3
+ * @param sourcePath 源文件路径
4
+ * @param outputPath 输出目录
5
+ * @param projectPath 项目路径
6
+ * */
7
+ export declare function compileFile(sourcePath: string, outputPath: string, projectPath: string): Promise<void>;
@@ -0,0 +1,131 @@
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 outputPath 输出目录
14
+ * @param projectPath 项目路径
15
+ * */
16
+ async function compileFile(sourcePath, outputPath, projectPath) {
17
+ if (!sourcePath)
18
+ throw errors_1.errors.argumentNull("sourcePath");
19
+ if (!outputPath)
20
+ throw errors_1.errors.argumentNull("outputPath");
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: babelOptions.sourceMaps
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(outputPath, path.basename(sourcePath).replace(ext, outExt)); //sourcePath.replace(new RegExp(ext + "$"), outExt).replace(path.dirname(sourcePath), outDir);
65
+ let outDirPath = path.resolve(targetPath, "..");
66
+ let mapPath = targetPath + ".map";
67
+ if (r.map) {
68
+ r.map.file = path.basename(targetPath);
69
+ r.map.sources = [path.relative(outputPath, sourcePath)];
70
+ if (!fs.existsSync(outDirPath))
71
+ fs.mkdirSync(outDirPath, { recursive: true });
72
+ fs.writeFileSync(mapPath, JSON.stringify(r.map));
73
+ r.code += `\n//babel file path = ${bablePath}`;
74
+ r.code += "\n//# sourceMappingURL=" + path.basename(mapPath);
75
+ }
76
+ else if (fs.existsSync(mapPath)) {
77
+ fs.rmSync(targetPath);
78
+ }
79
+ if (!fs.existsSync(outDirPath))
80
+ fs.mkdirSync(outDirPath, { recursive: true });
81
+ fs.writeFileSync(targetPath, r.code);
82
+ }
83
+ exports.compileFile = compileFile;
84
+ function extname(file) {
85
+ // let ext = /\.[a-zA-Z]+/.exec(file)?.[0] || '';
86
+ let ext = path.extname(file);
87
+ return ext;
88
+ }
89
+ /**
90
+ * 获取源文件所对应生成文件的扩展名
91
+ * @param file 源文件名
92
+ * */
93
+ function fileOutExt(file) {
94
+ let ext = extname(file);
95
+ if (ext === ".ts")
96
+ return outExt;
97
+ if (ext === ".tsx")
98
+ return outExt;
99
+ return ext;
100
+ }
101
+ class ImportPathRewrite {
102
+ filePath;
103
+ constructor(filePath, node) {
104
+ this.filePath = filePath;
105
+ this.traverse(node);
106
+ }
107
+ traverse(node) {
108
+ switch (node.type) {
109
+ case "Program":
110
+ node.body.forEach(c => this.traverse(c));
111
+ break;
112
+ case "File":
113
+ this.traverse(node.program);
114
+ break;
115
+ case "ImportDeclaration":
116
+ if (node.source.type === "StringLiteral" && node.source.value.startsWith(".")) {
117
+ let ext = extname(node.source.value);
118
+ if (ext != outExt) {
119
+ let dir = path.dirname(this.filePath);
120
+ let fullPath = path.join(dir, node.source.value);
121
+ console.log(`ImportDeclaration: ${fullPath} -> ${ext}`);
122
+ if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
123
+ node.source.value = node.source.value + "/index";
124
+ }
125
+ node.source.value += outExt;
126
+ }
127
+ }
128
+ break;
129
+ }
130
+ }
131
+ }
@@ -0,0 +1,3 @@
1
+ import { FileAction } from "../../types";
2
+ declare let copyFile: FileAction;
3
+ export default copyFile;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const errors_1 = require("../errors");
4
+ const fs = require("fs");
5
+ const path = require("path");
6
+ let copyFile = (filePath, outPath, projectPath) => {
7
+ if (!filePath)
8
+ throw errors_1.errors.argumentNull("filePath");
9
+ if (!outPath)
10
+ throw errors_1.errors.argumentNull("outPath");
11
+ let out = filePath.replace(path.dirname(filePath), outPath);
12
+ let outDirPath = path.resolve(out, "..");
13
+ fs.mkdirSync(outDirPath, { recursive: true });
14
+ fs.copyFileSync(filePath, out);
15
+ };
16
+ exports.default = copyFile;
@@ -0,0 +1,10 @@
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;
@@ -0,0 +1,74 @@
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
+ }
@@ -4,4 +4,4 @@
4
4
  * @param outDir 输出目录
5
5
  * @param projectPath 项目路径
6
6
  * */
7
- export declare function compileFile(sourcePath: string, outDir: string, projectPath?: string): Promise<void>;
7
+ export declare function compileFile(sourcePath: string, outDir: string, projectPath: string): Promise<void>;
@@ -18,6 +18,8 @@ async function compileFile(sourcePath, outDir, projectPath) {
18
18
  throw errors_1.errors.argumentNull("sourcePath");
19
19
  if (!outDir)
20
20
  throw errors_1.errors.argumentNull("outDir");
21
+ if (!projectPath)
22
+ throw errors_1.errors.argumentNull("projectPath");
21
23
  // if (!fs.existsSync(sourcePath)) throw errors.pathNotExists(sourcePath);
22
24
  if (!fs.existsSync(sourcePath)) {
23
25
  console.warn(`Path not exists: ${sourcePath}`);
@@ -0,0 +1,10 @@
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;
@@ -0,0 +1,74 @@
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
+ }
@@ -1,6 +1,5 @@
1
1
  /** 将 sourceDir 目录下所有文件生成到 outDir */
2
- export declare function generateCode(sourceDir: string, outDir: string, projectPath?: string): void;
2
+ export declare function generateCode(sourceDir: string, outDir: string, projectPath: string): void;
3
3
  /** 监听 sourceRoot 目录下所有文件变化,并生成到 outRoot */
4
- export declare function watchDirectory(sourceRoot: string, outRoot: string, projectPath?: string): void;
5
- export declare function copyFile(filePath: string, outDir: string): void;
4
+ export declare function watchDirectory(sourceRoot: string, outRoot: string, projectPath: string): void;
6
5
  export declare function isIgnoredFile(filePath: string): boolean;
@@ -1,23 +1,27 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isIgnoredFile = exports.copyFile = exports.watchDirectory = exports.generateCode = void 0;
3
+ exports.isIgnoredFile = exports.watchDirectory = exports.generateCode = void 0;
4
4
  const fs = require("fs");
5
5
  const node_watch_1 = require("node-watch");
6
6
  const path = require("path");
7
7
  const errors_1 = require("./errors");
8
- const biel_compile_1 = require("./compile/biel-compile");
8
+ const biel_compile_1 = require("./actions/biel-compile");
9
+ const copy_file_1 = require("./actions/copy-file");
9
10
  const skipFiles = ["\\S+\\.d\\.tsx?$", "\\S+\\.test\\.tsx?$", "\\S+\\.spec\\.tsx?$"];
10
- const outExt = ".js";
11
+ const extCopy = [".js", ".html", ".css", ".jpg", ".png", ".gif"];
11
12
  let fileActions = {
12
13
  ".ts": biel_compile_1.compileFile,
13
14
  ".tsx": biel_compile_1.compileFile,
14
15
  };
16
+ extCopy.forEach(ext => fileActions[ext] = copy_file_1.default);
15
17
  /** 将 sourceDir 目录下所有文件生成到 outDir */
16
18
  function generateCode(sourceDir, outDir, projectPath) {
17
19
  if (!sourceDir)
18
20
  throw errors_1.errors.argumentNull("sourceDir");
19
21
  if (!outDir)
20
22
  throw errors_1.errors.argumentNull("outDir");
23
+ if (!projectPath)
24
+ throw errors_1.errors.argumentNull("projectPath");
21
25
  if (!fs.existsSync(sourceDir))
22
26
  throw errors_1.errors.pathNotExists(sourceDir);
23
27
  let files = fs.readdirSync(sourceDir);
@@ -49,6 +53,12 @@ function generateCode(sourceDir, outDir, projectPath) {
49
53
  exports.generateCode = generateCode;
50
54
  /** 监听 sourceRoot 目录下所有文件变化,并生成到 outRoot */
51
55
  function watchDirectory(sourceRoot, outRoot, projectPath) {
56
+ if (!sourceRoot)
57
+ throw errors_1.errors.argumentNull("sourceRoot");
58
+ if (!outRoot)
59
+ throw errors_1.errors.argumentNull("outRoot");
60
+ if (!projectPath)
61
+ throw errors_1.errors.argumentNull("projectPath");
52
62
  (0, node_watch_1.default)(sourceRoot, { recursive: true }, async (evt, name) => {
53
63
  let action = fileActions[extname(name)];
54
64
  let outPath = path.dirname(name).replace(sourceRoot, outRoot);
@@ -58,17 +68,14 @@ function watchDirectory(sourceRoot, outRoot, projectPath) {
58
68
  });
59
69
  }
60
70
  exports.watchDirectory = watchDirectory;
61
- function copyFile(filePath, outDir) {
62
- if (!filePath)
63
- throw errors_1.errors.argumentNull("filePath");
64
- if (!outDir)
65
- throw errors_1.errors.argumentNull("outDir");
66
- let out = filePath.replace(path.dirname(filePath), outDir);
67
- let outDirPath = path.resolve(out, "..");
68
- fs.mkdirSync(outDirPath, { recursive: true });
69
- fs.copyFileSync(filePath, out);
70
- }
71
- exports.copyFile = copyFile;
71
+ // export function copyFile(filePath: string, outDir: string) {
72
+ // if (!filePath) throw errors.argumentNull("filePath");
73
+ // if (!outDir) throw errors.argumentNull("outDir");
74
+ // let out = filePath.replace(path.dirname(filePath), outDir);
75
+ // let outDirPath = path.resolve(out, "..");
76
+ // fs.mkdirSync(outDirPath, { recursive: true });
77
+ // fs.copyFileSync(filePath, out);
78
+ // }
72
79
  function isIgnoredFile(filePath) {
73
80
  if (!filePath)
74
81
  throw errors_1.errors.argumentNull("filePath");
@@ -88,7 +88,7 @@ class ProjectCompiler {
88
88
  fs.mkdirSync(outDirPath, { recursive: true });
89
89
  fs.writeFileSync(mapPath, JSON.stringify(r.map));
90
90
  r.code += `\n//babelPath:${babelPath}`;
91
- r.code += "\n//# sourceMappingURL=" + path.basename(mapPath);
91
+ r.code += "\n//# sourceMappingURL=" + path.basename(sourcePath);
92
92
  }
93
93
  if (!fs.existsSync(outDirPath))
94
94
  fs.mkdirSync(outDirPath, { recursive: true });
@@ -1 +1 @@
1
- export declare function run(): void;
1
+ export declare function run(args?: string[]): void;
@@ -2,10 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.run = void 0;
4
4
  const nodemon = require("nodemon");
5
- function run() {
5
+ function run(args) {
6
6
  nodemon({
7
- script: "./out/main.js",
7
+ script: "./out/main.js", //!args ? "./out/main.js" : "./out/main.js " + args,
8
8
  watch: ["./out/"],
9
+ args: args
9
10
  });
10
11
  }
11
12
  exports.run = run;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maishu-scripts",
3
- "version": "1.0.2",
3
+ "version": "1.2.0",
4
4
  "description": "用于对 node 项目进行代码生成,打包,运行",
5
5
  "dependencies": {
6
6
  "@babel/core": "^7.24.3",
@@ -10,12 +10,14 @@ const outExt = ProjectCompiler.tsOutExt;
10
10
  /**
11
11
  * 编译特定文件,并生成到制定目录
12
12
  * @param sourcePath 源文件路径
13
- * @param outDir 输出目录
13
+ * @param outputPath 输出目录
14
14
  * @param projectPath 项目路径
15
15
  * */
16
- export async function compileFile(sourcePath: string, outDir: string, projectPath?: string) {
16
+ export async function compileFile(sourcePath: string, outputPath: string, projectPath: string) {
17
17
  if (!sourcePath) throw errors.argumentNull("sourcePath");
18
- if (!outDir) throw errors.argumentNull("outDir");
18
+ if (!outputPath) throw errors.argumentNull("outputPath");
19
+ if (!projectPath) throw errors.argumentNull("projectPath");
20
+
19
21
  // if (!fs.existsSync(sourcePath)) throw errors.pathNotExists(sourcePath);
20
22
  if (!fs.existsSync(sourcePath)) {
21
23
  console.warn(`Path not exists: ${sourcePath}`);
@@ -57,25 +59,21 @@ export async function compileFile(sourcePath: string, outDir: string, projectPat
57
59
  new ImportPathRewrite(sourcePath, ast);
58
60
  let r = babel.transformFromAstSync(ast, undefined, {
59
61
  filename: sourcePath, plugins: babelOptions.plugins,
60
- presets: babelOptions.presets, sourceMaps: true
62
+ presets: babelOptions.presets, sourceMaps: babelOptions.sourceMaps
61
63
  });
62
64
  if (!r || r.code == null)
63
65
  throw errors.compileError(sourcePath);
64
66
 
65
67
  let ext = extname(sourcePath);
66
68
  let outExt = fileOutExt(sourcePath);
67
- let targetPath = path.join(outDir, path.basename(sourcePath).replace(ext, outExt));//sourcePath.replace(new RegExp(ext + "$"), outExt).replace(path.dirname(sourcePath), outDir);
69
+ let targetPath = path.join(outputPath, path.basename(sourcePath).replace(ext, outExt));//sourcePath.replace(new RegExp(ext + "$"), outExt).replace(path.dirname(sourcePath), outDir);
68
70
  let outDirPath = path.resolve(targetPath, "..");
69
71
 
72
+ let mapPath = targetPath + ".map";
70
73
  if (r.map) {
71
74
  r.map.file = path.basename(targetPath);
72
- let sources = r.map.sources || [];
73
- let sourceDir = path.dirname(sourcePath);
74
- sources.forEach((s, i) => {
75
- sources[i] = path.relative(outDirPath, path.join(sourceDir, s));
76
- });
77
- r.map.sources = sources;
78
- let mapPath = targetPath + ".map";
75
+ r.map.sources = [path.relative(outputPath, sourcePath)];
76
+
79
77
  if (!fs.existsSync(outDirPath))
80
78
  fs.mkdirSync(outDirPath, { recursive: true });
81
79
 
@@ -83,6 +81,9 @@ export async function compileFile(sourcePath: string, outDir: string, projectPat
83
81
  r.code += `\n//babel file path = ${bablePath}`;
84
82
  r.code += "\n//# sourceMappingURL=" + path.basename(mapPath);
85
83
  }
84
+ else if (fs.existsSync(mapPath)) {
85
+ fs.rmSync(targetPath);
86
+ }
86
87
 
87
88
  if (!fs.existsSync(outDirPath))
88
89
  fs.mkdirSync(outDirPath, { recursive: true });
@@ -0,0 +1,18 @@
1
+ import { FileAction } from "../../types";
2
+ import { errors } from "../errors";
3
+ import * as fs from "fs";
4
+ import * as path from "path";
5
+
6
+ let copyFile: FileAction = (filePath: string, outPath: string, projectPath: string) => {
7
+ if (!filePath) throw errors.argumentNull("filePath");
8
+ if (!outPath) throw errors.argumentNull("outPath");
9
+
10
+ let out = filePath.replace(path.dirname(filePath), outPath);
11
+ let outDirPath = path.resolve(out, "..");
12
+
13
+ fs.mkdirSync(outDirPath, { recursive: true });
14
+
15
+ fs.copyFileSync(filePath, out);
16
+ }
17
+
18
+ export default copyFile;
@@ -0,0 +1,39 @@
1
+ import { compileFile, findTypeScriptConfigPath, getTypescriptConfig } from './ts-compile';
2
+ import * as path from 'path';
3
+ import * as fs from 'fs';
4
+
5
+ describe('ts-compile', () => {
6
+
7
+ let projectPath = path.join(__dirname, '..', '..', '..', 'test');
8
+ let outDir = "out";
9
+
10
+ test('should compile typescript files', async () => {
11
+
12
+ let tsFile = path.join(__dirname, '..', '..', '..', 'test', 'src', 'main.ts');
13
+ expect(fs.existsSync(tsFile)).toBeTruthy();
14
+
15
+
16
+ let jsFile = path.join(projectPath, outDir, "main.js");
17
+ if (fs.existsSync(jsFile)) {
18
+ fs.rmSync(jsFile, { recursive: true, force: true });
19
+ }
20
+
21
+ await compileFile(tsFile, outDir, projectPath);
22
+ expect(fs.existsSync(jsFile));
23
+ })
24
+
25
+ test("should throw error if static path tsconfig.json not found", function () {
26
+
27
+ let scriptPath = findTypeScriptConfigPath(projectPath, "src/static");
28
+ expect(scriptPath).not.toBeNull();
29
+
30
+ })
31
+
32
+ test("design path tsconfig.json same as static path tsconfig.json", function () {
33
+
34
+ let scriptPath = findTypeScriptConfigPath(projectPath, "src/static/design");
35
+ expect(scriptPath).not.toBeNull();
36
+ expect(scriptPath).toBe(findTypeScriptConfigPath(projectPath, "src/static"));
37
+
38
+ })
39
+ });
@@ -0,0 +1,91 @@
1
+ import * as ts from 'typescript';
2
+ import { errors } from '../errors';
3
+ import * as fs from 'fs';
4
+ import * as path from 'path';
5
+ import { ProjectCompiler } from '../project-compiler';
6
+
7
+ const TS_CONFIG_FILE = 'tsconfig.json';
8
+
9
+ type TypeScriptProjectConfig = {
10
+ compilerOptions: ts.CompilerOptions;
11
+ }
12
+
13
+ /**
14
+ * 编译特定文件,并生成到制定目录
15
+ * @param filePath 源文件路径,绝对路径
16
+ * @param outDir 输出目录,相对于项目路径
17
+ * @param projectPath 项目路径,绝对路径
18
+ * */
19
+ export async function compileFile(filePath: string, outDir: string, projectPath: string) {
20
+ if (!filePath)
21
+ throw errors.argumentNull('sourcePath');
22
+ if (!outDir)
23
+ throw errors.argumentNull('outDir');
24
+ if (!projectPath)
25
+ throw errors.argumentNull('projectPath');
26
+
27
+ if (!path.isAbsolute(filePath))
28
+ throw errors.notAbsolutePath(filePath);
29
+
30
+
31
+ let content = fs.readFileSync(filePath, 'utf-8');
32
+ let compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
33
+ let result = ts.transpileModule(content, { compilerOptions });
34
+
35
+ console.log(result.outputText);
36
+
37
+ let ext = path.extname(filePath);
38
+ let outExt = ProjectCompiler.tsOutExt;
39
+ let targetPath = path.join(projectPath, outDir, path.basename(filePath).replace(ext, outExt));
40
+ let outDirPath = path.resolve(targetPath, "..");
41
+
42
+ if (!fs.existsSync(outDirPath))
43
+ fs.mkdirSync(outDirPath, { recursive: true });
44
+
45
+ fs.writeFileSync(targetPath, result.outputText);
46
+ }
47
+
48
+ export function getTypescriptConfig(projectPath: string, directoryPath: string): ts.CompilerOptions {
49
+ let configPath = findTypeScriptConfigPath(projectPath, directoryPath);
50
+ let compilerOptions: ts.CompilerOptions;
51
+ if (!configPath) {
52
+ compilerOptions = defaultCompilerOptions();
53
+ }
54
+ else {
55
+ let configContent = fs.readFileSync(configPath, 'utf-8');
56
+ let config = JSON.parse(configContent) as TypeScriptProjectConfig;
57
+ compilerOptions = config.compilerOptions;
58
+ }
59
+
60
+ return compilerOptions;
61
+ }
62
+
63
+ export function findTypeScriptConfigPath(projectPath: string, directoryPath: string): string | null {
64
+ if (!path.isAbsolute(directoryPath)) {
65
+ directoryPath = path.join(projectPath, directoryPath);
66
+ }
67
+
68
+ console.assert(path.isAbsolute(projectPath), `projectPath ${projectPath} is not absolute`);
69
+ console.assert(path.isAbsolute(directoryPath), `directoryPath ${directoryPath} is not absolute`);
70
+ let tsConfigPath = path.join(directoryPath, TS_CONFIG_FILE);
71
+
72
+ if (fs.existsSync(tsConfigPath)) {
73
+ return tsConfigPath;
74
+ }
75
+
76
+ if (projectPath == directoryPath)
77
+ return null;
78
+
79
+ let parentPath = path.resolve(directoryPath, "..");
80
+
81
+ return findTypeScriptConfigPath(projectPath, parentPath);
82
+ }
83
+
84
+ function defaultCompilerOptions(): ts.CompilerOptions {
85
+ let r: ts.CompilerOptions = {
86
+ module: ts.ModuleKind.CommonJS,
87
+ target: ts.ScriptTarget.ESNext,
88
+ }
89
+
90
+ return r;
91
+ }
@@ -1,11 +1,11 @@
1
- import { copyFile, generateCode, isIgnoredFile } from "./compile";
1
+ import { generateCode, isIgnoredFile } from "./compile";
2
2
  import * as path from "path";
3
3
  import * as fs from "fs";
4
4
  import * as ts from "typescript";
5
5
 
6
6
  describe("Compile Test", function () {
7
7
 
8
- test("copyFile test", function () {
8
+ it("copyFile test", function () {
9
9
 
10
10
  let srcPath = "test/src";
11
11
  let srcExists = fs.existsSync(srcPath);
@@ -19,7 +19,9 @@ describe("Compile Test", function () {
19
19
  if (fs.existsSync(destPath)) {
20
20
  fs.rmdirSync(destPath, { recursive: true });
21
21
  }
22
- generateCode(srcPath, destPath);
22
+
23
+ let projectPath = path.join(__dirname, "..", "..");
24
+ generateCode(srcPath, destPath, projectPath);
23
25
  })
24
26
 
25
27
  test("isIgnored test", function () {
@@ -1,25 +1,27 @@
1
- import * as babel from "@babel/core";
2
1
  import * as fs from "fs";
3
2
  import watch from "node-watch";
4
3
  import * as path from "path";
5
4
  import { errors } from "./errors";
6
- import { ProjectCompiler } from "./project-compiler";
7
- import { compileFile as bielCompileFile } from "./compile/biel-compile";
5
+ import { compileFile as bielCompileFile } from "./actions/biel-compile";
6
+ import copyFile from "./actions/copy-file";
7
+ import { FileAction } from "../types";
8
8
 
9
9
  const skipFiles = ["\\S+\\.d\\.tsx?$", "\\S+\\.test\\.tsx?$", "\\S+\\.spec\\.tsx?$"]
10
- const outExt = ".js";
10
+ const extCopy = [".js", ".html", ".css", ".jpg", ".png", ".gif"];
11
11
 
12
- type FileAction = (filePath: string, outDir: string, projectPath?: string) => void;
13
12
  let fileActions: { [ext: string]: FileAction } = {
14
13
  ".ts": bielCompileFile,
15
14
  ".tsx": bielCompileFile,
16
15
  }
17
16
 
17
+ extCopy.forEach(ext => fileActions[ext] = copyFile);
18
+
18
19
  /** 将 sourceDir 目录下所有文件生成到 outDir */
19
- export function generateCode(sourceDir: string, outDir: string, projectPath?: string) {
20
+ export function generateCode(sourceDir: string, outDir: string, projectPath: string) {
20
21
 
21
22
  if (!sourceDir) throw errors.argumentNull("sourceDir");
22
23
  if (!outDir) throw errors.argumentNull("outDir");
24
+ if (!projectPath) throw errors.argumentNull("projectPath");
23
25
 
24
26
  if (!fs.existsSync(sourceDir))
25
27
  throw errors.pathNotExists(sourceDir);
@@ -55,7 +57,11 @@ export function generateCode(sourceDir: string, outDir: string, projectPath?: st
55
57
  }
56
58
 
57
59
  /** 监听 sourceRoot 目录下所有文件变化,并生成到 outRoot */
58
- export function watchDirectory(sourceRoot: string, outRoot: string, projectPath?: string) {
60
+ export function watchDirectory(sourceRoot: string, outRoot: string, projectPath: string) {
61
+ if (!sourceRoot) throw errors.argumentNull("sourceRoot");
62
+ if (!outRoot) throw errors.argumentNull("outRoot");
63
+ if (!projectPath) throw errors.argumentNull("projectPath");
64
+
59
65
  watch(sourceRoot, { recursive: true }, async (evt, name) => {
60
66
  let action = fileActions[extname(name)];
61
67
  let outPath = path.dirname(name).replace(sourceRoot, outRoot);
@@ -65,17 +71,17 @@ export function watchDirectory(sourceRoot: string, outRoot: string, projectPath?
65
71
  })
66
72
  }
67
73
 
68
- export function copyFile(filePath: string, outDir: string) {
69
- if (!filePath) throw errors.argumentNull("filePath");
70
- if (!outDir) throw errors.argumentNull("outDir");
74
+ // export function copyFile(filePath: string, outDir: string) {
75
+ // if (!filePath) throw errors.argumentNull("filePath");
76
+ // if (!outDir) throw errors.argumentNull("outDir");
71
77
 
72
- let out = filePath.replace(path.dirname(filePath), outDir);
73
- let outDirPath = path.resolve(out, "..");
78
+ // let out = filePath.replace(path.dirname(filePath), outDir);
79
+ // let outDirPath = path.resolve(out, "..");
74
80
 
75
- fs.mkdirSync(outDirPath, { recursive: true });
81
+ // fs.mkdirSync(outDirPath, { recursive: true });
76
82
 
77
- fs.copyFileSync(filePath, out);
78
- }
83
+ // fs.copyFileSync(filePath, out);
84
+ // }
79
85
 
80
86
  export function isIgnoredFile(filePath: string) {
81
87
  if (!filePath) throw errors.argumentNull("filePath");
@@ -1,10 +1,14 @@
1
1
  import * as path from "path";
2
2
  import * as fs from "fs";
3
3
  import { ProjectCompiler } from "./project-compiler";
4
+ import { errors } from "./errors";
4
5
  describe('project-compiler test', function () {
5
6
 
6
- let projectPath = path.join(__dirname, "..", "test");
7
- let projectCompiler = new ProjectCompiler(projectPath, "src", "out");
7
+ let projectPath = path.join(__dirname, "..", "..", "test");
8
+ if (fs.existsSync(projectPath) === false)
9
+ throw errors.pathNotExists(projectPath);
10
+
11
+ // let projectCompiler = new ProjectCompiler(projectPath, "src", "out");
8
12
 
9
13
  test("findBabelConfigPath test", function () {
10
14
  let staticPath = path.join(projectPath, "src", "static");
@@ -93,7 +93,7 @@ export class ProjectCompiler {
93
93
 
94
94
  fs.writeFileSync(mapPath, JSON.stringify(r.map));
95
95
  r.code += `\n//babelPath:${babelPath}`;
96
- r.code += "\n//# sourceMappingURL=" + path.basename(mapPath);
96
+ r.code += "\n//# sourceMappingURL=" + path.basename(sourcePath);
97
97
  }
98
98
 
99
99
  if (!fs.existsSync(outDirPath))
@@ -1,8 +1,9 @@
1
1
  import * as nodemon from "nodemon";
2
2
 
3
- export function run() {
3
+ export function run(args?: string[]) {
4
4
  nodemon({
5
- script: "./out/main.js",
5
+ script: "./out/main.js",//!args ? "./out/main.js" : "./out/main.js " + args,
6
6
  watch: ["./out/"],
7
+ args: args
7
8
  })
8
9
  }
package/src/types.d.ts ADDED
@@ -0,0 +1 @@
1
+ export type FileAction = (filePath: string, outPath: string, projectPath: string) => void;
package/src/start.ts DELETED
@@ -1,8 +0,0 @@
1
- import { generateCode, watchDirectory } from "./modules/compile";
2
- import { run } from "./modules/run";
3
-
4
- let sourceDir = "src";
5
- let outDir = "out";
6
- generateCode(sourceDir, outDir);
7
- watchDirectory(sourceDir, outDir);
8
- run();