glyphix 1.0.32 → 1.0.34

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 +203 -113
  2. package/package.json +1 -1
package/bin/glyphix.js CHANGED
@@ -132,129 +132,219 @@ 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
+ dotenvFiles.reverse().forEach((dotenvFile) => {
185
+ import_dotenv.default.config({ path: (0, import_node_path3.resolve)(this.sourceRoot, "..", dotenvFile) });
186
+ });
187
+ let envDefine = {};
188
+ Array.from(Object.keys(process.env)).forEach((key) => {
189
+ if (key.startsWith("GLYPHIX"))
190
+ envDefine[`process.env.${key}`] = JSON.stringify(process.env[key]);
191
+ });
192
+ return envDefine;
193
+ }
194
+ generateEsbuildConfig() {
195
+ const resolveExtensions = [".ts", ".js"];
196
+ const that = this;
197
+ const internalConfig = {
198
+ entryPoints: this.entries,
199
+ // 入口文件
200
+ bundle: true,
201
+ // 启用打包
202
+ outdir: (0, import_node_path3.resolve)(this.buildRoot, ".vite-dist"),
203
+ // 输出目录
204
+ splitting: true,
205
+ // 启用代码分割
206
+ format: "esm",
207
+ // 输出为 ESM 格式
208
+ resolveExtensions,
209
+ external: ["@system*"],
210
+ // 将 lodash 和 axios 作为外部依赖
211
+ platform: "node",
212
+ absWorkingDir: (0, import_node_path3.resolve)(this.sourceRoot, ".."),
213
+ target: "node14",
214
+ // 目标平台设置为最新 ECMAScript
215
+ define: {
216
+ "process.env.GLYPHIX_APP_VERSION": `"${this.getGitVersion()}"`,
217
+ ...this.loadEnv()
218
+ },
219
+ nodePaths: [(0, import_node_path3.resolve)(this.sourceRoot, "..", "node_modules")],
220
+ sourcemap: true,
221
+ minify: process.env.GLYPHIX_ENV === "development" ? false : true,
222
+ plugins: [
223
+ {
224
+ name: "glyphix-resolve-path",
225
+ setup(build) {
226
+ build.onResolve({ filter: /^\// }, (args) => {
227
+ if ((0, import_node_fs.existsSync)(args.path)) {
219
228
  return {
220
- path: fullPath
229
+ path: args.path
221
230
  };
222
231
  }
223
- }
224
- return {
225
- path: args.path
226
- };
227
- });
232
+ for (let i = 0; i < resolveExtensions.length; i++) {
233
+ const fullPath = (0, import_node_path3.join)(
234
+ that.buildRoot,
235
+ `${args.path}${resolveExtensions[i]}`
236
+ );
237
+ if ((0, import_node_fs.existsSync)(fullPath)) {
238
+ return {
239
+ path: fullPath
240
+ };
241
+ }
242
+ const fullIndexPath = (0, import_node_path3.join)(
243
+ that.buildRoot,
244
+ `${args.path}/index${resolveExtensions[i]}`
245
+ );
246
+ if ((0, import_node_fs.existsSync)(fullIndexPath)) {
247
+ return {
248
+ path: fullIndexPath
249
+ };
250
+ }
251
+ }
252
+ return {
253
+ path: args.path
254
+ };
255
+ });
256
+ }
228
257
  }
258
+ ]
259
+ };
260
+ const customConfigPath = (0, import_node_path3.resolve)(this.buildRoot, "glyphix.config.js");
261
+ if (!(0, import_node_fs.existsSync)(customConfigPath)) return internalConfig;
262
+ try {
263
+ const useConfig = require(customConfigPath);
264
+ return (0, import_lodash.merge)(internalConfig, useConfig);
265
+ } catch (error) {
266
+ console.error(error);
267
+ }
268
+ return internalConfig;
269
+ }
270
+ judgeIsWorkspace(parentPath, childPath) {
271
+ const resolvedParent = (0, import_node_path3.resolve)(parentPath);
272
+ const resolvedChild = (0, import_node_path3.resolve)(childPath);
273
+ const normalizedParent = resolvedParent + import_node_path3.sep;
274
+ const normalizedChild = resolvedChild + import_node_path3.sep;
275
+ return !normalizedChild.startsWith(normalizedParent);
276
+ }
277
+ /**
278
+ * 安装依赖
279
+ * 1. 判断是否是 workspace, 非 workspace 不用安装
280
+ * 2. workspace 项目,需要将 package.json 拷贝到构建目录,在构建目录安装依赖
281
+ * @returns
282
+ */
283
+ installDependencies() {
284
+ return new Promise((resolve3, reject) => {
285
+ console.log(
286
+ "is workspace project",
287
+ this.judgeIsWorkspace(
288
+ (0, import_node_path3.resolve)(this.sourceRoot, ".."),
289
+ this.buildRoot
290
+ )
291
+ );
292
+ if (!this.judgeIsWorkspace(
293
+ (0, import_node_path3.resolve)(this.sourceRoot, ".."),
294
+ this.buildRoot
295
+ )) {
296
+ resolve3();
297
+ return;
229
298
  }
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);
299
+ const curDir = process.cwd();
300
+ const userConfig = (0, import_node_path3.resolve)(
301
+ this.sourceRoot,
302
+ "..",
303
+ "glyphix.config.js"
304
+ );
305
+ process.chdir((0, import_node_path3.resolve)(this.sourceRoot, ".."));
306
+ const installDepProcess = (0, import_node_child_process.spawn)("npm", ["install"]);
307
+ installDepProcess.on("message", (msg) => {
308
+ console.debug(msg);
309
+ });
310
+ installDepProcess.on("error", (err) => {
311
+ console.error(err);
312
+ });
313
+ installDepProcess.on("close", (code) => {
314
+ if (code === 0) {
315
+ console.info("dependencies install complete");
316
+ process.chdir(curDir);
317
+ resolve3();
318
+ } else {
319
+ process.chdir(curDir);
320
+ process.exit(1);
321
+ }
322
+ });
323
+ });
239
324
  }
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
- }
325
+ getGitVersion() {
326
+ return (0, import_node_child_process.execSync)("git rev-parse HEAD").toString().trim();
327
+ }
328
+ build() {
329
+ const curDir = process.cwd();
330
+ this.installDependencies().then(() => {
331
+ const config = this.generateEsbuildConfig();
332
+ process.chdir((0, import_node_path3.resolve)(this.sourceRoot, ".."));
333
+ return import_esbuild.default.build(config);
334
+ }).then(() => {
335
+ process.chdir(curDir);
336
+ console.log("Build completed!");
337
+ }).catch((error) => {
338
+ console.error("Build failed:", error);
339
+ });
340
+ }
341
+ };
254
342
  function generateGlyphix() {
255
- const config = dealResource();
256
- loadEnv();
257
- build(config.root, config.entry);
343
+ console.info("start gen", process.env["VITE_SOURCE_DIR"]);
344
+ if (!process.env["VITE_BUILD_DIR"] || !process.env["VITE_SOURCE_DIR"])
345
+ throw Error("not set glyphix project path");
346
+ const generate = new GxGenerate();
347
+ generate.build();
258
348
  }
259
349
 
260
350
  // 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.34",
4
4
  "main": "lib/index.js",
5
5
  "types": "types/index.d.ts",
6
6
  "files": [