dev-prism 0.3.0 → 0.6.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 +262 -70
- package/bin/dev-prism.js +45 -42
- package/dist/chunk-24OM3LGM.js +35 -0
- package/dist/chunk-3FYEGH2G.js +217 -0
- package/dist/chunk-3NW2OWIU.js +78 -0
- package/dist/chunk-3Q454U3I.js +71 -0
- package/dist/chunk-3TRRZEFR.js +38 -0
- package/dist/chunk-4UNCSJRM.js +70 -0
- package/dist/chunk-63II3EL4.js +98 -0
- package/dist/chunk-76LSQIZI.js +31 -0
- package/dist/chunk-7OSTLJLO.js +219 -0
- package/dist/chunk-7YEZWM6Y.js +97 -0
- package/dist/chunk-AEVARZQ4.js +203 -0
- package/dist/chunk-AIVPJ467.js +70 -0
- package/dist/chunk-AOM6BONB.js +98 -0
- package/dist/chunk-BXYXWNGH.js +30 -0
- package/dist/chunk-CUQVGZBX.js +44 -0
- package/dist/chunk-DQER5GNG.js +72 -0
- package/dist/chunk-DTE5YQMI.js +41 -0
- package/dist/chunk-EXRHG5KQ.js +60 -0
- package/dist/chunk-FKTFCSU7.js +78 -0
- package/dist/chunk-GKXXK2ZH.js +203 -0
- package/dist/chunk-H3L73URT.js +65 -0
- package/dist/chunk-H4HPDIY3.js +95 -0
- package/dist/chunk-HZBJF67X.js +60 -0
- package/dist/chunk-I3U6JK77.js +66 -0
- package/dist/chunk-JMKE3ZKI.js +61 -0
- package/dist/chunk-JZ2VPQXP.js +132 -0
- package/dist/chunk-KDYKLH6P.js +40 -0
- package/dist/chunk-KZJE62TK.js +203 -0
- package/dist/chunk-LAOWFCQL.js +21 -0
- package/dist/chunk-LNIOSGC4.js +78 -0
- package/dist/chunk-MRBTH5PL.js +66 -0
- package/dist/chunk-NNVP5F6I.js +77 -0
- package/dist/chunk-OL25TBYX.js +67 -0
- package/dist/chunk-OOJ6YOGS.js +53 -0
- package/dist/chunk-OPEZFBBI.js +219 -0
- package/dist/chunk-Q5DPX4WL.js +219 -0
- package/dist/chunk-RC2AUYZ7.js +49 -0
- package/dist/chunk-RQ245R7T.js +67 -0
- package/dist/chunk-SD3TON6N.js +32 -0
- package/dist/chunk-SEKH4ZV6.js +60 -0
- package/dist/chunk-SUMJLXT7.js +30 -0
- package/dist/chunk-UKYQN4A3.js +38 -0
- package/dist/chunk-URGGS3XM.js +95 -0
- package/dist/chunk-VUNPVDSO.js +74 -0
- package/dist/chunk-VXP2SPRI.js +51 -0
- package/dist/chunk-W54CPPSK.js +217 -0
- package/dist/chunk-X2PXZRYU.js +41 -0
- package/dist/chunk-X6FHBEAS.js +200 -0
- package/dist/chunk-YSO3IDZZ.js +40 -0
- package/dist/chunk-YY5DA35Z.js +40 -0
- package/dist/chunk-Z2ISJMLW.js +92 -0
- package/dist/chunk-ZKHNUDSL.js +119 -0
- package/dist/commands/create.d.ts +2 -1
- package/dist/commands/create.js +6 -5
- package/dist/commands/destroy.d.ts +1 -1
- package/dist/commands/destroy.js +4 -3
- package/dist/commands/info.d.ts +3 -0
- package/dist/commands/info.js +3 -2
- package/dist/commands/list.d.ts +1 -1
- package/dist/commands/list.js +3 -5
- package/dist/commands/logs.d.ts +8 -0
- package/dist/commands/logs.js +3 -2
- package/dist/commands/prune.d.ts +6 -0
- package/dist/commands/prune.js +4 -3
- package/dist/commands/start.d.ts +7 -0
- package/dist/commands/start.js +3 -2
- package/dist/commands/stop-all.d.ts +3 -0
- package/dist/commands/stop-all.js +3 -4
- package/dist/commands/stop.d.ts +3 -0
- package/dist/commands/stop.js +3 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +36 -18
- package/dist/lib/compose.d.ts +12 -0
- package/dist/lib/compose.js +12 -0
- package/dist/lib/config.d.ts +4 -0
- package/dist/lib/config.test.d.ts +1 -0
- package/dist/lib/config.test.js +32 -0
- package/dist/lib/docker-inspect.d.ts +24 -0
- package/dist/lib/docker-inspect.js +16 -0
- package/dist/lib/env.d.ts +5 -4
- package/dist/lib/env.js +1 -1
- package/dist/lib/env.test.d.ts +1 -0
- package/dist/lib/env.test.js +68 -0
- package/dist/lib/ports.d.ts +3 -3
- package/dist/lib/ports.js +3 -3
- package/dist/lib/ports.test.d.ts +1 -0
- package/dist/lib/ports.test.js +61 -0
- package/dist/lib/session.d.ts +16 -0
- package/dist/lib/session.js +13 -0
- package/dist/lib/store.d.ts +46 -0
- package/dist/lib/store.js +1 -1
- package/dist/lib/store.test.d.ts +1 -0
- package/dist/lib/store.test.js +205 -0
- package/dist/lib/worktree.d.ts +3 -13
- package/dist/lib/worktree.js +1 -1
- package/dist/lib/worktree.test.d.ts +1 -0
- package/dist/lib/worktree.test.js +41 -0
- package/package.json +2 -5
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import {
|
|
2
|
+
extractPorts,
|
|
3
|
+
formatPortsTable
|
|
4
|
+
} from "./chunk-LAOWFCQL.js";
|
|
5
|
+
import {
|
|
6
|
+
generateEnvStub,
|
|
7
|
+
getComposeProjectName,
|
|
8
|
+
writeComposeFile
|
|
9
|
+
} from "./chunk-RQ245R7T.js";
|
|
10
|
+
import {
|
|
11
|
+
writeAppEnvFiles,
|
|
12
|
+
writeEnvFile
|
|
13
|
+
} from "./chunk-HZBJF67X.js";
|
|
14
|
+
import {
|
|
15
|
+
createWorktree,
|
|
16
|
+
generateDefaultBranchName,
|
|
17
|
+
removeWorktree
|
|
18
|
+
} from "./chunk-3Q454U3I.js";
|
|
19
|
+
import {
|
|
20
|
+
down,
|
|
21
|
+
logs,
|
|
22
|
+
up
|
|
23
|
+
} from "./chunk-GBN67HYD.js";
|
|
24
|
+
import {
|
|
25
|
+
getSessionsDir,
|
|
26
|
+
loadConfig
|
|
27
|
+
} from "./chunk-25WQHUYW.js";
|
|
28
|
+
import {
|
|
29
|
+
getPortMappings,
|
|
30
|
+
sessionExists
|
|
31
|
+
} from "./chunk-JZ2VPQXP.js";
|
|
32
|
+
|
|
33
|
+
// src/commands/create.ts
|
|
34
|
+
import { existsSync, mkdirSync, copyFileSync, readFileSync, writeFileSync } from "fs";
|
|
35
|
+
import { basename, join, resolve } from "path";
|
|
36
|
+
import chalk from "chalk";
|
|
37
|
+
import { execa } from "execa";
|
|
38
|
+
function updateEnvDatabaseUrl(envPath, newDbUrl) {
|
|
39
|
+
if (!existsSync(envPath)) return;
|
|
40
|
+
let content = readFileSync(envPath, "utf-8");
|
|
41
|
+
if (content.includes("DATABASE_URL=")) {
|
|
42
|
+
content = content.replace(/^DATABASE_URL=.*/m, `DATABASE_URL=${newDbUrl}`);
|
|
43
|
+
} else {
|
|
44
|
+
content += `
|
|
45
|
+
DATABASE_URL=${newDbUrl}
|
|
46
|
+
`;
|
|
47
|
+
}
|
|
48
|
+
writeFileSync(envPath, content);
|
|
49
|
+
}
|
|
50
|
+
async function createSession(projectRoot, _sessionId, options) {
|
|
51
|
+
const config = await loadConfig(projectRoot);
|
|
52
|
+
const sessionsDir = getSessionsDir(config, projectRoot);
|
|
53
|
+
const inPlace = options.inPlace ?? false;
|
|
54
|
+
const mode = options.mode || "docker";
|
|
55
|
+
let workingDir;
|
|
56
|
+
let branchName = "";
|
|
57
|
+
if (inPlace) {
|
|
58
|
+
workingDir = projectRoot;
|
|
59
|
+
console.log(chalk.blue(`Creating session in current directory (${mode} mode)...`));
|
|
60
|
+
} else {
|
|
61
|
+
branchName = options.branch || generateDefaultBranchName();
|
|
62
|
+
workingDir = resolve(sessionsDir, branchName);
|
|
63
|
+
console.log(chalk.blue(`Creating session (${mode} mode)...`));
|
|
64
|
+
console.log(chalk.gray(`Branch: ${branchName}`));
|
|
65
|
+
console.log(chalk.gray(`Directory: ${workingDir}`));
|
|
66
|
+
}
|
|
67
|
+
if (await sessionExists(workingDir)) {
|
|
68
|
+
console.error(chalk.red(`
|
|
69
|
+
Error: Session already running in this directory.`));
|
|
70
|
+
console.error(chalk.gray(`Stop it first with: dev-prism stop`));
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
let profiles;
|
|
74
|
+
try {
|
|
75
|
+
if (!inPlace) {
|
|
76
|
+
if (!existsSync(sessionsDir)) {
|
|
77
|
+
mkdirSync(sessionsDir, { recursive: true });
|
|
78
|
+
}
|
|
79
|
+
console.log(chalk.blue("\nCreating git worktree..."));
|
|
80
|
+
await createWorktree(projectRoot, workingDir, branchName);
|
|
81
|
+
console.log(chalk.green(` Created: ${workingDir}`));
|
|
82
|
+
}
|
|
83
|
+
const projectName = config.projectName ?? basename(projectRoot);
|
|
84
|
+
const services = config.services ?? [
|
|
85
|
+
{ name: "postgres", internalPort: 5432 },
|
|
86
|
+
{ name: "app", internalPort: 3e3 }
|
|
87
|
+
];
|
|
88
|
+
console.log(chalk.blue("\nGenerating docker-compose.session.yml..."));
|
|
89
|
+
const composePath = writeComposeFile(workingDir, projectName, services);
|
|
90
|
+
console.log(chalk.green(` Written: ${composePath}`));
|
|
91
|
+
console.log(chalk.blue("\nGenerating .env.session stub..."));
|
|
92
|
+
const envStub = generateEnvStub(workingDir, projectName);
|
|
93
|
+
const envPath = resolve(workingDir, ".env.session");
|
|
94
|
+
writeFileSync(envPath, envStub, "utf-8");
|
|
95
|
+
console.log(chalk.green(` Written: ${envPath}`));
|
|
96
|
+
if (!inPlace) {
|
|
97
|
+
const envFilesToCopy = config.envFiles ?? [];
|
|
98
|
+
for (const envFile of envFilesToCopy) {
|
|
99
|
+
const srcPath = join(projectRoot, envFile);
|
|
100
|
+
const destPath = join(workingDir, envFile);
|
|
101
|
+
if (existsSync(srcPath)) {
|
|
102
|
+
copyFileSync(srcPath, destPath);
|
|
103
|
+
console.log(chalk.green(` Copied: ${envFile}`));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
console.log(chalk.blue("\nStarting Docker services..."));
|
|
108
|
+
if (mode === "docker") {
|
|
109
|
+
const allApps = config.apps ?? [];
|
|
110
|
+
const excludeApps = options.without ?? [];
|
|
111
|
+
profiles = allApps.filter((app) => !excludeApps.includes(app));
|
|
112
|
+
if (excludeApps.length > 0) {
|
|
113
|
+
console.log(chalk.gray(` Excluding apps: ${excludeApps.join(", ")}`));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
await up({ cwd: workingDir, profiles, detach: true });
|
|
117
|
+
console.log(chalk.blue("Waiting for services to be ready..."));
|
|
118
|
+
await new Promise((resolve2) => setTimeout(resolve2, 3e3));
|
|
119
|
+
console.log(chalk.blue("\nDiscovering ports from containers..."));
|
|
120
|
+
const portMappings = await getPortMappings(workingDir);
|
|
121
|
+
const ports = extractPorts(portMappings);
|
|
122
|
+
console.log(chalk.gray("Discovered ports:"));
|
|
123
|
+
console.log(chalk.gray(formatPortsTable(ports)));
|
|
124
|
+
const composeProjectName = getComposeProjectName(workingDir, projectName);
|
|
125
|
+
const finalEnvContent = writeEnvFile(workingDir, ports, composeProjectName);
|
|
126
|
+
console.log(chalk.green(` Updated: ${finalEnvContent}`));
|
|
127
|
+
const appEnvFiles = writeAppEnvFiles(config, workingDir, ports);
|
|
128
|
+
for (const file of appEnvFiles) {
|
|
129
|
+
console.log(chalk.green(` Written: ${file}`));
|
|
130
|
+
}
|
|
131
|
+
if (!inPlace && ports.POSTGRES_PORT) {
|
|
132
|
+
const sessionDbUrl = `postgresql://postgres:postgres@localhost:${ports.POSTGRES_PORT}/postgres`;
|
|
133
|
+
const envFilesToCopy = config.envFiles ?? [];
|
|
134
|
+
for (const envFile of envFilesToCopy) {
|
|
135
|
+
const destPath = join(workingDir, envFile);
|
|
136
|
+
if (existsSync(destPath)) {
|
|
137
|
+
updateEnvDatabaseUrl(destPath, sessionDbUrl);
|
|
138
|
+
console.log(chalk.green(` Updated DATABASE_URL in: ${envFile}`));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (config.setup.length > 0) {
|
|
143
|
+
console.log(chalk.blue("\nRunning setup commands..."));
|
|
144
|
+
const setupEnv = {
|
|
145
|
+
...process.env,
|
|
146
|
+
SESSION_DIR: workingDir
|
|
147
|
+
};
|
|
148
|
+
for (const [name, port] of Object.entries(ports)) {
|
|
149
|
+
setupEnv[name] = String(port);
|
|
150
|
+
}
|
|
151
|
+
if (ports.POSTGRES_PORT) {
|
|
152
|
+
setupEnv.DATABASE_URL = `postgresql://postgres:postgres@localhost:${ports.POSTGRES_PORT}/postgres`;
|
|
153
|
+
}
|
|
154
|
+
for (const cmd of config.setup) {
|
|
155
|
+
console.log(chalk.gray(` Running: ${cmd}`));
|
|
156
|
+
const [command, ...args] = cmd.split(" ");
|
|
157
|
+
try {
|
|
158
|
+
await execa(command, args, {
|
|
159
|
+
cwd: workingDir,
|
|
160
|
+
stdio: "inherit",
|
|
161
|
+
env: setupEnv
|
|
162
|
+
});
|
|
163
|
+
} catch {
|
|
164
|
+
console.warn(chalk.yellow(` Warning: Command failed: ${cmd}`));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
console.log(chalk.green(`
|
|
169
|
+
Session ready!`));
|
|
170
|
+
console.log(chalk.gray(`Directory: ${workingDir}`));
|
|
171
|
+
if (mode === "docker") {
|
|
172
|
+
console.log(chalk.gray("\nDocker mode - all services in containers."));
|
|
173
|
+
console.log(chalk.gray("View logs: docker compose -f docker-compose.session.yml logs -f"));
|
|
174
|
+
} else {
|
|
175
|
+
console.log(chalk.gray("\nNative mode - run apps with: pnpm dev"));
|
|
176
|
+
}
|
|
177
|
+
console.log(chalk.gray("\nPorts:"));
|
|
178
|
+
for (const [name, port] of Object.entries(ports)) {
|
|
179
|
+
console.log(chalk.cyan(` ${name}: http://localhost:${port}`));
|
|
180
|
+
}
|
|
181
|
+
if (options.detach === false) {
|
|
182
|
+
console.log(chalk.blue("\nStreaming logs (Ctrl+C to stop)..."));
|
|
183
|
+
console.log(chalk.gray("\u2500".repeat(60)));
|
|
184
|
+
try {
|
|
185
|
+
await logs({ cwd: workingDir, profiles });
|
|
186
|
+
} catch (error) {
|
|
187
|
+
const execaError = error;
|
|
188
|
+
if (execaError.signal === "SIGINT") {
|
|
189
|
+
console.log(chalk.gray("\n\u2500".repeat(60)));
|
|
190
|
+
console.log(chalk.yellow("\nLog streaming stopped. Services are still running."));
|
|
191
|
+
console.log(
|
|
192
|
+
chalk.gray(
|
|
193
|
+
`Resume logs: cd ${workingDir} && docker compose -f docker-compose.session.yml --env-file .env.session logs -f`
|
|
194
|
+
)
|
|
195
|
+
);
|
|
196
|
+
} else {
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
} catch (error) {
|
|
202
|
+
console.error(chalk.red("Session creation failed. Cleaning up..."));
|
|
203
|
+
try {
|
|
204
|
+
await down({ cwd: workingDir });
|
|
205
|
+
} catch {
|
|
206
|
+
}
|
|
207
|
+
if (!inPlace && branchName) {
|
|
208
|
+
try {
|
|
209
|
+
await removeWorktree(projectRoot, workingDir, branchName);
|
|
210
|
+
} catch {
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export {
|
|
218
|
+
createSession
|
|
219
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
removeWorktree
|
|
3
|
+
} from "./chunk-3Q454U3I.js";
|
|
4
|
+
import {
|
|
5
|
+
down
|
|
6
|
+
} from "./chunk-GBN67HYD.js";
|
|
7
|
+
import {
|
|
8
|
+
getSessionsDir,
|
|
9
|
+
loadConfig
|
|
10
|
+
} from "./chunk-25WQHUYW.js";
|
|
11
|
+
import {
|
|
12
|
+
getSession,
|
|
13
|
+
listActiveSessions
|
|
14
|
+
} from "./chunk-DQER5GNG.js";
|
|
15
|
+
|
|
16
|
+
// src/commands/destroy.ts
|
|
17
|
+
import { existsSync } from "fs";
|
|
18
|
+
import { resolve, basename } from "path";
|
|
19
|
+
import chalk from "chalk";
|
|
20
|
+
async function destroySession(workingDirOrProjectRoot, options) {
|
|
21
|
+
if (options.all) {
|
|
22
|
+
console.log(chalk.blue("Destroying all sessions..."));
|
|
23
|
+
const sessions = await listActiveSessions();
|
|
24
|
+
if (sessions.length === 0) {
|
|
25
|
+
console.log(chalk.gray("No active sessions found."));
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
for (const session2 of sessions) {
|
|
29
|
+
await destroySingleSession(session2.workingDir);
|
|
30
|
+
}
|
|
31
|
+
console.log(chalk.green(`
|
|
32
|
+
Destroyed ${sessions.length} session(s).`));
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (!workingDirOrProjectRoot) {
|
|
36
|
+
console.error(chalk.red("Error: Working directory required. Use --all to destroy all sessions."));
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
const session = await getSession(workingDirOrProjectRoot);
|
|
40
|
+
if (!session) {
|
|
41
|
+
console.error(chalk.red(`Error: No session found in ${workingDirOrProjectRoot}`));
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
await destroySingleSession(workingDirOrProjectRoot);
|
|
45
|
+
console.log(chalk.green(`
|
|
46
|
+
Session destroyed.`));
|
|
47
|
+
}
|
|
48
|
+
async function destroySingleSession(workingDir) {
|
|
49
|
+
console.log(chalk.blue(`
|
|
50
|
+
Destroying session in ${workingDir}...`));
|
|
51
|
+
const envFile = resolve(workingDir, ".env.session");
|
|
52
|
+
if (existsSync(envFile)) {
|
|
53
|
+
console.log(chalk.gray(" Stopping and removing Docker containers..."));
|
|
54
|
+
try {
|
|
55
|
+
await down({ cwd: workingDir });
|
|
56
|
+
} catch {
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const parentDir = resolve(workingDir, "..");
|
|
60
|
+
let isWorktree = false;
|
|
61
|
+
let projectRoot = "";
|
|
62
|
+
let branchName = "";
|
|
63
|
+
try {
|
|
64
|
+
let currentDir = workingDir;
|
|
65
|
+
for (let i = 0; i < 5; i++) {
|
|
66
|
+
currentDir = resolve(currentDir, "..");
|
|
67
|
+
const configPath = resolve(currentDir, "session.config.mjs");
|
|
68
|
+
const altConfigPath = resolve(currentDir, "session.config.js");
|
|
69
|
+
if (existsSync(configPath) || existsSync(altConfigPath)) {
|
|
70
|
+
projectRoot = currentDir;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (projectRoot) {
|
|
75
|
+
const config = await loadConfig(projectRoot);
|
|
76
|
+
const sessionsDir = getSessionsDir(config, projectRoot);
|
|
77
|
+
if (workingDir.startsWith(sessionsDir)) {
|
|
78
|
+
isWorktree = true;
|
|
79
|
+
branchName = basename(workingDir);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
if (isWorktree && projectRoot && branchName) {
|
|
85
|
+
console.log(chalk.gray(" Removing git worktree..."));
|
|
86
|
+
try {
|
|
87
|
+
await removeWorktree(projectRoot, workingDir, branchName);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.warn(chalk.yellow(` Warning: Could not remove worktree: ${error}`));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
console.log(chalk.green(` Session destroyed.`));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export {
|
|
96
|
+
destroySession
|
|
97
|
+
};
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import {
|
|
2
|
+
writeAppEnvFiles,
|
|
3
|
+
writeEnvFile
|
|
4
|
+
} from "./chunk-J36LRUXM.js";
|
|
5
|
+
import {
|
|
6
|
+
calculatePorts,
|
|
7
|
+
formatPortsTable
|
|
8
|
+
} from "./chunk-3ATDGV4Y.js";
|
|
9
|
+
import {
|
|
10
|
+
getSessionDir,
|
|
11
|
+
getSessionsDir,
|
|
12
|
+
loadConfig
|
|
13
|
+
} from "./chunk-25WQHUYW.js";
|
|
14
|
+
import {
|
|
15
|
+
createWorktree,
|
|
16
|
+
findNextSessionId,
|
|
17
|
+
generateDefaultBranchName,
|
|
18
|
+
removeWorktree
|
|
19
|
+
} from "./chunk-Y3GR6XK7.js";
|
|
20
|
+
import {
|
|
21
|
+
down,
|
|
22
|
+
logs,
|
|
23
|
+
up
|
|
24
|
+
} from "./chunk-GBN67HYD.js";
|
|
25
|
+
import {
|
|
26
|
+
SessionStore
|
|
27
|
+
} from "./chunk-ZKHNUDSL.js";
|
|
28
|
+
|
|
29
|
+
// src/commands/create.ts
|
|
30
|
+
import { existsSync, mkdirSync, copyFileSync, readFileSync, writeFileSync } from "fs";
|
|
31
|
+
import { basename, join } from "path";
|
|
32
|
+
import chalk from "chalk";
|
|
33
|
+
import { execa } from "execa";
|
|
34
|
+
function updateEnvDatabaseUrl(envPath, newDbUrl) {
|
|
35
|
+
if (!existsSync(envPath)) return;
|
|
36
|
+
let content = readFileSync(envPath, "utf-8");
|
|
37
|
+
if (content.includes("DATABASE_URL=")) {
|
|
38
|
+
content = content.replace(/^DATABASE_URL=.*/m, `DATABASE_URL=${newDbUrl}`);
|
|
39
|
+
} else {
|
|
40
|
+
content += `
|
|
41
|
+
DATABASE_URL=${newDbUrl}
|
|
42
|
+
`;
|
|
43
|
+
}
|
|
44
|
+
writeFileSync(envPath, content);
|
|
45
|
+
}
|
|
46
|
+
async function createSession(projectRoot, sessionId, options) {
|
|
47
|
+
const config = await loadConfig(projectRoot);
|
|
48
|
+
const sessionsDir = getSessionsDir(config, projectRoot);
|
|
49
|
+
const store = new SessionStore();
|
|
50
|
+
try {
|
|
51
|
+
if (!sessionId) {
|
|
52
|
+
sessionId = findNextSessionId(store.getUsedSessionIds());
|
|
53
|
+
console.log(chalk.gray(`Auto-assigned session ID: ${sessionId}`));
|
|
54
|
+
}
|
|
55
|
+
if (!/^\d{3}$/.test(sessionId)) {
|
|
56
|
+
console.error(chalk.red("Error: Session ID must be exactly 3 digits (001-999)"));
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
const inPlace = options.inPlace ?? false;
|
|
60
|
+
const branchName = options.branch || generateDefaultBranchName(sessionId);
|
|
61
|
+
const mode = options.mode || "docker";
|
|
62
|
+
console.log(chalk.blue(`Creating session ${sessionId} (${mode} mode${inPlace ? ", in-place" : ""})...`));
|
|
63
|
+
if (!inPlace) {
|
|
64
|
+
console.log(chalk.gray(`Branch: ${branchName}`));
|
|
65
|
+
}
|
|
66
|
+
const ports = calculatePorts(config, sessionId);
|
|
67
|
+
console.log(chalk.gray("\nPorts:"));
|
|
68
|
+
console.log(chalk.gray(formatPortsTable(ports)));
|
|
69
|
+
const sessionDir = inPlace ? projectRoot : getSessionDir(config, projectRoot, sessionId);
|
|
70
|
+
try {
|
|
71
|
+
store.reserve({
|
|
72
|
+
sessionId,
|
|
73
|
+
projectRoot,
|
|
74
|
+
sessionDir,
|
|
75
|
+
branch: inPlace ? "" : branchName,
|
|
76
|
+
mode,
|
|
77
|
+
inPlace
|
|
78
|
+
});
|
|
79
|
+
} catch {
|
|
80
|
+
console.error(chalk.red(`Error: Session ${sessionId} is already being created by another process.`));
|
|
81
|
+
process.exit(1);
|
|
82
|
+
}
|
|
83
|
+
let profiles;
|
|
84
|
+
try {
|
|
85
|
+
if (inPlace) {
|
|
86
|
+
console.log(chalk.blue("\nUsing current directory (in-place mode)..."));
|
|
87
|
+
console.log(chalk.green(` Directory: ${sessionDir}`));
|
|
88
|
+
} else {
|
|
89
|
+
if (!existsSync(sessionsDir)) {
|
|
90
|
+
mkdirSync(sessionsDir, { recursive: true });
|
|
91
|
+
}
|
|
92
|
+
console.log(chalk.blue("\nCreating git worktree..."));
|
|
93
|
+
await createWorktree(projectRoot, sessionDir, branchName);
|
|
94
|
+
console.log(chalk.green(` Created: ${sessionDir}`));
|
|
95
|
+
const sessionDbUrl = `postgresql://postgres:postgres@localhost:${ports.POSTGRES_PORT}/postgres`;
|
|
96
|
+
const envFilesToCopy = config.envFiles ?? [];
|
|
97
|
+
for (const envFile of envFilesToCopy) {
|
|
98
|
+
const srcPath = join(projectRoot, envFile);
|
|
99
|
+
const destPath = join(sessionDir, envFile);
|
|
100
|
+
if (existsSync(srcPath)) {
|
|
101
|
+
copyFileSync(srcPath, destPath);
|
|
102
|
+
updateEnvDatabaseUrl(destPath, sessionDbUrl);
|
|
103
|
+
console.log(chalk.green(` Copied: ${envFile} (updated DATABASE_URL)`));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
console.log(chalk.blue("\nGenerating .env.session..."));
|
|
108
|
+
const projectName = config.projectName ?? basename(projectRoot);
|
|
109
|
+
const envPath = writeEnvFile(sessionDir, sessionId, ports, projectName);
|
|
110
|
+
console.log(chalk.green(` Written: ${envPath}`));
|
|
111
|
+
const appEnvFiles = writeAppEnvFiles(config, sessionDir, sessionId, ports);
|
|
112
|
+
for (const file of appEnvFiles) {
|
|
113
|
+
console.log(chalk.green(` Written: ${file}`));
|
|
114
|
+
}
|
|
115
|
+
console.log(chalk.blue("\nStarting Docker services..."));
|
|
116
|
+
if (mode === "docker") {
|
|
117
|
+
const allApps = config.apps ?? [];
|
|
118
|
+
const excludeApps = options.without ?? [];
|
|
119
|
+
profiles = allApps.filter((app) => !excludeApps.includes(app));
|
|
120
|
+
if (excludeApps.length > 0) {
|
|
121
|
+
console.log(chalk.gray(` Excluding apps: ${excludeApps.join(", ")}`));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
await up({ cwd: sessionDir, profiles });
|
|
125
|
+
console.log(chalk.blue("Waiting for services to be ready..."));
|
|
126
|
+
await new Promise((resolve) => setTimeout(resolve, 3e3));
|
|
127
|
+
if (config.setup.length > 0) {
|
|
128
|
+
console.log(chalk.blue("\nRunning setup commands..."));
|
|
129
|
+
const setupEnv = {
|
|
130
|
+
...process.env,
|
|
131
|
+
SESSION_ID: sessionId,
|
|
132
|
+
// Add DATABASE_URL for db commands
|
|
133
|
+
DATABASE_URL: `postgresql://postgres:postgres@localhost:${ports.POSTGRES_PORT}/postgres`
|
|
134
|
+
};
|
|
135
|
+
for (const [name, port] of Object.entries(ports)) {
|
|
136
|
+
setupEnv[name] = String(port);
|
|
137
|
+
}
|
|
138
|
+
for (const cmd of config.setup) {
|
|
139
|
+
console.log(chalk.gray(` Running: ${cmd}`));
|
|
140
|
+
const [command, ...args] = cmd.split(" ");
|
|
141
|
+
try {
|
|
142
|
+
await execa(command, args, {
|
|
143
|
+
cwd: sessionDir,
|
|
144
|
+
stdio: "inherit",
|
|
145
|
+
env: setupEnv
|
|
146
|
+
});
|
|
147
|
+
} catch {
|
|
148
|
+
console.warn(chalk.yellow(` Warning: Command failed: ${cmd}`));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
} catch (error) {
|
|
153
|
+
console.error(chalk.red("Session creation failed. Cleaning up..."));
|
|
154
|
+
store.remove(sessionId);
|
|
155
|
+
try {
|
|
156
|
+
await down({ cwd: sessionDir });
|
|
157
|
+
} catch {
|
|
158
|
+
}
|
|
159
|
+
if (!inPlace) {
|
|
160
|
+
try {
|
|
161
|
+
await removeWorktree(projectRoot, sessionDir, branchName);
|
|
162
|
+
} catch {
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
167
|
+
console.log(chalk.green(`
|
|
168
|
+
Session ${sessionId} ready!`));
|
|
169
|
+
console.log(chalk.gray(`Directory: ${sessionDir}`));
|
|
170
|
+
if (mode === "docker") {
|
|
171
|
+
console.log(chalk.gray("\nDocker mode - all services in containers."));
|
|
172
|
+
console.log(chalk.gray("View logs: docker compose -f docker-compose.session.yml logs -f"));
|
|
173
|
+
} else {
|
|
174
|
+
console.log(chalk.gray("\nNative mode - run apps with: pnpm dev"));
|
|
175
|
+
}
|
|
176
|
+
console.log(chalk.gray("\nPorts:"));
|
|
177
|
+
for (const [name, port] of Object.entries(ports)) {
|
|
178
|
+
console.log(chalk.cyan(` ${name}: http://localhost:${port}`));
|
|
179
|
+
}
|
|
180
|
+
if (options.detach === false) {
|
|
181
|
+
console.log(chalk.blue("\nStreaming logs (Ctrl+C to stop)..."));
|
|
182
|
+
console.log(chalk.gray("\u2500".repeat(60)));
|
|
183
|
+
try {
|
|
184
|
+
await logs({ cwd: sessionDir, profiles });
|
|
185
|
+
} catch (error) {
|
|
186
|
+
const execaError = error;
|
|
187
|
+
if (execaError.signal === "SIGINT") {
|
|
188
|
+
console.log(chalk.gray("\n\u2500".repeat(60)));
|
|
189
|
+
console.log(chalk.yellow("\nLog streaming stopped. Services are still running."));
|
|
190
|
+
console.log(chalk.gray(`Resume logs: cd ${sessionDir} && docker compose -f docker-compose.session.yml --env-file .env.session logs -f`));
|
|
191
|
+
} else {
|
|
192
|
+
throw error;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
} finally {
|
|
197
|
+
store.close();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export {
|
|
202
|
+
createSession
|
|
203
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
calculatePorts
|
|
3
|
+
} from "./chunk-3ATDGV4Y.js";
|
|
4
|
+
import {
|
|
5
|
+
loadConfig
|
|
6
|
+
} from "./chunk-25WQHUYW.js";
|
|
7
|
+
import {
|
|
8
|
+
isRunning
|
|
9
|
+
} from "./chunk-GBN67HYD.js";
|
|
10
|
+
import {
|
|
11
|
+
SessionStore
|
|
12
|
+
} from "./chunk-ZKHNUDSL.js";
|
|
13
|
+
|
|
14
|
+
// src/commands/list.ts
|
|
15
|
+
import { existsSync } from "fs";
|
|
16
|
+
import { resolve } from "path";
|
|
17
|
+
import chalk from "chalk";
|
|
18
|
+
async function listSessions() {
|
|
19
|
+
const store = new SessionStore();
|
|
20
|
+
let sessions;
|
|
21
|
+
try {
|
|
22
|
+
sessions = store.listAll();
|
|
23
|
+
} finally {
|
|
24
|
+
store.close();
|
|
25
|
+
}
|
|
26
|
+
if (sessions.length === 0) {
|
|
27
|
+
console.log(chalk.gray("No active sessions found."));
|
|
28
|
+
console.log(chalk.gray("\nTo create a session:"));
|
|
29
|
+
console.log(chalk.cyan(" dev-prism create"));
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
console.log(chalk.blue("Active Sessions:"));
|
|
33
|
+
console.log(chalk.gray("================\n"));
|
|
34
|
+
for (const session of sessions) {
|
|
35
|
+
const sessionConfig = await loadConfig(session.project_root).catch(() => ({ portBase: 47e3, sessionsDir: "../sessions", ports: {}, setup: [] }));
|
|
36
|
+
const status = await getSessionStatus(session.session_id, session.session_dir, session.branch, sessionConfig);
|
|
37
|
+
printSessionStatus(status);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async function getSessionStatus(sessionId, path, branch, config) {
|
|
41
|
+
const ports = calculatePorts(config, sessionId);
|
|
42
|
+
let running = false;
|
|
43
|
+
const envFile = resolve(path, ".env.session");
|
|
44
|
+
if (existsSync(envFile)) {
|
|
45
|
+
running = await isRunning({ cwd: path });
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
sessionId,
|
|
49
|
+
path,
|
|
50
|
+
branch,
|
|
51
|
+
running,
|
|
52
|
+
ports
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function printSessionStatus(status) {
|
|
56
|
+
const statusIcon = status.running ? chalk.green("\u25CF") : chalk.red("\u25CB");
|
|
57
|
+
const statusText = status.running ? chalk.green("running") : chalk.gray("stopped");
|
|
58
|
+
console.log(`${statusIcon} Session ${chalk.bold(status.sessionId)} ${statusText}`);
|
|
59
|
+
console.log(chalk.gray(` Path: ${status.path}`));
|
|
60
|
+
console.log(chalk.gray(` Branch: ${status.branch}`));
|
|
61
|
+
console.log(chalk.gray(" Ports:"));
|
|
62
|
+
for (const [name, port] of Object.entries(status.ports)) {
|
|
63
|
+
console.log(chalk.cyan(` ${name}: http://localhost:${port}`));
|
|
64
|
+
}
|
|
65
|
+
console.log("");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export {
|
|
69
|
+
listSessions
|
|
70
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {
|
|
2
|
+
removeWorktree
|
|
3
|
+
} from "./chunk-FKTFCSU7.js";
|
|
4
|
+
import {
|
|
5
|
+
down,
|
|
6
|
+
isRunning
|
|
7
|
+
} from "./chunk-GBN67HYD.js";
|
|
8
|
+
import {
|
|
9
|
+
loadConfig
|
|
10
|
+
} from "./chunk-25WQHUYW.js";
|
|
11
|
+
import {
|
|
12
|
+
SessionStore
|
|
13
|
+
} from "./chunk-6YMQTISJ.js";
|
|
14
|
+
|
|
15
|
+
// src/commands/prune.ts
|
|
16
|
+
import { existsSync } from "fs";
|
|
17
|
+
import { resolve } from "path";
|
|
18
|
+
import { createInterface } from "readline";
|
|
19
|
+
import chalk from "chalk";
|
|
20
|
+
async function pruneSessions(projectRoot, options) {
|
|
21
|
+
const config = await loadConfig(projectRoot);
|
|
22
|
+
const store = new SessionStore();
|
|
23
|
+
try {
|
|
24
|
+
const sessions = store.listByProject(projectRoot);
|
|
25
|
+
if (sessions.length === 0) {
|
|
26
|
+
console.log(chalk.gray("No sessions found."));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const stoppedSessions = [];
|
|
30
|
+
for (const session of sessions) {
|
|
31
|
+
const envFile = resolve(session.session_dir, ".env.session");
|
|
32
|
+
let running = false;
|
|
33
|
+
if (existsSync(envFile)) {
|
|
34
|
+
running = await isRunning({ cwd: session.session_dir });
|
|
35
|
+
}
|
|
36
|
+
if (!running) {
|
|
37
|
+
stoppedSessions.push({
|
|
38
|
+
sessionId: session.session_id,
|
|
39
|
+
path: session.session_dir,
|
|
40
|
+
branch: session.branch,
|
|
41
|
+
inPlace: session.in_place === 1
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (stoppedSessions.length === 0) {
|
|
46
|
+
console.log(chalk.gray("No stopped sessions to prune."));
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
console.log(chalk.yellow(`
|
|
50
|
+
Found ${stoppedSessions.length} stopped session(s) to prune:`));
|
|
51
|
+
for (const session of stoppedSessions) {
|
|
52
|
+
console.log(chalk.gray(` - Session ${session.sessionId} (${session.branch})`));
|
|
53
|
+
}
|
|
54
|
+
console.log("");
|
|
55
|
+
if (!options.yes) {
|
|
56
|
+
const rl = createInterface({
|
|
57
|
+
input: process.stdin,
|
|
58
|
+
output: process.stdout
|
|
59
|
+
});
|
|
60
|
+
const answer = await new Promise((resolve2) => {
|
|
61
|
+
rl.question(chalk.red("Are you sure you want to delete these sessions? This cannot be undone. [y/N] "), resolve2);
|
|
62
|
+
});
|
|
63
|
+
rl.close();
|
|
64
|
+
if (answer.toLowerCase() !== "y" && answer.toLowerCase() !== "yes") {
|
|
65
|
+
console.log(chalk.gray("Cancelled."));
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
console.log(chalk.blue("\nPruning stopped sessions...\n"));
|
|
70
|
+
for (const session of stoppedSessions) {
|
|
71
|
+
console.log(chalk.gray(` Removing session ${session.sessionId}...`));
|
|
72
|
+
try {
|
|
73
|
+
const envFile = resolve(session.path, ".env.session");
|
|
74
|
+
if (existsSync(envFile)) {
|
|
75
|
+
try {
|
|
76
|
+
await down({ cwd: session.path });
|
|
77
|
+
} catch {
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (!session.inPlace) {
|
|
81
|
+
await removeWorktree(projectRoot, session.path, session.branch);
|
|
82
|
+
}
|
|
83
|
+
store.markDestroyed(projectRoot, session.sessionId);
|
|
84
|
+
console.log(chalk.green(` Session ${session.sessionId} removed.`));
|
|
85
|
+
} catch {
|
|
86
|
+
console.log(chalk.yellow(` Warning: Could not fully remove session ${session.sessionId}`));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
console.log(chalk.green(`
|
|
90
|
+
Pruned ${stoppedSessions.length} session(s).`));
|
|
91
|
+
} finally {
|
|
92
|
+
store.close();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export {
|
|
97
|
+
pruneSessions
|
|
98
|
+
};
|