glyphix 1.0.32 → 1.0.33

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.
Files changed (2) hide show
  1. package/bin/glyphix.js +206 -113
  2. package/package.json +1 -1
package/bin/glyphix.js CHANGED
@@ -132,129 +132,222 @@ function buildGlyphix(args) {
132
132
  var import_esbuild = __toESM(require("esbuild"));
133
133
  var import_node_path3 = require("path");
134
134
  var import_node_fs = require("fs");
135
- var import_fs_extra2 = require("fs-extra");
136
- var import_eslint = require("eslint");
137
- var import_glob = require("glob");
138
- var import_chalk2 = __toESM(require("chalk"));
139
135
  var import_dotenv = __toESM(require("dotenv"));
140
136
  var import_lodash = require("lodash");
141
- function loadEnv() {
142
- const dotenvTags = [
143
- // 本地环境
144
- "development",
145
- // 测试环境
146
- // 比如:单元测试
147
- "test",
148
- // 部署环境
149
- // 比如:日常、预发、线上
150
- "production"
151
- ];
152
- if (!dotenvTags.includes(process.env.GLYPHIX_ENV ?? "")) {
153
- process.env.GLYPHIX_ENV = dotenvTags[0];
137
+ var import_node_child_process = require("child_process");
138
+ var GxGenerate = class {
139
+ constructor() {
140
+ // 执行 gx 命令的目录路径
141
+ this.workspaceRoot = process.cwd();
142
+ // 正在构建的应用的 src 目录路径
143
+ this.sourceRoot = (0, import_node_path3.resolve)(
144
+ this.workspaceRoot,
145
+ process.env["VITE_SOURCE_DIR"]
146
+ );
147
+ // 构建的 build 目录路径
148
+ this.buildRoot = (0, import_node_path3.resolve)(
149
+ this.workspaceRoot,
150
+ process.env["VITE_BUILD_DIR"]
151
+ );
152
+ // gx 工具传递过来的入口文件,包括 app.{t,j}s 和各个页面的 js 文件路径
153
+ this.entries = [];
154
+ const content = JSON.parse(
155
+ (0, import_node_fs.readFileSync)((0, import_node_path3.resolve)(this.buildRoot, "entry.vite.json")).toString()
156
+ );
157
+ this.entries = content.entry.map(
158
+ (item) => (0, import_node_path3.resolve)(this.buildRoot, item)
159
+ );
154
160
  }
155
- const dotenvPath = (0, import_node_path3.resolve)(".env");
156
- const dotenvFiles = [
157
- dotenvPath,
158
- `${dotenvPath}.local`,
159
- `${dotenvPath}.${process.env.GLYPHIX_ENV}`,
160
- `${dotenvPath}.${process.env.GLYPHIX_ENV}.local`
161
- ].filter(import_node_fs.existsSync);
162
- dotenvFiles.reverse().forEach((dotenvFile) => {
163
- import_dotenv.default.config({ path: dotenvFile });
164
- });
165
- }
166
- function dealResource() {
167
- if (!process.env["VITE_BUILD_DIR"])
168
- throw Error("not set glyphix project path");
169
- const rootPath = (0, import_node_path3.resolve)(process.cwd(), process.env["VITE_BUILD_DIR"]);
170
- const entryFileContent = JSON.parse(
171
- (0, import_node_fs.readFileSync)((0, import_node_path3.resolve)(rootPath, "entry.vite.json")).toString()
172
- );
173
- const entries = entryFileContent.entry;
174
- (0, import_fs_extra2.removeSync)((0, import_node_path3.resolve)(rootPath, ".vite-dist"));
175
- let entriedFullPath = entries.map((item) => (0, import_node_path3.resolve)(rootPath, item));
176
- return {
177
- root: rootPath,
178
- entry: entriedFullPath
179
- };
180
- }
181
- function generateEsbuildConfig(rootPath, entriedFullPath, envDefine) {
182
- const resolveExtensions = [".ts", ".js"];
183
- const internalConfig = {
184
- entryPoints: entriedFullPath,
185
- // 入口文件
186
- bundle: true,
187
- // 启用打包
188
- outdir: (0, import_node_path3.resolve)(rootPath, ".vite-dist"),
189
- // 输出目录
190
- splitting: true,
191
- // 启用代码分割
192
- format: "esm",
193
- // 输出为 ESM 格式
194
- resolveExtensions,
195
- external: ["@system*"],
196
- // 将 lodash 和 axios 作为外部依赖
197
- platform: "node",
198
- target: "node14",
199
- // 目标平台设置为最新 ECMAScript
200
- define: envDefine,
201
- sourcemap: true,
202
- minify: process.env.GLYPHIX_ENV === "development" ? false : true,
203
- plugins: [
204
- {
205
- name: "glyphix-resolve-path",
206
- setup(build2) {
207
- build2.onResolve({ filter: /^\// }, (args) => {
208
- if ((0, import_node_fs.existsSync)(args.path)) {
209
- return {
210
- path: args.path
211
- };
212
- }
213
- for (let i = 0; i < resolveExtensions.length; i++) {
214
- const fullPath = (0, import_node_path3.join)(
215
- rootPath,
216
- `${args.path}${resolveExtensions[i]}`
217
- );
218
- if ((0, import_node_fs.existsSync)(fullPath)) {
161
+ loadEnv() {
162
+ const dotenvTags = [
163
+ // 本地环境
164
+ "development",
165
+ // 测试环境
166
+ // 比如:单元测试
167
+ "test",
168
+ // 部署环境
169
+ // 比如:日常、预发、线上
170
+ "production"
171
+ ];
172
+ if (!dotenvTags.includes(process.env.GLYPHIX_ENV ?? "")) {
173
+ process.env.GLYPHIX_ENV = dotenvTags[0];
174
+ }
175
+ const dotenvPath = (0, import_node_path3.resolve)(this.sourceRoot, "..", ".env");
176
+ const dotenvFiles = [
177
+ dotenvPath,
178
+ `${dotenvPath}.local`,
179
+ `${dotenvPath}.${process.env.GLYPHIX_ENV}`,
180
+ `${dotenvPath}.${process.env.GLYPHIX_ENV}.local`
181
+ ].filter((item) => {
182
+ return (0, import_node_fs.existsSync)(item);
183
+ });
184
+ console.info(dotenvFiles);
185
+ dotenvFiles.reverse().forEach((dotenvFile) => {
186
+ import_dotenv.default.config({ path: (0, import_node_path3.resolve)(this.sourceRoot, "..", dotenvFile) });
187
+ });
188
+ let envDefine = {};
189
+ Array.from(Object.keys(process.env)).forEach((key) => {
190
+ if (key.startsWith("GLYPHIX"))
191
+ envDefine[`process.env.${key}`] = JSON.stringify(process.env[key]);
192
+ });
193
+ return envDefine;
194
+ }
195
+ generateEsbuildConfig() {
196
+ const resolveExtensions = [".ts", ".js"];
197
+ const that = this;
198
+ const internalConfig = {
199
+ entryPoints: this.entries,
200
+ // 入口文件
201
+ bundle: true,
202
+ // 启用打包
203
+ outdir: (0, import_node_path3.resolve)(this.buildRoot, ".vite-dist"),
204
+ // 输出目录
205
+ splitting: true,
206
+ // 启用代码分割
207
+ format: "esm",
208
+ // 输出为 ESM 格式
209
+ resolveExtensions,
210
+ external: ["@system*"],
211
+ // 将 lodash 和 axios 作为外部依赖
212
+ platform: "node",
213
+ absWorkingDir: (0, import_node_path3.resolve)(this.sourceRoot, ".."),
214
+ target: "node14",
215
+ // 目标平台设置为最新 ECMAScript
216
+ define: {
217
+ "process.env.GLYPHIX_APP_VERSION": `"${this.getGitVersion()}"`,
218
+ ...this.loadEnv()
219
+ },
220
+ nodePaths: [(0, import_node_path3.resolve)(this.sourceRoot, "..", "node_modules")],
221
+ sourcemap: true,
222
+ minify: process.env.GLYPHIX_ENV === "development" ? false : true,
223
+ plugins: [
224
+ {
225
+ name: "glyphix-resolve-path",
226
+ setup(build) {
227
+ build.onResolve({ filter: /^\// }, (args) => {
228
+ if ((0, import_node_fs.existsSync)(args.path)) {
219
229
  return {
220
- path: fullPath
230
+ path: args.path
221
231
  };
222
232
  }
223
- }
224
- return {
225
- path: args.path
226
- };
227
- });
233
+ for (let i = 0; i < resolveExtensions.length; i++) {
234
+ const fullPath = (0, import_node_path3.join)(
235
+ that.buildRoot,
236
+ `${args.path}${resolveExtensions[i]}`
237
+ );
238
+ if ((0, import_node_fs.existsSync)(fullPath)) {
239
+ return {
240
+ path: fullPath
241
+ };
242
+ }
243
+ const fullIndexPath = (0, import_node_path3.join)(
244
+ that.buildRoot,
245
+ `${args.path}/index${resolveExtensions[i]}`
246
+ );
247
+ if ((0, import_node_fs.existsSync)(fullIndexPath)) {
248
+ return {
249
+ path: fullIndexPath
250
+ };
251
+ }
252
+ }
253
+ return {
254
+ path: args.path
255
+ };
256
+ });
257
+ }
228
258
  }
259
+ ]
260
+ };
261
+ const customConfigPath = (0, import_node_path3.resolve)(this.buildRoot, "glyphix.config.js");
262
+ if (!(0, import_node_fs.existsSync)(customConfigPath)) return internalConfig;
263
+ try {
264
+ const useConfig = require(customConfigPath);
265
+ return (0, import_lodash.merge)(internalConfig, useConfig);
266
+ } catch (error) {
267
+ console.error(error);
268
+ }
269
+ return internalConfig;
270
+ }
271
+ judgeIsWorkspace(parentPath, childPath) {
272
+ const resolvedParent = (0, import_node_path3.resolve)(parentPath);
273
+ const resolvedChild = (0, import_node_path3.resolve)(childPath);
274
+ const normalizedParent = resolvedParent + import_node_path3.sep;
275
+ const normalizedChild = resolvedChild + import_node_path3.sep;
276
+ return !normalizedChild.startsWith(normalizedParent);
277
+ }
278
+ /**
279
+ * 安装依赖
280
+ * 1. 判断是否是 workspace, 非 workspace 不用安装
281
+ * 2. workspace 项目,需要将 package.json 拷贝到构建目录,在构建目录安装依赖
282
+ * @returns
283
+ */
284
+ installDependencies() {
285
+ return new Promise((resolve3, reject) => {
286
+ console.log(
287
+ "is workspace project",
288
+ this.judgeIsWorkspace(
289
+ (0, import_node_path3.resolve)(this.sourceRoot, ".."),
290
+ this.buildRoot
291
+ )
292
+ );
293
+ if (!this.judgeIsWorkspace(
294
+ (0, import_node_path3.resolve)(this.sourceRoot, ".."),
295
+ this.buildRoot
296
+ )) {
297
+ resolve3();
298
+ return;
229
299
  }
230
- ]
231
- };
232
- const customConfigPath = (0, import_node_path3.resolve)(process.cwd(), "glyphix.config.js");
233
- if (!(0, import_node_fs.existsSync)(customConfigPath)) return internalConfig;
234
- try {
235
- const useConfig = require(customConfigPath);
236
- return (0, import_lodash.merge)(internalConfig, useConfig);
237
- } catch (error) {
238
- console.error(error);
300
+ const curDir = process.cwd();
301
+ const userConfig = (0, import_node_path3.resolve)(
302
+ this.sourceRoot,
303
+ "..",
304
+ "glyphix.config.js"
305
+ );
306
+ process.chdir((0, import_node_path3.resolve)(this.sourceRoot, ".."));
307
+ const installDepProcess = (0, import_node_child_process.spawn)("npm", ["install"]);
308
+ installDepProcess.on("message", (msg) => {
309
+ console.debug(msg);
310
+ });
311
+ installDepProcess.on("error", (err) => {
312
+ console.error(err);
313
+ });
314
+ installDepProcess.on("close", (code) => {
315
+ if (code === 0) {
316
+ console.info("dependencies install complete");
317
+ process.chdir(curDir);
318
+ resolve3();
319
+ } else {
320
+ process.chdir(curDir);
321
+ process.exit(1);
322
+ }
323
+ });
324
+ });
239
325
  }
240
- return internalConfig;
241
- }
242
- function build(rootPath, entriedFullPath) {
243
- let envDefine = {};
244
- Array.from(Object.keys(process.env)).forEach((key) => {
245
- if (key.startsWith("GLYPHIX"))
246
- envDefine[`process.env.${key}`] = JSON.stringify(process.env[key]);
247
- });
248
- import_esbuild.default.build(generateEsbuildConfig(rootPath, entriedFullPath, envDefine)).then(() => {
249
- console.log("Build completed!");
250
- }).catch((error) => {
251
- console.error("Build failed:", error);
252
- });
253
- }
326
+ getGitVersion() {
327
+ return (0, import_node_child_process.execSync)("git rev-parse HEAD").toString().trim();
328
+ }
329
+ build() {
330
+ const curDir = process.cwd();
331
+ this.installDependencies().then(() => {
332
+ const config = this.generateEsbuildConfig();
333
+ process.chdir((0, import_node_path3.resolve)(this.sourceRoot, ".."));
334
+ console.info("cur build directory", process.cwd());
335
+ console.log(JSON.stringify(config));
336
+ return import_esbuild.default.build(config);
337
+ }).then(() => {
338
+ process.chdir(curDir);
339
+ console.log("Build completed!");
340
+ }).catch((error) => {
341
+ console.error("Build failed:", error);
342
+ });
343
+ }
344
+ };
254
345
  function generateGlyphix() {
255
- const config = dealResource();
256
- loadEnv();
257
- build(config.root, config.entry);
346
+ console.info("start gen", process.env["VITE_SOURCE_DIR"]);
347
+ if (!process.env["VITE_BUILD_DIR"] || !process.env["VITE_SOURCE_DIR"])
348
+ throw Error("not set glyphix project path");
349
+ const generate = new GxGenerate();
350
+ generate.build();
258
351
  }
259
352
 
260
353
  // src/cli/index.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "glyphix",
3
- "version": "1.0.32",
3
+ "version": "1.0.33",
4
4
  "main": "lib/index.js",
5
5
  "types": "types/index.d.ts",
6
6
  "files": [