@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.
- package/README.md +301 -2
- package/dist/changelog-gen.d.ts +105 -0
- package/dist/changelog-gen.js +498 -0
- package/dist/changelog-text.d.ts +39 -0
- package/dist/changelog-text.js +82 -0
- package/dist/cli-support.d.ts +140 -0
- package/dist/cli-support.js +244 -0
- package/dist/cli-system/app-opts.d.ts +15 -0
- package/dist/cli-system/app-opts.js +13 -0
- package/dist/cli-system/app.d.ts +29 -0
- package/dist/cli-system/app.js +12 -0
- package/dist/cli-system/handlers/completions-setup.d.ts +14 -0
- package/dist/cli-system/handlers/completions-setup.js +139 -0
- package/dist/cli-system/handlers/completions.d.ts +3 -0
- package/dist/cli-system/handlers/completions.js +79 -0
- package/dist/cli-system/handlers/doctor.d.ts +8 -0
- package/dist/cli-system/handlers/doctor.js +146 -0
- package/dist/cli-system/handlers/info.d.ts +3 -0
- package/dist/cli-system/handlers/info.js +69 -0
- package/dist/cli-system/handlers/install.d.ts +8 -0
- package/dist/cli-system/handlers/install.js +137 -0
- package/dist/cli-system/handlers/mod.d.ts +12 -0
- package/dist/cli-system/handlers/mod.js +13 -0
- package/dist/cli-system/handlers/uninstall.d.ts +3 -0
- package/dist/cli-system/handlers/uninstall.js +60 -0
- package/dist/cli-system/handlers/update.d.ts +76 -0
- package/dist/cli-system/handlers/update.js +310 -0
- package/dist/cli-system/handlers/version-check.d.ts +16 -0
- package/dist/cli-system/handlers/version-check.js +176 -0
- package/dist/cli-system/handlers/version.d.ts +3 -0
- package/dist/cli-system/handlers/version.js +45 -0
- package/dist/cli-system/mod.d.ts +19 -0
- package/dist/cli-system/mod.js +52 -0
- package/dist/commitmsg.d.ts +28 -0
- package/dist/commitmsg.js +232 -0
- package/dist/directive-analysis.d.ts +75 -0
- package/dist/directive-analysis.js +223 -0
- package/dist/ffi-client.d.ts +11 -0
- package/dist/ffi-client.js +12 -0
- package/dist/file-tool.d.ts +96 -0
- package/dist/file-tool.js +274 -0
- package/dist/file-tools-shared.d.ts +81 -0
- package/dist/file-tools-shared.js +276 -0
- package/dist/gh-contributors.d.ts +89 -0
- package/dist/gh-contributors.js +205 -0
- package/dist/gh.d.ts +3 -0
- package/dist/gh.js +64 -0
- package/dist/git-listing.d.ts +27 -0
- package/dist/git-listing.js +69 -0
- package/dist/git.d.ts +172 -0
- package/dist/git.js +277 -0
- package/dist/install.d.ts +34 -0
- package/dist/install.js +421 -0
- package/dist/main.d.ts +22 -0
- package/dist/main.js +14 -0
- package/dist/mod.d.ts +25 -0
- package/dist/mod.js +8 -0
- package/dist/module.d.ts +10 -0
- package/dist/module.js +221 -0
- package/dist/native-functions.d.ts +1088 -0
- package/dist/native-functions.js +2 -0
- package/dist/npm-externals.d.ts +27 -0
- package/dist/npm-externals.js +34 -0
- package/dist/npm-workspace-specifiers.d.ts +6 -0
- package/dist/npm-workspace-specifiers.js +45 -0
- package/dist/package/loader.d.ts +34 -0
- package/dist/package/loader.js +203 -0
- package/dist/package/mod.d.ts +36 -0
- package/dist/package/mod.js +5 -0
- package/dist/package/types.d.ts +142 -0
- package/dist/package/types.js +32 -0
- package/dist/package/workspace.d.ts +33 -0
- package/dist/package/workspace.js +158 -0
- package/dist/package/writer.d.ts +52 -0
- package/dist/package/writer.js +278 -0
- package/dist/package.json +275 -0
- package/dist/release-notes.d.ts +90 -0
- package/dist/release-notes.js +236 -0
- package/dist/release-tag.d.ts +46 -0
- package/dist/release-tag.js +95 -0
- package/dist/release.d.ts +145 -0
- package/dist/release.js +584 -0
- package/dist/source-mask.d.ts +3 -0
- package/dist/source-mask.js +185 -0
- package/dist/validate-bom.d.ts +5 -0
- package/dist/validate-bom.js +43 -0
- package/dist/validate-case-conflict.d.ts +5 -0
- package/dist/validate-case-conflict.js +43 -0
- package/dist/validate-circular-deps.d.ts +11 -0
- package/dist/validate-circular-deps.js +53 -0
- package/dist/validate-code-conventions.d.ts +22 -0
- package/dist/validate-code-conventions.js +166 -0
- package/dist/validate-commit-msg.d.ts +50 -0
- package/dist/validate-commit-msg.js +182 -0
- package/dist/validate-docs.d.ts +20 -0
- package/dist/validate-docs.js +78 -0
- package/dist/validate-eof.d.ts +5 -0
- package/dist/validate-eof.js +61 -0
- package/dist/validate-error-coverage.d.ts +28 -0
- package/dist/validate-error-coverage.js +93 -0
- package/dist/validate-export-names.d.ts +17 -0
- package/dist/validate-export-names.js +56 -0
- package/dist/validate-filenames.d.ts +5 -0
- package/dist/validate-filenames.js +151 -0
- package/dist/validate-json.d.ts +5 -0
- package/dist/validate-json.js +71 -0
- package/dist/validate-large-files.d.ts +5 -0
- package/dist/validate-large-files.js +41 -0
- package/dist/validate-laroux-boundaries.d.ts +19 -0
- package/dist/validate-laroux-boundaries.js +59 -0
- package/dist/validate-licenses.d.ts +5 -0
- package/dist/validate-licenses.js +137 -0
- package/dist/validate-line-endings.d.ts +5 -0
- package/dist/validate-line-endings.js +47 -0
- package/dist/validate-merge-conflict.d.ts +5 -0
- package/dist/validate-merge-conflict.js +46 -0
- package/dist/validate-mod-exports.d.ts +16 -0
- package/dist/validate-mod-exports.js +56 -0
- package/dist/validate-naming-and-todos.d.ts +5 -0
- package/dist/validate-naming-and-todos.js +70 -0
- package/dist/validate-runtime-js-apis.d.ts +5 -0
- package/dist/validate-runtime-js-apis.js +78 -0
- package/dist/validate-secrets.d.ts +5 -0
- package/dist/validate-secrets.js +82 -0
- package/dist/validate-self-imports.d.ts +5 -0
- package/dist/validate-self-imports.js +78 -0
- package/dist/validate-server-loc.d.ts +32 -0
- package/dist/validate-server-loc.js +94 -0
- package/dist/validate-shebangs.d.ts +5 -0
- package/dist/validate-shebangs.js +33 -0
- package/dist/validate-stream.d.ts +25 -0
- package/dist/validate-stream.js +62 -0
- package/dist/validate-submodules.d.ts +5 -0
- package/dist/validate-submodules.js +43 -0
- package/dist/validate-symlinks.d.ts +5 -0
- package/dist/validate-symlinks.js +46 -0
- package/dist/validate-toml.d.ts +5 -0
- package/dist/validate-toml.js +40 -0
- package/dist/validate-trailing-whitespace.d.ts +5 -0
- package/dist/validate-trailing-whitespace.js +49 -0
- package/dist/validate-yaml.d.ts +5 -0
- package/dist/validate-yaml.js +40 -0
- package/dist/validation/config.d.ts +17 -0
- package/dist/validation/config.js +30 -0
- package/dist/validation/mod.d.ts +71 -0
- package/dist/validation/mod.js +479 -0
- package/dist/validation/registry.d.ts +57 -0
- package/dist/validation/registry.js +256 -0
- package/dist/validation/types.d.ts +101 -0
- package/dist/validation/types.js +2 -0
- package/dist/validation/validators/circular-deps.d.ts +7 -0
- package/dist/validation/validators/circular-deps.js +31 -0
- package/dist/validation/validators/docs.d.ts +7 -0
- package/dist/validation/validators/docs.js +38 -0
- package/dist/validation/validators/error-coverage.d.ts +2 -0
- package/dist/validation/validators/error-coverage.js +46 -0
- package/dist/validation/validators/export-names.d.ts +7 -0
- package/dist/validation/validators/export-names.js +31 -0
- package/dist/validation/validators/laroux-boundaries.d.ts +2 -0
- package/dist/validation/validators/laroux-boundaries.js +29 -0
- package/dist/validation/validators/mod-exports.d.ts +7 -0
- package/dist/validation/validators/mod-exports.js +32 -0
- package/dist/validation/validators/mod.d.ts +11 -0
- package/dist/validation/validators/mod.js +12 -0
- package/dist/validation/validators/server-loc.d.ts +2 -0
- package/dist/validation/validators/server-loc.js +48 -0
- package/dist/versions.d.ts +166 -0
- package/dist/versions.js +349 -0
- package/dist/violation-baseline.d.ts +58 -0
- package/dist/violation-baseline.js +144 -0
- package/dist/walk-stream.d.ts +24 -0
- package/dist/walk-stream.js +55 -0
- package/dist/workspace-discovery.d.ts +60 -0
- package/dist/workspace-discovery.js +130 -0
- 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
|
+
};
|