release-cherry-pick 0.0.0-stage → 1.0.1
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/.atl/.skill-registry.cache.json +3 -0
- package/.atl/skill-registry.md +55 -0
- package/.husky/pre-commit +1 -0
- package/CLAUDE.md +74 -0
- package/LICENSE +21 -0
- package/README.md +181 -2
- package/eslint.config.js +34 -0
- package/openspec/changes/archive/.gitkeep +0 -0
- package/openspec/changes/node-cli-release-cherry-pick/.openspec.yaml +2 -0
- package/openspec/changes/node-cli-release-cherry-pick/design.md +126 -0
- package/openspec/changes/node-cli-release-cherry-pick/proposal.md +39 -0
- package/openspec/changes/node-cli-release-cherry-pick/specs/branch-selection/spec.md +42 -0
- package/openspec/changes/node-cli-release-cherry-pick/specs/cherry-pick-orchestration/spec.md +49 -0
- package/openspec/changes/node-cli-release-cherry-pick/specs/cli-core/spec.md +37 -0
- package/openspec/changes/node-cli-release-cherry-pick/specs/config-management/spec.md +52 -0
- package/openspec/changes/node-cli-release-cherry-pick/specs/documentation-generation/spec.md +38 -0
- package/openspec/changes/node-cli-release-cherry-pick/specs/gitlab-integration/spec.md +49 -0
- package/openspec/changes/node-cli-release-cherry-pick/specs/release-summary/spec.md +37 -0
- package/openspec/changes/node-cli-release-cherry-pick/tasks.md +70 -0
- package/openspec/config.yaml +32 -0
- package/openspec/specs/.gitkeep +0 -0
- package/package.json +48 -4
- package/scripts/sandbox/harness.js +130 -0
- package/scripts/sandbox/make-sandbox.sh +90 -0
- package/scripts/sandbox/run-config.js +26 -0
- package/scripts/sandbox/run-release.js +26 -0
- package/scripts/sandbox/test-package.js +571 -0
- package/skills-lock.json +11 -0
- package/src/cli/index.js +13 -0
- package/src/config/ai-providers.js +285 -0
- package/src/config/defaults.js +45 -0
- package/src/config/loader.js +127 -0
- package/src/config/setup.js +463 -0
- package/src/config/validator.js +80 -0
- package/src/doc/agents-generator.js +83 -0
- package/src/doc/rpd-generator.js +87 -0
- package/src/git/branch-parser.js +88 -0
- package/src/git/branch-selector.js +97 -0
- package/src/git/cherry-pick.js +216 -0
- package/src/git/git-auth.js +43 -0
- package/src/git/instance.js +17 -0
- package/src/git/release-branch.js +106 -0
- package/src/git/release-status.js +121 -0
- package/src/git/remote-url.js +34 -0
- package/src/git/repo-state.js +153 -0
- package/src/gitlab/ai-description.js +157 -0
- package/src/gitlab/client.js +324 -0
- package/src/gitlab/mr-creator.js +55 -0
- package/src/gitlab/reviewer-selector.js +52 -0
- package/src/index.js +58 -0
- package/src/report/summary.js +91 -0
- package/src/utils/errors.js +47 -0
- package/src/utils/logger.js +36 -0
- package/src/utils/prompts.js +59 -0
- package/src/utils/secrets.js +24 -0
- package/src/workflow/release.js +464 -0
- package/test-e2e.js +56 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const logger = require('../utils/logger');
|
|
4
|
+
|
|
5
|
+
const DEFAULT_PROVIDER = 'anthropic';
|
|
6
|
+
const MODELS_DEV_URL = 'https://models.dev/api.json';
|
|
7
|
+
const CATALOG_TIMEOUT_MS = 8000;
|
|
8
|
+
const LIVE_TIMEOUT_MS = 10000;
|
|
9
|
+
|
|
10
|
+
// Used when models.dev is unreachable, or when a popular provider has no `api` field in the catalog.
|
|
11
|
+
const KNOWN_BASE_URLS = {
|
|
12
|
+
anthropic: 'https://api.anthropic.com/v1',
|
|
13
|
+
openai: 'https://api.openai.com/v1',
|
|
14
|
+
google: null,
|
|
15
|
+
groq: 'https://api.groq.com/openai/v1',
|
|
16
|
+
mistral: 'https://api.mistral.ai/v1',
|
|
17
|
+
xai: 'https://api.x.ai/v1',
|
|
18
|
+
cerebras: 'https://api.cerebras.ai/v1',
|
|
19
|
+
togetherai: 'https://api.together.xyz/v1',
|
|
20
|
+
fireworksai: 'https://api.fireworks.ai/inference/v1',
|
|
21
|
+
perplexity: 'https://api.perplexity.ai',
|
|
22
|
+
deepseek: 'https://api.deepseek.com',
|
|
23
|
+
openrouter: 'https://openrouter.ai/api/v1',
|
|
24
|
+
'github-copilot': 'https://api.githubcopilot.com',
|
|
25
|
+
lmstudio: 'http://127.0.0.1:1234/v1',
|
|
26
|
+
alibaba: 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1',
|
|
27
|
+
moonshotai: 'https://api.moonshot.ai/v1',
|
|
28
|
+
nvidia: 'https://integrate.api.nvidia.com/v1'
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// Minimal offline fallback so the wizard still works without network access.
|
|
32
|
+
const FALLBACK_CATALOG = {
|
|
33
|
+
anthropic: {
|
|
34
|
+
id: 'anthropic',
|
|
35
|
+
name: 'Anthropic',
|
|
36
|
+
env: 'ANTHROPIC_API_KEY',
|
|
37
|
+
api: KNOWN_BASE_URLS.anthropic,
|
|
38
|
+
models: {
|
|
39
|
+
'claude-haiku-4-5': { id: 'claude-haiku-4-5', tool_call: true },
|
|
40
|
+
'claude-sonnet-4-5': { id: 'claude-sonnet-4-5', tool_call: true },
|
|
41
|
+
'claude-opus-4-5': { id: 'claude-opus-4-5', tool_call: true }
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
openai: {
|
|
45
|
+
id: 'openai',
|
|
46
|
+
name: 'OpenAI',
|
|
47
|
+
env: 'OPENAI_API_KEY',
|
|
48
|
+
api: KNOWN_BASE_URLS.openai,
|
|
49
|
+
models: {
|
|
50
|
+
'gpt-4o-mini': { id: 'gpt-4o-mini', tool_call: true },
|
|
51
|
+
'gpt-4o': { id: 'gpt-4o', tool_call: true },
|
|
52
|
+
'gpt-4.1-mini': { id: 'gpt-4.1-mini', tool_call: true },
|
|
53
|
+
'gpt-4.1': { id: 'gpt-4.1', tool_call: true }
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
google: {
|
|
57
|
+
id: 'google',
|
|
58
|
+
name: 'Google',
|
|
59
|
+
env: 'GOOGLE_API_KEY,GEMINI_API_KEY',
|
|
60
|
+
api: null,
|
|
61
|
+
models: {
|
|
62
|
+
'gemini-2.5-flash': { id: 'gemini-2.5-flash', tool_call: true },
|
|
63
|
+
'gemini-2.5-pro': { id: 'gemini-2.5-pro', tool_call: true }
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
groq: {
|
|
67
|
+
id: 'groq',
|
|
68
|
+
name: 'Groq',
|
|
69
|
+
env: 'GROQ_API_KEY',
|
|
70
|
+
api: KNOWN_BASE_URLS.groq,
|
|
71
|
+
models: {
|
|
72
|
+
'llama-3.3-70b-versatile': { id: 'llama-3.3-70b-versatile', tool_call: true }
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
deepseek: {
|
|
76
|
+
id: 'deepseek',
|
|
77
|
+
name: 'DeepSeek',
|
|
78
|
+
env: 'DEEPSEEK_API_KEY',
|
|
79
|
+
api: KNOWN_BASE_URLS.deepseek,
|
|
80
|
+
models: {
|
|
81
|
+
'deepseek-chat': { id: 'deepseek-chat', tool_call: true },
|
|
82
|
+
'deepseek-reasoner': { id: 'deepseek-reasoner', tool_call: true }
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
openrouter: {
|
|
86
|
+
id: 'openrouter',
|
|
87
|
+
name: 'OpenRouter',
|
|
88
|
+
env: 'OPENROUTER_API_KEY',
|
|
89
|
+
api: KNOWN_BASE_URLS.openrouter,
|
|
90
|
+
models: {
|
|
91
|
+
'openai/gpt-4o-mini': { id: 'openai/gpt-4o-mini', tool_call: true },
|
|
92
|
+
'anthropic/claude-sonnet-4.5': { id: 'anthropic/claude-sonnet-4.5', tool_call: true }
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
let catalogCache = null;
|
|
98
|
+
let catalogCacheAt = 0;
|
|
99
|
+
|
|
100
|
+
function normalizeEnvKeys(env) {
|
|
101
|
+
if (!env) return [];
|
|
102
|
+
if (Array.isArray(env)) return env.filter(Boolean);
|
|
103
|
+
return String(env).split(',').map((key) => key.trim()).filter(Boolean);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function getEnvKeyNames(provider) {
|
|
107
|
+
if (!provider) return [];
|
|
108
|
+
return normalizeEnvKeys(provider.env);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function findEnvValue(envKeyNames) {
|
|
112
|
+
for (const key of envKeyNames) {
|
|
113
|
+
if (process.env[key]) return process.env[key];
|
|
114
|
+
}
|
|
115
|
+
return '';
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function fetchWithTimeout(url, options = {}, timeoutMs = CATALOG_TIMEOUT_MS) {
|
|
119
|
+
const controller = new globalThis.AbortController();
|
|
120
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
121
|
+
try {
|
|
122
|
+
return await globalThis.fetch(url, { ...options, signal: controller.signal });
|
|
123
|
+
} finally {
|
|
124
|
+
clearTimeout(timer);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Loads the full provider catalog (200+ providers) from models.dev — same source OpenCode uses.
|
|
129
|
+
async function loadProviderCatalog({ force = false } = {}) {
|
|
130
|
+
const now = Date.now();
|
|
131
|
+
if (!force && catalogCache && now - catalogCacheAt < 5 * 60 * 1000) {
|
|
132
|
+
return catalogCache;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
const response = await fetchWithTimeout(MODELS_DEV_URL);
|
|
137
|
+
if (!response.ok) {
|
|
138
|
+
throw new Error(`models.dev HTTP ${response.status}`);
|
|
139
|
+
}
|
|
140
|
+
const data = await response.json();
|
|
141
|
+
if (!data || typeof data !== 'object' || Object.keys(data).length === 0) {
|
|
142
|
+
throw new Error('models.dev returned an empty catalog');
|
|
143
|
+
}
|
|
144
|
+
catalogCache = data;
|
|
145
|
+
catalogCacheAt = Date.now();
|
|
146
|
+
return data;
|
|
147
|
+
} catch (error) {
|
|
148
|
+
logger.warn(`Could not load provider catalog from models.dev (${error.message}); using built-in fallback list`);
|
|
149
|
+
catalogCache = FALLBACK_CATALOG;
|
|
150
|
+
catalogCacheAt = Date.now();
|
|
151
|
+
return catalogCache;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function isChatModel(model) {
|
|
156
|
+
if (!model) return false;
|
|
157
|
+
if (model.tool_call) return true;
|
|
158
|
+
const output = model.modalities && model.modalities.output;
|
|
159
|
+
return Array.isArray(output) && output.includes('text');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function catalogModelIds(provider) {
|
|
163
|
+
if (!provider || !provider.models) return [];
|
|
164
|
+
return Object.keys(provider.models)
|
|
165
|
+
.filter((id) => isChatModel(provider.models[id]))
|
|
166
|
+
.sort((a, b) => a.localeCompare(b));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function getProviderChoices(catalog) {
|
|
170
|
+
return Object.values(catalog)
|
|
171
|
+
.map((provider) => ({
|
|
172
|
+
name: provider.name || provider.id,
|
|
173
|
+
value: provider.id
|
|
174
|
+
}))
|
|
175
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function resolveApiKey(config, providerId, providerEntry) {
|
|
179
|
+
if (config && config.ai && config.ai.apiKey) {
|
|
180
|
+
return config.ai.apiKey;
|
|
181
|
+
}
|
|
182
|
+
const envValue = findEnvValue(getEnvKeyNames(providerEntry));
|
|
183
|
+
if (envValue) return envValue;
|
|
184
|
+
// Legacy single-key configs from earlier releases
|
|
185
|
+
const legacyEnv = providerId === 'anthropic' ? 'ANTHROPIC_API_KEY'
|
|
186
|
+
: providerId === 'openai' ? 'OPENAI_API_KEY' : '';
|
|
187
|
+
return legacyEnv && process.env[legacyEnv] ? process.env[legacyEnv] : '';
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function resolveBaseURL(providerId, providerEntry, config) {
|
|
191
|
+
if (config && config.ai && config.ai.baseURL) return config.ai.baseURL;
|
|
192
|
+
if (providerEntry && providerEntry.api) return providerEntry.api;
|
|
193
|
+
return KNOWN_BASE_URLS[providerId] || null;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function joinUrl(base, path) {
|
|
197
|
+
return `${String(base).replace(/\/+$/, '')}${path}`;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function listModelsLive(providerId, provider, apiKey, baseURL) {
|
|
201
|
+
if (!apiKey) return null;
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
if (providerId === 'anthropic') {
|
|
205
|
+
const response = await fetchWithTimeout(joinUrl(baseURL || KNOWN_BASE_URLS.anthropic, '/models?limit=1000'), {
|
|
206
|
+
headers: {
|
|
207
|
+
'x-api-key': apiKey,
|
|
208
|
+
'anthropic-version': '2023-06-01'
|
|
209
|
+
}
|
|
210
|
+
}, LIVE_TIMEOUT_MS);
|
|
211
|
+
if (!response.ok) return null;
|
|
212
|
+
const data = await response.json();
|
|
213
|
+
const models = (data.data || []).map((m) => m.id).filter(Boolean);
|
|
214
|
+
return models.length ? models.sort((a, b) => a.localeCompare(b)) : null;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (providerId === 'google') {
|
|
218
|
+
const response = await fetchWithTimeout(
|
|
219
|
+
`https://generativelanguage.googleapis.com/v1beta/models?key=${encodeURIComponent(apiKey)}&pageSize=200`,
|
|
220
|
+
{},
|
|
221
|
+
LIVE_TIMEOUT_MS
|
|
222
|
+
);
|
|
223
|
+
if (!response.ok) return null;
|
|
224
|
+
const data = await response.json();
|
|
225
|
+
const models = (data.models || [])
|
|
226
|
+
.map((m) => (m.name || '').replace(/^models\//, ''))
|
|
227
|
+
.filter(Boolean);
|
|
228
|
+
return models.length ? models.sort((a, b) => a.localeCompare(b)) : null;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (baseURL) {
|
|
232
|
+
const response = await fetchWithTimeout(joinUrl(baseURL, '/models'), {
|
|
233
|
+
headers: { Authorization: `Bearer ${apiKey}` }
|
|
234
|
+
}, LIVE_TIMEOUT_MS);
|
|
235
|
+
if (!response.ok) return null;
|
|
236
|
+
const data = await response.json();
|
|
237
|
+
const raw = Array.isArray(data.data) ? data.data : Array.isArray(data.models) ? data.models : [];
|
|
238
|
+
const models = raw
|
|
239
|
+
.map((m) => m.id || m.name || (typeof m === 'string' ? m : null))
|
|
240
|
+
.filter(Boolean);
|
|
241
|
+
return models.length ? models.sort((a, b) => a.localeCompare(b)) : null;
|
|
242
|
+
}
|
|
243
|
+
} catch (error) {
|
|
244
|
+
logger.warn(`Live model list from provider failed: ${error.message}`);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Connects to the provider and lists its models; falls back to the models.dev catalog.
|
|
251
|
+
async function listModelsForProvider(providerId, provider, apiKey, baseURL) {
|
|
252
|
+
const live = await listModelsLive(providerId, provider, apiKey, baseURL);
|
|
253
|
+
if (live && live.length > 0) {
|
|
254
|
+
return { models: live, source: 'live' };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const fromCatalog = catalogModelIds(provider);
|
|
258
|
+
if (fromCatalog.length > 0) {
|
|
259
|
+
return { models: fromCatalog, source: 'catalog' };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (provider && provider.models) {
|
|
263
|
+
const all = Object.keys(provider.models).sort((a, b) => a.localeCompare(b));
|
|
264
|
+
if (all.length > 0) {
|
|
265
|
+
return { models: all, source: 'catalog' };
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return { models: [], source: 'none' };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
module.exports = {
|
|
273
|
+
DEFAULT_PROVIDER,
|
|
274
|
+
KNOWN_BASE_URLS,
|
|
275
|
+
FALLBACK_CATALOG,
|
|
276
|
+
loadProviderCatalog,
|
|
277
|
+
getProviderChoices,
|
|
278
|
+
getEnvKeyNames,
|
|
279
|
+
findEnvValue,
|
|
280
|
+
catalogModelIds,
|
|
281
|
+
resolveApiKey,
|
|
282
|
+
resolveBaseURL,
|
|
283
|
+
listModelsForProvider,
|
|
284
|
+
normalizeEnvKeys
|
|
285
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const defaultConfig = {
|
|
4
|
+
git: {
|
|
5
|
+
stagingBranch: 'staging',
|
|
6
|
+
branchPrefix: {
|
|
7
|
+
feature: 'feature/',
|
|
8
|
+
hotfix: 'hotfix/',
|
|
9
|
+
release: 'release/'
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
gitlab: {
|
|
13
|
+
host: '',
|
|
14
|
+
token: ''
|
|
15
|
+
},
|
|
16
|
+
ai: {
|
|
17
|
+
enabled: true,
|
|
18
|
+
// Left empty on purpose: when AI is enabled and no provider/model is saved, ensureAiSettings
|
|
19
|
+
// asks for them at release time. baseURL stays empty too, so each provider resolves its own
|
|
20
|
+
// endpoint (resolveBaseURL prefers config.ai.baseURL, which would otherwise win for any provider).
|
|
21
|
+
provider: '',
|
|
22
|
+
apiKey: '',
|
|
23
|
+
model: '',
|
|
24
|
+
baseURL: ''
|
|
25
|
+
},
|
|
26
|
+
release: {
|
|
27
|
+
autoCreateMR: true,
|
|
28
|
+
mrTitleFormat: '[{taskId}] {description}',
|
|
29
|
+
// GitLab usernames, picked from the real project members in the setup wizard
|
|
30
|
+
defaultReviewers: [],
|
|
31
|
+
defaultAssignees: [],
|
|
32
|
+
mrSquash: true,
|
|
33
|
+
mrRemoveSourceBranch: true,
|
|
34
|
+
branches: []
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
function getDefaultConfig() {
|
|
39
|
+
return JSON.parse(JSON.stringify(defaultConfig));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = {
|
|
43
|
+
getDefaultConfig,
|
|
44
|
+
defaultConfig
|
|
45
|
+
};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const crypto = require('crypto');
|
|
7
|
+
const { execFileSync } = require('child_process');
|
|
8
|
+
const { ConfigError } = require('../utils/errors');
|
|
9
|
+
const { parseRemoteUrl } = require('../git/remote-url');
|
|
10
|
+
const { getDefaultConfig } = require('./defaults');
|
|
11
|
+
const { validateConfig } = require('./validator');
|
|
12
|
+
|
|
13
|
+
// The config can hold secrets (gitlab.token, ai.apiKey), so it lives in the developer's home
|
|
14
|
+
// directory, never inside the project, where it could end up committed. One file per project.
|
|
15
|
+
// RELEASE_CHERRY_PICK_CONFIG_DIR overrides the directory (used by the sandbox and tests).
|
|
16
|
+
function getConfigDir() {
|
|
17
|
+
return process.env.RELEASE_CHERRY_PICK_CONFIG_DIR || path.join(os.homedir(), '.release-cherry-pick');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function runGit(args) {
|
|
21
|
+
try {
|
|
22
|
+
return execFileSync('git', args, { cwd: process.cwd(), encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim();
|
|
23
|
+
} catch {
|
|
24
|
+
return '';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function toFileName(value) {
|
|
29
|
+
return value.toLowerCase().replace(/[^a-z0-9._-]+/g, '_').replace(/^[_.]+|_+$/g, '');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Identifies the current project: from the origin remote (host + project path, e.g.
|
|
33
|
+
// "gitlab.example.com_group_proj"), so it's unique even when two checkouts share a folder name and
|
|
34
|
+
// stable if the folder is moved or renamed; without a usable remote, from the repository folder name
|
|
35
|
+
// plus a short hash of its absolute path.
|
|
36
|
+
function getProjectKey() {
|
|
37
|
+
const remote = parseRemoteUrl(runGit(['remote', 'get-url', 'origin']));
|
|
38
|
+
if (remote && remote.host && remote.projectPath) {
|
|
39
|
+
return toFileName(`${remote.host}_${remote.projectPath}`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const root = path.resolve(runGit(['rev-parse', '--show-toplevel']) || process.cwd());
|
|
43
|
+
const normalizedRoot = process.platform === 'win32' ? root.toLowerCase() : root;
|
|
44
|
+
const hash = crypto.createHash('sha1').update(normalizedRoot).digest('hex').slice(0, 8);
|
|
45
|
+
return `${toFileName(path.basename(root)) || 'project'}-${hash}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Resolved once per run (per working directory): the project doesn't change during a release.
|
|
49
|
+
const configPathCache = new Map();
|
|
50
|
+
function getConfigPath() {
|
|
51
|
+
const cwd = process.cwd();
|
|
52
|
+
if (!configPathCache.has(cwd)) {
|
|
53
|
+
configPathCache.set(cwd, path.join(getConfigDir(), `${getProjectKey()}.json`));
|
|
54
|
+
}
|
|
55
|
+
return configPathCache.get(cwd);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function isPlainObject(value) {
|
|
59
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Recursively fills in whatever `config` is missing from `defaults`. Values present in `config`
|
|
63
|
+
// win (arrays are replaced, not merged); keys unknown to the defaults are kept as-is.
|
|
64
|
+
function mergeWithDefaults(defaults, config) {
|
|
65
|
+
if (!isPlainObject(defaults) || !isPlainObject(config)) {
|
|
66
|
+
return config === undefined ? defaults : config;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const merged = { ...defaults };
|
|
70
|
+
for (const [key, value] of Object.entries(config)) {
|
|
71
|
+
merged[key] = key in defaults ? mergeWithDefaults(defaults[key], value) : value;
|
|
72
|
+
}
|
|
73
|
+
return merged;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Returns the config file merged over the defaults (so options added in newer versions are always
|
|
77
|
+
// present), or null if there's no config file. Validates the result unless `validate` is false.
|
|
78
|
+
function loadConfig({ validate = true } = {}) {
|
|
79
|
+
const configPath = getConfigPath();
|
|
80
|
+
|
|
81
|
+
if (!fs.existsSync(configPath)) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let fileConfig;
|
|
86
|
+
try {
|
|
87
|
+
fileConfig = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
|
88
|
+
} catch (error) {
|
|
89
|
+
throw new ConfigError(`Error reading config file ${configPath}: ${error.message}`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (!isPlainObject(fileConfig)) {
|
|
93
|
+
throw new ConfigError(`Config file ${configPath} must contain a JSON object`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const config = mergeWithDefaults(getDefaultConfig(), fileConfig);
|
|
97
|
+
if (validate) {
|
|
98
|
+
validateConfig(config, configPath);
|
|
99
|
+
}
|
|
100
|
+
return config;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function saveConfig(config) {
|
|
104
|
+
const configPath = getConfigPath();
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
// Owner-only permissions (applied on Linux/macOS; on Windows the home folder is already private)
|
|
108
|
+
fs.mkdirSync(path.dirname(configPath), { recursive: true, mode: 0o700 });
|
|
109
|
+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), { encoding: 'utf8', mode: 0o600 });
|
|
110
|
+
return true;
|
|
111
|
+
} catch (error) {
|
|
112
|
+
throw new ConfigError(`Error saving config file ${configPath}: ${error.message}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function configExists() {
|
|
117
|
+
return fs.existsSync(getConfigPath());
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
module.exports = {
|
|
121
|
+
loadConfig,
|
|
122
|
+
saveConfig,
|
|
123
|
+
configExists,
|
|
124
|
+
getConfigPath,
|
|
125
|
+
getConfigDir,
|
|
126
|
+
mergeWithDefaults
|
|
127
|
+
};
|