@eserstack/codebase 0.0.0-stage → 5.0.0

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 (175) hide show
  1. package/README.md +301 -2
  2. package/dist/changelog-gen.d.ts +105 -0
  3. package/dist/changelog-gen.js +498 -0
  4. package/dist/changelog-text.d.ts +39 -0
  5. package/dist/changelog-text.js +82 -0
  6. package/dist/cli-support.d.ts +140 -0
  7. package/dist/cli-support.js +244 -0
  8. package/dist/cli-system/app-opts.d.ts +15 -0
  9. package/dist/cli-system/app-opts.js +13 -0
  10. package/dist/cli-system/app.d.ts +29 -0
  11. package/dist/cli-system/app.js +12 -0
  12. package/dist/cli-system/handlers/completions-setup.d.ts +14 -0
  13. package/dist/cli-system/handlers/completions-setup.js +139 -0
  14. package/dist/cli-system/handlers/completions.d.ts +3 -0
  15. package/dist/cli-system/handlers/completions.js +79 -0
  16. package/dist/cli-system/handlers/doctor.d.ts +8 -0
  17. package/dist/cli-system/handlers/doctor.js +146 -0
  18. package/dist/cli-system/handlers/info.d.ts +3 -0
  19. package/dist/cli-system/handlers/info.js +69 -0
  20. package/dist/cli-system/handlers/install.d.ts +8 -0
  21. package/dist/cli-system/handlers/install.js +137 -0
  22. package/dist/cli-system/handlers/mod.d.ts +12 -0
  23. package/dist/cli-system/handlers/mod.js +13 -0
  24. package/dist/cli-system/handlers/uninstall.d.ts +3 -0
  25. package/dist/cli-system/handlers/uninstall.js +60 -0
  26. package/dist/cli-system/handlers/update.d.ts +76 -0
  27. package/dist/cli-system/handlers/update.js +310 -0
  28. package/dist/cli-system/handlers/version-check.d.ts +16 -0
  29. package/dist/cli-system/handlers/version-check.js +176 -0
  30. package/dist/cli-system/handlers/version.d.ts +3 -0
  31. package/dist/cli-system/handlers/version.js +45 -0
  32. package/dist/cli-system/mod.d.ts +19 -0
  33. package/dist/cli-system/mod.js +52 -0
  34. package/dist/commitmsg.d.ts +28 -0
  35. package/dist/commitmsg.js +232 -0
  36. package/dist/directive-analysis.d.ts +75 -0
  37. package/dist/directive-analysis.js +223 -0
  38. package/dist/ffi-client.d.ts +11 -0
  39. package/dist/ffi-client.js +12 -0
  40. package/dist/file-tool.d.ts +96 -0
  41. package/dist/file-tool.js +274 -0
  42. package/dist/file-tools-shared.d.ts +81 -0
  43. package/dist/file-tools-shared.js +276 -0
  44. package/dist/gh-contributors.d.ts +89 -0
  45. package/dist/gh-contributors.js +205 -0
  46. package/dist/gh.d.ts +3 -0
  47. package/dist/gh.js +64 -0
  48. package/dist/git-listing.d.ts +27 -0
  49. package/dist/git-listing.js +69 -0
  50. package/dist/git.d.ts +172 -0
  51. package/dist/git.js +277 -0
  52. package/dist/install.d.ts +34 -0
  53. package/dist/install.js +421 -0
  54. package/dist/main.d.ts +22 -0
  55. package/dist/main.js +14 -0
  56. package/dist/mod.d.ts +25 -0
  57. package/dist/mod.js +8 -0
  58. package/dist/module.d.ts +10 -0
  59. package/dist/module.js +221 -0
  60. package/dist/native-functions.d.ts +1088 -0
  61. package/dist/native-functions.js +2 -0
  62. package/dist/npm-externals.d.ts +27 -0
  63. package/dist/npm-externals.js +34 -0
  64. package/dist/npm-workspace-specifiers.d.ts +6 -0
  65. package/dist/npm-workspace-specifiers.js +45 -0
  66. package/dist/package/loader.d.ts +34 -0
  67. package/dist/package/loader.js +203 -0
  68. package/dist/package/mod.d.ts +36 -0
  69. package/dist/package/mod.js +5 -0
  70. package/dist/package/types.d.ts +142 -0
  71. package/dist/package/types.js +32 -0
  72. package/dist/package/workspace.d.ts +33 -0
  73. package/dist/package/workspace.js +158 -0
  74. package/dist/package/writer.d.ts +52 -0
  75. package/dist/package/writer.js +278 -0
  76. package/dist/package.json +275 -0
  77. package/dist/release-notes.d.ts +90 -0
  78. package/dist/release-notes.js +236 -0
  79. package/dist/release-tag.d.ts +46 -0
  80. package/dist/release-tag.js +95 -0
  81. package/dist/release.d.ts +145 -0
  82. package/dist/release.js +584 -0
  83. package/dist/source-mask.d.ts +3 -0
  84. package/dist/source-mask.js +185 -0
  85. package/dist/validate-bom.d.ts +5 -0
  86. package/dist/validate-bom.js +43 -0
  87. package/dist/validate-case-conflict.d.ts +5 -0
  88. package/dist/validate-case-conflict.js +43 -0
  89. package/dist/validate-circular-deps.d.ts +11 -0
  90. package/dist/validate-circular-deps.js +53 -0
  91. package/dist/validate-code-conventions.d.ts +22 -0
  92. package/dist/validate-code-conventions.js +166 -0
  93. package/dist/validate-commit-msg.d.ts +50 -0
  94. package/dist/validate-commit-msg.js +182 -0
  95. package/dist/validate-docs.d.ts +20 -0
  96. package/dist/validate-docs.js +78 -0
  97. package/dist/validate-eof.d.ts +5 -0
  98. package/dist/validate-eof.js +61 -0
  99. package/dist/validate-error-coverage.d.ts +28 -0
  100. package/dist/validate-error-coverage.js +93 -0
  101. package/dist/validate-export-names.d.ts +17 -0
  102. package/dist/validate-export-names.js +56 -0
  103. package/dist/validate-filenames.d.ts +5 -0
  104. package/dist/validate-filenames.js +151 -0
  105. package/dist/validate-json.d.ts +5 -0
  106. package/dist/validate-json.js +71 -0
  107. package/dist/validate-large-files.d.ts +5 -0
  108. package/dist/validate-large-files.js +41 -0
  109. package/dist/validate-laroux-boundaries.d.ts +19 -0
  110. package/dist/validate-laroux-boundaries.js +59 -0
  111. package/dist/validate-licenses.d.ts +5 -0
  112. package/dist/validate-licenses.js +137 -0
  113. package/dist/validate-line-endings.d.ts +5 -0
  114. package/dist/validate-line-endings.js +47 -0
  115. package/dist/validate-merge-conflict.d.ts +5 -0
  116. package/dist/validate-merge-conflict.js +46 -0
  117. package/dist/validate-mod-exports.d.ts +16 -0
  118. package/dist/validate-mod-exports.js +56 -0
  119. package/dist/validate-naming-and-todos.d.ts +5 -0
  120. package/dist/validate-naming-and-todos.js +70 -0
  121. package/dist/validate-runtime-js-apis.d.ts +5 -0
  122. package/dist/validate-runtime-js-apis.js +78 -0
  123. package/dist/validate-secrets.d.ts +5 -0
  124. package/dist/validate-secrets.js +82 -0
  125. package/dist/validate-self-imports.d.ts +5 -0
  126. package/dist/validate-self-imports.js +78 -0
  127. package/dist/validate-server-loc.d.ts +32 -0
  128. package/dist/validate-server-loc.js +94 -0
  129. package/dist/validate-shebangs.d.ts +5 -0
  130. package/dist/validate-shebangs.js +33 -0
  131. package/dist/validate-stream.d.ts +25 -0
  132. package/dist/validate-stream.js +62 -0
  133. package/dist/validate-submodules.d.ts +5 -0
  134. package/dist/validate-submodules.js +43 -0
  135. package/dist/validate-symlinks.d.ts +5 -0
  136. package/dist/validate-symlinks.js +46 -0
  137. package/dist/validate-toml.d.ts +5 -0
  138. package/dist/validate-toml.js +40 -0
  139. package/dist/validate-trailing-whitespace.d.ts +5 -0
  140. package/dist/validate-trailing-whitespace.js +49 -0
  141. package/dist/validate-yaml.d.ts +5 -0
  142. package/dist/validate-yaml.js +40 -0
  143. package/dist/validation/config.d.ts +17 -0
  144. package/dist/validation/config.js +30 -0
  145. package/dist/validation/mod.d.ts +71 -0
  146. package/dist/validation/mod.js +479 -0
  147. package/dist/validation/registry.d.ts +57 -0
  148. package/dist/validation/registry.js +256 -0
  149. package/dist/validation/types.d.ts +101 -0
  150. package/dist/validation/types.js +2 -0
  151. package/dist/validation/validators/circular-deps.d.ts +7 -0
  152. package/dist/validation/validators/circular-deps.js +31 -0
  153. package/dist/validation/validators/docs.d.ts +7 -0
  154. package/dist/validation/validators/docs.js +38 -0
  155. package/dist/validation/validators/error-coverage.d.ts +2 -0
  156. package/dist/validation/validators/error-coverage.js +46 -0
  157. package/dist/validation/validators/export-names.d.ts +7 -0
  158. package/dist/validation/validators/export-names.js +31 -0
  159. package/dist/validation/validators/laroux-boundaries.d.ts +2 -0
  160. package/dist/validation/validators/laroux-boundaries.js +29 -0
  161. package/dist/validation/validators/mod-exports.d.ts +7 -0
  162. package/dist/validation/validators/mod-exports.js +32 -0
  163. package/dist/validation/validators/mod.d.ts +11 -0
  164. package/dist/validation/validators/mod.js +12 -0
  165. package/dist/validation/validators/server-loc.d.ts +2 -0
  166. package/dist/validation/validators/server-loc.js +48 -0
  167. package/dist/versions.d.ts +166 -0
  168. package/dist/versions.js +349 -0
  169. package/dist/violation-baseline.d.ts +58 -0
  170. package/dist/violation-baseline.js +144 -0
  171. package/dist/walk-stream.d.ts +24 -0
  172. package/dist/walk-stream.js +55 -0
  173. package/dist/workspace-discovery.d.ts +60 -0
  174. package/dist/workspace-discovery.js +130 -0
  175. package/package.json +222 -3
