@joripspace/cli 0.5.0 → 0.5.2
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 +1 -1
- package/bin/joripspace.js +1623 -1610
- package/lib/checkpoint-commands.js +210 -210
- package/lib/deploy-files.js +245 -245
- package/lib/deployment-error.js +14 -14
- package/lib/github-commands.js +185 -0
- package/lib/help.js +138 -137
- package/lib/installer-args.js +28 -28
- package/lib/installer-target.js +134 -134
- package/lib/mcp-config.js +981 -981
- package/lib/project-env.js +198 -198
- package/lib/project-paths.js +257 -257
- package/lib/remote-commands.js +392 -392
- package/lib/start-command.js +123 -123
- package/lib/start.js +1467 -1407
- package/lib/template-commands.js +812 -812
- package/package.json +1 -1
- package/vendor/core/github-workflow.cjs +199 -186
- package/vendor/core/onboarding.cjs +2404 -2403
- package/vendor/core/template-publish-workflow.cjs +169 -169
- package/vendor/templates/index.js +104 -104
- package/vendor/templates/templates/basic-worker/files/worker.js +9 -9
- package/vendor/templates/templates/basic-worker/manifest.json +14 -14
- package/vendor/templates/templates/r2-file-api/files/worker.js +42 -42
- package/vendor/templates/templates/r2-file-api/manifest.json +7 -7
package/lib/project-env.js
CHANGED
|
@@ -1,198 +1,198 @@
|
|
|
1
|
-
const fs = require('node:fs');
|
|
2
|
-
const path = require('node:path');
|
|
3
|
-
const { spawnSync } = require('node:child_process');
|
|
4
|
-
const { resolveProjectFileTarget } = require('./project-paths');
|
|
5
|
-
|
|
6
|
-
const PROJECT_ENV_FILE = '.env.joripspace';
|
|
7
|
-
function readProjectEnv(workspaceDir) {
|
|
8
|
-
assertProjectEnvUntracked(workspaceDir);
|
|
9
|
-
const envPath = managedProjectFilePath(workspaceDir, PROJECT_ENV_FILE);
|
|
10
|
-
return readDotEnvFile(envPath);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function readDotEnvFile(filePath) {
|
|
14
|
-
const values = {};
|
|
15
|
-
try {
|
|
16
|
-
for (const line of fs.readFileSync(filePath, 'utf8').split(/\r?\n/)) {
|
|
17
|
-
const trimmed = line.trim();
|
|
18
|
-
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
19
|
-
const index = trimmed.indexOf('=');
|
|
20
|
-
if (index <= 0) continue;
|
|
21
|
-
const key = trimmed.slice(0, index).trim();
|
|
22
|
-
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) continue;
|
|
23
|
-
values[key] = parseDotEnvValue(trimmed.slice(index + 1));
|
|
24
|
-
}
|
|
25
|
-
} catch {}
|
|
26
|
-
return values;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function updateDotEnvFile(filePath, updates) {
|
|
30
|
-
if (path.basename(filePath).toLowerCase() === PROJECT_ENV_FILE) {
|
|
31
|
-
const workspaceDir = path.dirname(filePath);
|
|
32
|
-
assertProjectEnvUntracked(workspaceDir);
|
|
33
|
-
ensureProjectEnvIgnored(workspaceDir);
|
|
34
|
-
filePath = managedProjectFilePath(workspaceDir, PROJECT_ENV_FILE);
|
|
35
|
-
}
|
|
36
|
-
let lines = [];
|
|
37
|
-
try {
|
|
38
|
-
lines = fs.readFileSync(filePath, 'utf8').split(/\r?\n/);
|
|
39
|
-
} catch {}
|
|
40
|
-
const pending = new Map(
|
|
41
|
-
Object.entries(updates).filter(([, value]) => value !== undefined && value !== null && value !== '')
|
|
42
|
-
);
|
|
43
|
-
const written = new Set();
|
|
44
|
-
const next = lines
|
|
45
|
-
.filter((line, index) => index < lines.length - 1 || line !== '')
|
|
46
|
-
.flatMap((line) => {
|
|
47
|
-
const match = /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(line);
|
|
48
|
-
if (!match || !pending.has(match[1])) return [line];
|
|
49
|
-
if (written.has(match[1])) return [];
|
|
50
|
-
const value = pending.get(match[1]);
|
|
51
|
-
written.add(match[1]);
|
|
52
|
-
return [`${match[1]}=${formatDotEnvValue(value)}`];
|
|
53
|
-
});
|
|
54
|
-
for (const [key, value] of pending) {
|
|
55
|
-
if (!written.has(key)) next.push(`${key}=${formatDotEnvValue(value)}`);
|
|
56
|
-
}
|
|
57
|
-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
58
|
-
const temporaryPath = `${filePath}.tmp-${process.pid}-${Date.now()}`;
|
|
59
|
-
fs.writeFileSync(temporaryPath, `${next.join('\n')}\n`, { encoding: 'utf8', mode: 0o600 });
|
|
60
|
-
replaceFileAtomically(temporaryPath, filePath);
|
|
61
|
-
if (process.platform !== 'win32') fs.chmodSync(filePath, 0o600);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function parseDotEnvValue(value) {
|
|
65
|
-
const trimmed = String(value || '').trim();
|
|
66
|
-
if (trimmed.length < 2) return trimmed;
|
|
67
|
-
const quote = trimmed[0];
|
|
68
|
-
if ((quote !== '"' && quote !== "'") || trimmed.at(-1) !== quote) return trimmed;
|
|
69
|
-
const inner = trimmed.slice(1, -1);
|
|
70
|
-
if (quote === "'") return inner;
|
|
71
|
-
return inner.replace(/\\n/g, '\n').replace(/\\r/g, '\r').replace(/\\"/g, '"').replace(/\\\\/g, '\\');
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function formatDotEnvValue(value) {
|
|
75
|
-
const source = String(value);
|
|
76
|
-
if (/^[^\s#'"\\]+$/.test(source)) return source;
|
|
77
|
-
return `"${source.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\r/g, '\\r').replace(/\n/g, '\\n')}"`;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function ensureProjectEnvIgnored(workspaceDir) {
|
|
81
|
-
assertProjectEnvUntracked(workspaceDir);
|
|
82
|
-
const filePath = managedProjectFilePath(workspaceDir, '.gitignore');
|
|
83
|
-
const ignored = git(workspaceDir, ['check-ignore', '--no-index', '--quiet', '--', PROJECT_ENV_FILE]);
|
|
84
|
-
if (ignored.status === 0) return false;
|
|
85
|
-
const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath, 'utf8') : '';
|
|
86
|
-
const lines = existing.split(/\r?\n/).filter((line) => line.trim() !== PROJECT_ENV_FILE);
|
|
87
|
-
while (lines.at(-1) === '') lines.pop();
|
|
88
|
-
lines.push(PROJECT_ENV_FILE);
|
|
89
|
-
const temporaryPath = `${filePath}.tmp-${process.pid}-${Date.now()}`;
|
|
90
|
-
fs.writeFileSync(temporaryPath, `${lines.join('\n')}\n`, 'utf8');
|
|
91
|
-
replaceFileAtomically(temporaryPath, filePath);
|
|
92
|
-
const verified = git(workspaceDir, ['check-ignore', '--no-index', '--quiet', '--', PROJECT_ENV_FILE]);
|
|
93
|
-
if (verified.status !== 0 && findGitMetadata(workspaceDir)) {
|
|
94
|
-
const error = new Error(`${PROJECT_ENV_FILE} is not effectively ignored by Git`);
|
|
95
|
-
error.code = 'project_env_not_ignored';
|
|
96
|
-
throw error;
|
|
97
|
-
}
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function assertProjectEnvUntracked(workspaceDir) {
|
|
102
|
-
const tracked = git(workspaceDir, ['ls-files', '--error-unmatch', '--', `:(icase)${PROJECT_ENV_FILE}`]);
|
|
103
|
-
if (tracked.status === 0) {
|
|
104
|
-
const error = new Error(
|
|
105
|
-
`${PROJECT_ENV_FILE} is tracked by Git. Remove it from the index before storing a token: git rm --cached -- ${PROJECT_ENV_FILE}`
|
|
106
|
-
);
|
|
107
|
-
error.code = 'project_env_tracked';
|
|
108
|
-
throw error;
|
|
109
|
-
}
|
|
110
|
-
if (tracked.status !== 1 && findGitMetadata(workspaceDir)) {
|
|
111
|
-
const error = new Error(`Git could not verify that ${PROJECT_ENV_FILE} is untracked`);
|
|
112
|
-
error.code = 'project_env_git_verification_failed';
|
|
113
|
-
throw error;
|
|
114
|
-
}
|
|
115
|
-
assertCanonicalProjectEnvCasing(workspaceDir);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function assertCanonicalProjectEnvCasing(workspaceDir) {
|
|
119
|
-
const matches = fs
|
|
120
|
-
.readdirSync(path.resolve(workspaceDir))
|
|
121
|
-
.filter((entry) => entry.toLowerCase() === PROJECT_ENV_FILE);
|
|
122
|
-
if (!matches.length) return;
|
|
123
|
-
if (matches.length !== 1 || matches[0] !== PROJECT_ENV_FILE) {
|
|
124
|
-
const error = new Error(`${PROJECT_ENV_FILE} must use canonical lowercase casing`);
|
|
125
|
-
error.code = 'non_canonical_workspace_path';
|
|
126
|
-
throw error;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function findGitMetadata(workspaceDir) {
|
|
131
|
-
let current = path.resolve(workspaceDir);
|
|
132
|
-
while (true) {
|
|
133
|
-
if (fs.existsSync(path.join(current, '.git'))) return true;
|
|
134
|
-
const parent = path.dirname(current);
|
|
135
|
-
if (parent === current) return false;
|
|
136
|
-
current = parent;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function git(workspaceDir, args) {
|
|
141
|
-
return spawnSync('git', ['-C', path.resolve(workspaceDir), ...args], {
|
|
142
|
-
encoding: 'utf8',
|
|
143
|
-
windowsHide: true,
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function replaceFileAtomically(temporaryPath, filePath) {
|
|
148
|
-
try {
|
|
149
|
-
fs.renameSync(temporaryPath, filePath);
|
|
150
|
-
return;
|
|
151
|
-
} catch (error) {
|
|
152
|
-
const backupPath = `${filePath}.bak-${process.pid}-${Date.now()}`;
|
|
153
|
-
try {
|
|
154
|
-
if (fs.existsSync(filePath)) fs.renameSync(filePath, backupPath);
|
|
155
|
-
fs.renameSync(temporaryPath, filePath);
|
|
156
|
-
fs.rmSync(backupPath, { force: true });
|
|
157
|
-
return;
|
|
158
|
-
} catch {
|
|
159
|
-
try {
|
|
160
|
-
if (fs.existsSync(backupPath) && !fs.existsSync(filePath)) fs.renameSync(backupPath, filePath);
|
|
161
|
-
} catch {}
|
|
162
|
-
fs.rmSync(temporaryPath, { force: true });
|
|
163
|
-
throw error;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function removeGitignoreEntries(workspaceDir, entries) {
|
|
169
|
-
const filePath = managedProjectFilePath(workspaceDir, '.gitignore');
|
|
170
|
-
if (!fs.existsSync(filePath)) return;
|
|
171
|
-
const existing = fs.readFileSync(filePath, 'utf8');
|
|
172
|
-
const removals = new Set(entries);
|
|
173
|
-
const lines = existing.split(/\r?\n/).filter((line) => !removals.has(line.trim()));
|
|
174
|
-
while (lines.at(-1) === '') lines.pop();
|
|
175
|
-
const next = lines.length > 0 ? `${lines.join('\n')}\n` : '';
|
|
176
|
-
if (next === existing) return;
|
|
177
|
-
const temporaryPath = `${filePath}.tmp-${process.pid}-${Date.now()}`;
|
|
178
|
-
fs.writeFileSync(temporaryPath, next);
|
|
179
|
-
replaceFileAtomically(temporaryPath, filePath);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function managedProjectFilePath(workspaceDir, relative) {
|
|
183
|
-
const location = resolveProjectFileTarget(workspaceDir, relative);
|
|
184
|
-
if (location.blockers.length) {
|
|
185
|
-
throw new Error(`managed project file path is blocked: ${location.blockers.join(', ')}`);
|
|
186
|
-
}
|
|
187
|
-
return location.target;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
module.exports = {
|
|
191
|
-
PROJECT_ENV_FILE,
|
|
192
|
-
assertProjectEnvUntracked,
|
|
193
|
-
ensureProjectEnvIgnored,
|
|
194
|
-
readDotEnvFile,
|
|
195
|
-
readProjectEnv,
|
|
196
|
-
removeGitignoreEntries,
|
|
197
|
-
updateDotEnvFile,
|
|
198
|
-
};
|
|
1
|
+
const fs = require('node:fs');
|
|
2
|
+
const path = require('node:path');
|
|
3
|
+
const { spawnSync } = require('node:child_process');
|
|
4
|
+
const { resolveProjectFileTarget } = require('./project-paths');
|
|
5
|
+
|
|
6
|
+
const PROJECT_ENV_FILE = '.env.joripspace';
|
|
7
|
+
function readProjectEnv(workspaceDir) {
|
|
8
|
+
assertProjectEnvUntracked(workspaceDir);
|
|
9
|
+
const envPath = managedProjectFilePath(workspaceDir, PROJECT_ENV_FILE);
|
|
10
|
+
return readDotEnvFile(envPath);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function readDotEnvFile(filePath) {
|
|
14
|
+
const values = {};
|
|
15
|
+
try {
|
|
16
|
+
for (const line of fs.readFileSync(filePath, 'utf8').split(/\r?\n/)) {
|
|
17
|
+
const trimmed = line.trim();
|
|
18
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
19
|
+
const index = trimmed.indexOf('=');
|
|
20
|
+
if (index <= 0) continue;
|
|
21
|
+
const key = trimmed.slice(0, index).trim();
|
|
22
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) continue;
|
|
23
|
+
values[key] = parseDotEnvValue(trimmed.slice(index + 1));
|
|
24
|
+
}
|
|
25
|
+
} catch {}
|
|
26
|
+
return values;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function updateDotEnvFile(filePath, updates) {
|
|
30
|
+
if (path.basename(filePath).toLowerCase() === PROJECT_ENV_FILE) {
|
|
31
|
+
const workspaceDir = path.dirname(filePath);
|
|
32
|
+
assertProjectEnvUntracked(workspaceDir);
|
|
33
|
+
ensureProjectEnvIgnored(workspaceDir);
|
|
34
|
+
filePath = managedProjectFilePath(workspaceDir, PROJECT_ENV_FILE);
|
|
35
|
+
}
|
|
36
|
+
let lines = [];
|
|
37
|
+
try {
|
|
38
|
+
lines = fs.readFileSync(filePath, 'utf8').split(/\r?\n/);
|
|
39
|
+
} catch {}
|
|
40
|
+
const pending = new Map(
|
|
41
|
+
Object.entries(updates).filter(([, value]) => value !== undefined && value !== null && value !== '')
|
|
42
|
+
);
|
|
43
|
+
const written = new Set();
|
|
44
|
+
const next = lines
|
|
45
|
+
.filter((line, index) => index < lines.length - 1 || line !== '')
|
|
46
|
+
.flatMap((line) => {
|
|
47
|
+
const match = /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(line);
|
|
48
|
+
if (!match || !pending.has(match[1])) return [line];
|
|
49
|
+
if (written.has(match[1])) return [];
|
|
50
|
+
const value = pending.get(match[1]);
|
|
51
|
+
written.add(match[1]);
|
|
52
|
+
return [`${match[1]}=${formatDotEnvValue(value)}`];
|
|
53
|
+
});
|
|
54
|
+
for (const [key, value] of pending) {
|
|
55
|
+
if (!written.has(key)) next.push(`${key}=${formatDotEnvValue(value)}`);
|
|
56
|
+
}
|
|
57
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
58
|
+
const temporaryPath = `${filePath}.tmp-${process.pid}-${Date.now()}`;
|
|
59
|
+
fs.writeFileSync(temporaryPath, `${next.join('\n')}\n`, { encoding: 'utf8', mode: 0o600 });
|
|
60
|
+
replaceFileAtomically(temporaryPath, filePath);
|
|
61
|
+
if (process.platform !== 'win32') fs.chmodSync(filePath, 0o600);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function parseDotEnvValue(value) {
|
|
65
|
+
const trimmed = String(value || '').trim();
|
|
66
|
+
if (trimmed.length < 2) return trimmed;
|
|
67
|
+
const quote = trimmed[0];
|
|
68
|
+
if ((quote !== '"' && quote !== "'") || trimmed.at(-1) !== quote) return trimmed;
|
|
69
|
+
const inner = trimmed.slice(1, -1);
|
|
70
|
+
if (quote === "'") return inner;
|
|
71
|
+
return inner.replace(/\\n/g, '\n').replace(/\\r/g, '\r').replace(/\\"/g, '"').replace(/\\\\/g, '\\');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function formatDotEnvValue(value) {
|
|
75
|
+
const source = String(value);
|
|
76
|
+
if (/^[^\s#'"\\]+$/.test(source)) return source;
|
|
77
|
+
return `"${source.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\r/g, '\\r').replace(/\n/g, '\\n')}"`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function ensureProjectEnvIgnored(workspaceDir) {
|
|
81
|
+
assertProjectEnvUntracked(workspaceDir);
|
|
82
|
+
const filePath = managedProjectFilePath(workspaceDir, '.gitignore');
|
|
83
|
+
const ignored = git(workspaceDir, ['check-ignore', '--no-index', '--quiet', '--', PROJECT_ENV_FILE]);
|
|
84
|
+
if (ignored.status === 0) return false;
|
|
85
|
+
const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath, 'utf8') : '';
|
|
86
|
+
const lines = existing.split(/\r?\n/).filter((line) => line.trim() !== PROJECT_ENV_FILE);
|
|
87
|
+
while (lines.at(-1) === '') lines.pop();
|
|
88
|
+
lines.push(PROJECT_ENV_FILE);
|
|
89
|
+
const temporaryPath = `${filePath}.tmp-${process.pid}-${Date.now()}`;
|
|
90
|
+
fs.writeFileSync(temporaryPath, `${lines.join('\n')}\n`, 'utf8');
|
|
91
|
+
replaceFileAtomically(temporaryPath, filePath);
|
|
92
|
+
const verified = git(workspaceDir, ['check-ignore', '--no-index', '--quiet', '--', PROJECT_ENV_FILE]);
|
|
93
|
+
if (verified.status !== 0 && findGitMetadata(workspaceDir)) {
|
|
94
|
+
const error = new Error(`${PROJECT_ENV_FILE} is not effectively ignored by Git`);
|
|
95
|
+
error.code = 'project_env_not_ignored';
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function assertProjectEnvUntracked(workspaceDir) {
|
|
102
|
+
const tracked = git(workspaceDir, ['ls-files', '--error-unmatch', '--', `:(icase)${PROJECT_ENV_FILE}`]);
|
|
103
|
+
if (tracked.status === 0) {
|
|
104
|
+
const error = new Error(
|
|
105
|
+
`${PROJECT_ENV_FILE} is tracked by Git. Remove it from the index before storing a token: git rm --cached -- ${PROJECT_ENV_FILE}`
|
|
106
|
+
);
|
|
107
|
+
error.code = 'project_env_tracked';
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
if (tracked.status !== 1 && findGitMetadata(workspaceDir)) {
|
|
111
|
+
const error = new Error(`Git could not verify that ${PROJECT_ENV_FILE} is untracked`);
|
|
112
|
+
error.code = 'project_env_git_verification_failed';
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
assertCanonicalProjectEnvCasing(workspaceDir);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function assertCanonicalProjectEnvCasing(workspaceDir) {
|
|
119
|
+
const matches = fs
|
|
120
|
+
.readdirSync(path.resolve(workspaceDir))
|
|
121
|
+
.filter((entry) => entry.toLowerCase() === PROJECT_ENV_FILE);
|
|
122
|
+
if (!matches.length) return;
|
|
123
|
+
if (matches.length !== 1 || matches[0] !== PROJECT_ENV_FILE) {
|
|
124
|
+
const error = new Error(`${PROJECT_ENV_FILE} must use canonical lowercase casing`);
|
|
125
|
+
error.code = 'non_canonical_workspace_path';
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function findGitMetadata(workspaceDir) {
|
|
131
|
+
let current = path.resolve(workspaceDir);
|
|
132
|
+
while (true) {
|
|
133
|
+
if (fs.existsSync(path.join(current, '.git'))) return true;
|
|
134
|
+
const parent = path.dirname(current);
|
|
135
|
+
if (parent === current) return false;
|
|
136
|
+
current = parent;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function git(workspaceDir, args) {
|
|
141
|
+
return spawnSync('git', ['-C', path.resolve(workspaceDir), ...args], {
|
|
142
|
+
encoding: 'utf8',
|
|
143
|
+
windowsHide: true,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function replaceFileAtomically(temporaryPath, filePath) {
|
|
148
|
+
try {
|
|
149
|
+
fs.renameSync(temporaryPath, filePath);
|
|
150
|
+
return;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
const backupPath = `${filePath}.bak-${process.pid}-${Date.now()}`;
|
|
153
|
+
try {
|
|
154
|
+
if (fs.existsSync(filePath)) fs.renameSync(filePath, backupPath);
|
|
155
|
+
fs.renameSync(temporaryPath, filePath);
|
|
156
|
+
fs.rmSync(backupPath, { force: true });
|
|
157
|
+
return;
|
|
158
|
+
} catch {
|
|
159
|
+
try {
|
|
160
|
+
if (fs.existsSync(backupPath) && !fs.existsSync(filePath)) fs.renameSync(backupPath, filePath);
|
|
161
|
+
} catch {}
|
|
162
|
+
fs.rmSync(temporaryPath, { force: true });
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function removeGitignoreEntries(workspaceDir, entries) {
|
|
169
|
+
const filePath = managedProjectFilePath(workspaceDir, '.gitignore');
|
|
170
|
+
if (!fs.existsSync(filePath)) return;
|
|
171
|
+
const existing = fs.readFileSync(filePath, 'utf8');
|
|
172
|
+
const removals = new Set(entries);
|
|
173
|
+
const lines = existing.split(/\r?\n/).filter((line) => !removals.has(line.trim()));
|
|
174
|
+
while (lines.at(-1) === '') lines.pop();
|
|
175
|
+
const next = lines.length > 0 ? `${lines.join('\n')}\n` : '';
|
|
176
|
+
if (next === existing) return;
|
|
177
|
+
const temporaryPath = `${filePath}.tmp-${process.pid}-${Date.now()}`;
|
|
178
|
+
fs.writeFileSync(temporaryPath, next);
|
|
179
|
+
replaceFileAtomically(temporaryPath, filePath);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function managedProjectFilePath(workspaceDir, relative) {
|
|
183
|
+
const location = resolveProjectFileTarget(workspaceDir, relative);
|
|
184
|
+
if (location.blockers.length) {
|
|
185
|
+
throw new Error(`managed project file path is blocked: ${location.blockers.join(', ')}`);
|
|
186
|
+
}
|
|
187
|
+
return location.target;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
module.exports = {
|
|
191
|
+
PROJECT_ENV_FILE,
|
|
192
|
+
assertProjectEnvUntracked,
|
|
193
|
+
ensureProjectEnvIgnored,
|
|
194
|
+
readDotEnvFile,
|
|
195
|
+
readProjectEnv,
|
|
196
|
+
removeGitignoreEntries,
|
|
197
|
+
updateDotEnvFile,
|
|
198
|
+
};
|