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,9 +1,32 @@
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;
@@ -80,7 +103,6 @@ async function compileFile(sourcePath, outputPath, projectPath) {
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);
@@ -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
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  const errors_1 = require("../errors");
4
- const fs = require("fs");
5
- const path = require("path");
27
+ const fs = __importStar(require("fs"));
28
+ const path = __importStar(require("path"));
6
29
  let copyFile = (filePath, outPath, projectPath) => {
7
30
  if (!filePath)
8
31
  throw errors_1.errors.argumentNull("filePath");
9
32
  if (!outPath)
10
33
  throw errors_1.errors.argumentNull("outPath");
34
+ if (!fs.existsSync(filePath)) {
35
+ return;
36
+ }
11
37
  let out = filePath.replace(path.dirname(filePath), outPath);
12
38
  let outDirPath = path.resolve(out, "..");
13
39
  fs.mkdirSync(outDirPath, { recursive: true });
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 编译特定Less文件,并生成到指定目录
3
+ * @param filePath 源文件路径,绝对路径
4
+ * @param outDir 输出目录,相对于项目路径
5
+ * @param projectPath 项目路径,绝对路径
6
+ */
7
+ declare const compileFile: (filePath: string, outDir: string, projectPath: string) => Promise<void>;
8
+ export default compileFile;
@@ -0,0 +1,33 @@
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 less_1 = __importDefault(require("less"));
7
+ const errors_1 = require("../errors");
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const path_1 = __importDefault(require("path"));
10
+ /**
11
+ * 编译特定Less文件,并生成到指定目录
12
+ * @param filePath 源文件路径,绝对路径
13
+ * @param outDir 输出目录,相对于项目路径
14
+ * @param projectPath 项目路径,绝对路径
15
+ */
16
+ const compileFile = async (filePath, outDir, projectPath) => {
17
+ if (!filePath)
18
+ throw errors_1.errors.argumentNull('filePath');
19
+ if (!outDir)
20
+ throw errors_1.errors.argumentNull('outDir');
21
+ if (!projectPath)
22
+ throw errors_1.errors.argumentNull('projectPath');
23
+ let content = fs_1.default.readFileSync(filePath, 'utf-8');
24
+ let result = await less_1.default.render(content, { filename: filePath });
25
+ let ext = path_1.default.extname(filePath);
26
+ let outExt = '.css';
27
+ let targetPath = path_1.default.join(projectPath, outDir, path_1.default.basename(filePath).replace(ext, outExt));
28
+ let outDirPath = path_1.default.resolve(targetPath, "..");
29
+ if (!fs_1.default.existsSync(outDirPath))
30
+ fs_1.default.mkdirSync(outDirPath, { recursive: true });
31
+ fs_1.default.writeFileSync(targetPath, result.css);
32
+ };
33
+ exports.default = compileFile;
@@ -0,0 +1,9 @@
1
+ import type { FileAction } from '../../types';
2
+ /**
3
+ * 编译特定SCSS文件,并生成到指定目录
4
+ * @param filePath 源文件路径,绝对路径
5
+ * @param outDir 输出目录,相对于项目路径
6
+ * @param projectPath 项目路径,绝对路径
7
+ */
8
+ export declare const compileFile: FileAction;
9
+ export default compileFile;
@@ -0,0 +1,55 @@
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 = void 0;
27
+ const sass = __importStar(require("sass"));
28
+ const errors_1 = require("../errors");
29
+ const fs = __importStar(require("fs"));
30
+ const path = __importStar(require("path"));
31
+ /**
32
+ * 编译特定SCSS文件,并生成到指定目录
33
+ * @param filePath 源文件路径,绝对路径
34
+ * @param outDir 输出目录,相对于项目路径
35
+ * @param projectPath 项目路径,绝对路径
36
+ */
37
+ const compileFile = (filePath, outDir, projectPath) => {
38
+ if (!filePath)
39
+ throw errors_1.errors.argumentNull('filePath');
40
+ if (!outDir)
41
+ throw errors_1.errors.argumentNull('outDir');
42
+ if (!projectPath)
43
+ throw errors_1.errors.argumentNull('projectPath');
44
+ let content = fs.readFileSync(filePath, 'utf-8');
45
+ let result = sass.renderSync({ data: content });
46
+ let ext = path.extname(filePath);
47
+ let outExt = '.css';
48
+ let targetPath = path.join(projectPath, outDir, path.basename(filePath).replace(ext, outExt));
49
+ let outDirPath = path.resolve(targetPath, "..");
50
+ if (!fs.existsSync(outDirPath))
51
+ fs.mkdirSync(outDirPath, { recursive: true });
52
+ fs.writeFileSync(targetPath, result.css.toString());
53
+ };
54
+ exports.compileFile = compileFile;
55
+ exports.default = exports.compileFile;
@@ -1,10 +1,35 @@
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.findTypeScriptConfigPath = exports.getTypescriptConfig = exports.compileFile = void 0;
4
- const ts = require("typescript");
26
+ exports.compileFile = compileFile;
27
+ exports.getTypescriptConfig = getTypescriptConfig;
28
+ exports.findTypeScriptConfigPath = findTypeScriptConfigPath;
29
+ const ts = __importStar(require("typescript"));
5
30
  const errors_1 = require("../errors");
6
- const fs = require("fs");
7
- const path = require("path");
31
+ const fs = __importStar(require("fs"));
32
+ const path = __importStar(require("path"));
8
33
  const project_compiler_1 = require("../project-compiler");
9
34
  const TS_CONFIG_FILE = 'tsconfig.json';
10
35
  /**
@@ -34,7 +59,6 @@ async function compileFile(filePath, outDir, projectPath) {
34
59
  fs.mkdirSync(outDirPath, { recursive: true });
35
60
  fs.writeFileSync(targetPath, result.outputText);
36
61
  }
37
- exports.compileFile = compileFile;
38
62
  function getTypescriptConfig(projectPath, directoryPath) {
39
63
  let configPath = findTypeScriptConfigPath(projectPath, directoryPath);
40
64
  let compilerOptions;
@@ -48,7 +72,6 @@ function getTypescriptConfig(projectPath, directoryPath) {
48
72
  }
49
73
  return compilerOptions;
50
74
  }
51
- exports.getTypescriptConfig = getTypescriptConfig;
52
75
  function findTypeScriptConfigPath(projectPath, directoryPath) {
53
76
  if (!path.isAbsolute(directoryPath)) {
54
77
  directoryPath = path.join(projectPath, directoryPath);
@@ -64,7 +87,6 @@ function findTypeScriptConfigPath(projectPath, directoryPath) {
64
87
  let parentPath = path.resolve(directoryPath, "..");
65
88
  return findTypeScriptConfigPath(projectPath, parentPath);
66
89
  }
67
- exports.findTypeScriptConfigPath = findTypeScriptConfigPath;
68
90
  function defaultCompilerOptions() {
69
91
  let r = {
70
92
  module: ts.ModuleKind.CommonJS,
@@ -1,5 +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;
4
+ export declare function watchDirectory(sourceRoot: string, outRoot: string, projectPath: string, callback?: (filePath: string, outDir: string, projectPath: string) => void): void;
5
5
  export declare function isIgnoredFile(filePath: string): boolean;
@@ -1,21 +1,53 @@
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.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
36
  const biel_compile_1 = require("./actions/biel-compile");
9
- const copy_file_1 = require("./actions/copy-file");
37
+ const copy_file_1 = __importDefault(require("./actions/copy-file"));
38
+ const scss_compile_1 = __importDefault(require("./actions/scss-compile"));
39
+ const less_compile_1 = __importDefault(require("./actions/less-compile"));
10
40
  const skipFiles = ["\\S+\\.d\\.tsx?$", "\\S+\\.test\\.tsx?$", "\\S+\\.spec\\.tsx?$"];
11
- const extCopy = [".js", ".html", ".css", ".jpg", ".png", ".gif"];
41
+ const extCopy = [".js", ".html", ".css", ".jpg", ".png", ".gif", ".less"];
12
42
  let fileActions = {
13
43
  ".ts": biel_compile_1.compileFile,
14
44
  ".tsx": biel_compile_1.compileFile,
45
+ ".scss": scss_compile_1.default,
46
+ ".less": less_compile_1.default,
15
47
  };
16
48
  extCopy.forEach(ext => fileActions[ext] = copy_file_1.default);
17
49
  /** 将 sourceDir 目录下所有文件生成到 outDir */
18
- function generateCode(sourceDir, outDir, projectPath) {
50
+ function generateCode(sourceDir, outDir, projectPath, callback) {
19
51
  if (!sourceDir)
20
52
  throw errors_1.errors.argumentNull("sourceDir");
21
53
  if (!outDir)
@@ -39,6 +71,9 @@ function generateCode(sourceDir, outDir, projectPath) {
39
71
  let action = fileActions[ext];
40
72
  if (action) {
41
73
  action(filePath, outDir, projectPath);
74
+ if (callback) {
75
+ callback(filePath, outDir, projectPath);
76
+ }
42
77
  }
43
78
  }
44
79
  let dirs = fs.readdirSync(sourceDir);
@@ -50,9 +85,8 @@ function generateCode(sourceDir, outDir, projectPath) {
50
85
  }
51
86
  }
52
87
  }
53
- exports.generateCode = generateCode;
54
88
  /** 监听 sourceRoot 目录下所有文件变化,并生成到 outRoot */
55
- function watchDirectory(sourceRoot, outRoot, projectPath) {
89
+ function watchDirectory(sourceRoot, outRoot, projectPath, callback) {
56
90
  if (!sourceRoot)
57
91
  throw errors_1.errors.argumentNull("sourceRoot");
58
92
  if (!outRoot)
@@ -62,27 +96,25 @@ function watchDirectory(sourceRoot, outRoot, projectPath) {
62
96
  (0, node_watch_1.default)(sourceRoot, { recursive: true }, async (evt, name) => {
63
97
  let action = fileActions[extname(name)];
64
98
  let outPath = path.dirname(name).replace(sourceRoot, outRoot);
65
- if (action) {
99
+ if (!action) {
100
+ return;
101
+ }
102
+ try {
66
103
  action(name, outPath, projectPath);
104
+ if (callback)
105
+ callback(name, outPath, projectPath);
106
+ }
107
+ catch (e) {
108
+ console.error(e);
67
109
  }
68
110
  });
69
111
  }
70
- exports.watchDirectory = watchDirectory;
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
- // }
79
112
  function isIgnoredFile(filePath) {
80
113
  if (!filePath)
81
114
  throw errors_1.errors.argumentNull("filePath");
82
115
  let isSkip = skipFiles.some(pattern => new RegExp(pattern).test(filePath));
83
116
  return isSkip;
84
117
  }
85
- exports.isIgnoredFile = isIgnoredFile;
86
118
  function extname(file) {
87
119
  // let ext = /\.[a-zA-Z]+/.exec(file)?.[0] || '';
88
120
  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,19 +1,7 @@
1
1
  import * as babel from "@babel/core";
2
2
  export declare class ProjectCompiler {
3
- private projectPath;
4
- private sourceDirectoryNanme;
5
- private outputDirectoryName;
6
- private fileActions;
7
3
  private skipFiles;
8
4
  static tsOutExt: string;
9
- constructor(projectPath: string, sourceDirectoryNanme: string, outputDirectoryName: string);
10
- /**
11
- * 编译特定文件,并生成到制定目录
12
- * @param sourcePath 源文件路径
13
- * @param outDir 输出目录
14
- * */
15
- compileFile(filePath: string, outDir: string): Promise<void>;
16
- private compileTypeScriptFileByBabel;
17
5
  static loadBabelConfig(configPath: string): babel.TransformOptions;
18
6
  static findBabelConfigPath(projectPath: string, directoryPath: string): string | null;
19
7
  static getBabelConfig(projectPath: string, directoryPath: string): {
@@ -21,14 +9,4 @@ export declare class ProjectCompiler {
21
9
  path: string;
22
10
  };
23
11
  static getDefaultBabelConfig(): babel.TransformOptions;
24
- /**
25
- * 获取源文件所对应生成文件的扩展名
26
- * @param file 源文件名
27
- * */
28
- private fileOutExt;
29
- private isIgnoredFile;
30
- generateCode(): void;
31
- generateCode(sourceDir: string, outDir: string): void;
32
- watchDirectory(): void;
33
- run(): void;
34
12
  }
@@ -1,99 +1,36 @@
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.ProjectCompiler = void 0;
4
- const nodemon = require("nodemon");
5
27
  const errors_1 = require("./errors");
6
- const fs = require("fs");
7
- const path = require("path");
8
- const babel = require("@babel/core");
9
- const import_path_rewrite_1 = require("./import-path-rewrite");
10
- const node_watch_1 = require("node-watch");
28
+ const fs = __importStar(require("fs"));
29
+ const path = __importStar(require("path"));
11
30
  const tsOutExt = ".js";
12
31
  class ProjectCompiler {
13
- projectPath;
14
- sourceDirectoryNanme;
15
- outputDirectoryName;
16
- fileActions;
17
32
  skipFiles = ["\\S+\\.d\\.tsx?$", "\\S+\\.test\\.tsx?$", "\\S+\\.spec\\.tsx?$"];
18
33
  static tsOutExt = tsOutExt;
19
- constructor(projectPath, sourceDirectoryNanme, outputDirectoryName) {
20
- this.projectPath = projectPath;
21
- this.sourceDirectoryNanme = sourceDirectoryNanme;
22
- this.outputDirectoryName = outputDirectoryName;
23
- if (!projectPath)
24
- throw errors_1.errors.argumentNull("projectPath");
25
- if (!sourceDirectoryNanme)
26
- throw errors_1.errors.argumentNull("sourceDirectoryNanme");
27
- if (!outputDirectoryName)
28
- throw errors_1.errors.argumentNull("outputDirectoryName");
29
- if (!path.isAbsolute(projectPath))
30
- throw errors_1.errors.notAbsolutePath(projectPath);
31
- let sourcePath = path.join(projectPath, sourceDirectoryNanme);
32
- if (!fs.existsSync(sourcePath))
33
- throw errors_1.errors.pathNotExists(sourcePath);
34
- this.fileActions = {
35
- ".ts": this.compileFile.bind(this),
36
- ".tsx": this.compileFile.bind(this),
37
- };
38
- }
39
- /**
40
- * 编译特定文件,并生成到制定目录
41
- * @param sourcePath 源文件路径
42
- * @param outDir 输出目录
43
- * */
44
- async compileFile(filePath, outDir) {
45
- this.compileTypeScriptFileByBabel(filePath, outDir);
46
- }
47
- async compileTypeScriptFileByBabel(sourcePath, outDir) {
48
- if (!sourcePath)
49
- throw errors_1.errors.argumentNull("sourcePath");
50
- if (!outDir)
51
- throw errors_1.errors.argumentNull("outDir");
52
- if (!fs.existsSync(sourcePath))
53
- throw errors_1.errors.pathNotExists(sourcePath);
54
- let sourceDir = path.dirname(sourcePath);
55
- let babelConfig = ProjectCompiler.getBabelConfig(this.projectPath, sourceDir);
56
- let babelOptions = babelConfig.options;
57
- let babelPath = babelConfig.path;
58
- babelOptions.filename = sourcePath;
59
- babelOptions.code = false;
60
- babelOptions.ast = true;
61
- let fileResult = babel.transformFileSync(sourcePath, babelOptions);
62
- if (!fileResult)
63
- throw errors_1.errors.compileError(sourcePath);
64
- let ast = fileResult.ast;
65
- if (!ast)
66
- throw errors_1.errors.compileError(sourcePath);
67
- new import_path_rewrite_1.ImportPathRewrite(sourcePath, ast, tsOutExt);
68
- let r = babel.transformFromAstSync(ast, undefined, {
69
- filename: sourcePath, plugins: babelOptions.plugins,
70
- presets: babelOptions.presets, sourceMaps: true
71
- });
72
- if (!r || r.code == null)
73
- throw errors_1.errors.compileError(sourcePath);
74
- let ext = path.extname(sourcePath);
75
- let outExt = this.fileOutExt(sourcePath);
76
- let targetPath = path.join(outDir, path.basename(sourcePath).replace(ext, outExt));
77
- let outDirPath = path.resolve(targetPath, "..");
78
- if (r.map) {
79
- r.map.file = path.basename(targetPath);
80
- let sources = r.map.sources || [];
81
- let sourceDir = path.dirname(sourcePath);
82
- sources.forEach((s, i) => {
83
- sources[i] = path.relative(outDirPath, path.join(sourceDir, s));
84
- });
85
- r.map.sources = sources;
86
- let mapPath = targetPath + ".map";
87
- if (!fs.existsSync(outDirPath))
88
- fs.mkdirSync(outDirPath, { recursive: true });
89
- fs.writeFileSync(mapPath, JSON.stringify(r.map));
90
- r.code += `\n//babelPath:${babelPath}`;
91
- r.code += "\n//# sourceMappingURL=" + path.basename(sourcePath);
92
- }
93
- if (!fs.existsSync(outDirPath))
94
- fs.mkdirSync(outDirPath, { recursive: true });
95
- fs.writeFileSync(targetPath, r.code);
96
- }
97
34
  static loadBabelConfig(configPath) {
98
35
  if (!configPath)
99
36
  throw errors_1.errors.argumentNull("configPath");
@@ -156,74 +93,5 @@ class ProjectCompiler {
156
93
  let babelOptions = { plugins, presets };
157
94
  return babelOptions;
158
95
  }
159
- /**
160
- * 获取源文件所对应生成文件的扩展名
161
- * @param file 源文件名
162
- * */
163
- fileOutExt(file) {
164
- let ext = path.extname(file);
165
- if (ext === ".ts")
166
- return tsOutExt;
167
- if (ext === ".tsx")
168
- return tsOutExt;
169
- return ext;
170
- }
171
- isIgnoredFile(filePath) {
172
- if (!filePath)
173
- throw errors_1.errors.argumentNull("filePath");
174
- let isSkip = this.skipFiles.some(pattern => new RegExp(pattern).test(filePath));
175
- return isSkip;
176
- }
177
- generateCode(sourceDir, outDir) {
178
- // if (!sourceDir) throw errors.argumentNull("sourceDir");
179
- // if (!outDir) throw errors.argumentNull("outDir");
180
- if (!sourceDir)
181
- sourceDir = path.join(this.projectPath, this.sourceDirectoryNanme);
182
- if (!outDir)
183
- outDir = path.join(this.projectPath, this.outputDirectoryName);
184
- let fileActions = this.fileActions;
185
- if (!fs.existsSync(sourceDir))
186
- throw errors_1.errors.pathNotExists(sourceDir);
187
- let files = fs.readdirSync(sourceDir);
188
- for (let file of files) {
189
- let filePath = path.join(sourceDir, file);
190
- if (!fs.statSync(filePath).isFile()) {
191
- continue;
192
- }
193
- let isSkip = this.isIgnoredFile(filePath); //skipFiles.some(pattern => new RegExp(pattern).test(filePath));
194
- if (isSkip) {
195
- console.log(`Skip ${filePath}`);
196
- continue;
197
- }
198
- let ext = path.extname(file);
199
- let action = fileActions[ext];
200
- if (action) {
201
- action(filePath, outDir);
202
- }
203
- }
204
- let dirs = fs.readdirSync(sourceDir);
205
- for (let dir of dirs) {
206
- let fullPath = path.join(sourceDir, dir);
207
- let outDirPath = path.join(outDir, dir);
208
- if (fs.statSync(fullPath).isDirectory()) {
209
- this.generateCode(fullPath, outDirPath);
210
- }
211
- }
212
- }
213
- watchDirectory() {
214
- (0, node_watch_1.default)(this.sourceDirectoryNanme, { recursive: true }, async (evt, name) => {
215
- let action = this.fileActions[path.extname(name)];
216
- let outPath = path.dirname(name).replace(this.sourceDirectoryNanme, this.outputDirectoryName);
217
- if (action) {
218
- action(name, outPath);
219
- }
220
- });
221
- }
222
- run() {
223
- nodemon({
224
- script: `./${this.outputDirectoryName}/main.js`,
225
- watch: [`./${this.outputDirectoryName}/`],
226
- });
227
- }
228
96
  }
229
97
  exports.ProjectCompiler = ProjectCompiler;
@@ -1 +1 @@
1
- export declare function run(args?: string[]): void;
1
+ export declare function run(): void;