@@ -0,0 +1,158 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ import { runtime } from "@eserstack/standards/cross-runtime";
3
+ import * as loader from "./loader.js";
4
+ /**
5
+ * Error thrown when workspace configuration is invalid.
6
+ */
7
+ export class WorkspaceError extends Error {
8
+ constructor(message) {
9
+ super(message);
10
+ this.name = "WorkspaceError";
11
+ }
12
+ }
13
+ /**
14
+ * Loads package configuration from a directory.
15
+ * Throws an error if no config file is found.
16
+ *
17
+ * @param path - Directory to load config from
18
+ * @returns PackageConfig with loaded configuration
19
+ * @throws {WorkspaceError} If no package config file is found
20
+ */
21
+ export const loadPackageConfig = async (path) => {
22
+ try {
23
+ return await loader.load({ baseDir: path });
24
+ }
25
+ catch (e) {
26
+ if (e instanceof loader.PackageLoadError) {
27
+ throw new WorkspaceError(`No package config file found in ${runtime.path.resolve(path)}`);
28
+ }
29
+ throw e;
30
+ }
31
+ };
32
+ /**
33
+ * Expands workspace patterns (including globs) to actual directory paths.
34
+ *
35
+ * Supports simple trailing `/*` patterns (e.g. `pkg/*`, `apps/*`).
36
+ */
37
+ const expandWorkspacePaths = async (root, patterns) => {
38
+ const paths = [];
39
+ for (const pattern of patterns) {
40
+ // Check if pattern ends with a simple wildcard like "pkg/*"
41
+ if (pattern.endsWith("/*")) {
42
+ const parentDir = runtime.path.join(root, pattern.slice(0, -2));
43
+ try {
44
+ // Patterns expand one at a time, so the paths keep the pattern order.
45
+ // oxlint-disable-next-line no-await-in-loop
46
+ for await (const entry of runtime.fs.readDir(parentDir)) {
47
+ if (entry.isDirectory) {
48
+ paths.push(runtime.path.join(parentDir, entry.name));
49
+ }
50
+ }
51
+ }
52
+ catch {
53
+ // Directory doesn't exist — skip silently
54
+ }
55
+ }
56
+ else if (pattern.includes("*") || pattern.includes("?")) {
57
+ // More complex glob — walk parent and match with regex
58
+ const parts = pattern.split("/");
59
+ const staticParts = [];
60
+ let globStart = 0;
61
+ for (let i = 0; i < parts.length; i++) {
62
+ if (parts[i].includes("*") || parts[i].includes("?")) {
63
+ globStart = i;
64
+ break;
65
+ }
66
+ staticParts.push(parts[i]);
67
+ }
68
+ const parentDir = runtime.path.join(root, ...staticParts);
69
+ const globPart = parts.slice(globStart).join("/");
70
+ const regexPattern = globPart
71
+ .replace(/[.+^${}()|[\]\\]/g, "\\$&")
72
+ .replace(/\*\*/g, ".*")
73
+ .replace(/\*/g, "[^/]*")
74
+ .replace(/\?/g, "[^/]");
75
+ const regex = new RegExp(`^${regexPattern}$`);
76
+ try {
77
+ // oxlint-disable-next-line no-await-in-loop
78
+ for await (const entry of runtime.fs.walk(parentDir, {
79
+ includeDirs: true,
80
+ includeFiles: false,
81
+ })) {
82
+ const rel = runtime.path.relative(parentDir, entry.path);
83
+ if (rel !== "" && regex.test(rel)) {
84
+ paths.push(entry.path);
85
+ }
86
+ }
87
+ }
88
+ catch {
89
+ // Directory doesn't exist — skip silently
90
+ }
91
+ }
92
+ else {
93
+ // Direct path — no glob
94
+ paths.push(runtime.path.join(root, pattern));
95
+ }
96
+ }
97
+ return paths;
98
+ };
99
+ /**
100
+ * Gets all workspace member modules from a root configuration.
101
+ *
102
+ * @param root - Root directory containing workspace config
103
+ * @returns Tuple of [root config, workspace modules]
104
+ */
105
+ export const getWorkspaceModules = async (root) => {
106
+ const rootConfig = await loadPackageConfig(root);
107
+ const workspaces = rootConfig.workspaces?.value;
108
+ if (!Array.isArray(workspaces)) {
109
+ throw new WorkspaceError("Package config doesn't have workspace field.");
110
+ }
111
+ // Validate all workspace entries are strings
112
+ for (const workspace of workspaces) {
113
+ if (typeof workspace !== "string") {
114
+ throw new WorkspaceError("Package config workspace field should be an array of strings.");
115
+ }
116
+ }
117
+ // Expand glob patterns to actual paths
118
+ const workspacePaths = await expandWorkspacePaths(root, workspaces);
119
+ const result = [];
120
+ for (const workspacePath of workspacePaths) {
121
+ // Packages load one at a time to bound open file handles in a large
122
+ // workspace.
123
+ // oxlint-disable-next-line no-await-in-loop
124
+ const workspaceConfig = await loader.tryLoad({ baseDir: workspacePath });
125
+ if (workspaceConfig === undefined) {
126
+ continue;
127
+ }
128
+ const name = workspaceConfig.name?.value;
129
+ const version = workspaceConfig.version?.value;
130
+ const isPrivate = workspaceConfig.private?.value;
131
+ if (name === undefined) {
132
+ continue;
133
+ }
134
+ // Skip private packages (test apps, etc.)
135
+ if (isPrivate === true) {
136
+ continue;
137
+ }
138
+ result.push({
139
+ name,
140
+ version: version ?? "0.0.0",
141
+ config: workspaceConfig,
142
+ });
143
+ }
144
+ return [rootConfig, result];
145
+ };
146
+ /**
147
+ * Finds a module by name in a list of workspace modules.
148
+ * Matches either the full name or a partial name suffix.
149
+ *
150
+ * @param module - Module name to find (can be partial, e.g., "config" matches "@eserstack/config")
151
+ * @param modules - List of workspace modules to search
152
+ * @returns The matching module or undefined
153
+ */
154
+ export const getModule = (module, modules) => {
155
+ return modules.find((m) => m.name === module || m.name.endsWith(`/${module}`));
156
+ };
157
+ // Re-export getBaseDir for convenience
158
+ export { getBaseDir } from "./loader.js";
@@ -0,0 +1,52 @@
1
+ import type { ConfigFileType, PackageConfig, PackageFieldName, UpdateOptions, UpdateResult } from "./types.ts";
2
+ /**
3
+ * Error thrown when updating package configuration fails.
4
+ */
5
+ export declare class PackageUpdateError extends Error {
6
+ readonly filepath?: string;
7
+ constructor(message: string, filepath?: string, cause?: Error);
8
+ }
9
+ /**
10
+ * Updates a single field across config files.
11
+ *
12
+ * @param config - The package config to update
13
+ * @param fieldName - Name of the field to update
14
+ * @param value - New value for the field
15
+ * @param options - Update options
16
+ * @returns Result of the update operation
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const config = await load({ baseDir: "./pkg/@eserstack/config" });
21
+ * const result = await updateField(config, "version", "1.0.0");
22
+ * console.log(result.updated); // ["./pkg/@eserstack/config/package.json"]
23
+ * ```
24
+ */
25
+ export declare const updateField: <T>(config: PackageConfig, fieldName: PackageFieldName, value: T, options?: UpdateOptions) => Promise<UpdateResult>;
26
+ /**
27
+ * Updates multiple fields at once.
28
+ *
29
+ * @param config - The package config to update
30
+ * @param updates - Map of field names to new values
31
+ * @param options - Update options
32
+ * @returns Combined result of all update operations
33
+ */
34
+ export declare const updateFields: (config: PackageConfig, updates: Partial<Record<PackageFieldName, unknown>>, options?: UpdateOptions) => Promise<UpdateResult>;
35
+ /**
36
+ * Updates the version field specifically.
37
+ * Writes to ALL config files that have a version field.
38
+ */
39
+ export declare const updateVersion: (config: PackageConfig, newVersion: string, options?: Omit<UpdateOptions, "targetFiles">) => Promise<UpdateResult>;
40
+ /**
41
+ * Synchronizes a field value from its primary origin to all other files
42
+ * that can contain this field.
43
+ */
44
+ export declare const syncField: (config: PackageConfig, fieldName: PackageFieldName) => Promise<UpdateResult>;
45
+ /**
46
+ * Gets all files that would be updated for a given field.
47
+ * Useful for dry-run scenarios.
48
+ */
49
+ export declare const getUpdateTargets: (config: PackageConfig, fieldName: PackageFieldName, options?: UpdateOptions) => ReadonlyArray<{
50
+ filepath: string;
51
+ fileType: ConfigFileType;
52
+ }>;
@@ -0,0 +1,278 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ import { runtime } from "@eserstack/standards/cross-runtime";
3
+ import * as types from "./types.js";
4
+ import * as loader from "./loader.js";
5
+ /**
6
+ * Error thrown when updating package configuration fails.
7
+ */
8
+ export class PackageUpdateError extends Error {
9
+ filepath;
10
+ constructor(message, filepath, cause) {
11
+ super(message, { cause });
12
+ this.name = "PackageUpdateError";
13
+ this.filepath = filepath;
14
+ }
15
+ }
16
+ /**
17
+ * Gets a nested property value from an object using a dot-separated path.
18
+ * Guards against prototype pollution by blocking dangerous property names.
19
+ */
20
+ const getPropertyByPath = (obj, path) => {
21
+ const parts = path.split(".");
22
+ let current = obj;
23
+ for (const part of parts) {
24
+ if (UNSAFE_PROPS.has(part)) {
25
+ return undefined;
26
+ }
27
+ if (current === null ||
28
+ current === undefined ||
29
+ typeof current !== "object") {
30
+ return undefined;
31
+ }
32
+ if (!Object.hasOwn(current, part)) {
33
+ return undefined;
34
+ }
35
+ current = current[part];
36
+ }
37
+ return current;
38
+ };
39
+ /**
40
+ * Check if a property name is safe (not a prototype pollution vector).
41
+ */
42
+ const UNSAFE_PROPS = new Set(["__proto__", "constructor", "prototype"]);
43
+ /**
44
+ * Sets a nested property value in an object using a dot-separated path.
45
+ * Prevents prototype pollution by blocking dangerous property names
46
+ * and only writing to own properties of plain objects.
47
+ */
48
+ const setPropertyByPath = (obj, path, value) => {
49
+ const parts = path.split(".");
50
+ // Validate ALL parts against unsafe props before traversal
51
+ for (const part of parts) {
52
+ if (part === undefined || part.length === 0 || UNSAFE_PROPS.has(part)) {
53
+ return;
54
+ }
55
+ }
56
+ let current = obj;
57
+ for (let i = 0; i < parts.length - 1; i++) {
58
+ const part = parts[i];
59
+ if (!Object.hasOwn(current, part) ||
60
+ typeof current[part] !== "object" ||
61
+ current[part] === null) {
62
+ const child = Object.create(null);
63
+ current[part] = child;
64
+ current = child;
65
+ }
66
+ else {
67
+ current = current[part];
68
+ }
69
+ }
70
+ const lastPart = parts[parts.length - 1];
71
+ current[lastPart] = value;
72
+ };
73
+ /**
74
+ * Formats JSON content with consistent style.
75
+ */
76
+ const formatJson = (content) => {
77
+ return `${JSON.stringify(content, null, 2)}\n`;
78
+ };
79
+ /**
80
+ * Writes updated content back to a config file.
81
+ */
82
+ const writeConfigFile = async (file, updates) => {
83
+ // Clone the content to avoid mutation
84
+ const newContent = JSON.parse(JSON.stringify(file.content));
85
+ // Apply updates
86
+ for (const [path, value] of Object.entries(updates)) {
87
+ setPropertyByPath(newContent, path, value);
88
+ }
89
+ // Write back to file
90
+ const formatted = formatJson(newContent);
91
+ await runtime.fs.writeTextFile(file.filepath, formatted);
92
+ // Update the in-memory representation
93
+ // Note: We need to cast away readonly for internal mutation
94
+ file.content = newContent;
95
+ file.rawText = formatted;
96
+ };
97
+ /**
98
+ * Determines which files to update based on options and field presence.
99
+ */
100
+ const resolveTargetFiles = (config, fieldName, options) => {
101
+ const { targetFiles = "all", createIfMissing = false } = options;
102
+ const field = config[fieldName];
103
+ if (targetFiles === "origin") {
104
+ if (field === undefined) {
105
+ const firstFile = config._loadedFiles[0];
106
+ return createIfMissing && firstFile !== undefined ? [firstFile] : [];
107
+ }
108
+ const originFile = config._loadedFiles.find((f) => f.filepath === field.origin.filepath);
109
+ return originFile ? [originFile] : [];
110
+ }
111
+ if (targetFiles === "all") {
112
+ if (!field && !createIfMissing) {
113
+ return [];
114
+ }
115
+ if (!field && createIfMissing) {
116
+ // Return all loaded files to create the field in each
117
+ return [...config._loadedFiles];
118
+ }
119
+ // Return files that have this field
120
+ return loader.getFilesWithField(config, fieldName);
121
+ }
122
+ // Specific file types requested
123
+ const requestedTypes = new Set(targetFiles);
124
+ return config._loadedFiles.filter((f) => requestedTypes.has(f.fileType));
125
+ };
126
+ /**
127
+ * Updates a single field across config files.
128
+ *
129
+ * @param config - The package config to update
130
+ * @param fieldName - Name of the field to update
131
+ * @param value - New value for the field
132
+ * @param options - Update options
133
+ * @returns Result of the update operation
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * const config = await load({ baseDir: "./pkg/@eserstack/config" });
138
+ * const result = await updateField(config, "version", "1.0.0");
139
+ * console.log(result.updated); // ["./pkg/@eserstack/config/package.json"]
140
+ * ```
141
+ */
142
+ export const updateField = async (config, fieldName, value, options = {}) => {
143
+ const updated = [];
144
+ const failed = [];
145
+ const skipped = [];
146
+ const targetFiles = resolveTargetFiles(config, fieldName, options);
147
+ if (targetFiles.length === 0) {
148
+ return { updated, failed, skipped };
149
+ }
150
+ // Each target is a different file, so they are written concurrently; the
151
+ // result lists keep the order of targetFiles.
152
+ const outcomes = await Promise.all(targetFiles.map(async (file) => {
153
+ const propertyPath = types.DEFAULT_FIELD_MAPPINGS[fieldName]?.[file.fileType];
154
+ if (propertyPath === undefined || propertyPath === "") {
155
+ return { kind: "skipped", filepath: file.filepath };
156
+ }
157
+ // Skip if value already matches
158
+ const currentValue = getPropertyByPath(file.content, propertyPath);
159
+ if (currentValue === value) {
160
+ return { kind: "skipped", filepath: file.filepath };
161
+ }
162
+ try {
163
+ await writeConfigFile(file, { [propertyPath]: value });
164
+ return { kind: "updated", filepath: file.filepath };
165
+ }
166
+ catch (error) {
167
+ return {
168
+ kind: "failed",
169
+ filepath: file.filepath,
170
+ reason: error instanceof Error ? error.message : String(error),
171
+ };
172
+ }
173
+ }));
174
+ for (const outcome of outcomes) {
175
+ if (outcome.kind === "updated") {
176
+ updated.push(outcome.filepath);
177
+ }
178
+ else if (outcome.kind === "skipped") {
179
+ skipped.push(outcome.filepath);
180
+ }
181
+ else {
182
+ failed.push({ filepath: outcome.filepath, reason: outcome.reason });
183
+ }
184
+ }
185
+ // Update the in-memory config representation
186
+ if (updated.length > 0) {
187
+ const existingField = config[fieldName];
188
+ if (existingField) {
189
+ existingField.value = value;
190
+ }
191
+ else {
192
+ // Create new tracked field
193
+ const firstUpdated = config._loadedFiles.find((f) => updated.includes(f.filepath));
194
+ if (firstUpdated) {
195
+ const propertyPath = types.DEFAULT_FIELD_MAPPINGS[fieldName]?.[firstUpdated.fileType];
196
+ if (propertyPath) {
197
+ // Cast to allow assignment
198
+ config[fieldName] = {
199
+ value,
200
+ origin: {
201
+ filepath: firstUpdated.filepath,
202
+ fileType: firstUpdated.fileType,
203
+ propertyPath,
204
+ },
205
+ alternateOrigins: [],
206
+ };
207
+ }
208
+ }
209
+ }
210
+ }
211
+ return { updated, failed, skipped };
212
+ };
213
+ /**
214
+ * Updates multiple fields at once.
215
+ *
216
+ * @param config - The package config to update
217
+ * @param updates - Map of field names to new values
218
+ * @param options - Update options
219
+ * @returns Combined result of all update operations
220
+ */
221
+ export const updateFields = async (config, updates, options = {}) => {
222
+ const updatedSet = new Set();
223
+ const allFailed = [];
224
+ const allSkipped = [];
225
+ for (const [fieldName, value] of Object.entries(updates)) {
226
+ // Fields are updated one at a time: every field writes the same config
227
+ // files, so concurrent updates would overwrite each other.
228
+ // oxlint-disable-next-line no-await-in-loop
229
+ const result = await updateField(config, fieldName, value, options);
230
+ for (const filepath of result.updated) {
231
+ updatedSet.add(filepath);
232
+ }
233
+ allFailed.push(...result.failed);
234
+ allSkipped.push(...result.skipped);
235
+ }
236
+ return {
237
+ updated: Array.from(updatedSet),
238
+ failed: allFailed,
239
+ skipped: [...new Set(allSkipped)],
240
+ };
241
+ };
242
+ /**
243
+ * Updates the version field specifically.
244
+ * Writes to ALL config files that have a version field.
245
+ */
246
+ export const updateVersion = async (config, newVersion, options) => {
247
+ return await updateField(config, "version", newVersion, {
248
+ ...options,
249
+ targetFiles: "all", // Always update all files for version
250
+ });
251
+ };
252
+ /**
253
+ * Synchronizes a field value from its primary origin to all other files
254
+ * that can contain this field.
255
+ */
256
+ export const syncField = async (config, fieldName) => {
257
+ const field = config[fieldName];
258
+ if (field === undefined) {
259
+ return { updated: [], failed: [], skipped: [] };
260
+ }
261
+ return await updateField(config, fieldName, field.value, {
262
+ targetFiles: "all",
263
+ createIfMissing: false,
264
+ });
265
+ };
266
+ /**
267
+ * Gets all files that would be updated for a given field.
268
+ * Useful for dry-run scenarios.
269
+ */
270
+ export const getUpdateTargets = (config, fieldName, options = {}) => {
271
+ const targetFiles = resolveTargetFiles(config, fieldName, options);
272
+ return targetFiles
273
+ .filter((file) => types.DEFAULT_FIELD_MAPPINGS[fieldName]?.[file.fileType])
274
+ .map((file) => ({
275
+ filepath: file.filepath,
276
+ fileType: file.fileType,
277
+ }));
278
+ };