@x18n/node 0.0.7 → 0.0.9

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/dist/index.mjs +25 -6
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -4,7 +4,7 @@ import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
4
4
  import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
5
5
  import { fileURLToPath, pathToFileURL } from "node:url";
6
6
  import { compileYamlPlugin } from "@x18n/builtins/yaml-plugin";
7
- import { PluginFileSchema, X18nError, createProject, hashText, isX18nError, resolveConfig, validate } from "@x18n/core";
7
+ import { PluginFileSchema, createProject, errors, hashText, isX18nError, resolveConfig, validate } from "@x18n/core";
8
8
  import { createJiti } from "jiti";
9
9
  import { glob } from "tinyglobby";
10
10
  import { parse, stringify } from "yaml";
@@ -73,18 +73,28 @@ async function loadConfigFile(file) {
73
73
  if (file.endsWith(".json")) return JSON.parse(await readFile(file, "utf8"));
74
74
  const loaded = await importModule(file);
75
75
  const config = typeof loaded === "function" ? await loaded() : loaded;
76
- if (!isObject(config)) throw new X18nError("X18N0002", `${file} does not export a config object`);
76
+ if (!isObject(config)) throw errors.X18N0002({
77
+ file,
78
+ reason: "it does not export a config object"
79
+ });
77
80
  return config;
78
81
  } catch (error) {
79
82
  if (isX18nError(error)) throw error;
80
- throw new X18nError("X18N0002", `${file}: ${error instanceof Error ? error.message : String(error)}`);
83
+ throw errors.X18N0002({
84
+ file,
85
+ reason: error instanceof Error ? error.message : String(error),
86
+ cause: error
87
+ });
81
88
  }
82
89
  }
83
90
  /** Loads a YAML plugin file (`*.x18n-plugin.yml`). */
84
91
  async function loadYamlPlugin(file) {
85
92
  const data = parse(await readFile(file, "utf8"));
86
93
  const result = validate(PluginFileSchema, data);
87
- if (!result.success) throw new X18nError("X18N1004", `${file}: ${result.issues.map((i) => `${i.path || "(root)"}: ${i.message}`).join("; ")}`);
94
+ if (!result.success) throw errors.X18N1004({
95
+ file,
96
+ issues: result.issues.map((i) => `${i.path || "(root)"}: ${i.message}`).join("; ")
97
+ });
88
98
  return compileYamlPlugin(result.output);
89
99
  }
90
100
  function isPluginLike(value) {
@@ -102,7 +112,11 @@ function createPluginLoader(dir) {
102
112
  return plugin;
103
113
  } catch (error) {
104
114
  if (isX18nError(error)) throw error;
105
- throw new X18nError("X18N1001", `${specifier}: ${error instanceof Error ? error.message : String(error)}`);
115
+ throw errors.X18N1001({
116
+ specifier,
117
+ reason: error instanceof Error ? error.message : String(error),
118
+ cause: error
119
+ });
106
120
  }
107
121
  };
108
122
  }
@@ -1161,8 +1175,10 @@ async function loadWorkspace(options) {
1161
1175
  plugins: [...defaults.plugins, gitHistory()]
1162
1176
  } : defaults;
1163
1177
  const loaded = [];
1178
+ const names = [];
1164
1179
  for (const spec of specs) {
1165
1180
  const name = spec.config.name ?? (spec.root === root ? relative(dirname(root), root) : relative(root, spec.root)).replace(/\\/g, "/");
1181
+ names.push(name);
1166
1182
  if (options.project !== void 0 && options.project !== name) continue;
1167
1183
  const host = createNodeHost(spec.root, {
1168
1184
  cacheDir: options.cache === false ? false : void 0,
@@ -1193,7 +1209,10 @@ async function loadWorkspace(options) {
1193
1209
  project
1194
1210
  });
1195
1211
  }
1196
- if (options.project !== void 0 && loaded.length === 0) throw new X18nError("X18N0003", options.project);
1212
+ if (options.project !== void 0 && loaded.length === 0) throw errors.X18N0003({
1213
+ name: options.project,
1214
+ available: names
1215
+ });
1197
1216
  return {
1198
1217
  root,
1199
1218
  configFile,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@x18n/node",
3
3
  "type": "module",
4
- "version": "0.0.7",
4
+ "version": "0.0.9",
5
5
  "description": "Node.js host for x18n: file system access, config and plugin loading",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -25,8 +25,8 @@
25
25
  "node": ">=22"
26
26
  },
27
27
  "dependencies": {
28
- "@x18n/builtins": "0.0.7",
29
- "@x18n/core": "0.0.7",
28
+ "@x18n/builtins": "0.0.9",
29
+ "@x18n/core": "0.0.9",
30
30
  "chokidar": "^6.0.0",
31
31
  "ignore": "^7.0.13",
32
32
  "jiti": "^2.7.0",