-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathtask-config.js
More file actions
323 lines (292 loc) · 11.9 KB
/
Copy pathtask-config.js
File metadata and controls
323 lines (292 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const fs = require('fs');
const os = require('os');
const path = require('path');
const { parse, printParseErrorCode } = require('jsonc-parser');
const dotenv = require('dotenv');
const TASKS_RELATIVE_PATH = path.join('.vscode', 'tasks.json');
function platformKey(platform = process.platform) {
if (platform === 'darwin') return 'osx';
if (platform === 'win32') return 'windows';
return 'linux';
}
function mergeOptions(...values) {
const result = {};
for (const value of values) {
if (!value || typeof value !== 'object' || Array.isArray(value)) continue;
const previousEnv = result.env;
Object.assign(result, value);
if (value.env || previousEnv) result.env = { ...(previousEnv || {}), ...(value.env || {}) };
}
return result;
}
function mergeConfig(base, override) {
if (!override || typeof override !== 'object' || Array.isArray(override)) return { ...base };
return {
...base,
...override,
options: mergeOptions(base.options, override.options),
presentation: { ...(base.presentation || {}), ...(override.presentation || {}) },
};
}
function valueOf(value) {
if (value && typeof value === 'object' && !Array.isArray(value) && 'value' in value) {
return value.value;
}
return value;
}
function expandString(value, context) {
if (typeof value !== 'string') return value;
const variables = {
workspaceFolder: context.workspaceFolder,
workspaceFolderBasename: path.basename(context.workspaceFolder),
userHome: context.userHome || os.homedir(),
pathSeparator: path.sep,
'/': path.sep,
cwd: context.workspaceFolder,
};
return value.replace(/\$\{([^}]+)\}/g, (match, name) => {
if (Object.prototype.hasOwnProperty.call(variables, name)) return variables[name];
if (name.startsWith('env:')) return context.env?.[name.slice(4)] ?? '';
const error = new Error(`Unsupported task variable ${match}`);
error.unsupportedVariable = true;
throw error;
});
}
function expandValue(value, context) {
if (typeof value === 'string') return expandString(value, context);
if (Array.isArray(value)) return value.map(item => expandValue(item, context));
if (value && typeof value === 'object') {
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, expandValue(item, context)]));
}
return value;
}
function parseJsonc(text, filePath = 'tasks.json') {
const errors = [];
const document = parse(text, errors, { allowTrailingComma: true, disallowComments: false });
if (errors.length) {
const first = errors[0];
throw new Error(`${filePath}: ${printParseErrorCode(first.error)} at offset ${first.offset}`);
}
if (!document || typeof document !== 'object' || Array.isArray(document)) {
throw new Error(`${filePath}: expected a JSON object`);
}
return document;
}
function loadEnvFile(envFile, context, readFile = fs.readFileSync) {
if (!envFile) return {};
const expanded = expandString(String(envFile), context);
const filePath = path.isAbsolute(expanded)
? expanded
: path.resolve(context.workspaceFolder, expanded);
try {
return dotenv.parse(readFile(filePath, 'utf8'));
} catch (error) {
if (error?.code === 'ENOENT' && context.fallbackWorkspaceFolder) {
const fallbackContext = { ...context, workspaceFolder: context.fallbackWorkspaceFolder };
const fallbackExpanded = expandString(String(envFile), fallbackContext);
const fallbackPath = path.isAbsolute(fallbackExpanded)
? fallbackExpanded
: path.resolve(context.fallbackWorkspaceFolder, fallbackExpanded);
try {
return dotenv.parse(readFile(fallbackPath, 'utf8'));
} catch {}
}
throw new Error(`Could not read task env file ${filePath}: ${error.message}`);
}
}
function normalizeArgs(args, context) {
if (args == null) return [];
if (!Array.isArray(args)) throw new Error('Task args must be an array');
return args.map(arg => String(expandValue(valueOf(arg), context)));
}
function normalizeTask(rawTask, root, context, key) {
const platform = key || platformKey();
const rootWithPlatform = mergeConfig(root, root[platform]);
let task = mergeConfig(rawTask, rawTask?.[platform]);
task = {
...task,
options: mergeOptions(rootWithPlatform.options, task.options),
presentation: { ...(rootWithPlatform.presentation || {}), ...(task.presentation || {}) },
};
const label = task.label || task.taskName;
if (!label || typeof label !== 'string') throw new Error('Every task must have a string label');
const variableContext = {
...context,
env: { ...(context.env || process.env) },
};
const envFile = task.options?.envFile || task.envFile;
const fileEnv = loadEnvFile(envFile, variableContext, context.readFile);
const rawEnv = { ...fileEnv, ...(task.options?.env || {}) };
const expandedEnv = {};
for (const [name, value] of Object.entries(rawEnv)) {
if (value === null) expandedEnv[name] = null;
else expandedEnv[name] = String(expandValue(value, { ...variableContext, env: { ...variableContext.env, ...fileEnv } }));
}
variableContext.env = { ...variableContext.env, ...fileEnv, ...expandedEnv };
const dependsOn = task.dependsOn == null
? []
: (Array.isArray(task.dependsOn) ? task.dependsOn : [task.dependsOn]).map(String);
const type = task.type || (task.command != null ? 'shell' : 'compound');
const compound = type === 'compound' || (task.command == null && dependsOn.length > 0 && type !== 'npm');
const cwdValue = task.options?.cwd || context.workspaceFolder;
const cwd = path.resolve(context.workspaceFolder, expandString(String(cwdValue), variableContext));
const normalized = {
label,
type: compound ? 'compound' : type,
detail: typeof task.detail === 'string' ? expandString(task.detail, variableContext) : '',
dependsOn,
dependsOrder: task.dependsOrder === 'sequence' ? 'sequence' : 'parallel',
isBackground: task.isBackground === true,
cwd,
env: expandedEnv,
presentation: task.presentation || {},
problemMatcher: task.problemMatcher,
supported: true,
};
if (compound) return normalized;
if (type === 'npm') {
const script = valueOf(task.script || task.command);
if (!script) throw new Error(`Task "${label}" is missing its npm script`);
normalized.type = 'npm';
normalized.executable = process.platform === 'win32' ? 'npm.cmd' : 'npm';
normalized.args = ['run', expandString(String(script), variableContext)];
const extraArgs = normalizeArgs(task.args, variableContext);
if (extraArgs.length) normalized.args.push('--', ...extraArgs);
normalized.useShell = true;
return normalized;
}
if (type !== 'shell' && type !== 'process') {
normalized.supported = false;
normalized.error = `Task type "${type}" is not supported yet`;
return normalized;
}
const command = valueOf(task.command);
if (command == null || command === '') throw new Error(`Task "${label}" is missing its command`);
normalized.executable = expandString(String(command), variableContext);
if (context.fallbackWorkspaceFolder
&& path.isAbsolute(normalized.executable)
&& !fs.existsSync(normalized.executable)) {
const fallbackExecutable = expandString(String(command), {
...variableContext,
workspaceFolder: context.fallbackWorkspaceFolder,
});
if (fs.existsSync(fallbackExecutable)) normalized.executable = fallbackExecutable;
}
normalized.args = normalizeArgs(task.args, variableContext);
normalized.useShell = type === 'shell';
return normalized;
}
// Editor-context variables (${file}, ${relativeFile}, ${lineNumber}, ...) have no
// value outside a focused editor, so a headless run can never resolve them. Keep
// that task visible but unrunnable instead of failing the whole file with it.
function unsupportedTask(rawTask, label, cwd, message) {
const detail = typeof rawTask?.detail === 'string' && !rawTask.detail.includes('${')
? rawTask.detail
: '';
return {
label,
type: 'shell',
detail,
dependsOn: [],
dependsOrder: 'parallel',
isBackground: false,
cwd,
env: {},
presentation: rawTask?.presentation || {},
problemMatcher: undefined,
supported: false,
error: message,
};
}
function loadTasksFromText(text, options) {
const workspaceFolder = path.resolve(options.workspaceFolder);
const filePath = options.filePath || path.join(workspaceFolder, TASKS_RELATIVE_PATH);
const root = parseJsonc(text, filePath);
if (root.version && root.version !== '2.0.0') {
throw new Error(`${filePath}: only tasks.json version 2.0.0 is supported`);
}
if (!Array.isArray(root.tasks)) throw new Error(`${filePath}: expected a tasks array`);
const context = {
workspaceFolder,
userHome: options.userHome,
env: options.env || process.env,
readFile: options.readFile || fs.readFileSync,
fallbackWorkspaceFolder: options.fallbackWorkspaceFolder,
};
const key = platformKey(options.platform);
const tasks = root.tasks.map(rawTask => {
try {
return normalizeTask(rawTask, root, context, key);
} catch (error) {
const label = rawTask?.label || rawTask?.taskName;
// Without a label there is no row to degrade into, so let it fail the file.
if (!error?.unsupportedVariable || !label || typeof label !== 'string') throw error;
return unsupportedTask(rawTask, label, workspaceFolder, error.message);
}
});
const duplicate = tasks.find((task, index) => tasks.findIndex(other => other.label === task.label) !== index);
if (duplicate) throw new Error(`${filePath}: duplicate task label "${duplicate.label}"`);
return tasks;
}
function worktreeParentPath(workspaceFolder) {
const marker = `${path.sep}.claude${path.sep}worktrees${path.sep}`;
const markerIndex = workspaceFolder.indexOf(marker);
if (markerIndex === -1) return null;
const branchPart = workspaceFolder.slice(markerIndex + marker.length);
if (!branchPart || branchPart.includes(path.sep)) return null;
return workspaceFolder.slice(0, markerIndex);
}
// A worktree without its own tasks.json inherits its parent repo's. The parent
// is either known to the caller (a project worktree under <project>/repos/,
// whose source repo is recorded in the database) or inferred from the
// `.claude/worktrees/<name>` layout the Claude CLI uses.
function taskFileForWorkspace(workspaceFolder, existsSync = fs.existsSync, knownParentPath = null) {
const localFile = path.join(workspaceFolder, TASKS_RELATIVE_PATH);
if (existsSync(localFile)) return { filePath: localFile, inherited: false };
const parentPath = knownParentPath || worktreeParentPath(workspaceFolder);
if (!parentPath) return null;
const parentFile = path.join(parentPath, TASKS_RELATIVE_PATH);
if (!existsSync(parentFile)) return null;
return { filePath: parentFile, inherited: true, parentPath };
}
function loadProjectTasks(workspaceFolder, options = {}) {
const source = taskFileForWorkspace(workspaceFolder, fs.existsSync, options.parentPath || null);
if (!source) return [];
const { filePath } = source;
const text = fs.readFileSync(filePath, 'utf8');
return loadTasksFromText(text, {
...options,
workspaceFolder,
filePath,
fallbackWorkspaceFolder: source.inherited ? source.parentPath : options.fallbackWorkspaceFolder,
});
}
function resolveTaskGraph(tasks, label) {
const byLabel = new Map(tasks.map(task => [task.label, task]));
const visiting = new Set();
function visit(taskLabel) {
const task = byLabel.get(taskLabel);
if (!task) throw new Error(`Task "${taskLabel}" was not found`);
if (!task.supported) throw new Error(task.error);
if (visiting.has(taskLabel)) {
throw new Error(`Task dependency cycle detected at "${taskLabel}"`);
}
visiting.add(taskLabel);
const dependencies = task.dependsOn.map(visit);
visiting.delete(taskLabel);
return { task, dependencies };
}
return visit(label);
}
module.exports = {
TASKS_RELATIVE_PATH,
expandString,
loadProjectTasks,
loadTasksFromText,
normalizeTask,
parseJsonc,
platformKey,
resolveTaskGraph,
taskFileForWorkspace,
worktreeParentPath,
};