maishu-scripts 1.1.2 → 1.2.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.
@@ -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>;
@@ -1,23 +1,48 @@
1
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 (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  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");
26
+ exports.compileFile = compileFile;
27
+ const babel = __importStar(require("@babel/core"));
28
+ const fs = __importStar(require("fs"));
29
+ const path = __importStar(require("path"));
7
30
  const errors_1 = require("../errors");
8
31
  const project_compiler_1 = require("../project-compiler");
9
32
  const outExt = project_compiler_1.ProjectCompiler.tsOutExt;
10
33
  /**
11
34
  * 编译特定文件,并生成到制定目录
12
35
  * @param sourcePath 源文件路径
13
- * @param outDir 输出目录
36
+ * @param outputPath 输出目录
14
37
  * @param projectPath 项目路径
15
38
  * */
16
- async function compileFile(sourcePath, outDir, projectPath) {
39
+ async function compileFile(sourcePath, outputPath, projectPath) {
17
40
  if (!sourcePath)
18
41
  throw errors_1.errors.argumentNull("sourcePath");
19
- if (!outDir)
20
- throw errors_1.errors.argumentNull("outDir");
42
+ if (!outputPath)
43
+ throw errors_1.errors.argumentNull("outputPath");
44
+ if (!projectPath)
45
+ throw errors_1.errors.argumentNull("projectPath");
21
46
  // if (!fs.existsSync(sourcePath)) throw errors.pathNotExists(sourcePath);
22
47
  if (!fs.existsSync(sourcePath)) {
23
48
  console.warn(`Path not exists: ${sourcePath}`);
@@ -53,34 +78,31 @@ async function compileFile(sourcePath, outDir, projectPath) {
53
78
  new ImportPathRewrite(sourcePath, ast);
54
79
  let r = babel.transformFromAstSync(ast, undefined, {
55
80
  filename: sourcePath, plugins: babelOptions.plugins,
56
- presets: babelOptions.presets, sourceMaps: true
81
+ presets: babelOptions.presets, sourceMaps: babelOptions.sourceMaps
57
82
  });
58
83
  if (!r || r.code == null)
59
84
  throw errors_1.errors.compileError(sourcePath);
60
85
  let ext = extname(sourcePath);
61
86
  let outExt = fileOutExt(sourcePath);
62
- let targetPath = path.join(outDir, path.basename(sourcePath).replace(ext, outExt)); //sourcePath.replace(new RegExp(ext + "$"), outExt).replace(path.dirname(sourcePath), outDir);
87
+ let targetPath = path.join(outputPath, path.basename(sourcePath).replace(ext, outExt)); //sourcePath.replace(new RegExp(ext + "$"), outExt).replace(path.dirname(sourcePath), outDir);
63
88
  let outDirPath = path.resolve(targetPath, "..");
89
+ let mapPath = targetPath + ".map";
64
90
  if (r.map) {
65
91
  r.map.file = path.basename(targetPath);
66
- let sources = r.map.sources || [];
67
- let sourceDir = path.dirname(sourcePath);
68
- sources.forEach((s, i) => {
69
- sources[i] = path.relative(outDirPath, path.join(sourceDir, s));
70
- });
71
- r.map.sources = sources;
72
- let mapPath = targetPath + ".map";
92
+ r.map.sources = [path.relative(outputPath, sourcePath)];
73
93
  if (!fs.existsSync(outDirPath))
74
94
  fs.mkdirSync(outDirPath, { recursive: true });
75
95
  fs.writeFileSync(mapPath, JSON.stringify(r.map));
76
96
  r.code += `\n//babel file path = ${bablePath}`;
77
97
  r.code += "\n//# sourceMappingURL=" + path.basename(mapPath);
78
98
  }
99
+ else if (fs.existsSync(mapPath)) {
100
+ fs.rmSync(targetPath);
101
+ }
79
102
  if (!fs.existsSync(outDirPath))
80
103
  fs.mkdirSync(outDirPath, { recursive: true });
81
104
  fs.writeFileSync(targetPath, r.code);
82
105
  }
83
- exports.compileFile = compileFile;
84
106
  function extname(file) {
85
107
  // let ext = /\.[a-zA-Z]+/.exec(file)?.[0] || '';
86
108
  let ext = path.extname(file);
@@ -0,0 +1,3 @@
1
+ import { FileAction } from "../../types";
2
+ declare let copyFile: FileAction;
3
+ export default copyFile;
@@ -0,0 +1,42 @@
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 (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const errors_1 = require("../errors");
27
+ const fs = __importStar(require("fs"));
28
+ const path = __importStar(require("path"));
29
+ let copyFile = (filePath, outPath, projectPath) => {
30
+ if (!filePath)
31
+ throw errors_1.errors.argumentNull("filePath");
32
+ if (!outPath)
33
+ throw errors_1.errors.argumentNull("outPath");
34
+ if (!fs.existsSync(filePath)) {
35
+ return;
36
+ }
37
+ let out = filePath.replace(path.dirname(filePath), outPath);
38
+ let outDirPath = path.resolve(out, "..");
39
+ fs.mkdirSync(outDirPath, { recursive: true });
40
+ fs.copyFileSync(filePath, out);
41
+ };
42
+ 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,96 @@
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 (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.compileFile = compileFile;
27
+ exports.getTypescriptConfig = getTypescriptConfig;
28
+ exports.findTypeScriptConfigPath = findTypeScriptConfigPath;
29
+ const ts = __importStar(require("typescript"));
30
+ const errors_1 = require("../errors");
31
+ const fs = __importStar(require("fs"));
32
+ const path = __importStar(require("path"));
33
+ const project_compiler_1 = require("../project-compiler");
34
+ const TS_CONFIG_FILE = 'tsconfig.json';
35
+ /**
36
+ * 编译特定文件,并生成到制定目录
37
+ * @param filePath 源文件路径,绝对路径
38
+ * @param outDir 输出目录,相对于项目路径
39
+ * @param projectPath 项目路径,绝对路径
40
+ * */
41
+ async function compileFile(filePath, outDir, projectPath) {
42
+ if (!filePath)
43
+ throw errors_1.errors.argumentNull('sourcePath');
44
+ if (!outDir)
45
+ throw errors_1.errors.argumentNull('outDir');
46
+ if (!projectPath)
47
+ throw errors_1.errors.argumentNull('projectPath');
48
+ if (!path.isAbsolute(filePath))
49
+ throw errors_1.errors.notAbsolutePath(filePath);
50
+ let content = fs.readFileSync(filePath, 'utf-8');
51
+ let compilerOptions = getTypescriptConfig(projectPath, path.dirname(filePath));
52
+ let result = ts.transpileModule(content, { compilerOptions });
53
+ console.log(result.outputText);
54
+ let ext = path.extname(filePath);
55
+ let outExt = project_compiler_1.ProjectCompiler.tsOutExt;
56
+ let targetPath = path.join(projectPath, outDir, path.basename(filePath).replace(ext, outExt));
57
+ let outDirPath = path.resolve(targetPath, "..");
58
+ if (!fs.existsSync(outDirPath))
59
+ fs.mkdirSync(outDirPath, { recursive: true });
60
+ fs.writeFileSync(targetPath, result.outputText);
61
+ }
62
+ function getTypescriptConfig(projectPath, directoryPath) {
63
+ let configPath = findTypeScriptConfigPath(projectPath, directoryPath);
64
+ let compilerOptions;
65
+ if (!configPath) {
66
+ compilerOptions = defaultCompilerOptions();
67
+ }
68
+ else {
69
+ let configContent = fs.readFileSync(configPath, 'utf-8');
70
+ let config = JSON.parse(configContent);
71
+ compilerOptions = config.compilerOptions;
72
+ }
73
+ return compilerOptions;
74
+ }
75
+ function findTypeScriptConfigPath(projectPath, directoryPath) {
76
+ if (!path.isAbsolute(directoryPath)) {
77
+ directoryPath = path.join(projectPath, directoryPath);
78
+ }
79
+ console.assert(path.isAbsolute(projectPath), `projectPath ${projectPath} is not absolute`);
80
+ console.assert(path.isAbsolute(directoryPath), `directoryPath ${directoryPath} is not absolute`);
81
+ let tsConfigPath = path.join(directoryPath, TS_CONFIG_FILE);
82
+ if (fs.existsSync(tsConfigPath)) {
83
+ return tsConfigPath;
84
+ }
85
+ if (projectPath == directoryPath)
86
+ return null;
87
+ let parentPath = path.resolve(directoryPath, "..");
88
+ return findTypeScriptConfigPath(projectPath, parentPath);
89
+ }
90
+ function defaultCompilerOptions() {
91
+ let r = {
92
+ module: ts.ModuleKind.CommonJS,
93
+ target: ts.ScriptTarget.ESNext,
94
+ };
95
+ return r;
96
+ }
@@ -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, callback?: (filePath: string, outDir: string, projectPath: string) => void): 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, callback?: (filePath: string, outDir: string, projectPath: string) => void): void;
6
5
  export declare function isIgnoredFile(filePath: string): boolean;
@@ -1,23 +1,55 @@
1
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 (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
2
28
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isIgnoredFile = exports.copyFile = exports.watchDirectory = exports.generateCode = void 0;
4
- const fs = require("fs");
5
- const node_watch_1 = require("node-watch");
6
- const path = require("path");
29
+ exports.generateCode = generateCode;
30
+ exports.watchDirectory = watchDirectory;
31
+ exports.isIgnoredFile = isIgnoredFile;
32
+ const fs = __importStar(require("fs"));
33
+ const node_watch_1 = __importDefault(require("node-watch"));
34
+ const path = __importStar(require("path"));
7
35
  const errors_1 = require("./errors");
8
- const biel_compile_1 = require("./compile/biel-compile");
36
+ const biel_compile_1 = require("./actions/biel-compile");
37
+ const copy_file_1 = __importDefault(require("./actions/copy-file"));
9
38
  const skipFiles = ["\\S+\\.d\\.tsx?$", "\\S+\\.test\\.tsx?$", "\\S+\\.spec\\.tsx?$"];
10
- const outExt = ".js";
39
+ const extCopy = [".js", ".html", ".css", ".jpg", ".png", ".gif", ".scss", ".less"];
11
40
  let fileActions = {
12
41
  ".ts": biel_compile_1.compileFile,
13
42
  ".tsx": biel_compile_1.compileFile,
14
43
  };
44
+ extCopy.forEach(ext => fileActions[ext] = copy_file_1.default);
15
45
  /** 将 sourceDir 目录下所有文件生成到 outDir */
16
- function generateCode(sourceDir, outDir, projectPath) {
46
+ function generateCode(sourceDir, outDir, projectPath, callback) {
17
47
  if (!sourceDir)
18
48
  throw errors_1.errors.argumentNull("sourceDir");
19
49
  if (!outDir)
20
50
  throw errors_1.errors.argumentNull("outDir");
51
+ if (!projectPath)
52
+ throw errors_1.errors.argumentNull("projectPath");
21
53
  if (!fs.existsSync(sourceDir))
22
54
  throw errors_1.errors.pathNotExists(sourceDir);
23
55
  let files = fs.readdirSync(sourceDir);
@@ -35,6 +67,9 @@ function generateCode(sourceDir, outDir, projectPath) {
35
67
  let action = fileActions[ext];
36
68
  if (action) {
37
69
  action(filePath, outDir, projectPath);
70
+ if (callback) {
71
+ callback(filePath, outDir, projectPath);
72
+ }
38
73
  }
39
74
  }
40
75
  let dirs = fs.readdirSync(sourceDir);
@@ -46,36 +81,44 @@ function generateCode(sourceDir, outDir, projectPath) {
46
81
  }
47
82
  }
48
83
  }
49
- exports.generateCode = generateCode;
50
84
  /** 监听 sourceRoot 目录下所有文件变化,并生成到 outRoot */
51
- function watchDirectory(sourceRoot, outRoot, projectPath) {
85
+ function watchDirectory(sourceRoot, outRoot, projectPath, callback) {
86
+ if (!sourceRoot)
87
+ throw errors_1.errors.argumentNull("sourceRoot");
88
+ if (!outRoot)
89
+ throw errors_1.errors.argumentNull("outRoot");
90
+ if (!projectPath)
91
+ throw errors_1.errors.argumentNull("projectPath");
52
92
  (0, node_watch_1.default)(sourceRoot, { recursive: true }, async (evt, name) => {
53
93
  let action = fileActions[extname(name)];
54
94
  let outPath = path.dirname(name).replace(sourceRoot, outRoot);
55
- if (action) {
95
+ if (!action) {
96
+ return;
97
+ }
98
+ try {
56
99
  action(name, outPath, projectPath);
100
+ if (callback)
101
+ callback(name, outPath, projectPath);
102
+ }
103
+ catch (e) {
104
+ console.error(e);
57
105
  }
58
106
  });
59
107
  }
60
- 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;
108
+ // export function copyFile(filePath: string, outDir: string) {
109
+ // if (!filePath) throw errors.argumentNull("filePath");
110
+ // if (!outDir) throw errors.argumentNull("outDir");
111
+ // let out = filePath.replace(path.dirname(filePath), outDir);
112
+ // let outDirPath = path.resolve(out, "..");
113
+ // fs.mkdirSync(outDirPath, { recursive: true });
114
+ // fs.copyFileSync(filePath, out);
115
+ // }
72
116
  function isIgnoredFile(filePath) {
73
117
  if (!filePath)
74
118
  throw errors_1.errors.argumentNull("filePath");
75
119
  let isSkip = skipFiles.some(pattern => new RegExp(pattern).test(filePath));
76
120
  return isSkip;
77
121
  }
78
- exports.isIgnoredFile = isIgnoredFile;
79
122
  function extname(file) {
80
123
  // let ext = /\.[a-zA-Z]+/.exec(file)?.[0] || '';
81
124
  let ext = path.extname(file);
@@ -1,8 +1,31 @@
1
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 (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  exports.ImportPathRewrite = void 0;
4
- const path = require("path");
5
- const fs = require("fs");
27
+ const path = __importStar(require("path"));
28
+ const fs = __importStar(require("fs"));
6
29
  // const outExt = ".js";
7
30
  class ImportPathRewrite {
8
31
  filePath;
@@ -1,13 +1,39 @@
1
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 (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
2
28
  Object.defineProperty(exports, "__esModule", { value: true });
3
29
  exports.ProjectCompiler = void 0;
4
- const nodemon = require("nodemon");
30
+ const nodemon_1 = __importDefault(require("nodemon"));
5
31
  const errors_1 = require("./errors");
6
- const fs = require("fs");
7
- const path = require("path");
8
- const babel = require("@babel/core");
32
+ const fs = __importStar(require("fs"));
33
+ const path = __importStar(require("path"));
34
+ const babel = __importStar(require("@babel/core"));
9
35
  const import_path_rewrite_1 = require("./import-path-rewrite");
10
- const node_watch_1 = require("node-watch");
36
+ const node_watch_1 = __importDefault(require("node-watch"));
11
37
  const tsOutExt = ".js";
12
38
  class ProjectCompiler {
13
39
  projectPath;
@@ -88,7 +114,7 @@ class ProjectCompiler {
88
114
  fs.mkdirSync(outDirPath, { recursive: true });
89
115
  fs.writeFileSync(mapPath, JSON.stringify(r.map));
90
116
  r.code += `\n//babelPath:${babelPath}`;
91
- r.code += "\n//# sourceMappingURL=" + path.basename(mapPath);
117
+ r.code += "\n//# sourceMappingURL=" + path.basename(sourcePath);
92
118
  }
93
119
  if (!fs.existsSync(outDirPath))
94
120
  fs.mkdirSync(outDirPath, { recursive: true });
@@ -220,7 +246,7 @@ class ProjectCompiler {
220
246
  });
221
247
  }
222
248
  run() {
223
- nodemon({
249
+ (0, nodemon_1.default)({
224
250
  script: `./${this.outputDirectoryName}/main.js`,
225
251
  watch: [`./${this.outputDirectoryName}/`],
226
252
  });
@@ -1,11 +1,14 @@
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 = void 0;
4
- const nodemon = require("nodemon");
6
+ exports.run = run;
7
+ const nodemon_1 = __importDefault(require("nodemon"));
5
8
  function run() {
6
- nodemon({
7
- script: "./out/main.js",
9
+ (0, nodemon_1.default)({
10
+ script: "./out/main.js", //!args ? "./out/main.js" : "./out/main.js " + args,
8
11
  watch: ["./out/"],
12
+ ignoreRoot: ['./out/static/'],
9
13
  });
10
14
  }
11
- exports.run = run;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maishu-scripts",
3
- "version": "1.1.2",
3
+ "version": "1.2.1",
4
4
  "description": "用于对 node 项目进行代码生成,打包,运行",
5
5
  "dependencies": {
6
6
  "@babel/core": "^7.24.3",
@@ -11,7 +11,7 @@
11
11
  "babel-plugin-transform-typescript-metadata": "^0.3.2",
12
12
  "maishu-toolkit": "^1.12.6",
13
13
  "node-watch": "^0.7.4",
14
- "nodemon": "^3.1.0",
14
+ "nodemon": "^3.1.4",
15
15
  "typescript": "^5.4.5"
16
16
  },
17
17
  "devDependencies": {
@@ -25,5 +25,8 @@
25
25
  "files": [
26
26
  "out",
27
27
  "src"
28
- ]
29
- }
28
+ ],
29
+ "scripts": {
30
+ "build": "tsc"
31
+ }
32
+ }
@@ -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,22 @@
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
+ if (!fs.existsSync(filePath)) {
11
+ return;
12
+ }
13
+
14
+ let out = filePath.replace(path.dirname(filePath), outPath);
15
+ let outDirPath = path.resolve(out, "..");
16
+
17
+ fs.mkdirSync(outDirPath, { recursive: true });
18
+
19
+ fs.copyFileSync(filePath, out);
20
+ }
21
+
22
+ 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,29 @@
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", ".scss", ".less"];
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,
21
+ callback?: (filePath: string, outDir: string, projectPath: string) => void
22
+ ) {
20
23
 
21
24
  if (!sourceDir) throw errors.argumentNull("sourceDir");
22
25
  if (!outDir) throw errors.argumentNull("outDir");
26
+ if (!projectPath) throw errors.argumentNull("projectPath");
23
27
 
24
28
  if (!fs.existsSync(sourceDir))
25
29
  throw errors.pathNotExists(sourceDir);
@@ -41,6 +45,9 @@ export function generateCode(sourceDir: string, outDir: string, projectPath?: st
41
45
  let action = fileActions[ext];
42
46
  if (action) {
43
47
  action(filePath, outDir, projectPath);
48
+ if (callback) {
49
+ callback(filePath, outDir, projectPath);
50
+ }
44
51
  }
45
52
  }
46
53
 
@@ -55,27 +62,40 @@ export function generateCode(sourceDir: string, outDir: string, projectPath?: st
55
62
  }
56
63
 
57
64
  /** 监听 sourceRoot 目录下所有文件变化,并生成到 outRoot */
58
- export function watchDirectory(sourceRoot: string, outRoot: string, projectPath?: string) {
65
+ export function watchDirectory(sourceRoot: string, outRoot: string, projectPath: string, callback?: (filePath: string, outDir: string, projectPath: string) => void) {
66
+ if (!sourceRoot) throw errors.argumentNull("sourceRoot");
67
+ if (!outRoot) throw errors.argumentNull("outRoot");
68
+ if (!projectPath) throw errors.argumentNull("projectPath");
69
+
59
70
  watch(sourceRoot, { recursive: true }, async (evt, name) => {
60
71
  let action = fileActions[extname(name)];
61
72
  let outPath = path.dirname(name).replace(sourceRoot, outRoot);
62
- if (action) {
73
+ if (!action) {
74
+ return;
75
+ }
76
+
77
+ try {
63
78
  action(name, outPath, projectPath);
79
+ if (callback)
80
+ callback(name, outPath, projectPath);
81
+ }
82
+ catch (e) {
83
+ console.error(e);
64
84
  }
65
85
  })
66
86
  }
67
87
 
68
- export function copyFile(filePath: string, outDir: string) {
69
- if (!filePath) throw errors.argumentNull("filePath");
70
- if (!outDir) throw errors.argumentNull("outDir");
88
+ // export function copyFile(filePath: string, outDir: string) {
89
+ // if (!filePath) throw errors.argumentNull("filePath");
90
+ // if (!outDir) throw errors.argumentNull("outDir");
71
91
 
72
- let out = filePath.replace(path.dirname(filePath), outDir);
73
- let outDirPath = path.resolve(out, "..");
92
+ // let out = filePath.replace(path.dirname(filePath), outDir);
93
+ // let outDirPath = path.resolve(out, "..");
74
94
 
75
- fs.mkdirSync(outDirPath, { recursive: true });
95
+ // fs.mkdirSync(outDirPath, { recursive: true });
76
96
 
77
- fs.copyFileSync(filePath, out);
78
- }
97
+ // fs.copyFileSync(filePath, out);
98
+ // }
79
99
 
80
100
  export function isIgnoredFile(filePath: string) {
81
101
  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");
@@ -1,4 +1,4 @@
1
- import * as nodemon from "nodemon";
1
+ import nodemon from "nodemon";
2
2
  import { errors } from "./errors";
3
3
  import * as fs from "fs";
4
4
  import * as path from "path";
@@ -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
- import * as nodemon from "nodemon";
1
+ import nodemon from "nodemon";
2
2
 
3
3
  export function run() {
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
+ ignoreRoot: ['./out/static/'],
7
8
  })
8
9
  }
package/src/types.d.ts ADDED
@@ -0,0 +1 @@
1
+ export type FileAction = (filePath: string, outPath: string, projectPath: string) => void;
@@ -1,7 +0,0 @@
1
- /**
2
- * 编译特定文件,并生成到制定目录
3
- * @param sourcePath 源文件路径
4
- * @param outDir 输出目录
5
- * @param projectPath 项目路径
6
- * */
7
- export declare function compileFile(sourcePath: string, outDir: string, projectPath?: string): Promise<void>;
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)();
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();