glyphix 1.0.31 → 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.
package/bin/glyphix.js CHANGED
@@ -132,194 +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
- var import_node_process = require("process");
140
135
  var import_dotenv = __toESM(require("dotenv"));
141
136
  var import_lodash = require("lodash");
142
- function loadEnv() {
143
- const dotenvTags = [
144
- // 本地环境
145
- "development",
146
- // 测试环境
147
- // 比如:单元测试
148
- "test",
149
- // 部署环境
150
- // 比如:日常、预发、线上
151
- "production"
152
- ];
153
- if (!dotenvTags.includes(process.env.GLYPHIX_ENV ?? "")) {
154
- process.env.GLYPHIX_ENV = dotenvTags[0];
155
- }
156
- const dotenvPath = (0, import_node_path3.resolve)(".env");
157
- const dotenvFiles = [
158
- dotenvPath,
159
- `${dotenvPath}.local`,
160
- `${dotenvPath}.${process.env.GLYPHIX_ENV}`,
161
- `${dotenvPath}.${process.env.GLYPHIX_ENV}.local`
162
- ].filter(import_node_fs.existsSync);
163
- dotenvFiles.reverse().forEach((dotenvFile) => {
164
- import_dotenv.default.config({ path: dotenvFile });
165
- });
166
- }
167
- function getOriginFilePath(filePath, rootPath) {
168
- const relativePath = (0, import_node_path3.relative)(rootPath, filePath);
169
- const projectRoot = process.cwd();
170
- const originPath = (0, import_node_path3.resolve)(projectRoot, "src", relativePath);
171
- if (originPath.endsWith(".ts")) {
172
- const uxOrigin = originPath.slice(0, -3) + ".ux";
173
- if ((0, import_node_fs.existsSync)(uxOrigin)) {
174
- return uxOrigin;
175
- }
176
- }
177
- return originPath;
178
- }
179
- function outErrorLog(results, filepath) {
180
- results.messages.forEach((item) => {
181
- console.log(
182
- `${filepath}:${item.line}:${item.column}`,
183
- "\n ",
184
- import_chalk2.default.red("error"),
185
- " ",
186
- item.message,
187
- " ",
188
- import_chalk2.default.gray(item.ruleId)
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)
189
159
  );
190
- });
191
- }
192
- function lintFile(rootPath) {
193
- const distPath = (0, import_node_path3.join)(rootPath, ".vite-dist");
194
- if ((0, import_node_fs.existsSync)(distPath)) {
195
- (0, import_fs_extra2.removeSync)(distPath);
196
160
  }
197
- const sources = (0, import_glob.sync)([
198
- `${(0, import_node_path3.resolve)(rootPath)}/**/*.ts`,
199
- `${(0, import_node_path3.resolve)(rootPath)}/**/*.js`
200
- ]);
201
- const eslint = new import_eslint.ESLint({
202
- warnIgnored: true,
203
- overrideConfig: {
204
- rules: {
205
- "no-console": "off",
206
- "prettier/prettier": "off",
207
- "@typescript-eslint/ban-ts-comment": "warn",
208
- "@typescript-eslint/no-explicit-any": "off",
209
- "@typescript-eslint/no-unused-vars": "warn",
210
- quotes: "off"
211
- }
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];
212
174
  }
213
- });
214
- return eslint.lintFiles(sources).then((res) => {
215
- return res.filter((item) => item.errorCount).map((item) => {
216
- return item;
217
- }).flat();
218
- }).then((res) => {
219
- res.forEach((item) => {
220
- const originPath = getOriginFilePath(item.filePath, rootPath);
221
- outErrorLog(item, originPath);
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);
222
183
  });
223
- return res;
224
- });
225
- }
226
- function dealResource() {
227
- if (!process.env["VITE_BUILD_DIR"])
228
- throw Error("not set glyphix project path");
229
- const rootPath = (0, import_node_path3.resolve)(process.cwd(), process.env["VITE_BUILD_DIR"]);
230
- const entryFileContent = JSON.parse(
231
- (0, import_node_fs.readFileSync)((0, import_node_path3.resolve)(rootPath, "entry.vite.json")).toString()
232
- );
233
- const entries = entryFileContent.entry;
234
- (0, import_fs_extra2.removeSync)((0, import_node_path3.resolve)(rootPath, ".vite-dist"));
235
- let entriedFullPath = entries.map((item) => (0, import_node_path3.resolve)(rootPath, item));
236
- return {
237
- root: rootPath,
238
- entry: entriedFullPath
239
- };
240
- }
241
- function generateEsbuildConfig(rootPath, entriedFullPath, envDefine) {
242
- const resolveExtensions = [".ts", ".js"];
243
- const internalConfig = {
244
- entryPoints: entriedFullPath,
245
- // 入口文件
246
- bundle: true,
247
- // 启用打包
248
- outdir: (0, import_node_path3.resolve)(rootPath, ".vite-dist"),
249
- // 输出目录
250
- splitting: true,
251
- // 启用代码分割
252
- format: "esm",
253
- // 输出为 ESM 格式
254
- resolveExtensions,
255
- external: ["@system*"],
256
- // 将 lodash 和 axios 作为外部依赖
257
- platform: "node",
258
- target: "node14",
259
- // 目标平台设置为最新 ECMAScript
260
- define: envDefine,
261
- sourcemap: true,
262
- minify: process.env.GLYPHIX_ENV === "development" ? false : true,
263
- plugins: [
264
- {
265
- name: "glyphix-resolve-path",
266
- setup(build2) {
267
- build2.onResolve({ filter: /^\// }, (args) => {
268
- if ((0, import_node_fs.existsSync)(args.path)) {
269
- return {
270
- path: args.path
271
- };
272
- }
273
- for (let i = 0; i < resolveExtensions.length; i++) {
274
- const fullPath = (0, import_node_path3.join)(
275
- rootPath,
276
- `${args.path}${resolveExtensions[i]}`
277
- );
278
- if ((0, import_node_fs.existsSync)(fullPath)) {
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)) {
279
229
  return {
280
- path: fullPath
230
+ path: args.path
281
231
  };
282
232
  }
283
- }
284
- return {
285
- path: args.path
286
- };
287
- });
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
+ }
288
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;
289
299
  }
290
- ]
291
- };
292
- const customConfigPath = (0, import_node_path3.resolve)(process.cwd(), "glyphix.config.js");
293
- if (!(0, import_node_fs.existsSync)(customConfigPath)) return internalConfig;
294
- try {
295
- const useConfig = require(customConfigPath);
296
- return (0, import_lodash.merge)(internalConfig, useConfig);
297
- } catch (error) {
298
- 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
+ });
299
325
  }
