kryptheon 0.1.68 → 0.1.69
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 +38 -38
- package/bin/kryptheon.js +67 -5
- package/kryptheon-api.js +144 -4
- package/kryptheon-authz.js +26 -4
- package/kryptheon-baselines.js +232 -232
- package/kryptheon-changes.js +222 -222
- package/kryptheon-daily.js +192 -0
- package/kryptheon-dashboard-page.js +264 -2
- package/kryptheon-dashboard.html +25 -0
- package/kryptheon-dashboard.js +65 -3
- package/kryptheon-fonts-OFL.txt +98 -98
- package/kryptheon-probe-reporter.js +29 -29
- package/kryptheon-probe.config.js +37 -37
- package/kryptheon-project.js +600 -600
- package/kryptheon-questions.js +217 -0
- package/kryptheon-roles.js +4 -3
- package/kryptheon-selectors.js +301 -301
- package/kryptheon-store.js +334 -334
- package/kryptheon-testfiles.js +98 -98
- package/package.json +8 -2
- package/playwright.config.js +73 -73
package/kryptheon-project.js
CHANGED
|
@@ -1,600 +1,600 @@
|
|
|
1
|
-
// Working out whether this folder is a project at all.
|
|
2
|
-
//
|
|
3
|
-
// `kryptheon check` reads recordings out of ./tests and baselines out of the
|
|
4
|
-
// folder it was run in. Run it somewhere that is not a project - a home folder,
|
|
5
|
-
// the place a global install happened to leave you - and it will happily find
|
|
6
|
-
// whatever tests/ happens to be lying around and run those instead, reporting
|
|
7
|
-
// results for a project nobody asked about. That has already happened to
|
|
8
|
-
// somebody who installed into their home folder.
|
|
9
|
-
//
|
|
10
|
-
// Quietly testing the wrong project is the worst thing this tool can do. A
|
|
11
|
-
// green that belongs to something else is not a result, it is a lie with a tick
|
|
12
|
-
// next to it. So the answer to "is this a project?" has to be settled before
|
|
13
|
-
// anything runs, and the answer has to be a stop rather than a guess.
|
|
14
|
-
//
|
|
15
|
-
// Pure path work: nothing here runs a test or reads a recording.
|
|
16
|
-
|
|
17
|
-
const fs = require('fs');
|
|
18
|
-
const os = require('os');
|
|
19
|
-
const path = require('path');
|
|
20
|
-
|
|
21
|
-
// For someone who genuinely keeps recordings outside a package. Deliberately
|
|
22
|
-
// long-winded: it should be something you decide to type, not something you
|
|
23
|
-
// stumble into.
|
|
24
|
-
const ESCAPE_HATCH = 'KRYPTHEON_ALLOW_NO_PACKAGE_JSON';
|
|
25
|
-
|
|
26
|
-
// Where `record` keeps recordings for somebody who ran it in their home folder.
|
|
27
|
-
// A new terminal opens there, so for anyone who has only a web address - an
|
|
28
|
-
// app built in Lovable, no source folder anywhere - it is where the command
|
|
29
|
-
// gets typed.
|
|
30
|
-
const HOME_RECORDINGS = 'kryptheon-tests';
|
|
31
|
-
|
|
32
|
-
function hasPackageJson(dir) {
|
|
33
|
-
try {
|
|
34
|
-
return fs.statSync(path.join(dir, 'package.json')).isFile();
|
|
35
|
-
} catch (e) {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* One comparable form of a path.
|
|
42
|
-
*
|
|
43
|
-
* The trailing separator has to go or C:\Users and C:\Users\ never match, but
|
|
44
|
-
* it must not be taken off before the root test below. "C:" without a separator
|
|
45
|
-
* is not the drive - Node resolves it to the current directory - so a root
|
|
46
|
-
* compared in this form would quietly become "wherever I happen to be".
|
|
47
|
-
*/
|
|
48
|
-
function tidy(dir) {
|
|
49
|
-
return path.resolve(String(dir || '')).replace(/[\\/]+$/, '').toLowerCase();
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** The folders that are somebody's whole account or the machine itself. */
|
|
53
|
-
function isHomeOrSystem(dir) {
|
|
54
|
-
// A drive or filesystem root: C:\, D:\, /. Done on the resolved path, with
|
|
55
|
-
// its separator still on, for the reason above.
|
|
56
|
-
const resolved = path.resolve(String(dir || ''));
|
|
57
|
-
if (resolved === path.parse(resolved).root) return true;
|
|
58
|
-
|
|
59
|
-
const here = tidy(dir);
|
|
60
|
-
if (here === tidy(os.homedir())) return true;
|
|
61
|
-
|
|
62
|
-
for (const candidate of systemFolders()) {
|
|
63
|
-
if (here === tidy(candidate)) return true;
|
|
64
|
-
}
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Every folder that belongs to the machine or to an account rather than to a
|
|
70
|
-
* project, resolved.
|
|
71
|
-
*
|
|
72
|
-
* The first two entries are derived from the running system; the literals after
|
|
73
|
-
* them are fallbacks for the usual layouts. They overlap - on Windows
|
|
74
|
-
* USERPROFILE is the home folder and "/Users" resolves to C:\Users - and the
|
|
75
|
-
* overlap is deliberate: a machine whose home is somewhere unusual is covered
|
|
76
|
-
* by the derived pair, and a machine whose environment is missing entries is
|
|
77
|
-
* covered by the literals.
|
|
78
|
-
*/
|
|
79
|
-
function systemFolders() {
|
|
80
|
-
return [
|
|
81
|
-
// The folder every account lives in - C:\Users, /home, /Users. On its own
|
|
82
|
-
// it has no package.json so it would be refused anyway, but one stray npm
|
|
83
|
-
// install there and it would look exactly like a project.
|
|
84
|
-
path.dirname(os.homedir()),
|
|
85
|
-
process.env.SystemRoot,
|
|
86
|
-
process.env.ProgramFiles,
|
|
87
|
-
process.env['ProgramFiles(x86)'],
|
|
88
|
-
process.env.ProgramData,
|
|
89
|
-
process.env.APPDATA,
|
|
90
|
-
process.env.LOCALAPPDATA,
|
|
91
|
-
process.env.USERPROFILE,
|
|
92
|
-
'/root',
|
|
93
|
-
'/home',
|
|
94
|
-
'/Users',
|
|
95
|
-
'/usr',
|
|
96
|
-
'/etc',
|
|
97
|
-
'/bin',
|
|
98
|
-
'/var',
|
|
99
|
-
'/opt',
|
|
100
|
-
].filter(Boolean);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Whether this folder can be checked, and if not, why not.
|
|
105
|
-
*
|
|
106
|
-
* `allowed` is the escape hatch having been set. It is reported rather than
|
|
107
|
-
* hidden, so a run that only worked because of it says so.
|
|
108
|
-
*/
|
|
109
|
-
function inspectProject(dir, env) {
|
|
110
|
-
const environment = env || process.env;
|
|
111
|
-
const allowed = String(environment[ESCAPE_HATCH] || '') !== '';
|
|
112
|
-
const packaged = hasPackageJson(dir);
|
|
113
|
-
const personal = isHomeOrSystem(dir);
|
|
114
|
-
const base = { dir: dir, packaged: packaged, personal: personal, allowed: allowed };
|
|
115
|
-
|
|
116
|
-
// Somebody whose recordings `record` put in a folder of their own, standing
|
|
117
|
-
// in their home folder again because that is where a new terminal opens.
|
|
118
|
-
// Pointed at by name, rather than told to go and find a project they never
|
|
119
|
-
// had.
|
|
120
|
-
const kept = tidy(dir) === tidy(os.homedir()) ? path.join(path.resolve(dir), HOME_RECORDINGS) : null;
|
|
121
|
-
if (kept && hasPackageJson(kept)) base.recordingsAt = kept;
|
|
122
|
-
|
|
123
|
-
if (allowed) return Object.assign({ ok: true, reason: null }, base);
|
|
124
|
-
|
|
125
|
-
// A home folder is refused even when it has a package.json, and this is not
|
|
126
|
-
// belt and braces - it is the case that actually happened. Someone installed
|
|
127
|
-
// the tool into their home folder, so npm left a package.json and a
|
|
128
|
-
// node_modules there, and a tests folder alongside them. Every sign this
|
|
129
|
-
// could look for says "project"; none of them is true. The measured state of
|
|
130
|
-
// one real machine: C:\Users\<name> holding package.json, node_modules and
|
|
131
|
-
// tests, none of which belong to anything.
|
|
132
|
-
if (personal) return Object.assign({ ok: false, reason: 'personal' }, base);
|
|
133
|
-
if (!packaged) return Object.assign({ ok: false, reason: 'no-package' }, base);
|
|
134
|
-
return Object.assign({ ok: true, reason: null }, base);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Whether `kryptheon code` may read this folder.
|
|
139
|
-
*
|
|
140
|
-
* The same as any command, with one opening: a plain site - index.html and
|
|
141
|
-
* a few scripts, no npm anywhere - has no package.json and never will. Found
|
|
142
|
-
* on a blind test: exactly such an app was refused, and nothing was read. The
|
|
143
|
-
* refusal exists so that a check does not run somebody else's recordings;
|
|
144
|
-
* reading the files in front of you cannot do that. A page at the top of the
|
|
145
|
-
* folder, or in one of the folders a site's pages usually live in, is what
|
|
146
|
-
* says it is a site - found on the next blind test, where the pages were in
|
|
147
|
-
* src/ and the read refused at the project's root. A home or system folder
|
|
148
|
-
* stays refused.
|
|
149
|
-
*/
|
|
150
|
-
const PAGE_FOLDERS = ['', 'src', 'public', 'www', 'site', 'static', 'web', 'client', 'frontend'];
|
|
151
|
-
|
|
152
|
-
function inspectCodeFolder(dir, env) {
|
|
153
|
-
const here = inspectProject(dir, env);
|
|
154
|
-
if (here.ok || here.reason !== 'no-package') return here;
|
|
155
|
-
const hasPage = PAGE_FOLDERS.some((sub) => {
|
|
156
|
-
try {
|
|
157
|
-
const at = path.join(dir, sub);
|
|
158
|
-
return fs.readdirSync(at).some((name) => /\.html?$/i.test(name) && fs.statSync(path.join(at, name)).isFile());
|
|
159
|
-
} catch (e) {
|
|
160
|
-
return false;
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
return hasPage ? Object.assign({}, here, { ok: true, reason: null, plainSite: true }) : here;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Why this folder is not a project, and what to do instead.
|
|
168
|
-
*
|
|
169
|
-
* The folder is printed back because the commonest version of this is not
|
|
170
|
-
* knowing where you are - a new terminal, a global install, a shell that opened
|
|
171
|
-
* somewhere else.
|
|
172
|
-
*/
|
|
173
|
-
function noProjectLines(inspection) {
|
|
174
|
-
const lines = [''];
|
|
175
|
-
|
|
176
|
-
if (inspection.recordingsAt) {
|
|
177
|
-
lines.push(' This is your home folder, not a project.');
|
|
178
|
-
lines.push('');
|
|
179
|
-
lines.push(' Your recordings are in: ' + inspection.recordingsAt);
|
|
180
|
-
lines.push('');
|
|
181
|
-
lines.push(' To check them:');
|
|
182
|
-
lines.push('');
|
|
183
|
-
lines.push(' cd "' + inspection.recordingsAt + '"');
|
|
184
|
-
lines.push(' npx kryptheon check');
|
|
185
|
-
lines.push('');
|
|
186
|
-
return lines;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (inspection.personal) {
|
|
190
|
-
lines.push(' This is your home or a system folder, not a project.');
|
|
191
|
-
lines.push('');
|
|
192
|
-
lines.push(' You are in: ' + inspection.dir);
|
|
193
|
-
lines.push('');
|
|
194
|
-
// Almost never deliberate, and the one place where running anyway does the
|
|
195
|
-
// most damage. Said explicitly because a home folder that has had anything
|
|
196
|
-
// installed into it looks exactly like a project from the outside.
|
|
197
|
-
lines.push(' Recordings and results are kept beside the app they belong to,');
|
|
198
|
-
lines.push(' so running here would check whichever tests folder happens to be');
|
|
199
|
-
lines.push(' sitting in this one - almost certainly not the one you meant.');
|
|
200
|
-
if (inspection.packaged) {
|
|
201
|
-
lines.push('');
|
|
202
|
-
// Said plainly, because otherwise the package.json sitting right there
|
|
203
|
-
// makes this message look like the bug rather than the diagnosis.
|
|
204
|
-
lines.push(' There is a package.json here, but that is not what makes a project.');
|
|
205
|
-
lines.push(' It looks like npm install was run in this folder by mistake, which');
|
|
206
|
-
lines.push(' leaves a package.json and a node_modules behind.');
|
|
207
|
-
}
|
|
208
|
-
lines.push('');
|
|
209
|
-
} else {
|
|
210
|
-
lines.push(' There is no project here - this folder has no package.json.');
|
|
211
|
-
lines.push('');
|
|
212
|
-
lines.push(' You are in: ' + inspection.dir);
|
|
213
|
-
lines.push('');
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
lines.push(' Change into your project folder and run this again:');
|
|
217
|
-
lines.push('');
|
|
218
|
-
lines.push(' cd path\\to\\your\\project');
|
|
219
|
-
lines.push(' npx kryptheon check');
|
|
220
|
-
lines.push('');
|
|
221
|
-
// The person with a web address and no folder at all - an app built in
|
|
222
|
-
// Lovable - has no project to change into, and would be stuck on the line
|
|
223
|
-
// above. Not offered in a system folder: record refuses to set one up.
|
|
224
|
-
if (!inspection.personal || tidy(inspection.dir) === tidy(os.homedir())) {
|
|
225
|
-
lines.push(' No project folder, only a web address? Record from here and it');
|
|
226
|
-
lines.push(' will be set up for you:');
|
|
227
|
-
lines.push('');
|
|
228
|
-
lines.push(' npx kryptheon record https://your-app.example.com');
|
|
229
|
-
lines.push('');
|
|
230
|
-
}
|
|
231
|
-
lines.push(' If you really do keep recordings outside a package, set');
|
|
232
|
-
lines.push(' ' + ESCAPE_HATCH + '=1 and this will run anyway.');
|
|
233
|
-
lines.push('');
|
|
234
|
-
return lines;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/** A real project, with nothing recorded in it yet. */
|
|
238
|
-
function noRecordingsLines() {
|
|
239
|
-
return [
|
|
240
|
-
'',
|
|
241
|
-
' No recordings were found in this project.',
|
|
242
|
-
'',
|
|
243
|
-
' This is not a failure, and there is nothing in the code to fix.',
|
|
244
|
-
'',
|
|
245
|
-
' Record a flow through your app first:',
|
|
246
|
-
'',
|
|
247
|
-
' npx kryptheon record http://localhost:3000',
|
|
248
|
-
'',
|
|
249
|
-
' That opens a browser window, so it has to be run by a person in a',
|
|
250
|
-
' normal terminal - it cannot be done from an AI assistant.',
|
|
251
|
-
'',
|
|
252
|
-
];
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// --- Setting a folder up, before recording into it ---------------------------
|
|
256
|
-
//
|
|
257
|
-
// Somebody who built their app in Lovable has an address and nothing else: no
|
|
258
|
-
// source folder, no package.json, nothing to change into. Their recordings
|
|
259
|
-
// still need somewhere to live, `check` refuses a folder with no package.json,
|
|
260
|
-
// and a recording cannot load the fixture unless kryptheon is installed where
|
|
261
|
-
// the recording is.
|
|
262
|
-
//
|
|
263
|
-
// `record` used to find all of that out afterwards. Measured in an empty
|
|
264
|
-
// folder: it started the browser, would have let the person spend five minutes
|
|
265
|
-
// using their app and saved the recording - and then `check`, in that same
|
|
266
|
-
// folder, said "There is no project here". So record now works out what is
|
|
267
|
-
// missing before the browser, which is a 200MB download on a new machine, and
|
|
268
|
-
// offers to add it.
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* The installed kryptheon a recording saved in this folder will load - the
|
|
272
|
-
* nearest node_modules/kryptheon going up - or null if there is none.
|
|
273
|
-
*
|
|
274
|
-
* `check` must be run by this same copy. Found through the MCP server, then
|
|
275
|
-
* measured by hand in a folder `record` had set up: `npx kryptheon@latest
|
|
276
|
-
* check` runs npx's own copy, whose Playwright loads the config, while the
|
|
277
|
-
* recordings load this one's - and Playwright stops with "Requiring
|
|
278
|
-
* @playwright/test second time" before a single recording runs.
|
|
279
|
-
*/
|
|
280
|
-
function kryptheonCopyFor(dir) {
|
|
281
|
-
let current = path.resolve(String(dir || ''));
|
|
282
|
-
for (;;) {
|
|
283
|
-
const candidate = path.join(current, 'node_modules', 'kryptheon');
|
|
284
|
-
// Whole copies only. A half-finished install - the fixture there, the rest
|
|
285
|
-
// not - was handed the run and answered with Node's own stack trace.
|
|
286
|
-
const whole = ['kryptheon-fixture.js', 'package.json', path.join('bin', 'kryptheon.js')]
|
|
287
|
-
.every((f) => fs.existsSync(path.join(candidate, f)));
|
|
288
|
-
if (whole) return candidate;
|
|
289
|
-
const parent = path.dirname(current);
|
|
290
|
-
if (parent === current) return null;
|
|
291
|
-
current = parent;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* Whether kryptheon can be loaded by a recording saved in this folder.
|
|
297
|
-
*
|
|
298
|
-
* A filesystem walk, not require.resolve: this file lives inside the kryptheon
|
|
299
|
-
* package, and a package with a name and an "exports" map can always resolve
|
|
300
|
-
* itself by name, so require.resolve would answer "yes" even when the user's
|
|
301
|
-
* recordings have no way to find it.
|
|
302
|
-
*/
|
|
303
|
-
function kryptheonReachableFrom(dir) {
|
|
304
|
-
let current = path.resolve(String(dir || ''));
|
|
305
|
-
if (insideKryptheonItself(current)) return true;
|
|
306
|
-
for (;;) {
|
|
307
|
-
if (fs.existsSync(path.join(current, 'node_modules', 'kryptheon', 'kryptheon-fixture.js'))) {
|
|
308
|
-
return true;
|
|
309
|
-
}
|
|
310
|
-
const parent = path.dirname(current);
|
|
311
|
-
if (parent === current) return false;
|
|
312
|
-
current = parent;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* Inside the kryptheon package's own folder - this repository.
|
|
318
|
-
*
|
|
319
|
-
* Node lets a package load itself by its own name, so a recording made here
|
|
320
|
-
* finds the fixture with no node_modules/kryptheon anywhere. Without this,
|
|
321
|
-
* record in this repository offered to npm install kryptheon into kryptheon.
|
|
322
|
-
* Only the nearest package.json counts: that is the package a file belongs to.
|
|
323
|
-
*/
|
|
324
|
-
function insideKryptheonItself(dir) {
|
|
325
|
-
let current = dir;
|
|
326
|
-
for (;;) {
|
|
327
|
-
if (hasPackageJson(current)) {
|
|
328
|
-
// The name alone is not enough: somebody's own app can be called that.
|
|
329
|
-
// The fixture beside it is what makes it this package.
|
|
330
|
-
try {
|
|
331
|
-
return JSON.parse(fs.readFileSync(path.join(current, 'package.json'), 'utf8')).name === 'kryptheon' &&
|
|
332
|
-
fs.existsSync(path.join(current, 'kryptheon-fixture.js'));
|
|
333
|
-
} catch (err) {
|
|
334
|
-
return false;
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
const parent = path.dirname(current);
|
|
338
|
-
if (parent === current) return false;
|
|
339
|
-
current = parent;
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* Installed in this folder itself, not borrowed from one above it.
|
|
345
|
-
*
|
|
346
|
-
* A folder record sets up gets its own copy even when one is reachable from
|
|
347
|
-
* further up. The copy further up is, on the one machine where this was looked
|
|
348
|
-
* at, a stray install in a home folder - and recordings that lean on that stop
|
|
349
|
-
* working the day somebody tidies it away.
|
|
350
|
-
*/
|
|
351
|
-
function kryptheonInstalledIn(dir) {
|
|
352
|
-
return fs.existsSync(path.join(path.resolve(String(dir || '')), 'node_modules', 'kryptheon', 'kryptheon-fixture.js'));
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* The nearest folder above this one that is a project.
|
|
357
|
-
*
|
|
358
|
-
* Stops at a home or system folder rather than walking through it: the one
|
|
359
|
-
* package.json most likely to be found up there is the stray one a mistaken
|
|
360
|
-
* install left in a home folder, and that is nobody's project.
|
|
361
|
-
*/
|
|
362
|
-
function projectAbove(dir) {
|
|
363
|
-
let current = path.resolve(String(dir || ''));
|
|
364
|
-
for (;;) {
|
|
365
|
-
const parent = path.dirname(current);
|
|
366
|
-
if (parent === current) return null;
|
|
367
|
-
if (isHomeOrSystem(parent)) return null;
|
|
368
|
-
if (hasPackageJson(parent)) return parent;
|
|
369
|
-
current = parent;
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
/** A folder record set up on an earlier run - its package.json asks for kryptheon. */
|
|
374
|
-
function setUpForKryptheon(dir) {
|
|
375
|
-
try {
|
|
376
|
-
const json = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
|
|
377
|
-
return !!((json.devDependencies && json.devDependencies.kryptheon) ||
|
|
378
|
-
(json.dependencies && json.dependencies.kryptheon));
|
|
379
|
-
} catch (err) {
|
|
380
|
-
return false;
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
function isEmptyFolder(dir) {
|
|
385
|
-
try {
|
|
386
|
-
return fs.statSync(dir).isDirectory() && fs.readdirSync(dir).length === 0;
|
|
387
|
-
} catch (err) {
|
|
388
|
-
return false;
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* What this folder needs before a recording made in it can be checked. Only
|
|
394
|
-
* looks; never writes.
|
|
395
|
-
*
|
|
396
|
-
* ready nothing
|
|
397
|
-
* create no package.json and no project above: make one, then install
|
|
398
|
-
* install a project without kryptheon: install it
|
|
399
|
-
* above no package.json, but a project sits above this folder
|
|
400
|
-
* home a home folder: record in a kryptheon-tests folder inside it
|
|
401
|
-
* refuse a system folder, or a home whose kryptheon-tests is taken
|
|
402
|
-
*/
|
|
403
|
-
function setupPlan(dir, env) {
|
|
404
|
-
const environment = env || process.env;
|
|
405
|
-
const here = path.resolve(String(dir || ''));
|
|
406
|
-
|
|
407
|
-
// Somebody who has said they keep recordings outside a package is left
|
|
408
|
-
// exactly as they were before any of this existed.
|
|
409
|
-
if (String(environment[ESCAPE_HATCH] || '') !== '') return { action: 'ready', dir: here };
|
|
410
|
-
|
|
411
|
-
if (tidy(here) === tidy(os.homedir())) {
|
|
412
|
-
const into = path.join(here, HOME_RECORDINGS);
|
|
413
|
-
// Used only if it is plainly ours or plainly nothing. A folder of that name
|
|
414
|
-
// holding something else is somebody's, and recording into it would put a
|
|
415
|
-
// package.json and node_modules in the middle of it.
|
|
416
|
-
if (!fs.existsSync(into) || isEmptyFolder(into) || setUpForKryptheon(into)) {
|
|
417
|
-
return { action: 'home', dir: here, into: into };
|
|
418
|
-
}
|
|
419
|
-
return { action: 'refuse', reason: 'taken', dir: here, into: into };
|
|
420
|
-
}
|
|
421
|
-
if (isHomeOrSystem(here)) return { action: 'refuse', reason: 'system', dir: here };
|
|
422
|
-
|
|
423
|
-
if (!hasPackageJson(here)) {
|
|
424
|
-
const above = projectAbove(here);
|
|
425
|
-
if (above) return { action: 'above', dir: here, project: above };
|
|
426
|
-
return { action: 'create', dir: here };
|
|
427
|
-
}
|
|
428
|
-
if (!kryptheonReachableFrom(here)) return { action: 'install', dir: here };
|
|
429
|
-
return { action: 'ready', dir: here };
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
/** What is about to happen to this folder, said before it happens. */
|
|
433
|
-
function setupLines(plan, url) {
|
|
434
|
-
const address = url || 'https://your-app.example.com';
|
|
435
|
-
const lines = [''];
|
|
436
|
-
|
|
437
|
-
if (plan.action === 'create') {
|
|
438
|
-
lines.push(' This folder is not set up for recordings yet.');
|
|
439
|
-
lines.push('');
|
|
440
|
-
lines.push(' You are in: ' + plan.dir);
|
|
441
|
-
lines.push('');
|
|
442
|
-
lines.push(' Your recordings are saved here, and kryptheon has to be installed');
|
|
443
|
-
lines.push(' beside them or they cannot be checked later. So I will add:');
|
|
444
|
-
lines.push('');
|
|
445
|
-
lines.push(' package.json a small file that marks this folder as a project');
|
|
446
|
-
lines.push(' node_modules kryptheon itself, about 20MB');
|
|
447
|
-
lines.push('');
|
|
448
|
-
lines.push(' Nothing already in this folder is touched.');
|
|
449
|
-
} else if (plan.action === 'install') {
|
|
450
|
-
lines.push(' kryptheon is not installed in this project yet.');
|
|
451
|
-
lines.push('');
|
|
452
|
-
lines.push(' You are in: ' + plan.dir);
|
|
453
|
-
lines.push('');
|
|
454
|
-
lines.push(' Recordings load it by name, so without it here they cannot be');
|
|
455
|
-
lines.push(' checked later. I will run:');
|
|
456
|
-
lines.push('');
|
|
457
|
-
lines.push(' npm install --save-dev kryptheon');
|
|
458
|
-
lines.push('');
|
|
459
|
-
lines.push(' which adds one line to package.json and about 20MB to node_modules.');
|
|
460
|
-
} else if (plan.action === 'above') {
|
|
461
|
-
lines.push(' This folder is inside a project:');
|
|
462
|
-
lines.push('');
|
|
463
|
-
lines.push(' ' + plan.project);
|
|
464
|
-
lines.push('');
|
|
465
|
-
lines.push(' Recordings belong beside the app they test, so the usual thing is');
|
|
466
|
-
lines.push(' to run this from there:');
|
|
467
|
-
lines.push('');
|
|
468
|
-
lines.push(' cd "' + plan.project + '"');
|
|
469
|
-
lines.push(' npx kryptheon record ' + address);
|
|
470
|
-
lines.push('');
|
|
471
|
-
lines.push(' If that is not your project - a package.json can be left behind by');
|
|
472
|
-
lines.push(' an install run in the wrong place - I can set this folder up on its');
|
|
473
|
-
lines.push(' own instead.');
|
|
474
|
-
} else if (plan.action === 'home') {
|
|
475
|
-
lines.push(' You are in your home folder.');
|
|
476
|
-
lines.push('');
|
|
477
|
-
lines.push(' Recordings are kept in a folder of their own rather than loose in');
|
|
478
|
-
lines.push(' here, so I will ' + (fs.existsSync(plan.into) ? 'use this one' : 'make one') + ' and work in that:');
|
|
479
|
-
lines.push('');
|
|
480
|
-
lines.push(' ' + plan.into);
|
|
481
|
-
} else if (plan.action === 'refuse' && plan.reason === 'taken') {
|
|
482
|
-
lines.push(' You are in your home folder. Recordings are kept in a folder of');
|
|
483
|
-
lines.push(' their own, and I would normally use:');
|
|
484
|
-
lines.push('');
|
|
485
|
-
lines.push(' ' + plan.into);
|
|
486
|
-
lines.push('');
|
|
487
|
-
lines.push(' but something else is already there, and it is not one I made.');
|
|
488
|
-
lines.push(' Nothing was changed.');
|
|
489
|
-
lines.push('');
|
|
490
|
-
lines.push(' Make a new folder of your own, open a terminal in it, and run the');
|
|
491
|
-
lines.push(' same command again.');
|
|
492
|
-
} else if (plan.action === 'refuse') {
|
|
493
|
-
lines.push(' This is a system folder, not somewhere to keep recordings.');
|
|
494
|
-
lines.push('');
|
|
495
|
-
lines.push(' You are in: ' + plan.dir);
|
|
496
|
-
lines.push('');
|
|
497
|
-
lines.push(' Nothing was changed. Make a new folder of your own - on the Desktop');
|
|
498
|
-
lines.push(' is fine - open a terminal in it, and run the same command again.');
|
|
499
|
-
if (process.platform === 'win32') {
|
|
500
|
-
lines.push(' (In File Explorer: right-click inside the folder, Open in Terminal.)');
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
lines.push('');
|
|
504
|
-
return lines;
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
/**
|
|
508
|
-
* The question, and which way Enter answers it.
|
|
509
|
-
*
|
|
510
|
-
* Yes by default where saying yes is what the person came for. No by default
|
|
511
|
-
* for a folder inside another project, where yes puts a second project inside
|
|
512
|
-
* somebody's first.
|
|
513
|
-
*/
|
|
514
|
-
function setupQuestion(plan) {
|
|
515
|
-
if (plan.action === 'above') return { text: ' Set this folder up on its own instead? [y/N] ', yesByDefault: false };
|
|
516
|
-
if (plan.action === 'install') return { text: ' Install it? [Y/n] ', yesByDefault: true };
|
|
517
|
-
if (plan.action === 'home') return { text: ' Go ahead? [Y/n] ', yesByDefault: true };
|
|
518
|
-
return { text: ' Set this folder up? [Y/n] ', yesByDefault: true };
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
/** An answer typed at the question, read the way the question offered it. */
|
|
522
|
-
function answeredYes(answer, yesByDefault) {
|
|
523
|
-
const typed = String(answer || '').trim();
|
|
524
|
-
if (!typed) return !!yesByDefault;
|
|
525
|
-
return /^y/i.test(typed);
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
/** After recording from a home folder: where the recordings went, and how to get back to them. */
|
|
529
|
-
function homeDoneLines(into) {
|
|
530
|
-
return [
|
|
531
|
-
'',
|
|
532
|
-
' Your recordings are in: ' + into,
|
|
533
|
-
'',
|
|
534
|
-
' To check them after a change, open a terminal and run:',
|
|
535
|
-
'',
|
|
536
|
-
' cd "' + into + '"',
|
|
537
|
-
' npx kryptheon check',
|
|
538
|
-
'',
|
|
539
|
-
];
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
/** The install did not finish. Nothing needs undoing: running again picks up where this stopped. */
|
|
543
|
-
function installFailedLines() {
|
|
544
|
-
return [
|
|
545
|
-
'',
|
|
546
|
-
' kryptheon could not be installed in this folder.',
|
|
547
|
-
'',
|
|
548
|
-
' Installing needs an internet connection. Check it, then run the same',
|
|
549
|
-
' command again - nothing needs undoing first.',
|
|
550
|
-
'',
|
|
551
|
-
' Or install it yourself, then record:',
|
|
552
|
-
'',
|
|
553
|
-
' npm install --save-dev kryptheon',
|
|
554
|
-
'',
|
|
555
|
-
];
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
/**
|
|
559
|
-
* Whether version `a` is older than `b`, by their numbers. Anything that is
|
|
560
|
-
* not plain x.y.z is never called older: updating on a guess would change
|
|
561
|
-
* somebody's package.json for nothing, and only digits and dots ever reach
|
|
562
|
-
* the npm command line built from the answer.
|
|
563
|
-
*/
|
|
564
|
-
function olderVersion(a, b) {
|
|
565
|
-
const parse = (v) => {
|
|
566
|
-
const m = String(v == null ? '' : v).match(/^(\d+)\.(\d+)\.(\d+)$/);
|
|
567
|
-
return m ? m.slice(1).map(Number) : null;
|
|
568
|
-
};
|
|
569
|
-
const x = parse(a);
|
|
570
|
-
const y = parse(b);
|
|
571
|
-
if (!x || !y) return false;
|
|
572
|
-
for (let i = 0; i < 3; i++) {
|
|
573
|
-
if (x[i] !== y[i]) return x[i] < y[i];
|
|
574
|
-
}
|
|
575
|
-
return false;
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
module.exports = {
|
|
579
|
-
ESCAPE_HATCH: ESCAPE_HATCH,
|
|
580
|
-
olderVersion: olderVersion,
|
|
581
|
-
HOME_RECORDINGS: HOME_RECORDINGS,
|
|
582
|
-
kryptheonReachableFrom: kryptheonReachableFrom,
|
|
583
|
-
kryptheonCopyFor: kryptheonCopyFor,
|
|
584
|
-
kryptheonInstalledIn: kryptheonInstalledIn,
|
|
585
|
-
projectAbove: projectAbove,
|
|
586
|
-
setupPlan: setupPlan,
|
|
587
|
-
setupLines: setupLines,
|
|
588
|
-
setupQuestion: setupQuestion,
|
|
589
|
-
answeredYes: answeredYes,
|
|
590
|
-
homeDoneLines: homeDoneLines,
|
|
591
|
-
installFailedLines: installFailedLines,
|
|
592
|
-
hasPackageJson: hasPackageJson,
|
|
593
|
-
systemFolders: systemFolders,
|
|
594
|
-
tidy: tidy,
|
|
595
|
-
isHomeOrSystem: isHomeOrSystem,
|
|
596
|
-
inspectProject: inspectProject,
|
|
597
|
-
inspectCodeFolder: inspectCodeFolder,
|
|
598
|
-
noProjectLines: noProjectLines,
|
|
599
|
-
noRecordingsLines: noRecordingsLines,
|
|
600
|
-
};
|
|
1
|
+
// Working out whether this folder is a project at all.
|
|
2
|
+
//
|
|
3
|
+
// `kryptheon check` reads recordings out of ./tests and baselines out of the
|
|
4
|
+
// folder it was run in. Run it somewhere that is not a project - a home folder,
|
|
5
|
+
// the place a global install happened to leave you - and it will happily find
|
|
6
|
+
// whatever tests/ happens to be lying around and run those instead, reporting
|
|
7
|
+
// results for a project nobody asked about. That has already happened to
|
|
8
|
+
// somebody who installed into their home folder.
|
|
9
|
+
//
|
|
10
|
+
// Quietly testing the wrong project is the worst thing this tool can do. A
|
|
11
|
+
// green that belongs to something else is not a result, it is a lie with a tick
|
|
12
|
+
// next to it. So the answer to "is this a project?" has to be settled before
|
|
13
|
+
// anything runs, and the answer has to be a stop rather than a guess.
|
|
14
|
+
//
|
|
15
|
+
// Pure path work: nothing here runs a test or reads a recording.
|
|
16
|
+
|
|
17
|
+
const fs = require('fs');
|
|
18
|
+
const os = require('os');
|
|
19
|
+
const path = require('path');
|
|
20
|
+
|
|
21
|
+
// For someone who genuinely keeps recordings outside a package. Deliberately
|
|
22
|
+
// long-winded: it should be something you decide to type, not something you
|
|
23
|
+
// stumble into.
|
|
24
|
+
const ESCAPE_HATCH = 'KRYPTHEON_ALLOW_NO_PACKAGE_JSON';
|
|
25
|
+
|
|
26
|
+
// Where `record` keeps recordings for somebody who ran it in their home folder.
|
|
27
|
+
// A new terminal opens there, so for anyone who has only a web address - an
|
|
28
|
+
// app built in Lovable, no source folder anywhere - it is where the command
|
|
29
|
+
// gets typed.
|
|
30
|
+
const HOME_RECORDINGS = 'kryptheon-tests';
|
|
31
|
+
|
|
32
|
+
function hasPackageJson(dir) {
|
|
33
|
+
try {
|
|
34
|
+
return fs.statSync(path.join(dir, 'package.json')).isFile();
|
|
35
|
+
} catch (e) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* One comparable form of a path.
|
|
42
|
+
*
|
|
43
|
+
* The trailing separator has to go or C:\Users and C:\Users\ never match, but
|
|
44
|
+
* it must not be taken off before the root test below. "C:" without a separator
|
|
45
|
+
* is not the drive - Node resolves it to the current directory - so a root
|
|
46
|
+
* compared in this form would quietly become "wherever I happen to be".
|
|
47
|
+
*/
|
|
48
|
+
function tidy(dir) {
|
|
49
|
+
return path.resolve(String(dir || '')).replace(/[\\/]+$/, '').toLowerCase();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The folders that are somebody's whole account or the machine itself. */
|
|
53
|
+
function isHomeOrSystem(dir) {
|
|
54
|
+
// A drive or filesystem root: C:\, D:\, /. Done on the resolved path, with
|
|
55
|
+
// its separator still on, for the reason above.
|
|
56
|
+
const resolved = path.resolve(String(dir || ''));
|
|
57
|
+
if (resolved === path.parse(resolved).root) return true;
|
|
58
|
+
|
|
59
|
+
const here = tidy(dir);
|
|
60
|
+
if (here === tidy(os.homedir())) return true;
|
|
61
|
+
|
|
62
|
+
for (const candidate of systemFolders()) {
|
|
63
|
+
if (here === tidy(candidate)) return true;
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Every folder that belongs to the machine or to an account rather than to a
|
|
70
|
+
* project, resolved.
|
|
71
|
+
*
|
|
72
|
+
* The first two entries are derived from the running system; the literals after
|
|
73
|
+
* them are fallbacks for the usual layouts. They overlap - on Windows
|
|
74
|
+
* USERPROFILE is the home folder and "/Users" resolves to C:\Users - and the
|
|
75
|
+
* overlap is deliberate: a machine whose home is somewhere unusual is covered
|
|
76
|
+
* by the derived pair, and a machine whose environment is missing entries is
|
|
77
|
+
* covered by the literals.
|
|
78
|
+
*/
|
|
79
|
+
function systemFolders() {
|
|
80
|
+
return [
|
|
81
|
+
// The folder every account lives in - C:\Users, /home, /Users. On its own
|
|
82
|
+
// it has no package.json so it would be refused anyway, but one stray npm
|
|
83
|
+
// install there and it would look exactly like a project.
|
|
84
|
+
path.dirname(os.homedir()),
|
|
85
|
+
process.env.SystemRoot,
|
|
86
|
+
process.env.ProgramFiles,
|
|
87
|
+
process.env['ProgramFiles(x86)'],
|
|
88
|
+
process.env.ProgramData,
|
|
89
|
+
process.env.APPDATA,
|
|
90
|
+
process.env.LOCALAPPDATA,
|
|
91
|
+
process.env.USERPROFILE,
|
|
92
|
+
'/root',
|
|
93
|
+
'/home',
|
|
94
|
+
'/Users',
|
|
95
|
+
'/usr',
|
|
96
|
+
'/etc',
|
|
97
|
+
'/bin',
|
|
98
|
+
'/var',
|
|
99
|
+
'/opt',
|
|
100
|
+
].filter(Boolean);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Whether this folder can be checked, and if not, why not.
|
|
105
|
+
*
|
|
106
|
+
* `allowed` is the escape hatch having been set. It is reported rather than
|
|
107
|
+
* hidden, so a run that only worked because of it says so.
|
|
108
|
+
*/
|
|
109
|
+
function inspectProject(dir, env) {
|
|
110
|
+
const environment = env || process.env;
|
|
111
|
+
const allowed = String(environment[ESCAPE_HATCH] || '') !== '';
|
|
112
|
+
const packaged = hasPackageJson(dir);
|
|
113
|
+
const personal = isHomeOrSystem(dir);
|
|
114
|
+
const base = { dir: dir, packaged: packaged, personal: personal, allowed: allowed };
|
|
115
|
+
|
|
116
|
+
// Somebody whose recordings `record` put in a folder of their own, standing
|
|
117
|
+
// in their home folder again because that is where a new terminal opens.
|
|
118
|
+
// Pointed at by name, rather than told to go and find a project they never
|
|
119
|
+
// had.
|
|
120
|
+
const kept = tidy(dir) === tidy(os.homedir()) ? path.join(path.resolve(dir), HOME_RECORDINGS) : null;
|
|
121
|
+
if (kept && hasPackageJson(kept)) base.recordingsAt = kept;
|
|
122
|
+
|
|
123
|
+
if (allowed) return Object.assign({ ok: true, reason: null }, base);
|
|
124
|
+
|
|
125
|
+
// A home folder is refused even when it has a package.json, and this is not
|
|
126
|
+
// belt and braces - it is the case that actually happened. Someone installed
|
|
127
|
+
// the tool into their home folder, so npm left a package.json and a
|
|
128
|
+
// node_modules there, and a tests folder alongside them. Every sign this
|
|
129
|
+
// could look for says "project"; none of them is true. The measured state of
|
|
130
|
+
// one real machine: C:\Users\<name> holding package.json, node_modules and
|
|
131
|
+
// tests, none of which belong to anything.
|
|
132
|
+
if (personal) return Object.assign({ ok: false, reason: 'personal' }, base);
|
|
133
|
+
if (!packaged) return Object.assign({ ok: false, reason: 'no-package' }, base);
|
|
134
|
+
return Object.assign({ ok: true, reason: null }, base);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Whether `kryptheon code` may read this folder.
|
|
139
|
+
*
|
|
140
|
+
* The same as any command, with one opening: a plain site - index.html and
|
|
141
|
+
* a few scripts, no npm anywhere - has no package.json and never will. Found
|
|
142
|
+
* on a blind test: exactly such an app was refused, and nothing was read. The
|
|
143
|
+
* refusal exists so that a check does not run somebody else's recordings;
|
|
144
|
+
* reading the files in front of you cannot do that. A page at the top of the
|
|
145
|
+
* folder, or in one of the folders a site's pages usually live in, is what
|
|
146
|
+
* says it is a site - found on the next blind test, where the pages were in
|
|
147
|
+
* src/ and the read refused at the project's root. A home or system folder
|
|
148
|
+
* stays refused.
|
|
149
|
+
*/
|
|
150
|
+
const PAGE_FOLDERS = ['', 'src', 'public', 'www', 'site', 'static', 'web', 'client', 'frontend'];
|
|
151
|
+
|
|
152
|
+
function inspectCodeFolder(dir, env) {
|
|
153
|
+
const here = inspectProject(dir, env);
|
|
154
|
+
if (here.ok || here.reason !== 'no-package') return here;
|
|
155
|
+
const hasPage = PAGE_FOLDERS.some((sub) => {
|
|
156
|
+
try {
|
|
157
|
+
const at = path.join(dir, sub);
|
|
158
|
+
return fs.readdirSync(at).some((name) => /\.html?$/i.test(name) && fs.statSync(path.join(at, name)).isFile());
|
|
159
|
+
} catch (e) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
return hasPage ? Object.assign({}, here, { ok: true, reason: null, plainSite: true }) : here;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Why this folder is not a project, and what to do instead.
|
|
168
|
+
*
|
|
169
|
+
* The folder is printed back because the commonest version of this is not
|
|
170
|
+
* knowing where you are - a new terminal, a global install, a shell that opened
|
|
171
|
+
* somewhere else.
|
|
172
|
+
*/
|
|
173
|
+
function noProjectLines(inspection) {
|
|
174
|
+
const lines = [''];
|
|
175
|
+
|
|
176
|
+
if (inspection.recordingsAt) {
|
|
177
|
+
lines.push(' This is your home folder, not a project.');
|
|
178
|
+
lines.push('');
|
|
179
|
+
lines.push(' Your recordings are in: ' + inspection.recordingsAt);
|
|
180
|
+
lines.push('');
|
|
181
|
+
lines.push(' To check them:');
|
|
182
|
+
lines.push('');
|
|
183
|
+
lines.push(' cd "' + inspection.recordingsAt + '"');
|
|
184
|
+
lines.push(' npx kryptheon check');
|
|
185
|
+
lines.push('');
|
|
186
|
+
return lines;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (inspection.personal) {
|
|
190
|
+
lines.push(' This is your home or a system folder, not a project.');
|
|
191
|
+
lines.push('');
|
|
192
|
+
lines.push(' You are in: ' + inspection.dir);
|
|
193
|
+
lines.push('');
|
|
194
|
+
// Almost never deliberate, and the one place where running anyway does the
|
|
195
|
+
// most damage. Said explicitly because a home folder that has had anything
|
|
196
|
+
// installed into it looks exactly like a project from the outside.
|
|
197
|
+
lines.push(' Recordings and results are kept beside the app they belong to,');
|
|
198
|
+
lines.push(' so running here would check whichever tests folder happens to be');
|
|
199
|
+
lines.push(' sitting in this one - almost certainly not the one you meant.');
|
|
200
|
+
if (inspection.packaged) {
|
|
201
|
+
lines.push('');
|
|
202
|
+
// Said plainly, because otherwise the package.json sitting right there
|
|
203
|
+
// makes this message look like the bug rather than the diagnosis.
|
|
204
|
+
lines.push(' There is a package.json here, but that is not what makes a project.');
|
|
205
|
+
lines.push(' It looks like npm install was run in this folder by mistake, which');
|
|
206
|
+
lines.push(' leaves a package.json and a node_modules behind.');
|
|
207
|
+
}
|
|
208
|
+
lines.push('');
|
|
209
|
+
} else {
|
|
210
|
+
lines.push(' There is no project here - this folder has no package.json.');
|
|
211
|
+
lines.push('');
|
|
212
|
+
lines.push(' You are in: ' + inspection.dir);
|
|
213
|
+
lines.push('');
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
lines.push(' Change into your project folder and run this again:');
|
|
217
|
+
lines.push('');
|
|
218
|
+
lines.push(' cd path\\to\\your\\project');
|
|
219
|
+
lines.push(' npx kryptheon check');
|
|
220
|
+
lines.push('');
|
|
221
|
+
// The person with a web address and no folder at all - an app built in
|
|
222
|
+
// Lovable - has no project to change into, and would be stuck on the line
|
|
223
|
+
// above. Not offered in a system folder: record refuses to set one up.
|
|
224
|
+
if (!inspection.personal || tidy(inspection.dir) === tidy(os.homedir())) {
|
|
225
|
+
lines.push(' No project folder, only a web address? Record from here and it');
|
|
226
|
+
lines.push(' will be set up for you:');
|
|
227
|
+
lines.push('');
|
|
228
|
+
lines.push(' npx kryptheon record https://your-app.example.com');
|
|
229
|
+
lines.push('');
|
|
230
|
+
}
|
|
231
|
+
lines.push(' If you really do keep recordings outside a package, set');
|
|
232
|
+
lines.push(' ' + ESCAPE_HATCH + '=1 and this will run anyway.');
|
|
233
|
+
lines.push('');
|
|
234
|
+
return lines;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** A real project, with nothing recorded in it yet. */
|
|
238
|
+
function noRecordingsLines() {
|
|
239
|
+
return [
|
|
240
|
+
'',
|
|
241
|
+
' No recordings were found in this project.',
|
|
242
|
+
'',
|
|
243
|
+
' This is not a failure, and there is nothing in the code to fix.',
|
|
244
|
+
'',
|
|
245
|
+
' Record a flow through your app first:',
|
|
246
|
+
'',
|
|
247
|
+
' npx kryptheon record http://localhost:3000',
|
|
248
|
+
'',
|
|
249
|
+
' That opens a browser window, so it has to be run by a person in a',
|
|
250
|
+
' normal terminal - it cannot be done from an AI assistant.',
|
|
251
|
+
'',
|
|
252
|
+
];
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// --- Setting a folder up, before recording into it ---------------------------
|
|
256
|
+
//
|
|
257
|
+
// Somebody who built their app in Lovable has an address and nothing else: no
|
|
258
|
+
// source folder, no package.json, nothing to change into. Their recordings
|
|
259
|
+
// still need somewhere to live, `check` refuses a folder with no package.json,
|
|
260
|
+
// and a recording cannot load the fixture unless kryptheon is installed where
|
|
261
|
+
// the recording is.
|
|
262
|
+
//
|
|
263
|
+
// `record` used to find all of that out afterwards. Measured in an empty
|
|
264
|
+
// folder: it started the browser, would have let the person spend five minutes
|
|
265
|
+
// using their app and saved the recording - and then `check`, in that same
|
|
266
|
+
// folder, said "There is no project here". So record now works out what is
|
|
267
|
+
// missing before the browser, which is a 200MB download on a new machine, and
|
|
268
|
+
// offers to add it.
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* The installed kryptheon a recording saved in this folder will load - the
|
|
272
|
+
* nearest node_modules/kryptheon going up - or null if there is none.
|
|
273
|
+
*
|
|
274
|
+
* `check` must be run by this same copy. Found through the MCP server, then
|
|
275
|
+
* measured by hand in a folder `record` had set up: `npx kryptheon@latest
|
|
276
|
+
* check` runs npx's own copy, whose Playwright loads the config, while the
|
|
277
|
+
* recordings load this one's - and Playwright stops with "Requiring
|
|
278
|
+
* @playwright/test second time" before a single recording runs.
|
|
279
|
+
*/
|
|
280
|
+
function kryptheonCopyFor(dir) {
|
|
281
|
+
let current = path.resolve(String(dir || ''));
|
|
282
|
+
for (;;) {
|
|
283
|
+
const candidate = path.join(current, 'node_modules', 'kryptheon');
|
|
284
|
+
// Whole copies only. A half-finished install - the fixture there, the rest
|
|
285
|
+
// not - was handed the run and answered with Node's own stack trace.
|
|
286
|
+
const whole = ['kryptheon-fixture.js', 'package.json', path.join('bin', 'kryptheon.js')]
|
|
287
|
+
.every((f) => fs.existsSync(path.join(candidate, f)));
|
|
288
|
+
if (whole) return candidate;
|
|
289
|
+
const parent = path.dirname(current);
|
|
290
|
+
if (parent === current) return null;
|
|
291
|
+
current = parent;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Whether kryptheon can be loaded by a recording saved in this folder.
|
|
297
|
+
*
|
|
298
|
+
* A filesystem walk, not require.resolve: this file lives inside the kryptheon
|
|
299
|
+
* package, and a package with a name and an "exports" map can always resolve
|
|
300
|
+
* itself by name, so require.resolve would answer "yes" even when the user's
|
|
301
|
+
* recordings have no way to find it.
|
|
302
|
+
*/
|
|
303
|
+
function kryptheonReachableFrom(dir) {
|
|
304
|
+
let current = path.resolve(String(dir || ''));
|
|
305
|
+
if (insideKryptheonItself(current)) return true;
|
|
306
|
+
for (;;) {
|
|
307
|
+
if (fs.existsSync(path.join(current, 'node_modules', 'kryptheon', 'kryptheon-fixture.js'))) {
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
const parent = path.dirname(current);
|
|
311
|
+
if (parent === current) return false;
|
|
312
|
+
current = parent;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Inside the kryptheon package's own folder - this repository.
|
|
318
|
+
*
|
|
319
|
+
* Node lets a package load itself by its own name, so a recording made here
|
|
320
|
+
* finds the fixture with no node_modules/kryptheon anywhere. Without this,
|
|
321
|
+
* record in this repository offered to npm install kryptheon into kryptheon.
|
|
322
|
+
* Only the nearest package.json counts: that is the package a file belongs to.
|
|
323
|
+
*/
|
|
324
|
+
function insideKryptheonItself(dir) {
|
|
325
|
+
let current = dir;
|
|
326
|
+
for (;;) {
|
|
327
|
+
if (hasPackageJson(current)) {
|
|
328
|
+
// The name alone is not enough: somebody's own app can be called that.
|
|
329
|
+
// The fixture beside it is what makes it this package.
|
|
330
|
+
try {
|
|
331
|
+
return JSON.parse(fs.readFileSync(path.join(current, 'package.json'), 'utf8')).name === 'kryptheon' &&
|
|
332
|
+
fs.existsSync(path.join(current, 'kryptheon-fixture.js'));
|
|
333
|
+
} catch (err) {
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const parent = path.dirname(current);
|
|
338
|
+
if (parent === current) return false;
|
|
339
|
+
current = parent;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Installed in this folder itself, not borrowed from one above it.
|
|
345
|
+
*
|
|
346
|
+
* A folder record sets up gets its own copy even when one is reachable from
|
|
347
|
+
* further up. The copy further up is, on the one machine where this was looked
|
|
348
|
+
* at, a stray install in a home folder - and recordings that lean on that stop
|
|
349
|
+
* working the day somebody tidies it away.
|
|
350
|
+
*/
|
|
351
|
+
function kryptheonInstalledIn(dir) {
|
|
352
|
+
return fs.existsSync(path.join(path.resolve(String(dir || '')), 'node_modules', 'kryptheon', 'kryptheon-fixture.js'));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* The nearest folder above this one that is a project.
|
|
357
|
+
*
|
|
358
|
+
* Stops at a home or system folder rather than walking through it: the one
|
|
359
|
+
* package.json most likely to be found up there is the stray one a mistaken
|
|
360
|
+
* install left in a home folder, and that is nobody's project.
|
|
361
|
+
*/
|
|
362
|
+
function projectAbove(dir) {
|
|
363
|
+
let current = path.resolve(String(dir || ''));
|
|
364
|
+
for (;;) {
|
|
365
|
+
const parent = path.dirname(current);
|
|
366
|
+
if (parent === current) return null;
|
|
367
|
+
if (isHomeOrSystem(parent)) return null;
|
|
368
|
+
if (hasPackageJson(parent)) return parent;
|
|
369
|
+
current = parent;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** A folder record set up on an earlier run - its package.json asks for kryptheon. */
|
|
374
|
+
function setUpForKryptheon(dir) {
|
|
375
|
+
try {
|
|
376
|
+
const json = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
|
|
377
|
+
return !!((json.devDependencies && json.devDependencies.kryptheon) ||
|
|
378
|
+
(json.dependencies && json.dependencies.kryptheon));
|
|
379
|
+
} catch (err) {
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function isEmptyFolder(dir) {
|
|
385
|
+
try {
|
|
386
|
+
return fs.statSync(dir).isDirectory() && fs.readdirSync(dir).length === 0;
|
|
387
|
+
} catch (err) {
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* What this folder needs before a recording made in it can be checked. Only
|
|
394
|
+
* looks; never writes.
|
|
395
|
+
*
|
|
396
|
+
* ready nothing
|
|
397
|
+
* create no package.json and no project above: make one, then install
|
|
398
|
+
* install a project without kryptheon: install it
|
|
399
|
+
* above no package.json, but a project sits above this folder
|
|
400
|
+
* home a home folder: record in a kryptheon-tests folder inside it
|
|
401
|
+
* refuse a system folder, or a home whose kryptheon-tests is taken
|
|
402
|
+
*/
|
|
403
|
+
function setupPlan(dir, env) {
|
|
404
|
+
const environment = env || process.env;
|
|
405
|
+
const here = path.resolve(String(dir || ''));
|
|
406
|
+
|
|
407
|
+
// Somebody who has said they keep recordings outside a package is left
|
|
408
|
+
// exactly as they were before any of this existed.
|
|
409
|
+
if (String(environment[ESCAPE_HATCH] || '') !== '') return { action: 'ready', dir: here };
|
|
410
|
+
|
|
411
|
+
if (tidy(here) === tidy(os.homedir())) {
|
|
412
|
+
const into = path.join(here, HOME_RECORDINGS);
|
|
413
|
+
// Used only if it is plainly ours or plainly nothing. A folder of that name
|
|
414
|
+
// holding something else is somebody's, and recording into it would put a
|
|
415
|
+
// package.json and node_modules in the middle of it.
|
|
416
|
+
if (!fs.existsSync(into) || isEmptyFolder(into) || setUpForKryptheon(into)) {
|
|
417
|
+
return { action: 'home', dir: here, into: into };
|
|
418
|
+
}
|
|
419
|
+
return { action: 'refuse', reason: 'taken', dir: here, into: into };
|
|
420
|
+
}
|
|
421
|
+
if (isHomeOrSystem(here)) return { action: 'refuse', reason: 'system', dir: here };
|
|
422
|
+
|
|
423
|
+
if (!hasPackageJson(here)) {
|
|
424
|
+
const above = projectAbove(here);
|
|
425
|
+
if (above) return { action: 'above', dir: here, project: above };
|
|
426
|
+
return { action: 'create', dir: here };
|
|
427
|
+
}
|
|
428
|
+
if (!kryptheonReachableFrom(here)) return { action: 'install', dir: here };
|
|
429
|
+
return { action: 'ready', dir: here };
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** What is about to happen to this folder, said before it happens. */
|
|
433
|
+
function setupLines(plan, url) {
|
|
434
|
+
const address = url || 'https://your-app.example.com';
|
|
435
|
+
const lines = [''];
|
|
436
|
+
|
|
437
|
+
if (plan.action === 'create') {
|
|
438
|
+
lines.push(' This folder is not set up for recordings yet.');
|
|
439
|
+
lines.push('');
|
|
440
|
+
lines.push(' You are in: ' + plan.dir);
|
|
441
|
+
lines.push('');
|
|
442
|
+
lines.push(' Your recordings are saved here, and kryptheon has to be installed');
|
|
443
|
+
lines.push(' beside them or they cannot be checked later. So I will add:');
|
|
444
|
+
lines.push('');
|
|
445
|
+
lines.push(' package.json a small file that marks this folder as a project');
|
|
446
|
+
lines.push(' node_modules kryptheon itself, about 20MB');
|
|
447
|
+
lines.push('');
|
|
448
|
+
lines.push(' Nothing already in this folder is touched.');
|
|
449
|
+
} else if (plan.action === 'install') {
|
|
450
|
+
lines.push(' kryptheon is not installed in this project yet.');
|
|
451
|
+
lines.push('');
|
|
452
|
+
lines.push(' You are in: ' + plan.dir);
|
|
453
|
+
lines.push('');
|
|
454
|
+
lines.push(' Recordings load it by name, so without it here they cannot be');
|
|
455
|
+
lines.push(' checked later. I will run:');
|
|
456
|
+
lines.push('');
|
|
457
|
+
lines.push(' npm install --save-dev kryptheon');
|
|
458
|
+
lines.push('');
|
|
459
|
+
lines.push(' which adds one line to package.json and about 20MB to node_modules.');
|
|
460
|
+
} else if (plan.action === 'above') {
|
|
461
|
+
lines.push(' This folder is inside a project:');
|
|
462
|
+
lines.push('');
|
|
463
|
+
lines.push(' ' + plan.project);
|
|
464
|
+
lines.push('');
|
|
465
|
+
lines.push(' Recordings belong beside the app they test, so the usual thing is');
|
|
466
|
+
lines.push(' to run this from there:');
|
|
467
|
+
lines.push('');
|
|
468
|
+
lines.push(' cd "' + plan.project + '"');
|
|
469
|
+
lines.push(' npx kryptheon record ' + address);
|
|
470
|
+
lines.push('');
|
|
471
|
+
lines.push(' If that is not your project - a package.json can be left behind by');
|
|
472
|
+
lines.push(' an install run in the wrong place - I can set this folder up on its');
|
|
473
|
+
lines.push(' own instead.');
|
|
474
|
+
} else if (plan.action === 'home') {
|
|
475
|
+
lines.push(' You are in your home folder.');
|
|
476
|
+
lines.push('');
|
|
477
|
+
lines.push(' Recordings are kept in a folder of their own rather than loose in');
|
|
478
|
+
lines.push(' here, so I will ' + (fs.existsSync(plan.into) ? 'use this one' : 'make one') + ' and work in that:');
|
|
479
|
+
lines.push('');
|
|
480
|
+
lines.push(' ' + plan.into);
|
|
481
|
+
} else if (plan.action === 'refuse' && plan.reason === 'taken') {
|
|
482
|
+
lines.push(' You are in your home folder. Recordings are kept in a folder of');
|
|
483
|
+
lines.push(' their own, and I would normally use:');
|
|
484
|
+
lines.push('');
|
|
485
|
+
lines.push(' ' + plan.into);
|
|
486
|
+
lines.push('');
|
|
487
|
+
lines.push(' but something else is already there, and it is not one I made.');
|
|
488
|
+
lines.push(' Nothing was changed.');
|
|
489
|
+
lines.push('');
|
|
490
|
+
lines.push(' Make a new folder of your own, open a terminal in it, and run the');
|
|
491
|
+
lines.push(' same command again.');
|
|
492
|
+
} else if (plan.action === 'refuse') {
|
|
493
|
+
lines.push(' This is a system folder, not somewhere to keep recordings.');
|
|
494
|
+
lines.push('');
|
|
495
|
+
lines.push(' You are in: ' + plan.dir);
|
|
496
|
+
lines.push('');
|
|
497
|
+
lines.push(' Nothing was changed. Make a new folder of your own - on the Desktop');
|
|
498
|
+
lines.push(' is fine - open a terminal in it, and run the same command again.');
|
|
499
|
+
if (process.platform === 'win32') {
|
|
500
|
+
lines.push(' (In File Explorer: right-click inside the folder, Open in Terminal.)');
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
lines.push('');
|
|
504
|
+
return lines;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* The question, and which way Enter answers it.
|
|
509
|
+
*
|
|
510
|
+
* Yes by default where saying yes is what the person came for. No by default
|
|
511
|
+
* for a folder inside another project, where yes puts a second project inside
|
|
512
|
+
* somebody's first.
|
|
513
|
+
*/
|
|
514
|
+
function setupQuestion(plan) {
|
|
515
|
+
if (plan.action === 'above') return { text: ' Set this folder up on its own instead? [y/N] ', yesByDefault: false };
|
|
516
|
+
if (plan.action === 'install') return { text: ' Install it? [Y/n] ', yesByDefault: true };
|
|
517
|
+
if (plan.action === 'home') return { text: ' Go ahead? [Y/n] ', yesByDefault: true };
|
|
518
|
+
return { text: ' Set this folder up? [Y/n] ', yesByDefault: true };
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/** An answer typed at the question, read the way the question offered it. */
|
|
522
|
+
function answeredYes(answer, yesByDefault) {
|
|
523
|
+
const typed = String(answer || '').trim();
|
|
524
|
+
if (!typed) return !!yesByDefault;
|
|
525
|
+
return /^y/i.test(typed);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/** After recording from a home folder: where the recordings went, and how to get back to them. */
|
|
529
|
+
function homeDoneLines(into) {
|
|
530
|
+
return [
|
|
531
|
+
'',
|
|
532
|
+
' Your recordings are in: ' + into,
|
|
533
|
+
'',
|
|
534
|
+
' To check them after a change, open a terminal and run:',
|
|
535
|
+
'',
|
|
536
|
+
' cd "' + into + '"',
|
|
537
|
+
' npx kryptheon check',
|
|
538
|
+
'',
|
|
539
|
+
];
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/** The install did not finish. Nothing needs undoing: running again picks up where this stopped. */
|
|
543
|
+
function installFailedLines() {
|
|
544
|
+
return [
|
|
545
|
+
'',
|
|
546
|
+
' kryptheon could not be installed in this folder.',
|
|
547
|
+
'',
|
|
548
|
+
' Installing needs an internet connection. Check it, then run the same',
|
|
549
|
+
' command again - nothing needs undoing first.',
|
|
550
|
+
'',
|
|
551
|
+
' Or install it yourself, then record:',
|
|
552
|
+
'',
|
|
553
|
+
' npm install --save-dev kryptheon',
|
|
554
|
+
'',
|
|
555
|
+
];
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* Whether version `a` is older than `b`, by their numbers. Anything that is
|
|
560
|
+
* not plain x.y.z is never called older: updating on a guess would change
|
|
561
|
+
* somebody's package.json for nothing, and only digits and dots ever reach
|
|
562
|
+
* the npm command line built from the answer.
|
|
563
|
+
*/
|
|
564
|
+
function olderVersion(a, b) {
|
|
565
|
+
const parse = (v) => {
|
|
566
|
+
const m = String(v == null ? '' : v).match(/^(\d+)\.(\d+)\.(\d+)$/);
|
|
567
|
+
return m ? m.slice(1).map(Number) : null;
|
|
568
|
+
};
|
|
569
|
+
const x = parse(a);
|
|
570
|
+
const y = parse(b);
|
|
571
|
+
if (!x || !y) return false;
|
|
572
|
+
for (let i = 0; i < 3; i++) {
|
|
573
|
+
if (x[i] !== y[i]) return x[i] < y[i];
|
|
574
|
+
}
|
|
575
|
+
return false;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
module.exports = {
|
|
579
|
+
ESCAPE_HATCH: ESCAPE_HATCH,
|
|
580
|
+
olderVersion: olderVersion,
|
|
581
|
+
HOME_RECORDINGS: HOME_RECORDINGS,
|
|
582
|
+
kryptheonReachableFrom: kryptheonReachableFrom,
|
|
583
|
+
kryptheonCopyFor: kryptheonCopyFor,
|
|
584
|
+
kryptheonInstalledIn: kryptheonInstalledIn,
|
|
585
|
+
projectAbove: projectAbove,
|
|
586
|
+
setupPlan: setupPlan,
|
|
587
|
+
setupLines: setupLines,
|
|
588
|
+
setupQuestion: setupQuestion,
|
|
589
|
+
answeredYes: answeredYes,
|
|
590
|
+
homeDoneLines: homeDoneLines,
|
|
591
|
+
installFailedLines: installFailedLines,
|
|
592
|
+
hasPackageJson: hasPackageJson,
|
|
593
|
+
systemFolders: systemFolders,
|
|
594
|
+
tidy: tidy,
|
|
595
|
+
isHomeOrSystem: isHomeOrSystem,
|
|
596
|
+
inspectProject: inspectProject,
|
|
597
|
+
inspectCodeFolder: inspectCodeFolder,
|
|
598
|
+
noProjectLines: noProjectLines,
|
|
599
|
+
noRecordingsLines: noRecordingsLines,
|
|
600
|
+
};
|