@joberthel/worktree 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +315 -2
- package/dist/cli-spec.js +138 -0
- package/dist/cli.js +264 -0
- package/dist/completion.js +178 -0
- package/dist/config/load.js +46 -0
- package/dist/config/schema.js +57 -0
- package/dist/docker/compose.js +253 -0
- package/dist/git/repository.js +196 -0
- package/dist/operations/attached.js +294 -0
- package/dist/operations/commands.js +158 -0
- package/dist/operations/confirmation.js +15 -0
- package/dist/operations/copy-paths.js +109 -0
- package/dist/operations/create.js +255 -0
- package/dist/operations/hooks.js +113 -0
- package/dist/operations/manage.js +446 -0
- package/dist/operations/manage.test/fixture.js +67 -0
- package/dist/operations/process.js +122 -0
- package/dist/operations/progress.js +28 -0
- package/dist/state/atomic-file.js +14 -0
- package/dist/state/store.js +162 -0
- package/dist/ui/confirmation-prompt.js +48 -0
- package/dist/ui/environment-info.js +85 -0
- package/dist/ui/environment-table.js +43 -0
- package/dist/ui/format-status.js +29 -0
- package/dist/ui/hook-log-panel.js +57 -0
- package/dist/ui/json-output.js +41 -0
- package/dist/ui/operation-view.js +33 -0
- package/dist/ui/progress-state.js +111 -0
- package/dist/ui/progress.js +16 -0
- package/dist/ui/show-operation.js +56 -0
- package/dist/ui/use-progress.js +34 -0
- package/package.json +72 -3
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { link, mkdir, readdir, readFile, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { writeAtomicFile } from './atomic-file.js';
|
|
7
|
+
const status = z.enum([
|
|
8
|
+
'creating',
|
|
9
|
+
'ready',
|
|
10
|
+
'resetting',
|
|
11
|
+
'removing',
|
|
12
|
+
'failed',
|
|
13
|
+
]);
|
|
14
|
+
const stateSchema = z.object({
|
|
15
|
+
schemaVersion: z.literal(1),
|
|
16
|
+
repositoryId: z.string(),
|
|
17
|
+
repositoryRoot: z.string().optional(),
|
|
18
|
+
name: z.string(),
|
|
19
|
+
path: z.string(),
|
|
20
|
+
/** Checked-out branch, or an empty string for a detached checkout. */
|
|
21
|
+
branch: z.string(),
|
|
22
|
+
sourceWorktreePath: z.string(),
|
|
23
|
+
sourceProject: z.string(),
|
|
24
|
+
configurationPath: z.string().nullable().optional(),
|
|
25
|
+
composeFiles: z.array(z.string()),
|
|
26
|
+
overridePath: z.string(),
|
|
27
|
+
projectName: z.string(),
|
|
28
|
+
volumes: z.record(z.string(), z.string()),
|
|
29
|
+
retiredVolumes: z.array(z.string()).optional(),
|
|
30
|
+
/** Worktree of another repository that is nested in this environment and owns it. */
|
|
31
|
+
attachment: z.object({
|
|
32
|
+
gitCommonDir: z.string(),
|
|
33
|
+
name: z.string(),
|
|
34
|
+
path: z.string(),
|
|
35
|
+
}).optional(),
|
|
36
|
+
status,
|
|
37
|
+
error: z.string().optional(),
|
|
38
|
+
});
|
|
39
|
+
const attachedSchema = z.object({
|
|
40
|
+
schemaVersion: z.literal(1),
|
|
41
|
+
name: z.string(),
|
|
42
|
+
path: z.string(),
|
|
43
|
+
branch: z.string(),
|
|
44
|
+
sourceWorktreePath: z.string(),
|
|
45
|
+
configurationPath: z.string().nullable(),
|
|
46
|
+
/** Relative path of the checkout inside the host checkout. */
|
|
47
|
+
mountPath: z.string(),
|
|
48
|
+
host: z.object({
|
|
49
|
+
gitCommonDir: z.string(),
|
|
50
|
+
repositoryRoot: z.string(),
|
|
51
|
+
name: z.string(),
|
|
52
|
+
}),
|
|
53
|
+
status,
|
|
54
|
+
error: z.string().optional(),
|
|
55
|
+
});
|
|
56
|
+
function isMissing(error) {
|
|
57
|
+
return error.code === 'ENOENT';
|
|
58
|
+
}
|
|
59
|
+
class JsonStore {
|
|
60
|
+
directory;
|
|
61
|
+
schema;
|
|
62
|
+
kind;
|
|
63
|
+
constructor(directory, schema, kind) {
|
|
64
|
+
this.directory = directory;
|
|
65
|
+
this.schema = schema;
|
|
66
|
+
this.kind = kind;
|
|
67
|
+
}
|
|
68
|
+
pathFor(name, suffix) {
|
|
69
|
+
return join(this.directory, `${encodeURIComponent(name)}${suffix}`);
|
|
70
|
+
}
|
|
71
|
+
async read(path) {
|
|
72
|
+
const data = await readFile(path, 'utf8');
|
|
73
|
+
try {
|
|
74
|
+
return this.schema.parse(JSON.parse(data));
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
throw new Error(`Invalid ${this.kind} state in ${path}`, { cause: error });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async save(state) {
|
|
81
|
+
await mkdir(this.directory, { recursive: true });
|
|
82
|
+
await writeAtomicFile(this.pathFor(state.name, '.json'), `${JSON.stringify(state, null, 2)}\n`);
|
|
83
|
+
}
|
|
84
|
+
/** Returns the state saved under `name`, or `undefined` when there is none. */
|
|
85
|
+
async find(name) {
|
|
86
|
+
const state = await this.read(this.pathFor(name, '.json')).catch((error) => {
|
|
87
|
+
if (!isMissing(error))
|
|
88
|
+
throw error;
|
|
89
|
+
return undefined;
|
|
90
|
+
});
|
|
91
|
+
if (state !== undefined && state.name !== name)
|
|
92
|
+
throw new Error(`No managed worktree named ${name}. Did you mean ${state.name}?`);
|
|
93
|
+
return state;
|
|
94
|
+
}
|
|
95
|
+
async get(name) {
|
|
96
|
+
const state = await this.find(name);
|
|
97
|
+
if (state === undefined)
|
|
98
|
+
throw new Error(`No managed worktree named ${name}. Run wt ls to see the available worktrees.`);
|
|
99
|
+
return state;
|
|
100
|
+
}
|
|
101
|
+
async remove(name) {
|
|
102
|
+
await unlink(this.pathFor(name, '.json'));
|
|
103
|
+
}
|
|
104
|
+
async list() {
|
|
105
|
+
const files = await readdir(this.directory).catch((error) => {
|
|
106
|
+
if (isMissing(error))
|
|
107
|
+
return [];
|
|
108
|
+
throw error;
|
|
109
|
+
});
|
|
110
|
+
const states = await Promise.all(files.filter(file => file.endsWith('.json')).sort().map(async (file) => {
|
|
111
|
+
const path = join(this.directory, file);
|
|
112
|
+
try {
|
|
113
|
+
const state = await this.read(path);
|
|
114
|
+
if (this.pathFor(state.name, '.json') !== path)
|
|
115
|
+
throw new Error('State name does not match its filename.');
|
|
116
|
+
return state;
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
if (!isMissing(error))
|
|
120
|
+
process.stderr.write(`Skipping unreadable ${this.kind} state ${path}: ${error.message}\n`);
|
|
121
|
+
}
|
|
122
|
+
}));
|
|
123
|
+
return states.filter(state => state !== undefined);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
export class StateStore extends JsonStore {
|
|
127
|
+
constructor(gitCommonDir) {
|
|
128
|
+
super(join(gitCommonDir, 'wt'), stateSchema, 'worktree');
|
|
129
|
+
}
|
|
130
|
+
async lock(name) {
|
|
131
|
+
await mkdir(this.directory, { recursive: true });
|
|
132
|
+
const path = this.pathFor(name, '.lock');
|
|
133
|
+
const temporary = this.pathFor(randomUUID(), '.tmp');
|
|
134
|
+
const started = new Date(Date.now() - process.uptime() * 1000).toISOString();
|
|
135
|
+
await writeFile(temporary, `${process.pid} (started ${started})\n`, { mode: 0o600, flag: 'wx' });
|
|
136
|
+
try {
|
|
137
|
+
await link(temporary, path);
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
if (error.code !== 'EEXIST')
|
|
141
|
+
throw error;
|
|
142
|
+
const owner = await readFile(path, 'utf8').catch(() => '');
|
|
143
|
+
throw new Error(`Worktree operations are locked at ${path}${owner.trim()
|
|
144
|
+
? ` by PID ${owner.trim()}`
|
|
145
|
+
: ''}. Retry when it exits. If the lock is stale, verify the owner has exited before removing the lock file.`);
|
|
146
|
+
}
|
|
147
|
+
finally {
|
|
148
|
+
await unlink(temporary);
|
|
149
|
+
}
|
|
150
|
+
return async () => unlink(path);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Worktrees of a repository that has no Docker environment of its own. Each
|
|
155
|
+
* one is nested in an environment of its host repository. Their state lives
|
|
156
|
+
* next to the environment state and uses the same repository lock.
|
|
157
|
+
*/
|
|
158
|
+
export class AttachedStore extends JsonStore {
|
|
159
|
+
constructor(gitCommonDir) {
|
|
160
|
+
super(join(gitCommonDir, 'wt', 'attached'), attachedSchema, 'attached worktree');
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Box, Text, useInput, useStdin, useStdout } from 'ink';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { detailRows } from '../operations/confirmation.js';
|
|
5
|
+
function ChoiceLabel({ selected, color, children }) {
|
|
6
|
+
return selected
|
|
7
|
+
? (_jsx(Text, { bold: true, inverse: true, color: color, children: ` ❯ ${children} ` }))
|
|
8
|
+
: _jsx(Text, { dimColor: true, children: ` ${children} ` });
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Asks before a destructive operation. Cancel is preselected, so Enter alone
|
|
12
|
+
* never destroys anything. The user can select the action with `y`.
|
|
13
|
+
*/
|
|
14
|
+
export function ConfirmationPrompt({ request, onConfirm, onCancel }) {
|
|
15
|
+
const { stdout } = useStdout();
|
|
16
|
+
const { isRawModeSupported } = useStdin();
|
|
17
|
+
const choiceRef = React.useRef('cancel');
|
|
18
|
+
const [choice, setChoice] = React.useState('cancel');
|
|
19
|
+
function select(next) {
|
|
20
|
+
choiceRef.current = next;
|
|
21
|
+
setChoice(next);
|
|
22
|
+
}
|
|
23
|
+
useInput((input, key) => {
|
|
24
|
+
if (key.ctrl || key.meta)
|
|
25
|
+
return;
|
|
26
|
+
const character = input.toLowerCase();
|
|
27
|
+
if (character === 'n' || key.escape) {
|
|
28
|
+
onCancel();
|
|
29
|
+
}
|
|
30
|
+
else if (key.return) {
|
|
31
|
+
(choiceRef.current === 'confirm'
|
|
32
|
+
? onConfirm
|
|
33
|
+
: onCancel)();
|
|
34
|
+
}
|
|
35
|
+
else if (key.leftArrow || character === 'h') {
|
|
36
|
+
select('cancel');
|
|
37
|
+
}
|
|
38
|
+
else if (key.rightArrow || character === 'l' || character === 'y') {
|
|
39
|
+
select('confirm');
|
|
40
|
+
}
|
|
41
|
+
else if (key.tab) {
|
|
42
|
+
select(choiceRef.current === 'cancel'
|
|
43
|
+
? 'confirm'
|
|
44
|
+
: 'cancel');
|
|
45
|
+
}
|
|
46
|
+
}, { isActive: isRawModeSupported });
|
|
47
|
+
return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: "yellow", paddingX: 1, width: Math.min(stdout.columns ?? 80, 100), children: [_jsx(Text, { bold: true, color: "yellow", children: `⚠ ${request.title}` }), _jsx(Box, { flexDirection: "column", marginY: 1, children: detailRows(request.details).map(row => (_jsxs(Box, { flexDirection: "row", gap: 2, children: [_jsx(Box, { flexShrink: 0, children: _jsx(Text, { dimColor: true, children: row.label }) }), _jsx(Text, { children: row.value })] }, row.label))) }), _jsx(Text, { children: request.note })] }), _jsxs(Box, { flexDirection: "row", gap: 1, marginTop: 1, paddingX: 1, children: [_jsx(ChoiceLabel, { selected: choice === 'cancel', color: "white", children: "Cancel" }), _jsx(ChoiceLabel, { selected: choice === 'confirm', color: "red", children: request.action })] }), _jsx(Box, { paddingX: 1, marginTop: 1, children: _jsx(Text, { dimColor: true, children: "\u2190/\u2192 choose \u00B7 Enter select \u00B7 y choose action \u00B7 n cancel \u00B7 Esc cancel" }) })] }));
|
|
48
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { colorize } from './format-status.js';
|
|
2
|
+
/** Icon and color of a container, from its Docker state and health. */
|
|
3
|
+
export function containerIndicator({ state, status }) {
|
|
4
|
+
if (state === 'running') {
|
|
5
|
+
if (status.includes('(unhealthy)'))
|
|
6
|
+
return { icon: '●', variant: 'error' };
|
|
7
|
+
if (status.includes('(health: starting)'))
|
|
8
|
+
return { icon: '●', variant: 'warning' };
|
|
9
|
+
return { icon: '●', variant: 'success' };
|
|
10
|
+
}
|
|
11
|
+
if (state === 'restarting' || state === 'paused')
|
|
12
|
+
return { icon: '●', variant: 'warning' };
|
|
13
|
+
if (state === 'exited' && status.startsWith('Exited (0)'))
|
|
14
|
+
return { icon: '○', variant: 'plain' };
|
|
15
|
+
if (state === 'created')
|
|
16
|
+
return { icon: '○', variant: 'warning' };
|
|
17
|
+
return { icon: '○', variant: 'error' };
|
|
18
|
+
}
|
|
19
|
+
const statusVariants = {
|
|
20
|
+
ready: 'success',
|
|
21
|
+
failed: 'error',
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Display names of the containers: the Compose service, or the container
|
|
25
|
+
* name for a service with several containers so the rows stay distinct.
|
|
26
|
+
*/
|
|
27
|
+
function displayNames(containers) {
|
|
28
|
+
const counts = new Map();
|
|
29
|
+
for (const { service } of containers)
|
|
30
|
+
counts.set(service, (counts.get(service) ?? 0) + 1);
|
|
31
|
+
return containers.map(container => counts.get(container.service) === 1
|
|
32
|
+
? container.service
|
|
33
|
+
: container.name);
|
|
34
|
+
}
|
|
35
|
+
function section(title, rows) {
|
|
36
|
+
return rows.length
|
|
37
|
+
? [
|
|
38
|
+
title,
|
|
39
|
+
...rows.map(row => ` ${row}`),
|
|
40
|
+
]
|
|
41
|
+
: [`${title} ${colorize('none', 'plain')}`];
|
|
42
|
+
}
|
|
43
|
+
/** Formats `wt info` as aligned fields, then the services and volumes. */
|
|
44
|
+
export function formatEnvironmentInfo(info) {
|
|
45
|
+
const status = info.status ?? 'unknown';
|
|
46
|
+
const fields = [
|
|
47
|
+
['Worktree', info.name],
|
|
48
|
+
['Branch', info.branch || '(detached)'],
|
|
49
|
+
[
|
|
50
|
+
'Status',
|
|
51
|
+
colorize(status, statusVariants[status] ?? 'warning'),
|
|
52
|
+
],
|
|
53
|
+
['Path', info.path],
|
|
54
|
+
...(info.host === undefined
|
|
55
|
+
? []
|
|
56
|
+
: [
|
|
57
|
+
['Host', `${info.host.name} (${info.host.path})`],
|
|
58
|
+
]),
|
|
59
|
+
['Compose', info.projectName],
|
|
60
|
+
...(info.error === undefined
|
|
61
|
+
? []
|
|
62
|
+
: [
|
|
63
|
+
[
|
|
64
|
+
'Error',
|
|
65
|
+
colorize(info.error, 'error'),
|
|
66
|
+
],
|
|
67
|
+
]),
|
|
68
|
+
];
|
|
69
|
+
const labelWidth = Math.max(...fields.map(([label]) => label.length));
|
|
70
|
+
const names = displayNames(info.containers);
|
|
71
|
+
const nameWidth = Math.max(0, ...names.map(name => name.length));
|
|
72
|
+
const services = info.containers.map((container, index) => {
|
|
73
|
+
const { icon, variant } = containerIndicator(container);
|
|
74
|
+
return `${colorize(icon, variant)} ${names[index].padEnd(nameWidth)} ${container.status}`;
|
|
75
|
+
});
|
|
76
|
+
const volumes = Object.entries(info.volumes);
|
|
77
|
+
const volumeWidth = Math.max(0, ...volumes.map(([logical]) => logical.length));
|
|
78
|
+
return [
|
|
79
|
+
...fields.map(([label, value]) => `${label.padEnd(labelWidth)} ${value}`),
|
|
80
|
+
'',
|
|
81
|
+
...section('Services', services),
|
|
82
|
+
'',
|
|
83
|
+
...section('Volumes', volumes.map(([logical, volume]) => `${logical.padEnd(volumeWidth)} → ${volume}`)),
|
|
84
|
+
].join('\n');
|
|
85
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { colorize } from './format-status.js';
|
|
2
|
+
const states = {
|
|
3
|
+
running: { icon: '●', variant: 'success' },
|
|
4
|
+
stopped: { icon: '○', variant: 'warning' },
|
|
5
|
+
};
|
|
6
|
+
const unknownState = { icon: '!', variant: 'error' };
|
|
7
|
+
/**
|
|
8
|
+
* Formats managed worktrees as an aligned table. Cells are padded before they
|
|
9
|
+
* are colored so escape sequences never shift the columns.
|
|
10
|
+
*/
|
|
11
|
+
export function formatEnvironmentTable(items) {
|
|
12
|
+
const rows = items.map((item) => {
|
|
13
|
+
const status = item.status !== undefined && item.status !== 'ready'
|
|
14
|
+
? item.status
|
|
15
|
+
: item.dockerStatus;
|
|
16
|
+
const state = status in states
|
|
17
|
+
? states[status]
|
|
18
|
+
: unknownState;
|
|
19
|
+
return {
|
|
20
|
+
cells: [
|
|
21
|
+
item.name,
|
|
22
|
+
item.branch || '(detached)',
|
|
23
|
+
`${state.icon} ${status}`,
|
|
24
|
+
item.projectName,
|
|
25
|
+
],
|
|
26
|
+
variant: state.variant,
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
const header = ['NAME', 'BRANCH', 'STATE', 'PROJECT'];
|
|
30
|
+
const widths = header.map((title, column) => Math.max(title.length, ...rows.map(row => row.cells[column].length)));
|
|
31
|
+
const line = (cells, variant) => cells.map((cell, column) => {
|
|
32
|
+
const padded = column === cells.length - 1
|
|
33
|
+
? cell
|
|
34
|
+
: cell.padEnd(widths[column]);
|
|
35
|
+
return column === 2 && variant !== undefined
|
|
36
|
+
? colorize(padded, variant)
|
|
37
|
+
: padded;
|
|
38
|
+
}).join(' ');
|
|
39
|
+
return [
|
|
40
|
+
line(header),
|
|
41
|
+
...rows.map(row => line(row.cells, row.variant)),
|
|
42
|
+
].join('\n');
|
|
43
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
/** Colors text for a terminal without changing its visible width. */
|
|
3
|
+
export function colorize(text, variant, stream = process.stdout) {
|
|
4
|
+
const color = {
|
|
5
|
+
success: '\u001B[32m',
|
|
6
|
+
error: '\u001B[31m',
|
|
7
|
+
warning: '\u001B[33m',
|
|
8
|
+
info: '\u001B[36m',
|
|
9
|
+
plain: '',
|
|
10
|
+
}[variant];
|
|
11
|
+
if (!stream.isTTY || Boolean(process.env.NO_COLOR) || color.length === 0)
|
|
12
|
+
return text;
|
|
13
|
+
return `${color}${text}\u001B[39m`;
|
|
14
|
+
}
|
|
15
|
+
export function formatStatus(message, variant = 'plain') {
|
|
16
|
+
const symbol = {
|
|
17
|
+
success: '✓',
|
|
18
|
+
error: '✗',
|
|
19
|
+
warning: '!',
|
|
20
|
+
info: 'ℹ',
|
|
21
|
+
plain: '',
|
|
22
|
+
}[variant];
|
|
23
|
+
const prefix = symbol.length > 0
|
|
24
|
+
? `${symbol} `
|
|
25
|
+
: '';
|
|
26
|
+
return colorize(`${prefix}${message}`, variant, variant === 'error'
|
|
27
|
+
? process.stderr
|
|
28
|
+
: process.stdout);
|
|
29
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Box, Text, useInput, useStdin, useStdout } from 'ink';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
export function HookLogPanel({ hook }) {
|
|
5
|
+
const { stdout } = useStdout();
|
|
6
|
+
const { isRawModeSupported } = useStdin();
|
|
7
|
+
const visibleLineCount = Math.max(1, Math.min(12, (stdout.rows ?? 24) - 6));
|
|
8
|
+
const [firstLine, setFirstLine] = React.useState();
|
|
9
|
+
const lines = hook.tail
|
|
10
|
+
? [
|
|
11
|
+
...hook.lines,
|
|
12
|
+
{ text: hook.tail, stream: hook.tailStream },
|
|
13
|
+
]
|
|
14
|
+
: hook.lines;
|
|
15
|
+
const lastPage = Math.max(0, lines.length - visibleLineCount);
|
|
16
|
+
const start = Math.min(firstLine ?? lastPage, lastPage);
|
|
17
|
+
const visible = lines.slice(start, start + visibleLineCount);
|
|
18
|
+
const canScroll = isRawModeSupported && lastPage > 0;
|
|
19
|
+
useInput((_input, key) => {
|
|
20
|
+
const next = scrolledFirstLine(key, start, lastPage, visibleLineCount);
|
|
21
|
+
if (next !== null)
|
|
22
|
+
setFirstLine(next);
|
|
23
|
+
}, { isActive: canScroll });
|
|
24
|
+
const startLabel = hook.label.replace(/^Running /, '').replace(/^Completed /, '').replace(/^Failed /, '');
|
|
25
|
+
return (_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: "cyan", width: Math.min(stdout.columns ?? 80, 100), height: visibleLineCount + 4, paddingX: 1, marginBottom: 1, children: [_jsxs(Box, { flexDirection: "row", justifyContent: "space-between", children: [_jsx(Text, { bold: true, color: "cyan", children: startLabel }), _jsx(Text, { dimColor: true, children: hook.active
|
|
26
|
+
? 'running'
|
|
27
|
+
: hook.label.startsWith('Failed ')
|
|
28
|
+
? 'failed'
|
|
29
|
+
: 'finished' })] }), _jsx(Box, { flexDirection: "column", height: visibleLineCount, overflowY: "hidden", children: visible.length > 0
|
|
30
|
+
? visible.map((line, index) => (_jsx(Text, { color: line.stream === 'stderr'
|
|
31
|
+
? 'red'
|
|
32
|
+
: undefined, wrap: "truncate-end", children: line.text || ' ' }, `${start + index}-${line.stream}`)))
|
|
33
|
+
: _jsx(Text, { dimColor: true, children: hook.active ? 'Waiting for hook output…' : 'Hook finished without output.' }) }), _jsxs(Text, { dimColor: true, children: [lines.length > 0
|
|
34
|
+
? `Lines ${start + 1}–${Math.min(start + visibleLineCount, lines.length)} of ${lines.length}`
|
|
35
|
+
: 'No output yet', canScroll
|
|
36
|
+
? ' · ↑/↓ or PgUp/PgDn to scroll'
|
|
37
|
+
: ''] })] }));
|
|
38
|
+
}
|
|
39
|
+
// Undefined follows new output at the end; null leaves the view unchanged.
|
|
40
|
+
function scrolledFirstLine(key, start, lastPage, pageSize) {
|
|
41
|
+
const followEnd = (line) => line < lastPage
|
|
42
|
+
? line
|
|
43
|
+
: undefined;
|
|
44
|
+
if (key.upArrow)
|
|
45
|
+
return Math.max(0, start - 1);
|
|
46
|
+
if (key.downArrow)
|
|
47
|
+
return followEnd(start + 1);
|
|
48
|
+
if (key.pageUp)
|
|
49
|
+
return Math.max(0, start - pageSize);
|
|
50
|
+
if (key.pageDown)
|
|
51
|
+
return followEnd(start + pageSize);
|
|
52
|
+
if (key.home)
|
|
53
|
+
return 0;
|
|
54
|
+
if (key.end)
|
|
55
|
+
return undefined;
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
function missing(value) {
|
|
2
|
+
return value === undefined || value === '' || value === '-'
|
|
3
|
+
? null
|
|
4
|
+
: value;
|
|
5
|
+
}
|
|
6
|
+
export function environmentListJson(items) {
|
|
7
|
+
return items.map(item => ({
|
|
8
|
+
name: item.name,
|
|
9
|
+
branch: missing(item.branch),
|
|
10
|
+
path: missing(item.path),
|
|
11
|
+
status: missing(item.status),
|
|
12
|
+
dockerStatus: item.dockerStatus,
|
|
13
|
+
projectName: missing(item.projectName),
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
16
|
+
export function environmentInfoJson(info) {
|
|
17
|
+
return {
|
|
18
|
+
name: info.name,
|
|
19
|
+
branch: missing(info.branch),
|
|
20
|
+
path: info.path,
|
|
21
|
+
status: missing(info.status),
|
|
22
|
+
projectName: missing(info.projectName),
|
|
23
|
+
error: missing(info.error),
|
|
24
|
+
host: info.host === undefined
|
|
25
|
+
? null
|
|
26
|
+
: {
|
|
27
|
+
name: info.host.name,
|
|
28
|
+
path: missing(info.host.path),
|
|
29
|
+
},
|
|
30
|
+
containers: info.containers.map(({ name, service, state, status }) => ({
|
|
31
|
+
name,
|
|
32
|
+
service,
|
|
33
|
+
state,
|
|
34
|
+
status,
|
|
35
|
+
})),
|
|
36
|
+
volumes: info.volumes,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export function formatJson(value) {
|
|
40
|
+
return JSON.stringify(value, null, 2);
|
|
41
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useInput, useStdin } from 'ink';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { ConfirmationPrompt } from './confirmation-prompt.js';
|
|
5
|
+
import { ProgressView } from './progress.js';
|
|
6
|
+
import { useProgress } from './use-progress.js';
|
|
7
|
+
export function OperationView({ label, start, interrupt, finished = false }) {
|
|
8
|
+
const { isRawModeSupported } = useStdin();
|
|
9
|
+
const { progress, report } = useProgress();
|
|
10
|
+
const [prompt, setPrompt] = React.useState();
|
|
11
|
+
const startedRef = React.useRef(false);
|
|
12
|
+
useInput((input, key) => {
|
|
13
|
+
if (key.ctrl && input === 'c')
|
|
14
|
+
interrupt();
|
|
15
|
+
}, { isActive: isRawModeSupported });
|
|
16
|
+
React.useEffect(() => {
|
|
17
|
+
if (startedRef.current)
|
|
18
|
+
return;
|
|
19
|
+
startedRef.current = true;
|
|
20
|
+
start(report, async (request) => new Promise((resolve) => {
|
|
21
|
+
setPrompt({
|
|
22
|
+
request,
|
|
23
|
+
respond: (answer) => {
|
|
24
|
+
setPrompt(undefined);
|
|
25
|
+
resolve(answer);
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}));
|
|
29
|
+
}, [start, report]);
|
|
30
|
+
return prompt
|
|
31
|
+
? _jsx(ConfirmationPrompt, { request: prompt.request, onConfirm: () => prompt.respond(true), onCancel: () => prompt.respond(false) })
|
|
32
|
+
: _jsx(ProgressView, { progress: progress, fallback: label, finished: finished });
|
|
33
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
export const emptyHook = {
|
|
2
|
+
label: '',
|
|
3
|
+
active: false,
|
|
4
|
+
lines: [],
|
|
5
|
+
tail: '',
|
|
6
|
+
tailStream: 'stdout',
|
|
7
|
+
};
|
|
8
|
+
const maxStoredHookLines = 500;
|
|
9
|
+
export function isHookLabel(label) {
|
|
10
|
+
return /^Running (?:worktree|source)\.(?:before|after)_create hook/.test(label);
|
|
11
|
+
}
|
|
12
|
+
function appendHookOutput(hook, event) {
|
|
13
|
+
const cleaned = event.text
|
|
14
|
+
// ANSI CSI control sequences are emitted by many hooks; the ranges follow ECMA-48.
|
|
15
|
+
// eslint-disable-next-line no-control-regex, regexp/no-obscure-range
|
|
16
|
+
.replace(/\u001B\[[0-?]*[ -/]*[@-~]/g, '')
|
|
17
|
+
.replace(/\r\n/g, '\n');
|
|
18
|
+
const startsOutput = hook.label !== event.label;
|
|
19
|
+
const lines = startsOutput && hook.label.length > 0
|
|
20
|
+
? [
|
|
21
|
+
...hook.lines,
|
|
22
|
+
...(hook.tail
|
|
23
|
+
? [
|
|
24
|
+
{ text: hook.tail, stream: hook.tailStream },
|
|
25
|
+
]
|
|
26
|
+
: []),
|
|
27
|
+
{ text: `--- ${event.label} ---`, stream: 'stdout' },
|
|
28
|
+
]
|
|
29
|
+
: [...hook.lines];
|
|
30
|
+
const initialTail = !startsOutput && hook.tailStream === event.stream
|
|
31
|
+
? hook.tail
|
|
32
|
+
: '';
|
|
33
|
+
if (!startsOutput && hook.tail && hook.tailStream !== event.stream)
|
|
34
|
+
lines.push({ text: hook.tail, stream: hook.tailStream });
|
|
35
|
+
const parts = `${initialTail}${hook.carriageReturn && initialTail
|
|
36
|
+
? '\r'
|
|
37
|
+
: ''}${cleaned}`.split('\n');
|
|
38
|
+
for (const part of parts.slice(0, -1))
|
|
39
|
+
lines.push({ text: part.split('\r').filter(Boolean).at(-1) ?? '', stream: event.stream });
|
|
40
|
+
return {
|
|
41
|
+
...hook,
|
|
42
|
+
label: event.label,
|
|
43
|
+
active: true,
|
|
44
|
+
lines: lines.slice(-maxStoredHookLines),
|
|
45
|
+
tail: parts.at(-1)?.split('\r').filter(Boolean).at(-1) ?? '',
|
|
46
|
+
tailStream: event.stream,
|
|
47
|
+
carriageReturn: cleaned.endsWith('\r'),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function finishHook(hook, event) {
|
|
51
|
+
const lines = hook.tail
|
|
52
|
+
? [
|
|
53
|
+
...hook.lines,
|
|
54
|
+
{ text: hook.tail, stream: hook.tailStream },
|
|
55
|
+
]
|
|
56
|
+
: hook.lines;
|
|
57
|
+
return {
|
|
58
|
+
...hook,
|
|
59
|
+
active: false,
|
|
60
|
+
lines: lines.slice(-maxStoredHookLines),
|
|
61
|
+
tail: '',
|
|
62
|
+
...(event.status === 'failed'
|
|
63
|
+
? {
|
|
64
|
+
label: event.label.replace(/^Running /, 'Failed '),
|
|
65
|
+
}
|
|
66
|
+
: {
|
|
67
|
+
label: event.label.replace(/^Running /, 'Completed '),
|
|
68
|
+
}),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
export function updateProgress(previous, event) {
|
|
72
|
+
if (event.status === 'log') {
|
|
73
|
+
return {
|
|
74
|
+
...previous,
|
|
75
|
+
hook: appendHookOutput(previous.hook, event),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (event.status === 'started') {
|
|
79
|
+
let hook = previous.hook;
|
|
80
|
+
if (isHookLabel(event.label)) {
|
|
81
|
+
const lines = [...hook.lines];
|
|
82
|
+
if (hook.label.length > 0) {
|
|
83
|
+
if (hook.tail)
|
|
84
|
+
lines.push({ text: hook.tail, stream: hook.tailStream });
|
|
85
|
+
lines.push({ text: `--- ${event.label.replace(/^Running /, '')} ---`, stream: 'stdout' });
|
|
86
|
+
}
|
|
87
|
+
hook = {
|
|
88
|
+
label: event.label,
|
|
89
|
+
active: true,
|
|
90
|
+
lines: lines.slice(-maxStoredHookLines),
|
|
91
|
+
tail: '',
|
|
92
|
+
tailStream: 'stdout',
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
...previous,
|
|
97
|
+
current: event.label,
|
|
98
|
+
hook,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
const hook = event.label === previous.hook.label && previous.hook.active
|
|
102
|
+
? finishHook(previous.hook, event)
|
|
103
|
+
: previous.hook;
|
|
104
|
+
return {
|
|
105
|
+
steps: [...previous.steps, event],
|
|
106
|
+
hook,
|
|
107
|
+
...(previous.current === undefined
|
|
108
|
+
? {}
|
|
109
|
+
: { current: undefined }),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Spinner, StatusMessage } from '@inkjs/ui';
|
|
3
|
+
import { Box, Static } from 'ink';
|
|
4
|
+
import { HookLogPanel } from './hook-log-panel.js';
|
|
5
|
+
export function ProgressView({ progress, fallback, finished = false }) {
|
|
6
|
+
const active = finished
|
|
7
|
+
? undefined
|
|
8
|
+
: progress.current ?? (progress.steps.length === 0
|
|
9
|
+
? fallback
|
|
10
|
+
: undefined);
|
|
11
|
+
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Static, { items: progress.steps, children: (step, index) => (_jsx(StatusMessage, { variant: step.status === 'failed'
|
|
12
|
+
? 'error'
|
|
13
|
+
: 'success', children: step.label.replace(/^Running /, step.status === 'failed'
|
|
14
|
+
? 'Failed '
|
|
15
|
+
: 'Completed ') }, index)) }), active !== undefined && _jsx(Spinner, { label: active }), progress.hook.label && _jsx(HookLogPanel, { hook: progress.hook })] }));
|
|
16
|
+
}
|