300
- return internalConfig;
301
- }
302
- function build(rootPath, entriedFullPath) {
303
- let envDefine = {};
304
- Array.from(Object.keys(process.env)).forEach((key) => {
305
- if (key.startsWith("GLYPHIX"))
306
- envDefine[`process.env.${key}`] = JSON.stringify(process.env[key]);
307
- });
308
- import_esbuild.default.build(generateEsbuildConfig(rootPath, entriedFullPath, envDefine)).then(() => {
309
- console.log("Build completed!");
310
- }).catch((error) => {
311
- console.error("Build failed:", error);
312
- });
313
- }
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
+ };
314
345
  function generateGlyphix() {
315
- const config = dealResource();
316
- loadEnv();
317
- lintFile(config.root).then((res) => {
318
- if (res.length) {
319
- (0, import_node_process.exit)(1);
320
- }
321
- build(config.root, config.entry);
322
- });
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();
323
351
  }
324
352
 
325
353
  // src/cli/index.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "glyphix",
3
- "version": "1.0.31",
3
+ "version": "1.0.33",
4
4
  "main": "lib/index.js",
5
5
  "types": "types/index.d.ts",
6
6
  "files": [
package/types/index.d.ts CHANGED
@@ -17,10 +17,12 @@
17
17
  /// <reference path="./exchange.d.ts" />
18
18
  /// <reference path="./audiokit.d.ts" />
19
19
 
20
+
20
21
  declare global {
21
22
  function $t(key: string): string;
22
23
  }
23
24
 
25
+
24
26
  type ComputedResults<C, D> = {
25
27
  readonly [K in keyof C as C[K] extends (this: D) => any
26
28
  ? K