barndog 0.1.1 → 0.2.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 +8 -7
- package/dist/cli.js +11 -7
- package/dist/commands/add.js +33 -21
- package/dist/commands/arguments.js +12 -0
- package/dist/commands/asset-install.js +160 -0
- package/dist/commands/asset-list.js +121 -0
- package/dist/commands/asset-share.js +94 -0
- package/dist/commands/auth.js +4 -0
- package/dist/commands/availability.js +54 -26
- package/dist/commands/capabilities.js +71 -415
- package/dist/commands/collaboration.js +194 -110
- package/dist/commands/environment.js +18 -411
- package/dist/commands/github.js +200 -0
- package/dist/commands/helpers.js +39 -0
- package/dist/commands/hooks.js +87 -0
- package/dist/commands/init.js +4 -2
- package/dist/commands/knowledge.js +73 -0
- package/dist/commands/mcp.js +62 -0
- package/dist/commands/origin.js +21 -23
- package/dist/commands/people.js +51 -0
- package/dist/commands/sets.js +141 -0
- package/dist/commands/status.js +34 -24
- package/dist/commands/sync.js +11 -4
- package/dist/commands/workspace.js +33 -19
- package/dist/config/environment.js +13 -27
- package/dist/config/paths.js +62 -0
- package/dist/config/selection.js +30 -0
- package/dist/config/settings.js +97 -108
- package/dist/config/state.js +57 -57
- package/dist/entry/knowledge-startup.js +32 -0
- package/dist/entry/mcp-launch.js +71 -0
- package/dist/entry/opencode-plugin.js +62 -0
- package/dist/{features/git-workspace/startup.js → entry/workspace-startup.js} +7 -2
- package/dist/errors.js +198 -55
- package/dist/features/asset-kinds/index.js +120 -0
- package/dist/features/asset-selection/index.js +51 -19
- package/dist/features/auth/credentials.js +80 -47
- package/dist/features/auth/session.js +80 -45
- package/dist/features/auth/transport.js +66 -15
- package/dist/features/catalog/index.js +32 -61
- package/dist/features/collaboration/api.js +160 -0
- package/dist/features/collaboration/index.js +321 -233
- package/dist/features/collaboration/remote.js +35 -115
- package/dist/features/collaboration/results.js +2 -0
- package/dist/features/collaboration/workspace-git.js +233 -149
- package/dist/features/collaboration/workspace-github.js +117 -54
- package/dist/features/collaboration/workspace-store.js +140 -100
- package/dist/features/collaboration/workspace.js +257 -172
- package/dist/features/discovery/index.js +68 -57
- package/dist/features/doctor/index.js +32 -24
- package/dist/features/environment/auto-sync.js +28 -27
- package/dist/features/environment/names.js +67 -47
- package/dist/features/environment/operations.js +321 -228
- package/dist/features/environment/own-entries.js +89 -0
- package/dist/features/environment/settle-names.js +91 -0
- package/dist/features/environment/stage.js +178 -0
- package/dist/features/environment/sync.js +113 -484
- package/dist/features/environment/transaction.js +179 -147
- package/dist/features/environment/writes.js +196 -0
- package/dist/features/github/account.js +25 -0
- package/dist/features/github/connections.js +119 -0
- package/dist/features/github/pulls.js +118 -0
- package/dist/features/hooks/adapt.js +32 -25
- package/dist/features/hooks/catalog.js +28 -0
- package/dist/features/hooks/compatibility.js +49 -0
- package/dist/features/hooks/divergence.js +9 -6
- package/dist/features/hooks/test.js +74 -49
- package/dist/features/hooks/types.js +1 -0
- package/dist/features/imports/discover.js +134 -61
- package/dist/features/imports/folder.js +48 -14
- package/dist/features/imports/local.js +113 -134
- package/dist/features/imports/remote.js +44 -25
- package/dist/features/inspection/index.js +17 -17
- package/dist/features/inventory/index.js +99 -110
- package/dist/features/knowledge/index.js +69 -49
- package/dist/features/mcps/catalog.js +31 -0
- package/dist/features/mcps/import.js +316 -0
- package/dist/features/mcps/index.js +98 -49
- package/dist/features/migration/migrate.js +263 -0
- package/dist/features/{git-workspace → migration}/move.js +165 -109
- package/dist/features/migration/promote.js +24 -0
- package/dist/features/native/adapters.js +53 -94
- package/dist/features/native/configuration.js +128 -82
- package/dist/features/native/hook-shell.js +55 -0
- package/dist/features/native/plugin-module.js +56 -0
- package/dist/features/onboarding/environment.js +67 -34
- package/dist/features/onboarding/setup.js +43 -40
- package/dist/features/repositories/context.js +30 -13
- package/dist/features/server/catalog.js +58 -0
- package/dist/features/server/client.js +13 -0
- package/dist/features/server/connection.js +37 -0
- package/dist/features/server/origin.js +74 -0
- package/dist/features/server/pages.js +24 -0
- package/dist/features/skills/catalog.js +42 -0
- package/dist/features/workspace/assets.js +57 -0
- package/dist/features/workspace/cloud-source.js +34 -0
- package/dist/features/workspace/directory.js +38 -0
- package/dist/features/{git-workspace → workspace}/format.js +114 -51
- package/dist/features/workspace/git.js +214 -0
- package/dist/features/workspace/installed.js +92 -0
- package/dist/features/{git-workspace → workspace}/overlay.js +43 -23
- package/dist/features/workspace/packages.js +160 -0
- package/dist/features/{git-workspace → workspace}/publish.js +120 -64
- package/dist/features/workspace/repo-source.js +192 -0
- package/dist/features/{git-workspace → workspace}/setup.js +60 -42
- package/dist/features/{git-workspace → workspace}/snapshot.js +53 -18
- package/dist/features/workspace/source.js +1 -0
- package/dist/features/workspace/tree.js +52 -0
- package/dist/features/workspace/workspace-source.js +27 -0
- package/dist/harnesses/claude.js +23 -1
- package/dist/harnesses/codex.js +57 -1
- package/dist/harnesses/cursor.js +22 -1
- package/dist/harnesses/detect.js +6 -50
- package/dist/harnesses/formats.js +45 -0
- package/dist/harnesses/opencode.js +111 -0
- package/dist/harnesses/registry.js +35 -2
- package/dist/platform.js +375 -0
- package/dist/program.js +225 -54
- package/dist/runtime.js +1 -0
- package/dist/shared/cli-error.js +22 -0
- package/dist/shared/constants.js +32 -0
- package/dist/shared/files.js +72 -0
- package/dist/shared/lock.js +49 -0
- package/dist/shared/lookup.js +11 -0
- package/dist/shared/patterns.js +94 -0
- package/dist/shared/shell.js +22 -0
- package/dist/shared/values.js +26 -0
- package/dist/ui/add-asset.js +41 -400
- package/dist/ui/add-hook.js +213 -0
- package/dist/ui/add-mcp.js +107 -0
- package/dist/ui/add-prompts.js +47 -0
- package/dist/ui/add-skills.js +115 -0
- package/dist/ui/asset-list.js +31 -13
- package/dist/ui/assets.js +33 -44
- package/dist/ui/auth.js +43 -33
- package/dist/ui/clash.js +11 -6
- package/dist/ui/collaboration.js +116 -44
- package/dist/ui/connection.js +10 -4
- package/dist/ui/decision-error.js +24 -0
- package/dist/ui/environment-onboarding.js +35 -13
- package/dist/ui/format.js +66 -18
- package/dist/ui/hook-compatibility.js +14 -6
- package/dist/ui/mcp.js +28 -3
- package/dist/ui/move.js +10 -5
- package/dist/ui/onboarding.js +113 -73
- package/dist/ui/options.js +9 -0
- package/dist/ui/output.js +106 -43
- package/dist/ui/pick.js +45 -33
- package/dist/ui/scope-install.js +15 -7
- package/dist/ui/selection.js +15 -11
- package/dist/ui/theme.js +37 -14
- package/dist/version.js +6 -3
- package/package.json +13 -3
- package/dist/features/capabilities/index.js +0 -106
- package/dist/features/collaboration/domain.js +0 -90
- package/dist/features/environment/cloud-source.js +0 -27
- package/dist/features/environment/installed.js +0 -124
- package/dist/features/environment/packages.js +0 -120
- package/dist/features/environment/remote.js +0 -71
- package/dist/features/environment/workspace-source.js +0 -19
- package/dist/features/git-hosts/index.js +0 -148
- package/dist/features/git-workspace/assets.js +0 -71
- package/dist/features/git-workspace/git.js +0 -135
- package/dist/features/git-workspace/migrate.js +0 -188
- package/dist/features/git-workspace/source.js +0 -159
- package/dist/features/imports/mcp.js +0 -219
- package/dist/features/knowledge/startup.js +0 -25
- package/dist/features/native/mcp-launch.js +0 -57
- package/dist/features/skills/install.js +0 -25
- package/dist/features/skills/remote.js +0 -177
- package/dist/features/skills/sync.js +0 -121
- package/dist/features/skills/validation.js +0 -34
- /package/dist/features/environment/{source.js → events.js} +0 -0
package/dist/platform.js
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { access, readFile, stat } from "node:fs/promises";
|
|
2
|
+
import { constants } from "node:fs";
|
|
3
|
+
import { posix, win32 } from "node:path";
|
|
4
|
+
import { execFile, spawn } from "node:child_process";
|
|
5
|
+
import crossSpawn from "cross-spawn";
|
|
6
|
+
import { promisify } from "node:util";
|
|
7
|
+
import { CliError } from "./errors.js";
|
|
8
|
+
import { POSIX_MODE } from "./shared/constants.js";
|
|
9
|
+
import { ifExists } from "./shared/files.js";
|
|
10
|
+
import { posixQuote, powershellQuote } from "./shared/shell.js";
|
|
11
|
+
import { errorCode } from "./shared/values.js";
|
|
12
|
+
const execute = promisify(execFile);
|
|
13
|
+
/**
|
|
14
|
+
* Environment variables the operating system defines, read through
|
|
15
|
+
* `environmentValue()` (Windows names are case-insensitive).
|
|
16
|
+
*/
|
|
17
|
+
const OS_VARIABLES = {
|
|
18
|
+
path: "PATH",
|
|
19
|
+
/** Windows: extensions PATH lookup tries, such as `.EXE;.CMD`. */
|
|
20
|
+
pathExtensions: "PATHEXT",
|
|
21
|
+
/** Windows: `C:\Program Files` and its 32-bit twin. */
|
|
22
|
+
programFiles: "ProgramFiles",
|
|
23
|
+
programFilesX86: "ProgramFiles(x86)",
|
|
24
|
+
/** Windows: `C:\Users\<you>\AppData\Local`, where per-user apps install. */
|
|
25
|
+
localAppData: "LOCALAPPDATA",
|
|
26
|
+
/** Linux: XDG Base Directory specification. */
|
|
27
|
+
dataHome: "XDG_DATA_HOME",
|
|
28
|
+
dataDirectories: "XDG_DATA_DIRS",
|
|
29
|
+
};
|
|
30
|
+
/** cmd.exe's own PATHEXT when the variable is unset. */
|
|
31
|
+
const DEFAULT_PATH_EXTENSIONS = ".COM;.EXE;.BAT;.CMD";
|
|
32
|
+
/** XDG Base Directory specification default for `XDG_DATA_DIRS`. */
|
|
33
|
+
const DEFAULT_DATA_DIRECTORIES = "/usr/local/share:/usr/share";
|
|
34
|
+
/** Error codes meaning "not there" while probing a path that may not exist. */
|
|
35
|
+
const ABSENT = ["ENOENT", "ENOTDIR", "EACCES"];
|
|
36
|
+
export const isWindows = (platform) => platform === "win32";
|
|
37
|
+
export const pathsFor = (platform) => isWindows(platform) ? win32 : posix;
|
|
38
|
+
export const directoryLinkType = (platform) => isWindows(platform) ? "junction" : "dir";
|
|
39
|
+
/** Windows environment variable names are case insensitive. */
|
|
40
|
+
export function environmentValue(runtime, name) {
|
|
41
|
+
const key = isWindows(runtime.platform)
|
|
42
|
+
? Object.keys(runtime.env).find((key) => key.toLowerCase() === name.toLowerCase())
|
|
43
|
+
: name;
|
|
44
|
+
return key ? runtime.env[key] : undefined;
|
|
45
|
+
}
|
|
46
|
+
function executableNames(command, extensions, windows) {
|
|
47
|
+
if (windows && !win32.extname(command)) {
|
|
48
|
+
return extensions.map((extension) => command + extension.toLowerCase());
|
|
49
|
+
}
|
|
50
|
+
return [command];
|
|
51
|
+
}
|
|
52
|
+
/** Searches only the supplied PATH; tests can exercise Windows PATHEXT on any host. */
|
|
53
|
+
export async function findExecutable(command, runtime) {
|
|
54
|
+
const paths = pathsFor(runtime.platform);
|
|
55
|
+
const windows = isWindows(runtime.platform);
|
|
56
|
+
const extensions = windows
|
|
57
|
+
? (environmentValue(runtime, OS_VARIABLES.pathExtensions) ??
|
|
58
|
+
DEFAULT_PATH_EXTENSIONS).split(";")
|
|
59
|
+
: [""];
|
|
60
|
+
const names = executableNames(command, extensions, windows);
|
|
61
|
+
const locations = command.includes("/") || command.includes("\\")
|
|
62
|
+
? [""]
|
|
63
|
+
: (environmentValue(runtime, OS_VARIABLES.path) ?? "")
|
|
64
|
+
.split(windows ? ";" : ":")
|
|
65
|
+
.filter(Boolean);
|
|
66
|
+
for (const directory of locations) {
|
|
67
|
+
for (const name of names) {
|
|
68
|
+
const candidate = paths.resolve(runtime.cwd, directory.replaceAll('"', ""), name);
|
|
69
|
+
try {
|
|
70
|
+
if (!(await stat(candidate)).isFile()) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
await access(candidate, windows ? constants.F_OK : constants.X_OK);
|
|
74
|
+
return candidate;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
if (!ABSENT.includes(errorCode(error) ?? "")) {
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
/** Quotes for the shell people type fix hints into: PowerShell on Windows. */
|
|
86
|
+
const shellQuote = (value, platform) => isWindows(platform) ? powershellQuote(value) : posixQuote(value);
|
|
87
|
+
/**
|
|
88
|
+
* Fix hints name commands for the shell people type into: Bash or zsh on
|
|
89
|
+
* macOS and Linux, PowerShell on Windows (Windows PowerShell 5 has no `&&`).
|
|
90
|
+
*/
|
|
91
|
+
export function fixSteps(steps, platform) {
|
|
92
|
+
return steps.join(isWindows(platform) ? "; " : " && ");
|
|
93
|
+
}
|
|
94
|
+
export function removeCommand(path, platform) {
|
|
95
|
+
return isWindows(platform)
|
|
96
|
+
? `Remove-Item -Recurse -Force ${shellQuote(path, platform)}`
|
|
97
|
+
: `rm -rf ${shellQuote(path, platform)}`;
|
|
98
|
+
}
|
|
99
|
+
export function unsetCommand(variable, platform) {
|
|
100
|
+
return isWindows(platform)
|
|
101
|
+
? `Remove-Item Env:${variable}`
|
|
102
|
+
: `unset ${variable}`;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Windows PowerShell running `script`, passed encoded so no character in it
|
|
106
|
+
* needs quoting as a program argument.
|
|
107
|
+
*/
|
|
108
|
+
export function encodedPowerShell(script) {
|
|
109
|
+
return {
|
|
110
|
+
command: "powershell.exe",
|
|
111
|
+
args: [
|
|
112
|
+
"-NoLogo",
|
|
113
|
+
"-NoProfile",
|
|
114
|
+
"-NonInteractive",
|
|
115
|
+
"-EncodedCommand",
|
|
116
|
+
// -EncodedCommand takes Base64 of UTF-16LE text.
|
|
117
|
+
Buffer.from("[Console]::OutputEncoding=[System.Text.UTF8Encoding]::new($false); $OutputEncoding=[Console]::OutputEncoding; " +
|
|
118
|
+
script, "utf16le").toString("base64"),
|
|
119
|
+
],
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** A program run from a PowerShell line, keeping its exit code. */
|
|
123
|
+
export const powershellCall = (args) => `& ${args.map(powershellQuote).join(" ")}; exit $LASTEXITCODE`;
|
|
124
|
+
/**
|
|
125
|
+
* A program's stdout. Starts through cross-spawn, which also runs Windows
|
|
126
|
+
* `.cmd` and `.bat` shims (such as npm-installed tools) with safely quoted
|
|
127
|
+
* arguments. Fails when the program exits non-zero, runs past `timeoutMs` or
|
|
128
|
+
* prints more than `maxBytes`.
|
|
129
|
+
*/
|
|
130
|
+
export function programOutput(command, args, runtime, limits) {
|
|
131
|
+
return new Promise((resolve, reject) => {
|
|
132
|
+
const child = crossSpawn(command, [...args], {
|
|
133
|
+
env: { ...runtime.env },
|
|
134
|
+
cwd: runtime.cwd,
|
|
135
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
136
|
+
windowsHide: true,
|
|
137
|
+
});
|
|
138
|
+
let stdout = "";
|
|
139
|
+
const fail = (reason) => {
|
|
140
|
+
child.kill();
|
|
141
|
+
reject(new Error(`${command} ${reason}`));
|
|
142
|
+
};
|
|
143
|
+
const timer = setTimeout(() => fail("timed out"), limits.timeoutMs);
|
|
144
|
+
child.stdout?.on("data", (chunk) => {
|
|
145
|
+
stdout += chunk.toString("utf8");
|
|
146
|
+
if (stdout.length > limits.maxBytes) {
|
|
147
|
+
fail("printed too much");
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
child.once("error", (error) => {
|
|
151
|
+
clearTimeout(timer);
|
|
152
|
+
reject(error);
|
|
153
|
+
});
|
|
154
|
+
child.once("close", (code) => {
|
|
155
|
+
clearTimeout(timer);
|
|
156
|
+
if (code === 0) {
|
|
157
|
+
resolve(stdout);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
reject(new Error(`${command} exited with code ${code}`));
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Bash for a tool that runs hook commands in it: `/bin/sh` on macOS and
|
|
167
|
+
* Linux, Git for Windows' Bash on Windows (where Claude Code finds it too).
|
|
168
|
+
*/
|
|
169
|
+
export async function bashShell(runtime) {
|
|
170
|
+
if (!isWindows(runtime.platform)) {
|
|
171
|
+
return { command: "/bin/sh", args: ["-c"] };
|
|
172
|
+
}
|
|
173
|
+
const paths = pathsFor(runtime.platform);
|
|
174
|
+
for (const root of [
|
|
175
|
+
environmentValue(runtime, OS_VARIABLES.programFiles),
|
|
176
|
+
environmentValue(runtime, OS_VARIABLES.programFilesX86),
|
|
177
|
+
]) {
|
|
178
|
+
// Git for Windows' default install folder.
|
|
179
|
+
const candidate = root && paths.join(root, "Git", "bin", "bash.exe");
|
|
180
|
+
if (candidate && (await ifExists(stat(candidate)))?.isFile()) {
|
|
181
|
+
return { command: candidate, args: ["-c"] };
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const bash = await findExecutable("bash", runtime);
|
|
185
|
+
if (bash) {
|
|
186
|
+
return { command: bash, args: ["-c"] };
|
|
187
|
+
}
|
|
188
|
+
throw new CliError("This Hook runs in Bash, which isn't installed.", "Install Git for Windows from https://gitforwindows.org, then try again.");
|
|
189
|
+
}
|
|
190
|
+
/** POSIX Hook runtime declarations also recognize the Bash supplied by Git for Windows. */
|
|
191
|
+
export async function runtimeExecutable(command, runtime) {
|
|
192
|
+
const executable = await findExecutable(command, runtime);
|
|
193
|
+
if (executable ||
|
|
194
|
+
!isWindows(runtime.platform) ||
|
|
195
|
+
!["sh", "bash"].includes(command)) {
|
|
196
|
+
return executable;
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
return (await bashShell(runtime)).command;
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
if (error instanceof CliError) {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/** Kill the whole Hook/MCP process tree, including grandchildren on Windows. */
|
|
209
|
+
export function stopProcessTree(child, runtime, signal = "SIGKILL") {
|
|
210
|
+
if (!child.pid) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
if (isWindows(runtime.platform)) {
|
|
215
|
+
const killer = spawn("taskkill.exe",
|
|
216
|
+
// /T ends the process's children too, /F forces it.
|
|
217
|
+
["/pid", String(child.pid), "/T", "/F"], {
|
|
218
|
+
stdio: "ignore",
|
|
219
|
+
windowsHide: true,
|
|
220
|
+
env: { ...runtime.env },
|
|
221
|
+
cwd: runtime.cwd,
|
|
222
|
+
});
|
|
223
|
+
killer.on("error", () => child.kill(signal));
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
process.kill(-child.pid, signal);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
catch {
|
|
230
|
+
/* The child has already exited. */
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/** Windows DACLs replace POSIX modes: only the current user receives access. */
|
|
234
|
+
export async function protectPrivate(path, runtime) {
|
|
235
|
+
if (!isWindows(runtime.platform)) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const script = `$ErrorActionPreference='Stop'; $path=${shellQuote(path, runtime.platform)}; $sid=[Security.Principal.WindowsIdentity]::GetCurrent().User; $acl=Get-Acl -LiteralPath $path; $acl.SetOwner($sid); $acl.SetAccessRuleProtection($true,$false); foreach($rule in @($acl.Access)){$acl.RemoveAccessRuleSpecific($rule)}; $inherit=if((Get-Item -LiteralPath $path).PSIsContainer){[Security.AccessControl.InheritanceFlags]'ContainerInherit,ObjectInherit'}else{[Security.AccessControl.InheritanceFlags]::None}; $rule=New-Object Security.AccessControl.FileSystemAccessRule($sid,'FullControl',$inherit,'None','Allow'); $acl.AddAccessRule($rule); Set-Acl -LiteralPath $path -AclObject $acl`;
|
|
239
|
+
const invocation = encodedPowerShell(script);
|
|
240
|
+
await execute(invocation.command, invocation.args, {
|
|
241
|
+
env: { ...runtime.env },
|
|
242
|
+
cwd: runtime.cwd,
|
|
243
|
+
windowsHide: true,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Whether a file's POSIX mode keeps it private. On Windows the ACL that
|
|
248
|
+
* `protectPrivate()` sets on every write does that, and checking it would start
|
|
249
|
+
* PowerShell on every command, so any mode passes.
|
|
250
|
+
*/
|
|
251
|
+
export function privatePermissions(mode, platform) {
|
|
252
|
+
return isWindows(platform) || !(mode & POSIX_MODE.groupAndOtherBits);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Where a desktop app would be installed, as `[path to check, evidence to report]`:
|
|
256
|
+
* a macOS bundle counts only when it has a `Contents/MacOS` folder.
|
|
257
|
+
*/
|
|
258
|
+
function applicationCandidates(app, runtime, macRoots) {
|
|
259
|
+
const path = pathsFor(runtime.platform);
|
|
260
|
+
if (runtime.platform === "darwin") {
|
|
261
|
+
return macRoots.map((root) => {
|
|
262
|
+
const bundle = path.join(root, `${app.name}.app`);
|
|
263
|
+
return [path.join(bundle, "Contents", "MacOS"), bundle];
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
if (isWindows(runtime.platform)) {
|
|
267
|
+
const local = environmentValue(runtime, OS_VARIABLES.localAppData);
|
|
268
|
+
const programFiles = environmentValue(runtime, OS_VARIABLES.programFiles);
|
|
269
|
+
const executable = `${app.name}.exe`;
|
|
270
|
+
const folder = app.windowsFolder ?? app.name;
|
|
271
|
+
const paths = [
|
|
272
|
+
...(local
|
|
273
|
+
? [
|
|
274
|
+
path.join(local, "Programs", folder, executable),
|
|
275
|
+
path.join(local, folder, executable),
|
|
276
|
+
]
|
|
277
|
+
: []),
|
|
278
|
+
...(programFiles ? [path.join(programFiles, folder, executable)] : []),
|
|
279
|
+
];
|
|
280
|
+
return paths.map((candidate) => [candidate, candidate]);
|
|
281
|
+
}
|
|
282
|
+
const dataHome = environmentValue(runtime, OS_VARIABLES.dataHome) ??
|
|
283
|
+
path.join(runtime.home, ".local", "share");
|
|
284
|
+
const dataDirectories = environmentValue(runtime, OS_VARIABLES.dataDirectories) ??
|
|
285
|
+
DEFAULT_DATA_DIRECTORIES;
|
|
286
|
+
return [dataHome, ...dataDirectories.split(":")].map((root) => {
|
|
287
|
+
const entry = path.join(root, "applications", `${app.linuxEntry ?? app.name.toLowerCase()}.desktop`);
|
|
288
|
+
return [entry, entry];
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* The installed desktop app, or null. `macRoots` replaces `/Applications` and
|
|
293
|
+
* `~/Applications`, so tests don't see apps installed on the machine.
|
|
294
|
+
*/
|
|
295
|
+
export async function installedApplication(app, runtime, macRoots = [
|
|
296
|
+
"/Applications",
|
|
297
|
+
pathsFor(runtime.platform).join(runtime.home, "Applications"),
|
|
298
|
+
]) {
|
|
299
|
+
for (const [probe, evidence] of applicationCandidates(app, runtime, macRoots)) {
|
|
300
|
+
try {
|
|
301
|
+
await stat(probe);
|
|
302
|
+
return evidence;
|
|
303
|
+
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
if (!ABSENT.includes(errorCode(error) ?? "")) {
|
|
306
|
+
throw error;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
/** The `#!` that starts a script the kernel can run directly. */
|
|
313
|
+
const SHEBANG = "#!";
|
|
314
|
+
/**
|
|
315
|
+
* Whether a file is executable. Windows files have no executable bit, so
|
|
316
|
+
* there the flag comes from `known` (what BarnDog last wrote at that path),
|
|
317
|
+
* else from a `#!` first line, as Git for Windows guesses for a new script.
|
|
318
|
+
*/
|
|
319
|
+
export async function fileExecutable(path, mode, runtime, known) {
|
|
320
|
+
if (!isWindows(runtime.platform)) {
|
|
321
|
+
return !!(mode & POSIX_MODE.executableBits);
|
|
322
|
+
}
|
|
323
|
+
return (known ??
|
|
324
|
+
(await readFile(path)).subarray(0, SHEBANG.length).toString() === SHEBANG);
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* DOS device names are reserved even with an extension: `CON`, `con.txt`,
|
|
328
|
+
* `LPT1.log`, but not `console` or `com10`.
|
|
329
|
+
*
|
|
330
|
+
* @internal
|
|
331
|
+
*/
|
|
332
|
+
export const WINDOWS_DEVICE_NAME = /^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i;
|
|
333
|
+
/** A package path can be valid on macOS and Linux yet impossible to store on Windows. */
|
|
334
|
+
export function assertPackagePaths(entries, platform) {
|
|
335
|
+
if (!isWindows(platform)) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
const names = new Set(entries.map((entry) => entry.path.toLowerCase()));
|
|
339
|
+
if (names.size !== entries.length) {
|
|
340
|
+
throw new CliError("The package has paths that differ only in case, so it can't be stored on Windows.", "Ask its author to rename one of them.");
|
|
341
|
+
}
|
|
342
|
+
for (const { path } of entries) {
|
|
343
|
+
const parts = path.split("/");
|
|
344
|
+
for (const part of parts) {
|
|
345
|
+
if (invalidWindowsPart(part)) {
|
|
346
|
+
throw new CliError(`The package file ${path} has a name Windows can't store.`, "Ask its author to rename it.");
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
for (let depth = 1; depth < parts.length; depth++) {
|
|
350
|
+
if (names.has(parts.slice(0, depth).join("/").toLowerCase())) {
|
|
351
|
+
throw new CliError(`The package file ${path} is inside a folder that differs only in case from a file, so it can't be stored on Windows.`, "Ask its author to rename one of them.");
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function invalidWindowsPart(part) {
|
|
357
|
+
return (!part ||
|
|
358
|
+
part === "." ||
|
|
359
|
+
part === ".." ||
|
|
360
|
+
WINDOWS_DEVICE_NAME.test(part) ||
|
|
361
|
+
[...'<>:"\\|?*'].some((char) => part.includes(char)) ||
|
|
362
|
+
part.endsWith(".") ||
|
|
363
|
+
part.endsWith(" ") ||
|
|
364
|
+
[...part].some((char) => char.charCodeAt(0) < " ".charCodeAt(0)));
|
|
365
|
+
}
|
|
366
|
+
/** Resolve CLI input against the captured cwd; a drive-relative path must not consult Windows' hidden per-drive cwd. */
|
|
367
|
+
export function localPath(value, runtime) {
|
|
368
|
+
const paths = pathsFor(runtime.platform);
|
|
369
|
+
if (isWindows(runtime.platform) &&
|
|
370
|
+
value[1] === ":" &&
|
|
371
|
+
!paths.isAbsolute(value)) {
|
|
372
|
+
throw new CliError(`${value} is relative to the current folder of drive ${value.slice(0, 2)}, which BarnDog can't see.`, `Use an absolute path such as ${value.slice(0, 2)}\\${value.slice(2)}.`);
|
|
373
|
+
}
|
|
374
|
+
return paths.resolve(runtime.cwd, value);
|
|
375
|
+
}
|