pi-spaces 0.16.2 → 0.17.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/CHANGELOG.md +13 -0
- package/README.md +2 -0
- package/dist/command.js +34 -2
- package/dist/pin.js +207 -0
- package/extensions/spaces.ts +3 -1
- package/package.json +2 -1
- package/skills/pi-spaces/SKILL.md +1 -1
- package/src/command.ts +34 -2
- package/src/pin.ts +207 -0
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,19 @@ commands and options and a patch version fixes.
|
|
|
6
6
|
|
|
7
7
|
## Unreleased
|
|
8
8
|
|
|
9
|
+
## 0.17.0
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- `/space pin` pins the space this Pi runs in (`<profile>/<name>`, `<profile>/base` in base) to the
|
|
14
|
+
repository of the session's folder, the same marker as `pi-space pin`, so a bare `pi-space` there
|
|
15
|
+
starts it; `/space unpin` removes it. Only the current space.
|
|
16
|
+
- `pi-spaces/pin` exports the marker logic that `pi-space` and `/space` share.
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- The skill tells that `pi-space pin` alone opens a picker and `pi-space pin --show` prints the pinned space.
|
|
21
|
+
|
|
9
22
|
## 0.16.2
|
|
10
23
|
|
|
11
24
|
### Changed
|
package/README.md
CHANGED
|
@@ -111,6 +111,8 @@ A new space is a folder with an `agent/` folder inside it, empty except for `age
|
|
|
111
111
|
| `/space switch` | Same as `/space`. |
|
|
112
112
|
| `/space rename <old> <new>` | Renames a space. The active space cannot be renamed. |
|
|
113
113
|
| `/space remove <name> [--yes] [--no-backup]` | Deletes a space after asking, then asks whether to keep a backup in `<spaces folder>/.backups` (keeping one is first in the list; dismissing the question keeps the space). `--yes` skips the questions and keeps a backup; it is required without a dialog (print and JSON modes). `--no-backup` deletes without one. |
|
|
114
|
+
| `/space pin` | Pins the space this Pi runs in (`<profile>/<name>`, or `<profile>/base` in base) to the repository of the session's folder, so a bare `pi-space` there starts it (see [`pi-space pin`](cli/README.md#pinning-a-space-to-a-folder)). Only the current space; another one is pinned from a shell with `pi-space pin`. |
|
|
115
|
+
| `/space unpin` | Removes the space pinned to that repository, as `pi-space unpin` does. |
|
|
114
116
|
| `pi --space <name>` | Starts Pi directly in a space, keeping the prompt and every other option. |
|
|
115
117
|
| `pi --space <name> <command> [args...]` | Runs one of Pi's package commands (`install`, `remove`, `uninstall`, `update`, `list`) in a space, or in the base home with `base`: `pi --space work update --extensions`, `pi --space work install npm:<package>`. |
|
|
116
118
|
|
package/dist/command.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
|
|
4
4
|
import { profileCommand } from "./launch.js";
|
|
5
|
+
import { PinError, pinnedMessage, removePin, unpinnedMessage, writePin } from "./pin.js";
|
|
5
6
|
import { parseSpaceFlag, relaunchEnv, resolveSwitch } from "./relaunch.js";
|
|
6
7
|
import {
|
|
7
8
|
activeSpace,
|
|
@@ -19,10 +20,10 @@ import {
|
|
|
19
20
|
SpaceError,
|
|
20
21
|
} from "./spaces.js";
|
|
21
22
|
|
|
22
|
-
export const USAGE = "Usage: /space [list | new <name> | remove <name> [--yes] [--no-backup] | rename <old> <new> | switch [<name>]]";
|
|
23
|
+
export const USAGE = "Usage: /space [list | new <name> | remove <name> [--yes] [--no-backup] | rename <old> <new> | switch [<name>] | pin | unpin]";
|
|
23
24
|
const KEEP_BACKUP = "Keep a backup";
|
|
24
25
|
const NO_BACKUP = "Delete without a backup";
|
|
25
|
-
const SUBCOMMANDS = ["list", "new", "remove", "rename", "switch"];
|
|
26
|
+
const SUBCOMMANDS = ["list", "new", "remove", "rename", "switch", "pin", "unpin"];
|
|
26
27
|
|
|
27
28
|
|
|
28
29
|
|
|
@@ -49,6 +50,10 @@ const SUBCOMMANDS = ["list", "new", "remove", "rename", "switch"];
|
|
|
49
50
|
|
|
50
51
|
|
|
51
52
|
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
52
57
|
|
|
53
58
|
|
|
54
59
|
export async function runSpaceCommand(args , deps ) {
|
|
@@ -78,6 +83,8 @@ export async function runSpaceCommand(args , deps )
|
|
|
78
83
|
ctx.ui.notify(`Renamed space "${rest[0]}" to "${rest[1]}".`, "info");
|
|
79
84
|
return;
|
|
80
85
|
}
|
|
86
|
+
if (sub === "pin" && rest.length === 0) return pin(deps);
|
|
87
|
+
if (sub === "unpin" && rest.length === 0) return unpin(deps);
|
|
81
88
|
if (sub === "switch" && rest.length === 0) return await pickAndSwitch(deps);
|
|
82
89
|
if (sub === "switch" && rest.length === 1) return switchTo(rest[0], deps);
|
|
83
90
|
ctx.ui.notify(USAGE, "error");
|
|
@@ -139,6 +146,31 @@ async function remove({ name, yes, backup } , deps )
|
|
|
139
146
|
ctx.ui.notify(`Removed space "${name}".`, "info");
|
|
140
147
|
}
|
|
141
148
|
|
|
149
|
+
/**
|
|
150
|
+
* /space pin: pins this Pi's own space, as pi-space names it (`<profile>/<name>`, `<profile>/base` in base), to the
|
|
151
|
+
* repository of the session's folder; a bare pi-space there then starts it. Only the current space: another one is
|
|
152
|
+
* pinned from a shell with pi-space pin.
|
|
153
|
+
*/
|
|
154
|
+
function pin(deps ) {
|
|
155
|
+
const space = activeSpace(deps.dir, deps.currentHome()) ?? BASE;
|
|
156
|
+
try {
|
|
157
|
+
deps.ctx.ui.notify(pinnedMessage(writePin(deps.cwd(), deps.userHome, `${deps.profile?.name ?? "pi"}/${space}`)), "info");
|
|
158
|
+
} catch (error) {
|
|
159
|
+
if (!(error instanceof PinError)) throw error;
|
|
160
|
+
deps.ctx.ui.notify(error.message, "error");
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** /space unpin: removes the marker that applies in the session's folder, as pi-space unpin does. */
|
|
165
|
+
function unpin(deps ) {
|
|
166
|
+
try {
|
|
167
|
+
deps.ctx.ui.notify(unpinnedMessage(removePin(deps.cwd(), deps.userHome)), "info");
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (!(error instanceof PinError)) throw error;
|
|
170
|
+
deps.ctx.ui.notify(error.message, "error");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
142
174
|
function protectedHomes(deps ) {
|
|
143
175
|
return deps.env.PI_SPACES_BASE_HOME ? [deps.env.PI_SPACES_BASE_HOME] : [];
|
|
144
176
|
}
|
package/dist/pin.js
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// A space pinned to a folder, like .nvmrc: <folder>/.pi/pi-space/space holds one line, <profile>/<name>, next to a
|
|
2
|
+
// .gitignore that ignores the whole pi-space folder, itself included. .pi itself is Pi's project folder and is never
|
|
3
|
+
// touched. Shared by the pi-space command (pin, unpin, a bare pi-space) and /space pin and /space unpin.
|
|
4
|
+
// A marker may come with a cloned repository: nothing is written or removed through a symbolic link, and a marker
|
|
5
|
+
// whose line holds control or bidi characters names nothing (it is never echoed to a terminal).
|
|
6
|
+
import { spawnSync } from "node:child_process";
|
|
7
|
+
import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, renameSync, rmdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
9
|
+
import { samePath } from "./spaces.js";
|
|
10
|
+
|
|
11
|
+
export const PIN_FOLDER = join(".pi", "pi-space");
|
|
12
|
+
export const PIN_FILE = "space";
|
|
13
|
+
export const PIN_IGNORE = ".gitignore";
|
|
14
|
+
export const PIN_IGNORE_TEXT = "# pi-space: this folder is local to this machine; this file ignores all of it, itself included.\n*\n";
|
|
15
|
+
|
|
16
|
+
/** Why a pin or unpin did not happen; `code` is the exit status pi-space gives it (2: the home folder, 1: anything else). */
|
|
17
|
+
export class PinError extends Error {
|
|
18
|
+
code ;
|
|
19
|
+
constructor(message , code ) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.code = code;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Whether `path` is a file, following links; false for anything that cannot be read as one. */
|
|
26
|
+
function isFile(path ) {
|
|
27
|
+
try {
|
|
28
|
+
return statSync(path).isFile();
|
|
29
|
+
} catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Whether `path` is a symbolic link itself (not followed); false when there is nothing there. */
|
|
35
|
+
function isLink(path ) {
|
|
36
|
+
try {
|
|
37
|
+
return lstatSync(path).isSymbolicLink();
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The marker that applies in `from`: the first <folder>/.pi/pi-space/space file from it upwards. The home folder is
|
|
45
|
+
* skipped: its .pi is Pi's own folder, not a project.
|
|
46
|
+
*/
|
|
47
|
+
export function findPin(from , home ) {
|
|
48
|
+
for (let folder = resolve(from); ; folder = dirname(folder)) {
|
|
49
|
+
const file = join(folder, PIN_FOLDER, PIN_FILE);
|
|
50
|
+
if (!samePath(folder, home) && isFile(file)) return file;
|
|
51
|
+
if (dirname(folder) === folder) return undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Control characters (C0, DEL, C1) and the bidi controls that could rewrite what the terminal shows. */
|
|
56
|
+
const UNSAFE = /[\u0000-\u001f\u007f-\u009f\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069]/;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* What a marker names: `name` when it holds exactly one `<profile>/<name>` (blank lines and # comments left out),
|
|
60
|
+
* neither when it does not, and `error` when it cannot be read.
|
|
61
|
+
*/
|
|
62
|
+
export function readPin(file ) {
|
|
63
|
+
let text ;
|
|
64
|
+
try {
|
|
65
|
+
text = readFileSync(file, "utf8");
|
|
66
|
+
} catch (error) {
|
|
67
|
+
return { error: (error ).message };
|
|
68
|
+
}
|
|
69
|
+
const lines = text
|
|
70
|
+
.split(/\r?\n/)
|
|
71
|
+
.map((line) => line.trim())
|
|
72
|
+
.filter((line) => line !== "" && !line.startsWith("#"));
|
|
73
|
+
if (lines.length !== 1 || UNSAFE.test(lines[0])) return {};
|
|
74
|
+
const slash = lines[0].indexOf("/");
|
|
75
|
+
return slash > 0 && slash < lines[0].length - 1 ? { name: lines[0] } : {};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The real path of a folder, links and Windows short names (RUNNER~1) resolved; the path itself when it cannot be read. */
|
|
79
|
+
function realFolder(path ) {
|
|
80
|
+
try {
|
|
81
|
+
return realpathSync.native(path);
|
|
82
|
+
} catch {
|
|
83
|
+
return resolve(path);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Where pin and unpin work: the root of the git repository of `cwd`, else `cwd` (also when git is missing or hangs).
|
|
89
|
+
* The root is `cwd` or the folder above it that git names, spelled as `cwd` is: git gives the real path, which on
|
|
90
|
+
* Windows may be the long form of a short name, and on macOS /private/var for /var.
|
|
91
|
+
*/
|
|
92
|
+
export function pinRoot(cwd ) {
|
|
93
|
+
const here = resolve(cwd);
|
|
94
|
+
const git = spawnSync("git", ["rev-parse", "--show-toplevel"], { cwd: here, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 5000 });
|
|
95
|
+
const top = git.status === 0 ? git.stdout.trim() : "";
|
|
96
|
+
if (top === "") return here;
|
|
97
|
+
const real = realFolder(top);
|
|
98
|
+
for (let folder = here; ; folder = dirname(folder)) {
|
|
99
|
+
if (samePath(realFolder(folder), real)) return folder;
|
|
100
|
+
if (dirname(folder) === folder) return resolve(top);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The refusal of the home folder, whose .pi is Pi's own folder; undefined for any other root. */
|
|
105
|
+
export function homeRefusal(root , home ) {
|
|
106
|
+
return samePath(root, home) ? `${root} is your home folder, where .pi is Pi's own folder; pin a space in a project folder.` : undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The first of .pi, .pi/pi-space and the given files in it that is a symbolic link. */
|
|
110
|
+
function linkIn(owner , files ) {
|
|
111
|
+
const folder = join(owner, PIN_FOLDER);
|
|
112
|
+
return [dirname(folder), folder, ...files.map((name) => join(folder, name))].find(isLink);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Whether `path` is `folder` or inside it. */
|
|
116
|
+
function isWithin(path , folder ) {
|
|
117
|
+
const rel = relative(folder, path);
|
|
118
|
+
return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Pins `name` (`<profile>/<name>`, already known to exist) to the repository of `cwd`: writes the marker, and its
|
|
130
|
+
* .gitignore when there is none. The marker is written to a temporary file and renamed over the old one, so a failure
|
|
131
|
+
* leaves it as it was; a folder this call made is removed again. Throws a PinError for the home folder, a symbolic
|
|
132
|
+
* link, or a write that fails.
|
|
133
|
+
*/
|
|
134
|
+
export function writePin(cwd , home , name ) {
|
|
135
|
+
const root = pinRoot(cwd);
|
|
136
|
+
const refusal = homeRefusal(root, home);
|
|
137
|
+
if (refusal !== undefined) throw new PinError(refusal, 2);
|
|
138
|
+
const link = linkIn(root, [PIN_FILE, PIN_IGNORE]);
|
|
139
|
+
if (link !== undefined) throw new PinError(`${link} is a symbolic link; pi-space does not write through it.`, 1);
|
|
140
|
+
const folder = join(root, PIN_FOLDER);
|
|
141
|
+
const file = join(folder, PIN_FILE);
|
|
142
|
+
const before = isFile(file) ? readPin(file).name : undefined;
|
|
143
|
+
const temporary = join(folder, `.${PIN_FILE}-${process.pid}.tmp`);
|
|
144
|
+
let made ;
|
|
145
|
+
try {
|
|
146
|
+
made = mkdirSync(folder, { recursive: true });
|
|
147
|
+
const ignore = join(folder, PIN_IGNORE);
|
|
148
|
+
if (!existsSync(ignore)) writeFileSync(ignore, PIN_IGNORE_TEXT, { flag: "wx" });
|
|
149
|
+
writeFileSync(temporary, `${name}\n`, { flag: "wx" });
|
|
150
|
+
renameSync(temporary, file);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
// Cleaning up must not hide the reason: a path under a file (ENOTDIR) cannot be removed either.
|
|
153
|
+
try {
|
|
154
|
+
if (made !== undefined) rmSync(made, { recursive: true, force: true });
|
|
155
|
+
else rmSync(temporary, { force: true });
|
|
156
|
+
} catch {}
|
|
157
|
+
throw new PinError(`cannot write ${file}: ${(error ).message}`, 1);
|
|
158
|
+
}
|
|
159
|
+
return { name, file, ...(before !== undefined && before !== name ? { previous: before } : {}) };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The line that reports a pin. */
|
|
163
|
+
export function pinnedMessage({ name, file, previous } ) {
|
|
164
|
+
return `Pinned ${name} in ${file}${previous !== undefined ? ` (was ${previous})` : ""}.`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Removes the marker that applies in `cwd` when it is inside the repository of `cwd` (else `cwd` itself), the
|
|
173
|
+
* .gitignore pin wrote, and the folder once empty (and .pi, when nothing else is in it). A marker of a folder above is
|
|
174
|
+
* reported as inherited and left. Throws a PinError for a symbolic link or a removal that fails.
|
|
175
|
+
*/
|
|
176
|
+
export function removePin(cwd , home ) {
|
|
177
|
+
const root = pinRoot(cwd);
|
|
178
|
+
const here = findPin(cwd, home);
|
|
179
|
+
// <owner>/.pi/pi-space/space: the folder the marker belongs to.
|
|
180
|
+
const owner = here === undefined ? undefined : dirname(dirname(dirname(here)));
|
|
181
|
+
if (here === undefined || owner === undefined || !isWithin(owner, root)) return { kind: "none", root, ...(here !== undefined ? { inherited: here } : {}) };
|
|
182
|
+
const link = linkIn(owner, []);
|
|
183
|
+
if (link !== undefined) throw new PinError(`${link} is a symbolic link; pi-space does not remove through it.`, 1);
|
|
184
|
+
const folder = dirname(here);
|
|
185
|
+
const { name, error } = readPin(here);
|
|
186
|
+
const label = name ?? (error !== undefined ? "an unreadable marker" : "an invalid marker");
|
|
187
|
+
try {
|
|
188
|
+
rmSync(here);
|
|
189
|
+
const ignore = join(folder, PIN_IGNORE);
|
|
190
|
+
if (isFile(ignore) && !isLink(ignore) && readFileSync(ignore, "utf8") === PIN_IGNORE_TEXT) rmSync(ignore);
|
|
191
|
+
if (readdirSync(folder).length > 0) return { kind: "removed", label, file: here, folder, folderRemoved: false };
|
|
192
|
+
rmdirSync(folder);
|
|
193
|
+
// A .pi that pin made and that holds nothing else goes too; one with anything in it is Pi's and stays.
|
|
194
|
+
if (readdirSync(dirname(folder)).length === 0) rmdirSync(dirname(folder));
|
|
195
|
+
return { kind: "removed", label, file: here, folder, folderRemoved: true };
|
|
196
|
+
} catch (error) {
|
|
197
|
+
throw new PinError(`cannot remove ${here}: ${(error ).message}`, 1);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** The line that reports an unpin. */
|
|
202
|
+
export function unpinnedMessage(result ) {
|
|
203
|
+
if (result.kind === "none") return `No space pinned in ${result.root}${result.inherited !== undefined ? `; the one here comes from ${result.inherited}` : ""}.`;
|
|
204
|
+
return result.folderRemoved
|
|
205
|
+
? `Unpinned ${result.label} (removed ${result.folder}).`
|
|
206
|
+
: `Unpinned ${result.label} (removed ${result.file}; ${result.folder} holds other files and stays).`;
|
|
207
|
+
}
|
package/extensions/spaces.ts
CHANGED
|
@@ -83,7 +83,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
83
83
|
});
|
|
84
84
|
|
|
85
85
|
pi.registerCommand("space", {
|
|
86
|
-
description: "Pick a Pi space to switch to, or list, create, remove and rename spaces (separate agent homes)",
|
|
86
|
+
description: "Pick a Pi space to switch to, or list, create, remove and rename spaces (separate agent homes), or pin this one to the repository",
|
|
87
87
|
getArgumentCompletions: (prefix) => completeSpaceArgs(prefix, dir),
|
|
88
88
|
handler: async (args, ctx) => {
|
|
89
89
|
// Without a UI (print and JSON modes) notifications go nowhere, so they go to stderr;
|
|
@@ -100,6 +100,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
100
100
|
declaration: () => findOwnDeclaration(getAgentDir(), packageName, packageRoot),
|
|
101
101
|
command: () => relaunchCommand(),
|
|
102
102
|
relaunch: scheduleRelaunch,
|
|
103
|
+
cwd: () => ctx.cwd,
|
|
104
|
+
userHome: homedir(),
|
|
103
105
|
});
|
|
104
106
|
},
|
|
105
107
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-spaces",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "Named spaces for Pi: keep several agent homes (sign-ins, packages, settings) and pick one by name.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "jals1212",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"exports": {
|
|
22
22
|
"./spaces": "./dist/spaces.js",
|
|
23
23
|
"./config": "./dist/config.js",
|
|
24
|
+
"./pin": "./dist/pin.js",
|
|
24
25
|
"./package.json": "./package.json"
|
|
25
26
|
},
|
|
26
27
|
"pi": {
|
|
@@ -33,7 +33,7 @@ Use when a task depends on which Pi agent home this session uses (settings, sign
|
|
|
33
33
|
| Spaces folder | `PI_SPACES_DIR`, else the `dir` of the profile whose `home` is the base home, else `spaces` next to the base home (`PI_SPACES_BASE_HOME`, else this home) |
|
|
34
34
|
| Profiles, or why a variable differs per space | The config file (`PI_SPACES_CONFIG`, else `config.toml` or `config.json` in `~/.config/pi-spaces`, then in `~/.pi-spaces`): a profile's `spaceEnv` variables are set only inside spaces (`{home}` the agent home, `{space}` the space's root folder and `{name}` filled in) and removed in base; without `spaceEnv` whatever a program keeps outside its agent home is shared with base |
|
|
35
35
|
| Pick a space from a shell | bare `pi-space` on a terminal opens a picker of every profile's spaces (`<profile>/<name>`, `base` included) and starts Pi in the one chosen |
|
|
36
|
-
| Pin a space to a repository (like `.nvmrc`) | `pi-space pin <profile>/<name>` writes `.pi/pi-space/space` (one line `<profile>/<name>`) and a self-ignoring `.pi/pi-space/.gitignore` in the git root; a bare `pi-space` there or below then starts that space at once (a space named on the command line wins; `pi-space pick` opens the picker); `pi-space pin`
|
|
36
|
+
| Pin a space to a repository (like `.nvmrc`) | `pi-space pin <profile>/<name>` writes `.pi/pi-space/space` (one line `<profile>/<name>`) and a self-ignoring `.pi/pi-space/.gitignore` in the git root; a bare `pi-space` there or below then starts that space at once (a space named on the command line wins; `pi-space pick` opens the picker); `pi-space pin` alone opens a picker to choose it on a terminal (the pinned space marked `⌂`), `pi-space pin --show` prints it, `pi-space unpin` removes it. From inside Pi, the user can pin this session's own space with `/space pin` and remove it with `/space unpin`. Only `pi-space` reads the marker; Pi and `/space switch` do not |
|
|
37
37
|
| Rename a space | User request only: `pi-space rename <profile>/<old> <new>`; the new name alone or with the same profile (`<profile>/<new>`), a space never changes profile; refused for base and for the space you are in |
|
|
38
38
|
| Create a space | `pi-space new <profile>/<name>`, or ask the user for `/space new <name>` (this Pi's profile); `pi-space new` also runs the profile's setup command when there is one (or `--setup <cmd>` for one call), and asks whether the space declares the extension: pass `--extension` or `--no-extension` when running it for the user, since there is no terminal to answer |
|
|
39
39
|
| Prepare a space for the program that runs Pi | `pi-space setup <profile>/<name>` (or `<profile>/base`, `<profile>/--every` for base then its spaces, or `--every` for the base and spaces of every profile with a setup, which pi has not by default; the profile's `commands.setup`): runs the setup command (`{home}` becomes the space's agent home, `{space}` its root folder and `{name}` its name) with the agent home as `PI_CODING_AGENT_DIR`; in base `{home}` is the profile's home and `{name}` is `base` |
|
package/src/command.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import type { Profile } from "./config.ts";
|
|
4
4
|
import { profileCommand } from "./launch.ts";
|
|
5
|
+
import { PinError, pinnedMessage, removePin, unpinnedMessage, writePin } from "./pin.ts";
|
|
5
6
|
import { type Command, parseSpaceFlag, relaunchEnv, resolveSwitch } from "./relaunch.ts";
|
|
6
7
|
import {
|
|
7
8
|
activeSpace,
|
|
@@ -19,10 +20,10 @@ import {
|
|
|
19
20
|
SpaceError,
|
|
20
21
|
} from "./spaces.ts";
|
|
21
22
|
|
|
22
|
-
export const USAGE = "Usage: /space [list | new <name> | remove <name> [--yes] [--no-backup] | rename <old> <new> | switch [<name>]]";
|
|
23
|
+
export const USAGE = "Usage: /space [list | new <name> | remove <name> [--yes] [--no-backup] | rename <old> <new> | switch [<name>] | pin | unpin]";
|
|
23
24
|
const KEEP_BACKUP = "Keep a backup";
|
|
24
25
|
const NO_BACKUP = "Delete without a backup";
|
|
25
|
-
const SUBCOMMANDS = ["list", "new", "remove", "rename", "switch"];
|
|
26
|
+
const SUBCOMMANDS = ["list", "new", "remove", "rename", "switch", "pin", "unpin"];
|
|
26
27
|
|
|
27
28
|
export interface SpaceCommandContext {
|
|
28
29
|
mode: string;
|
|
@@ -49,6 +50,10 @@ export interface SpaceCommandDeps {
|
|
|
49
50
|
/** The command that starts this Pi process again. */
|
|
50
51
|
command(): Command;
|
|
51
52
|
relaunch(command: Command, env: Env): void;
|
|
53
|
+
/** The session's folder: /space pin and /space unpin work in its repository. */
|
|
54
|
+
cwd(): string;
|
|
55
|
+
/** The user's home folder, whose .pi is Pi's own and is never pinned. */
|
|
56
|
+
userHome: string;
|
|
52
57
|
}
|
|
53
58
|
|
|
54
59
|
export async function runSpaceCommand(args: string, deps: SpaceCommandDeps): Promise<void> {
|
|
@@ -78,6 +83,8 @@ export async function runSpaceCommand(args: string, deps: SpaceCommandDeps): Pro
|
|
|
78
83
|
ctx.ui.notify(`Renamed space "${rest[0]}" to "${rest[1]}".`, "info");
|
|
79
84
|
return;
|
|
80
85
|
}
|
|
86
|
+
if (sub === "pin" && rest.length === 0) return pin(deps);
|
|
87
|
+
if (sub === "unpin" && rest.length === 0) return unpin(deps);
|
|
81
88
|
if (sub === "switch" && rest.length === 0) return await pickAndSwitch(deps);
|
|
82
89
|
if (sub === "switch" && rest.length === 1) return switchTo(rest[0], deps);
|
|
83
90
|
ctx.ui.notify(USAGE, "error");
|
|
@@ -139,6 +146,31 @@ async function remove({ name, yes, backup }: Removal, deps: SpaceCommandDeps): P
|
|
|
139
146
|
ctx.ui.notify(`Removed space "${name}".`, "info");
|
|
140
147
|
}
|
|
141
148
|
|
|
149
|
+
/**
|
|
150
|
+
* /space pin: pins this Pi's own space, as pi-space names it (`<profile>/<name>`, `<profile>/base` in base), to the
|
|
151
|
+
* repository of the session's folder; a bare pi-space there then starts it. Only the current space: another one is
|
|
152
|
+
* pinned from a shell with pi-space pin.
|
|
153
|
+
*/
|
|
154
|
+
function pin(deps: SpaceCommandDeps): void {
|
|
155
|
+
const space = activeSpace(deps.dir, deps.currentHome()) ?? BASE;
|
|
156
|
+
try {
|
|
157
|
+
deps.ctx.ui.notify(pinnedMessage(writePin(deps.cwd(), deps.userHome, `${deps.profile?.name ?? "pi"}/${space}`)), "info");
|
|
158
|
+
} catch (error) {
|
|
159
|
+
if (!(error instanceof PinError)) throw error;
|
|
160
|
+
deps.ctx.ui.notify(error.message, "error");
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** /space unpin: removes the marker that applies in the session's folder, as pi-space unpin does. */
|
|
165
|
+
function unpin(deps: SpaceCommandDeps): void {
|
|
166
|
+
try {
|
|
167
|
+
deps.ctx.ui.notify(unpinnedMessage(removePin(deps.cwd(), deps.userHome)), "info");
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (!(error instanceof PinError)) throw error;
|
|
170
|
+
deps.ctx.ui.notify(error.message, "error");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
142
174
|
function protectedHomes(deps: SpaceCommandDeps): string[] {
|
|
143
175
|
return deps.env.PI_SPACES_BASE_HOME ? [deps.env.PI_SPACES_BASE_HOME] : [];
|
|
144
176
|
}
|
package/src/pin.ts
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// A space pinned to a folder, like .nvmrc: <folder>/.pi/pi-space/space holds one line, <profile>/<name>, next to a
|
|
2
|
+
// .gitignore that ignores the whole pi-space folder, itself included. .pi itself is Pi's project folder and is never
|
|
3
|
+
// touched. Shared by the pi-space command (pin, unpin, a bare pi-space) and /space pin and /space unpin.
|
|
4
|
+
// A marker may come with a cloned repository: nothing is written or removed through a symbolic link, and a marker
|
|
5
|
+
// whose line holds control or bidi characters names nothing (it is never echoed to a terminal).
|
|
6
|
+
import { spawnSync } from "node:child_process";
|
|
7
|
+
import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, renameSync, rmdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
9
|
+
import { samePath } from "./spaces.ts";
|
|
10
|
+
|
|
11
|
+
export const PIN_FOLDER = join(".pi", "pi-space");
|
|
12
|
+
export const PIN_FILE = "space";
|
|
13
|
+
export const PIN_IGNORE = ".gitignore";
|
|
14
|
+
export const PIN_IGNORE_TEXT = "# pi-space: this folder is local to this machine; this file ignores all of it, itself included.\n*\n";
|
|
15
|
+
|
|
16
|
+
/** Why a pin or unpin did not happen; `code` is the exit status pi-space gives it (2: the home folder, 1: anything else). */
|
|
17
|
+
export class PinError extends Error {
|
|
18
|
+
readonly code: 1 | 2;
|
|
19
|
+
constructor(message: string, code: 1 | 2) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.code = code;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Whether `path` is a file, following links; false for anything that cannot be read as one. */
|
|
26
|
+
function isFile(path: string): boolean {
|
|
27
|
+
try {
|
|
28
|
+
return statSync(path).isFile();
|
|
29
|
+
} catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Whether `path` is a symbolic link itself (not followed); false when there is nothing there. */
|
|
35
|
+
function isLink(path: string): boolean {
|
|
36
|
+
try {
|
|
37
|
+
return lstatSync(path).isSymbolicLink();
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The marker that applies in `from`: the first <folder>/.pi/pi-space/space file from it upwards. The home folder is
|
|
45
|
+
* skipped: its .pi is Pi's own folder, not a project.
|
|
46
|
+
*/
|
|
47
|
+
export function findPin(from: string, home: string): string | undefined {
|
|
48
|
+
for (let folder = resolve(from); ; folder = dirname(folder)) {
|
|
49
|
+
const file = join(folder, PIN_FOLDER, PIN_FILE);
|
|
50
|
+
if (!samePath(folder, home) && isFile(file)) return file;
|
|
51
|
+
if (dirname(folder) === folder) return undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Control characters (C0, DEL, C1) and the bidi controls that could rewrite what the terminal shows. */
|
|
56
|
+
const UNSAFE = /[\u0000-\u001f\u007f-\u009f\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069]/;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* What a marker names: `name` when it holds exactly one `<profile>/<name>` (blank lines and # comments left out),
|
|
60
|
+
* neither when it does not, and `error` when it cannot be read.
|
|
61
|
+
*/
|
|
62
|
+
export function readPin(file: string): { name?: string; error?: string } {
|
|
63
|
+
let text: string;
|
|
64
|
+
try {
|
|
65
|
+
text = readFileSync(file, "utf8");
|
|
66
|
+
} catch (error) {
|
|
67
|
+
return { error: (error as Error).message };
|
|
68
|
+
}
|
|
69
|
+
const lines = text
|
|
70
|
+
.split(/\r?\n/)
|
|
71
|
+
.map((line) => line.trim())
|
|
72
|
+
.filter((line) => line !== "" && !line.startsWith("#"));
|
|
73
|
+
if (lines.length !== 1 || UNSAFE.test(lines[0])) return {};
|
|
74
|
+
const slash = lines[0].indexOf("/");
|
|
75
|
+
return slash > 0 && slash < lines[0].length - 1 ? { name: lines[0] } : {};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The real path of a folder, links and Windows short names (RUNNER~1) resolved; the path itself when it cannot be read. */
|
|
79
|
+
function realFolder(path: string): string {
|
|
80
|
+
try {
|
|
81
|
+
return realpathSync.native(path);
|
|
82
|
+
} catch {
|
|
83
|
+
return resolve(path);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Where pin and unpin work: the root of the git repository of `cwd`, else `cwd` (also when git is missing or hangs).
|
|
89
|
+
* The root is `cwd` or the folder above it that git names, spelled as `cwd` is: git gives the real path, which on
|
|
90
|
+
* Windows may be the long form of a short name, and on macOS /private/var for /var.
|
|
91
|
+
*/
|
|
92
|
+
export function pinRoot(cwd: string): string {
|
|
93
|
+
const here = resolve(cwd);
|
|
94
|
+
const git = spawnSync("git", ["rev-parse", "--show-toplevel"], { cwd: here, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 5000 });
|
|
95
|
+
const top = git.status === 0 ? git.stdout.trim() : "";
|
|
96
|
+
if (top === "") return here;
|
|
97
|
+
const real = realFolder(top);
|
|
98
|
+
for (let folder = here; ; folder = dirname(folder)) {
|
|
99
|
+
if (samePath(realFolder(folder), real)) return folder;
|
|
100
|
+
if (dirname(folder) === folder) return resolve(top);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The refusal of the home folder, whose .pi is Pi's own folder; undefined for any other root. */
|
|
105
|
+
export function homeRefusal(root: string, home: string): string | undefined {
|
|
106
|
+
return samePath(root, home) ? `${root} is your home folder, where .pi is Pi's own folder; pin a space in a project folder.` : undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The first of .pi, .pi/pi-space and the given files in it that is a symbolic link. */
|
|
110
|
+
function linkIn(owner: string, files: string[]): string | undefined {
|
|
111
|
+
const folder = join(owner, PIN_FOLDER);
|
|
112
|
+
return [dirname(folder), folder, ...files.map((name) => join(folder, name))].find(isLink);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Whether `path` is `folder` or inside it. */
|
|
116
|
+
function isWithin(path: string, folder: string): boolean {
|
|
117
|
+
const rel = relative(folder, path);
|
|
118
|
+
return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface Pinned {
|
|
122
|
+
name: string;
|
|
123
|
+
file: string;
|
|
124
|
+
/** The space the marker named before, when another. */
|
|
125
|
+
previous?: string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Pins `name` (`<profile>/<name>`, already known to exist) to the repository of `cwd`: writes the marker, and its
|
|
130
|
+
* .gitignore when there is none. The marker is written to a temporary file and renamed over the old one, so a failure
|
|
131
|
+
* leaves it as it was; a folder this call made is removed again. Throws a PinError for the home folder, a symbolic
|
|
132
|
+
* link, or a write that fails.
|
|
133
|
+
*/
|
|
134
|
+
export function writePin(cwd: string, home: string, name: string): Pinned {
|
|
135
|
+
const root = pinRoot(cwd);
|
|
136
|
+
const refusal = homeRefusal(root, home);
|
|
137
|
+
if (refusal !== undefined) throw new PinError(refusal, 2);
|
|
138
|
+
const link = linkIn(root, [PIN_FILE, PIN_IGNORE]);
|
|
139
|
+
if (link !== undefined) throw new PinError(`${link} is a symbolic link; pi-space does not write through it.`, 1);
|
|
140
|
+
const folder = join(root, PIN_FOLDER);
|
|
141
|
+
const file = join(folder, PIN_FILE);
|
|
142
|
+
const before = isFile(file) ? readPin(file).name : undefined;
|
|
143
|
+
const temporary = join(folder, `.${PIN_FILE}-${process.pid}.tmp`);
|
|
144
|
+
let made: string | undefined;
|
|
145
|
+
try {
|
|
146
|
+
made = mkdirSync(folder, { recursive: true });
|
|
147
|
+
const ignore = join(folder, PIN_IGNORE);
|
|
148
|
+
if (!existsSync(ignore)) writeFileSync(ignore, PIN_IGNORE_TEXT, { flag: "wx" });
|
|
149
|
+
writeFileSync(temporary, `${name}\n`, { flag: "wx" });
|
|
150
|
+
renameSync(temporary, file);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
// Cleaning up must not hide the reason: a path under a file (ENOTDIR) cannot be removed either.
|
|
153
|
+
try {
|
|
154
|
+
if (made !== undefined) rmSync(made, { recursive: true, force: true });
|
|
155
|
+
else rmSync(temporary, { force: true });
|
|
156
|
+
} catch {}
|
|
157
|
+
throw new PinError(`cannot write ${file}: ${(error as Error).message}`, 1);
|
|
158
|
+
}
|
|
159
|
+
return { name, file, ...(before !== undefined && before !== name ? { previous: before } : {}) };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The line that reports a pin. */
|
|
163
|
+
export function pinnedMessage({ name, file, previous }: Pinned): string {
|
|
164
|
+
return `Pinned ${name} in ${file}${previous !== undefined ? ` (was ${previous})` : ""}.`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export type Unpinned =
|
|
168
|
+
| { kind: "none"; root: string; inherited?: string }
|
|
169
|
+
| { kind: "removed"; label: string; file: string; folder: string; folderRemoved: boolean };
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Removes the marker that applies in `cwd` when it is inside the repository of `cwd` (else `cwd` itself), the
|
|
173
|
+
* .gitignore pin wrote, and the folder once empty (and .pi, when nothing else is in it). A marker of a folder above is
|
|
174
|
+
* reported as inherited and left. Throws a PinError for a symbolic link or a removal that fails.
|
|
175
|
+
*/
|
|
176
|
+
export function removePin(cwd: string, home: string): Unpinned {
|
|
177
|
+
const root = pinRoot(cwd);
|
|
178
|
+
const here = findPin(cwd, home);
|
|
179
|
+
// <owner>/.pi/pi-space/space: the folder the marker belongs to.
|
|
180
|
+
const owner = here === undefined ? undefined : dirname(dirname(dirname(here)));
|
|
181
|
+
if (here === undefined || owner === undefined || !isWithin(owner, root)) return { kind: "none", root, ...(here !== undefined ? { inherited: here } : {}) };
|
|
182
|
+
const link = linkIn(owner, []);
|
|
183
|
+
if (link !== undefined) throw new PinError(`${link} is a symbolic link; pi-space does not remove through it.`, 1);
|
|
184
|
+
const folder = dirname(here);
|
|
185
|
+
const { name, error } = readPin(here);
|
|
186
|
+
const label = name ?? (error !== undefined ? "an unreadable marker" : "an invalid marker");
|
|
187
|
+
try {
|
|
188
|
+
rmSync(here);
|
|
189
|
+
const ignore = join(folder, PIN_IGNORE);
|
|
190
|
+
if (isFile(ignore) && !isLink(ignore) && readFileSync(ignore, "utf8") === PIN_IGNORE_TEXT) rmSync(ignore);
|
|
191
|
+
if (readdirSync(folder).length > 0) return { kind: "removed", label, file: here, folder, folderRemoved: false };
|
|
192
|
+
rmdirSync(folder);
|
|
193
|
+
// A .pi that pin made and that holds nothing else goes too; one with anything in it is Pi's and stays.
|
|
194
|
+
if (readdirSync(dirname(folder)).length === 0) rmdirSync(dirname(folder));
|
|
195
|
+
return { kind: "removed", label, file: here, folder, folderRemoved: true };
|
|
196
|
+
} catch (error) {
|
|
197
|
+
throw new PinError(`cannot remove ${here}: ${(error as Error).message}`, 1);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** The line that reports an unpin. */
|
|
202
|
+
export function unpinnedMessage(result: Unpinned): string {
|
|
203
|
+
if (result.kind === "none") return `No space pinned in ${result.root}${result.inherited !== undefined ? `; the one here comes from ${result.inherited}` : ""}.`;
|
|
204
|
+
return result.folderRemoved
|
|
205
|
+
? `Unpinned ${result.label} (removed ${result.folder}).`
|
|
206
|
+
: `Unpinned ${result.label} (removed ${result.file}; ${result.folder} holds other files and stays).`;
|
|
207
|
+
}
|