@jayfong/x-utils 1.0.0-202409020222 → 1.0.0-202409192345

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/_cjs/env.js CHANGED
@@ -37,28 +37,43 @@ var import_fs = require("./fs.js");
37
37
  var import_path = require("./path.js");
38
38
  var import_yaml = require("./yaml.js");
39
39
  class XEnv {
40
- static getEnvVarsFromFile = (0, import_vtils.memoize)(async function get(local = true) {
41
- const baseName = local ? ".env.local" : ".env";
42
- const envYmlFile = import_path.XPath.join(process.cwd(), `${baseName}.yml`);
43
- if (await import_fs.XFs.pathExists(envYmlFile)) {
44
- return import_yaml.XYaml.read(envYmlFile);
45
- }
46
- const envYamlFile = import_path.XPath.join(process.cwd(), `${baseName}.yaml`);
47
- if (await import_fs.XFs.pathExists(envYamlFile)) {
48
- return import_yaml.XYaml.read(envYamlFile);
49
- }
50
- const dotenvFile = import_path.XPath.join(process.cwd(), baseName);
51
- if (await import_fs.XFs.pathExists(dotenvFile)) {
52
- const dotenvContent = await import_fs.XFs.readFile(dotenvFile, "utf-8");
53
- return import_dotenv.default.parse(dotenvContent);
54
- }
55
- if (local) return get(false);
56
- });
57
- static async getEnvVar(name) {
40
+ static getEnvVarsFromFile = (0, import_vtils.memoize)(
41
+ async function get(mode = "", local = true) {
42
+ const baseName = local ? `.env${mode ? `.${mode}` : ""}.local` : `.env${mode ? `.${mode}` : ""}`;
43
+ const envYmlFile = import_path.XPath.join(process.cwd(), `${baseName}.yml`);
44
+ if (await import_fs.XFs.pathExists(envYmlFile)) {
45
+ return import_yaml.XYaml.read(envYmlFile);
46
+ }
47
+ const envYamlFile = import_path.XPath.join(process.cwd(), `${baseName}.yaml`);
48
+ if (await import_fs.XFs.pathExists(envYamlFile)) {
49
+ return import_yaml.XYaml.read(envYamlFile);
50
+ }
51
+ const dotenvFile = import_path.XPath.join(process.cwd(), baseName);
52
+ if (await import_fs.XFs.pathExists(dotenvFile)) {
53
+ const dotenvContent = await import_fs.XFs.readFile(dotenvFile, "utf-8");
54
+ return import_dotenv.default.parse(dotenvContent);
55
+ }
56
+ if (local) return get(mode, false);
57
+ },
58
+ (mode, local) => `${mode}_${local}`
59
+ );
60
+ static async getEnvVar(name, mode) {
58
61
  if (process.env[name] != null) {
59
62
  return process.env[name];
60
63
  }
61
- const envVars = await XEnv.getEnvVarsFromFile();
64
+ const envVars = await XEnv.getEnvVarsFromFile(mode);
62
65
  return (0, import_vtils.isPlainObject)(envVars) ? envVars[name] ?? void 0 : void 0;
63
66
  }
67
+ static async getEnvVars(mode) {
68
+ const envVars = await XEnv.getEnvVarsFromFile(mode);
69
+ return (0, import_vtils.isPlainObject)(envVars) ? envVars : {};
70
+ }
71
+ static async extendProcessEnv(mode) {
72
+ const envVars = await XEnv.getEnvVarsFromFile(mode);
73
+ if ((0, import_vtils.isPlainObject)(envVars)) {
74
+ Object.keys(envVars).forEach((env) => {
75
+ process.env[env] = envVars[env];
76
+ });
77
+ }
78
+ }
64
79
  }
package/_cjs/upload.js CHANGED
@@ -46,6 +46,7 @@ class XUpload {
46
46
  * @param options.source 默认 my-auto-tasks
47
47
  */
48
48
  static async upload(file, {
49
+ fileExt: customFileExt,
49
50
  serverUrl,
50
51
  serverToken,
51
52
  provider = "auto",
@@ -58,6 +59,7 @@ class XUpload {
58
59
  if (serverToken == null) {
59
60
  serverToken = await import_env.XEnv.getEnvVar("X_UPLOAD_SERVER_TOKEN");
60
61
  }
62
+ const rawFile = file;
61
63
  if (typeof file === "string") {
62
64
  if ((0, import_vtils.isUrl)(file)) {
63
65
  const { data: data2 } = await import_x_request.xr.get({
@@ -69,7 +71,9 @@ class XUpload {
69
71
  file = await fs.promises.readFile(file);
70
72
  }
71
73
  }
72
- const { ext: fileExt } = await (0, import_file_type.fileTypeFromBuffer)(file);
74
+ const { ext: fileExt } = customFileExt ? { ext: customFileExt } : await (0, import_file_type.fileTypeFromBuffer)(file) || {
75
+ ext: (typeof rawFile === "string" ? rawFile.split("?")[0].split(".").pop()?.toLowerCase() : "") || "unknown"
76
+ };
73
77
  const imageExts = ["jpg", "png", "gif"];
74
78
  const isImage = imageExts.includes(fileExt);
75
79
  provider = provider === "auto" ? isImage ? "image" : "file" : provider;
package/env.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export declare class XEnv {
2
2
  private static getEnvVarsFromFile;
3
- static getEnvVar<T>(name: string): Promise<T | undefined>;
3
+ static getEnvVar<T>(name: string, mode?: string): Promise<T | undefined>;
4
+ static getEnvVars(mode?: string): Promise<Record<string, any>>;
5
+ static extendProcessEnv(mode?: string): Promise<void>;
4
6
  }
package/env.js CHANGED
@@ -4,30 +4,45 @@ import { XFs } from "./fs.js";
4
4
  import { XPath } from "./path.js";
5
5
  import { XYaml } from "./yaml.js";
6
6
  class XEnv {
7
- static getEnvVarsFromFile = memoize(async function get(local = true) {
8
- const baseName = local ? ".env.local" : ".env";
9
- const envYmlFile = XPath.join(process.cwd(), `${baseName}.yml`);
10
- if (await XFs.pathExists(envYmlFile)) {
11
- return XYaml.read(envYmlFile);
12
- }
13
- const envYamlFile = XPath.join(process.cwd(), `${baseName}.yaml`);
14
- if (await XFs.pathExists(envYamlFile)) {
15
- return XYaml.read(envYamlFile);
16
- }
17
- const dotenvFile = XPath.join(process.cwd(), baseName);
18
- if (await XFs.pathExists(dotenvFile)) {
19
- const dotenvContent = await XFs.readFile(dotenvFile, "utf-8");
20
- return dotenv.parse(dotenvContent);
21
- }
22
- if (local) return get(false);
23
- });
24
- static async getEnvVar(name) {
7
+ static getEnvVarsFromFile = memoize(
8
+ async function get(mode = "", local = true) {
9
+ const baseName = local ? `.env${mode ? `.${mode}` : ""}.local` : `.env${mode ? `.${mode}` : ""}`;
10
+ const envYmlFile = XPath.join(process.cwd(), `${baseName}.yml`);
11
+ if (await XFs.pathExists(envYmlFile)) {
12
+ return XYaml.read(envYmlFile);
13
+ }
14
+ const envYamlFile = XPath.join(process.cwd(), `${baseName}.yaml`);
15
+ if (await XFs.pathExists(envYamlFile)) {
16
+ return XYaml.read(envYamlFile);
17
+ }
18
+ const dotenvFile = XPath.join(process.cwd(), baseName);
19
+ if (await XFs.pathExists(dotenvFile)) {
20
+ const dotenvContent = await XFs.readFile(dotenvFile, "utf-8");
21
+ return dotenv.parse(dotenvContent);
22
+ }
23
+ if (local) return get(mode, false);
24
+ },
25
+ (mode, local) => `${mode}_${local}`
26
+ );
27
+ static async getEnvVar(name, mode) {
25
28
  if (process.env[name] != null) {
26
29
  return process.env[name];
27
30
  }
28
- const envVars = await XEnv.getEnvVarsFromFile();
31
+ const envVars = await XEnv.getEnvVarsFromFile(mode);
29
32
  return isPlainObject(envVars) ? envVars[name] ?? void 0 : void 0;
30
33
  }
34
+ static async getEnvVars(mode) {
35
+ const envVars = await XEnv.getEnvVarsFromFile(mode);
36
+ return isPlainObject(envVars) ? envVars : {};
37
+ }
38
+ static async extendProcessEnv(mode) {
39
+ const envVars = await XEnv.getEnvVarsFromFile(mode);
40
+ if (isPlainObject(envVars)) {
41
+ Object.keys(envVars).forEach((env) => {
42
+ process.env[env] = envVars[env];
43
+ });
44
+ }
45
+ }
31
46
  }
32
47
  export {
33
48
  XEnv
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jayfong/x-utils",
3
3
  "description": "123456",
4
- "version": "1.0.0-202409020222",
4
+ "version": "1.0.0-202409192345",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "vtils": "^4.115.0",
package/upload.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ import { FileExtension } from 'file-type';
2
3
  import { LiteralUnion } from 'vtils/types';
3
4
  /**
4
5
  * 环境变量:
@@ -15,7 +16,8 @@ export declare class XUpload {
15
16
  * @param options.provider 默认 auto
16
17
  * @param options.source 默认 my-auto-tasks
17
18
  */
18
- static upload(file: string | Buffer, { serverUrl, serverToken, provider, source, beforeUpload, }?: {
19
+ static upload(file: string | Buffer, { fileExt: customFileExt, serverUrl, serverToken, provider, source, beforeUpload, }?: {
20
+ fileExt?: LiteralUnion<FileExtension, string>;
19
21
  serverUrl?: string;
20
22
  serverToken?: string;
21
23
  provider?: LiteralUnion<'image' | 'file' | 'auto', string>;
package/upload.js CHANGED
@@ -13,6 +13,7 @@ class XUpload {
13
13
  * @param options.source 默认 my-auto-tasks
14
14
  */
15
15
  static async upload(file, {
16
+ fileExt: customFileExt,
16
17
  serverUrl,
17
18
  serverToken,
18
19
  provider = "auto",
@@ -25,6 +26,7 @@ class XUpload {
25
26
  if (serverToken == null) {
26
27
  serverToken = await XEnv.getEnvVar("X_UPLOAD_SERVER_TOKEN");
27
28
  }
29
+ const rawFile = file;
28
30
  if (typeof file === "string") {
29
31
  if (isUrl(file)) {
30
32
  const { data: data2 } = await xr.get({
@@ -36,7 +38,9 @@ class XUpload {
36
38
  file = await fs.promises.readFile(file);
37
39
  }
38
40
  }
39
- const { ext: fileExt } = await fileTypeFromBuffer(file);
41
+ const { ext: fileExt } = customFileExt ? { ext: customFileExt } : await fileTypeFromBuffer(file) || {
42
+ ext: (typeof rawFile === "string" ? rawFile.split("?")[0].split(".").pop()?.toLowerCase() : "") || "unknown"
43
+ };
40
44
  const imageExts = ["jpg", "png", "gif"];
41
45
  const isImage = imageExts.includes(fileExt);
42
46
  provider = provider === "auto" ? isImage ? "image" : "file" : provider;