@stagepass/cli 2.1.0 → 2.2.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 +40 -7
- package/package.json +1 -1
- package/src/cli.js +11 -4
- package/src/commands/doctor.js +1 -0
- package/src/commands/new.js +96 -9
- package/src/commands/upgrade.js +96 -18
- package/src/lib/ai-guide.js +161 -0
- package/src/lib/doctor-site.js +89 -0
- package/src/lib/doctor.js +18 -0
- package/src/lib/project-modules.js +98 -0
- package/src/lib/project-sync.js +394 -0
- package/src/lib/scaffold.js +60 -34
- package/src/lib/site-detect.js +242 -0
- package/src/lib/snippets.js +18 -7
- package/src/lib/versions.js +27 -0
- package/templates/project/AGENTS.md.tmpl +6 -4
- package/templates/project/gitignore +7 -0
- package/templates/project/jsconfig.json.tmpl +1 -1
- package/templates/project/stagepass.json.tmpl +1 -1
- package/templates/project/tsconfig.json.tmpl +1 -1
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Doctor checks for the current project folder: declared modules in
|
|
3
|
+
* stagepass.json vs. the Stagepass scripts the live Webflow site loads.
|
|
4
|
+
* Read-only: one public GET of the live page, parsed and never executed.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import fs from 'fs-extra';
|
|
9
|
+
import { detectSite, describeDetection } from './site-detect.js';
|
|
10
|
+
import { formatModules, sameModules } from './project-modules.js';
|
|
11
|
+
import { declaredModules, readStagepassJson, resolveProjectOrigin } from './project-sync.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {object} [opts]
|
|
15
|
+
* @param {string} [opts.cwd]
|
|
16
|
+
* @param {boolean} [opts.skipSite]
|
|
17
|
+
* @param {(site: string) => Promise<any>} [opts.detectSite]
|
|
18
|
+
* @param {Function} [opts.loadProjects]
|
|
19
|
+
* @returns {Promise<{ id: string, status: 'ok'|'warn'|'fail'|'info', message: string, detail?: string }[]>}
|
|
20
|
+
*/
|
|
21
|
+
export async function runProjectSiteChecks(opts = {}) {
|
|
22
|
+
const cwd = opts.cwd || process.cwd();
|
|
23
|
+
if (!(await fs.pathExists(path.join(cwd, 'stagepass.json')))) return [];
|
|
24
|
+
|
|
25
|
+
const checks = [];
|
|
26
|
+
const sp = await readStagepassJson(cwd);
|
|
27
|
+
if (!sp.data) {
|
|
28
|
+
checks.push({ id: 'project.config', status: 'warn', message: sp.error || 'stagepass.json unreadable' });
|
|
29
|
+
return checks;
|
|
30
|
+
}
|
|
31
|
+
const declared = declaredModules(sp.data);
|
|
32
|
+
if (!declared) {
|
|
33
|
+
checks.push({
|
|
34
|
+
id: 'project.modules',
|
|
35
|
+
status: 'warn',
|
|
36
|
+
message: 'stagepass.json has no modules list (["cookies"], ["all"] or [])',
|
|
37
|
+
detail: 'Run: stagepass upgrade',
|
|
38
|
+
});
|
|
39
|
+
} else {
|
|
40
|
+
checks.push({ id: 'project.modules', status: 'ok', message: `Declared modules: ${formatModules(declared)}` });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (opts.skipSite) return checks;
|
|
44
|
+
const found = await resolveProjectOrigin(cwd, sp.data, { loadProjects: opts.loadProjects });
|
|
45
|
+
if (!found) {
|
|
46
|
+
checks.push({
|
|
47
|
+
id: 'project.site',
|
|
48
|
+
status: 'info',
|
|
49
|
+
message: 'No live site known for this project',
|
|
50
|
+
detail: 'Add "origin" to stagepass.json or run stagepass link <name> --origin <host>',
|
|
51
|
+
});
|
|
52
|
+
return checks;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const det = await (opts.detectSite || detectSite)(found.origin);
|
|
56
|
+
if (!det.ok) {
|
|
57
|
+
checks.push({
|
|
58
|
+
id: 'project.site',
|
|
59
|
+
status: 'warn',
|
|
60
|
+
message: `Live site ${found.origin} could not be checked`,
|
|
61
|
+
detail: det.error,
|
|
62
|
+
});
|
|
63
|
+
return checks;
|
|
64
|
+
}
|
|
65
|
+
if (!det.loader) {
|
|
66
|
+
checks.push({
|
|
67
|
+
id: 'project.site',
|
|
68
|
+
status: 'warn',
|
|
69
|
+
message: `Live site ${found.origin} does not load @stagepass/loader`,
|
|
70
|
+
detail: describeDetection(det),
|
|
71
|
+
});
|
|
72
|
+
return checks;
|
|
73
|
+
}
|
|
74
|
+
checks.push({
|
|
75
|
+
id: 'project.site',
|
|
76
|
+
status: 'ok',
|
|
77
|
+
message: `Live site ${found.origin} loads @stagepass/loader@${det.loader.version}`,
|
|
78
|
+
detail: describeDetection(det),
|
|
79
|
+
});
|
|
80
|
+
if (declared && !sameModules(declared, det.list)) {
|
|
81
|
+
checks.push({
|
|
82
|
+
id: 'project.site.modules',
|
|
83
|
+
status: 'warn',
|
|
84
|
+
message: `Modules differ: stagepass.json ${formatModules(declared)}, live site ${formatModules(det.list)}`,
|
|
85
|
+
detail: 'Run stagepass upgrade to sync, or change the Webflow script tags.',
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return checks;
|
|
89
|
+
}
|
package/src/lib/doctor.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from 'fs-extra';
|
|
2
|
+
import { runProjectSiteChecks } from './doctor-site.js';
|
|
2
3
|
import path from 'node:path';
|
|
3
4
|
import { defaultExec, resolveCommand } from './exec.js';
|
|
4
5
|
import {
|
|
@@ -484,6 +485,23 @@ export async function runDoctor(opts = {}) {
|
|
|
484
485
|
|
|
485
486
|
hints.push(CHROME_LNA_HINT);
|
|
486
487
|
|
|
488
|
+
// Current project folder: declared modules vs. live site (read-only GET)
|
|
489
|
+
if (opts.projectChecks !== false) {
|
|
490
|
+
try {
|
|
491
|
+
const projectChecks = await runProjectSiteChecks({
|
|
492
|
+
cwd: opts.cwd,
|
|
493
|
+
skipSite: opts.skipSite === true,
|
|
494
|
+
detectSite: opts.detectSite,
|
|
495
|
+
loadProjects: opts.loadProjects,
|
|
496
|
+
});
|
|
497
|
+
for (const c of projectChecks) checks.push(check(c.id, c.status, c.message, c.detail));
|
|
498
|
+
} catch (error) {
|
|
499
|
+
checks.push(
|
|
500
|
+
check('project', 'warn', 'Project check failed', error instanceof Error ? error.message : String(error))
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
487
505
|
const summary = { ok: 0, warn: 0, fail: 0, info: 0 };
|
|
488
506
|
for (const c of checks) {
|
|
489
507
|
if (c.status === 'ok') summary.ok += 1;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declared Stagepass modules for a project (`stagepass.json` → `modules`).
|
|
3
|
+
*
|
|
4
|
+
* Modules are separate script tags in Webflow Custom Code, so the npm
|
|
5
|
+
* dependency alone does not prove which ones run on the site. The declared
|
|
6
|
+
* list is the source of truth for AI guidance and TypeScript types.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Individual module ids (each has `modules/dist/<id>.min.js`). */
|
|
10
|
+
export const MODULE_IDS = Object.freeze(['inject', 'cookies', 'maintenance']);
|
|
11
|
+
|
|
12
|
+
/** Accepted values in the declared list (`all` = every module). */
|
|
13
|
+
export const MODULE_CHOICES = Object.freeze([...MODULE_IDS, 'all']);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Normalize a declared modules value.
|
|
17
|
+
* Accepts an array, a comma/space separated string, `none`, or `all`.
|
|
18
|
+
* Returns a sorted, deduped list; `all` is exclusive (`['all']`).
|
|
19
|
+
*
|
|
20
|
+
* @param {unknown} value
|
|
21
|
+
* @returns {{ ok: true, modules: string[] } | { ok: false, error: string }}
|
|
22
|
+
*/
|
|
23
|
+
export function normalizeModulesList(value) {
|
|
24
|
+
/** @type {unknown[]} */
|
|
25
|
+
let items;
|
|
26
|
+
if (Array.isArray(value)) {
|
|
27
|
+
items = value;
|
|
28
|
+
} else if (typeof value === 'string') {
|
|
29
|
+
const trimmed = value.trim().toLowerCase();
|
|
30
|
+
if (trimmed === '' || trimmed === 'none' || trimmed === '[]') {
|
|
31
|
+
return { ok: true, modules: [] };
|
|
32
|
+
}
|
|
33
|
+
items = trimmed.split(/[\s,]+/).filter(Boolean);
|
|
34
|
+
} else {
|
|
35
|
+
return {
|
|
36
|
+
ok: false,
|
|
37
|
+
error: 'modules must be a list such as ["cookies"], ["all"] or [].',
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const out = new Set();
|
|
42
|
+
for (const raw of items) {
|
|
43
|
+
const id = String(raw).trim().toLowerCase();
|
|
44
|
+
if (!MODULE_CHOICES.includes(id)) {
|
|
45
|
+
return {
|
|
46
|
+
ok: false,
|
|
47
|
+
error: `Unknown module "${String(raw)}". Use ${MODULE_CHOICES.join(', ')} or none.`,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
out.add(id);
|
|
51
|
+
}
|
|
52
|
+
if (out.has('all')) return { ok: true, modules: ['all'] };
|
|
53
|
+
return { ok: true, modules: MODULE_IDS.filter((id) => out.has(id)) };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Expand `all` into the individual module ids.
|
|
58
|
+
* @param {string[]} modules normalized list
|
|
59
|
+
* @returns {string[]}
|
|
60
|
+
*/
|
|
61
|
+
export function expandModules(modules) {
|
|
62
|
+
if (modules.includes('all')) return [...MODULE_IDS];
|
|
63
|
+
return MODULE_IDS.filter((id) => modules.includes(id));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Whether a module type entry belongs to Stagepass modules. */
|
|
67
|
+
export function isStagepassModulesTypeEntry(entry) {
|
|
68
|
+
return entry === '@stagepass/modules' || String(entry).startsWith('@stagepass/modules/');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* `compilerOptions.types` entries for a declared list.
|
|
73
|
+
* @param {string[]} modules normalized list
|
|
74
|
+
* @param {{ modulesDependency?: boolean }} [opts] module types need the npm dep
|
|
75
|
+
* @returns {string[]}
|
|
76
|
+
*/
|
|
77
|
+
export function typesForModules(modules, opts = {}) {
|
|
78
|
+
const types = ['@stagepass/loader'];
|
|
79
|
+
if (opts.modulesDependency === false) return types;
|
|
80
|
+
if (modules.includes('all')) {
|
|
81
|
+
types.push('@stagepass/modules');
|
|
82
|
+
} else {
|
|
83
|
+
for (const id of expandModules(modules)) types.push(`@stagepass/modules/${id}`);
|
|
84
|
+
}
|
|
85
|
+
return types;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Human-readable list (`none` when empty). */
|
|
89
|
+
export function formatModules(modules) {
|
|
90
|
+
return modules.length === 0 ? 'none' : modules.join(', ');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Same list (order-insensitive). */
|
|
94
|
+
export function sameModules(a, b) {
|
|
95
|
+
const x = [...a].sort().join(',');
|
|
96
|
+
const y = [...b].sort().join(',');
|
|
97
|
+
return x === y;
|
|
98
|
+
}
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sync generated project files with the declared Stagepass modules
|
|
3
|
+
* (used by `stagepass upgrade` and `stagepass doctor`).
|
|
4
|
+
*
|
|
5
|
+
* Writes only:
|
|
6
|
+
* - stagepass.json `modules` (and `origin` when passed via --site)
|
|
7
|
+
* - AGENTS.md managed block (created when missing)
|
|
8
|
+
* - .cursor/rules/stagepass.mdc (created when missing, managed block refreshed)
|
|
9
|
+
* - tsconfig.json / jsconfig.json `compilerOptions.types` (plain JSON only)
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import fs from 'fs-extra';
|
|
14
|
+
import {
|
|
15
|
+
BLOCK_BEGIN,
|
|
16
|
+
CURSOR_RULE_PATH,
|
|
17
|
+
buildCursorRule,
|
|
18
|
+
buildManagedBlock,
|
|
19
|
+
upsertManagedBlock,
|
|
20
|
+
} from './ai-guide.js';
|
|
21
|
+
import {
|
|
22
|
+
formatModules,
|
|
23
|
+
isStagepassModulesTypeEntry,
|
|
24
|
+
normalizeModulesList,
|
|
25
|
+
typesForModules,
|
|
26
|
+
} from './project-modules.js';
|
|
27
|
+
import { loadProjects } from './projects.js';
|
|
28
|
+
import { describeDetection, normalizeSiteUrl } from './site-detect.js';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Read and parse stagepass.json (null when missing or invalid).
|
|
32
|
+
* @param {string} cwd
|
|
33
|
+
* @param {typeof fs} [fse]
|
|
34
|
+
* @returns {Promise<{ exists: boolean, data: Record<string, unknown> | null, raw: string, error?: string }>}
|
|
35
|
+
*/
|
|
36
|
+
export async function readStagepassJson(cwd, fse = fs) {
|
|
37
|
+
const file = path.join(cwd, 'stagepass.json');
|
|
38
|
+
if (!(await fse.pathExists(file))) return { exists: false, data: null, raw: '' };
|
|
39
|
+
const raw = await fse.readFile(file, 'utf8');
|
|
40
|
+
try {
|
|
41
|
+
const data = JSON.parse(raw);
|
|
42
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
|
43
|
+
return { exists: true, data: null, raw, error: 'stagepass.json is not a JSON object.' };
|
|
44
|
+
}
|
|
45
|
+
return { exists: true, data, raw };
|
|
46
|
+
} catch {
|
|
47
|
+
return { exists: true, data: null, raw, error: 'stagepass.json is not valid JSON.' };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Declared modules from stagepass.json (`null` when missing or legacy boolean).
|
|
53
|
+
* @param {Record<string, unknown> | null} data
|
|
54
|
+
* @returns {string[] | null}
|
|
55
|
+
*/
|
|
56
|
+
export function declaredModules(data) {
|
|
57
|
+
if (!data || !Array.isArray(data.modules)) return null;
|
|
58
|
+
const result = normalizeModulesList(data.modules);
|
|
59
|
+
return result.ok ? result.modules : null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Live origin for a project: stagepass.json `origin`, else the first concrete
|
|
64
|
+
* (non-wildcard) origin stored by `stagepass link` for this project folder.
|
|
65
|
+
*
|
|
66
|
+
* @param {string} cwd
|
|
67
|
+
* @param {Record<string, unknown> | null} data
|
|
68
|
+
* @param {{ loadProjects?: typeof loadProjects, realpath?: (p: string) => Promise<string> }} [deps]
|
|
69
|
+
* @returns {Promise<{ origin: string, source: 'stagepass.json' | 'stagepass link' } | null>}
|
|
70
|
+
*/
|
|
71
|
+
export async function resolveProjectOrigin(cwd, data, deps = {}) {
|
|
72
|
+
if (data && typeof data.origin === 'string' && data.origin.trim() !== '') {
|
|
73
|
+
const check = normalizeSiteUrl(data.origin);
|
|
74
|
+
if (check.ok) return { origin: check.host, source: 'stagepass.json' };
|
|
75
|
+
}
|
|
76
|
+
const load = deps.loadProjects || loadProjects;
|
|
77
|
+
const realpath = deps.realpath || ((p) => fs.realpath(p));
|
|
78
|
+
let doc;
|
|
79
|
+
try {
|
|
80
|
+
doc = await load();
|
|
81
|
+
} catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
let here = path.resolve(cwd);
|
|
85
|
+
try {
|
|
86
|
+
here = await realpath(here);
|
|
87
|
+
} catch {
|
|
88
|
+
// keep resolved path
|
|
89
|
+
}
|
|
90
|
+
for (const entry of Object.values(doc.projects || {})) {
|
|
91
|
+
if (!entry || typeof entry !== 'object' || typeof entry.root !== 'string') continue;
|
|
92
|
+
let root = path.resolve(entry.root);
|
|
93
|
+
try {
|
|
94
|
+
root = await realpath(root);
|
|
95
|
+
} catch {
|
|
96
|
+
// keep
|
|
97
|
+
}
|
|
98
|
+
if (root !== here) continue;
|
|
99
|
+
for (const origin of Array.isArray(entry.origins) ? entry.origins : []) {
|
|
100
|
+
const check = normalizeSiteUrl(String(origin));
|
|
101
|
+
if (check.ok) return { origin: check.host, source: 'stagepass link' };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Decide the declared modules list for `upgrade`.
|
|
109
|
+
* Order: --site-modules flag, live site detection, existing stagepass.json,
|
|
110
|
+
* interactive prompt. The npm dependency is only mentioned as a hint.
|
|
111
|
+
*
|
|
112
|
+
* @param {object} input
|
|
113
|
+
* @param {string} input.cwd
|
|
114
|
+
* @param {Record<string, unknown> | null} input.data stagepass.json
|
|
115
|
+
* @param {object} input.pkg package.json
|
|
116
|
+
* @param {object} input.options { siteModules?, site?, skipSite?, yes? }
|
|
117
|
+
* @param {object} deps
|
|
118
|
+
* @param {(site: string) => Promise<any>} deps.detectSite
|
|
119
|
+
* @param {(question: string, def: string) => Promise<string>} [deps.ask]
|
|
120
|
+
* @param {(question: string, defYes: boolean) => Promise<boolean>} [deps.confirm]
|
|
121
|
+
* @param {boolean} deps.interactive
|
|
122
|
+
* @param {(msg: string) => void} deps.log
|
|
123
|
+
* @param {typeof loadProjects} [deps.loadProjects]
|
|
124
|
+
* @returns {Promise<{ ok: true, modules: string[], source: string, origin: string | null, detection: any } | { ok: false, error: string }>}
|
|
125
|
+
*/
|
|
126
|
+
export async function resolveUpgradeModules(input, deps) {
|
|
127
|
+
const { cwd, data, pkg, options } = input;
|
|
128
|
+
const log = deps.log;
|
|
129
|
+
const existing = declaredModules(data);
|
|
130
|
+
|
|
131
|
+
if (options.siteModules != null) {
|
|
132
|
+
const parsed = normalizeModulesList(options.siteModules);
|
|
133
|
+
if (!parsed.ok) return { ok: false, error: parsed.error };
|
|
134
|
+
let flagOrigin = null;
|
|
135
|
+
if (options.site) {
|
|
136
|
+
const check = normalizeSiteUrl(options.site);
|
|
137
|
+
if (!check.ok) return { ok: false, error: check.error };
|
|
138
|
+
flagOrigin = check.host;
|
|
139
|
+
}
|
|
140
|
+
return { ok: true, modules: parsed.modules, source: '--site-modules', origin: flagOrigin, detection: null };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** @type {string | null} */
|
|
144
|
+
let origin = null;
|
|
145
|
+
if (options.site) {
|
|
146
|
+
const check = normalizeSiteUrl(options.site);
|
|
147
|
+
if (!check.ok) return { ok: false, error: check.error };
|
|
148
|
+
origin = check.host;
|
|
149
|
+
} else {
|
|
150
|
+
const found = await resolveProjectOrigin(cwd, data, { loadProjects: deps.loadProjects });
|
|
151
|
+
origin = found ? found.origin : null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
let detection = null;
|
|
155
|
+
/** @type {string[] | null} */
|
|
156
|
+
let candidate = null;
|
|
157
|
+
let source = '';
|
|
158
|
+
|
|
159
|
+
if (origin && options.skipSite !== true) {
|
|
160
|
+
log(`Checking live site https://${origin}/ for Stagepass script tags...`);
|
|
161
|
+
detection = await deps.detectSite(origin);
|
|
162
|
+
log(` ${describeDetection(detection)}`);
|
|
163
|
+
if (detection.ok && detection.loader) {
|
|
164
|
+
candidate = detection.list;
|
|
165
|
+
source = `live site ${origin}`;
|
|
166
|
+
} else if (detection.ok) {
|
|
167
|
+
log(' The page does not load @stagepass/loader, so modules cannot be detected.');
|
|
168
|
+
}
|
|
169
|
+
} else if (!origin) {
|
|
170
|
+
log('No live site known (add "origin" to stagepass.json, pass --site, or link with --origin).');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const hasModulesDep = Boolean(
|
|
174
|
+
pkg?.dependencies?.['@stagepass/modules'] || pkg?.devDependencies?.['@stagepass/modules']
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
if (!candidate && existing) {
|
|
178
|
+
candidate = existing;
|
|
179
|
+
source = 'stagepass.json';
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (!deps.interactive) {
|
|
183
|
+
if (candidate) {
|
|
184
|
+
log(`Modules: ${formatModules(candidate)} (inferred from ${source}). Check it, or pass --site-modules.`);
|
|
185
|
+
return { ok: true, modules: candidate, source, origin, detection };
|
|
186
|
+
}
|
|
187
|
+
if (hasModulesDep) {
|
|
188
|
+
log('Hint: package.json has @stagepass/modules, but that does not show which module tags run on the site.');
|
|
189
|
+
}
|
|
190
|
+
log('Modules: none (no live site result and no list in stagepass.json). Pass --site-modules or run upgrade in a terminal.');
|
|
191
|
+
return { ok: true, modules: [], source: 'default', origin, detection };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (candidate && source.startsWith('live site')) {
|
|
195
|
+
const yes = await deps.confirm(
|
|
196
|
+
`Use modules detected on the live site: ${formatModules(candidate)}?`,
|
|
197
|
+
true
|
|
198
|
+
);
|
|
199
|
+
if (yes) return { ok: true, modules: candidate, source, origin, detection };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (hasModulesDep && !candidate) {
|
|
203
|
+
log('Hint: package.json has @stagepass/modules; enter the modules whose script tags are on the site.');
|
|
204
|
+
}
|
|
205
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
206
|
+
const answer = await deps.ask(
|
|
207
|
+
'Modules your Webflow site loads (none | all | inject,cookies,maintenance)',
|
|
208
|
+
candidate ? formatModules(candidate) : 'none'
|
|
209
|
+
);
|
|
210
|
+
const parsed = normalizeModulesList(answer);
|
|
211
|
+
if (parsed.ok) {
|
|
212
|
+
return { ok: true, modules: parsed.modules, source: 'confirmed', origin, detection };
|
|
213
|
+
}
|
|
214
|
+
log(parsed.error);
|
|
215
|
+
}
|
|
216
|
+
return { ok: false, error: 'No valid modules list entered.' };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Update `compilerOptions.types` in a tsconfig/jsconfig source.
|
|
221
|
+
*
|
|
222
|
+
* @param {string} raw file contents
|
|
223
|
+
* @param {string[]} modules
|
|
224
|
+
* @param {{ modulesDependency: boolean }} opts
|
|
225
|
+
* @returns {{ status: 'updated' | 'unchanged' | 'skipped', content?: string, reason?: string, types?: string[] }}
|
|
226
|
+
*/
|
|
227
|
+
export function updateConfigTypes(raw, modules, opts) {
|
|
228
|
+
let json;
|
|
229
|
+
try {
|
|
230
|
+
json = JSON.parse(raw);
|
|
231
|
+
} catch {
|
|
232
|
+
const hasComments = /(^|[^:"'])\/\/|\/\*/.test(raw);
|
|
233
|
+
return {
|
|
234
|
+
status: 'skipped',
|
|
235
|
+
reason: hasComments
|
|
236
|
+
? 'contains comments (JSONC); edit compilerOptions.types by hand'
|
|
237
|
+
: 'is not valid JSON; edit compilerOptions.types by hand',
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
if (!json || typeof json !== 'object' || Array.isArray(json)) {
|
|
241
|
+
return { status: 'skipped', reason: 'is not a JSON object' };
|
|
242
|
+
}
|
|
243
|
+
const co = json.compilerOptions;
|
|
244
|
+
if (!co || typeof co !== 'object' || !Array.isArray(co.types)) {
|
|
245
|
+
return {
|
|
246
|
+
status: 'skipped',
|
|
247
|
+
reason:
|
|
248
|
+
'has no compilerOptions.types array (adding one would hide other @types); add the entries by hand',
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
const wanted = typesForModules(modules, opts);
|
|
252
|
+
const kept = co.types.filter(
|
|
253
|
+
(t) => typeof t !== 'string' || (t !== '@stagepass/loader' && !isStagepassModulesTypeEntry(t))
|
|
254
|
+
);
|
|
255
|
+
const nextTypes = [...wanted, ...kept];
|
|
256
|
+
const same =
|
|
257
|
+
nextTypes.length === co.types.length && nextTypes.every((t, i) => t === co.types[i]);
|
|
258
|
+
if (same) return { status: 'unchanged', types: nextTypes };
|
|
259
|
+
json.compilerOptions = { ...co, types: nextTypes };
|
|
260
|
+
const trailing = raw.endsWith('\n') ? '\n' : '';
|
|
261
|
+
return { status: 'updated', content: `${JSON.stringify(json, null, 2)}${trailing}`, types: nextTypes };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Plan file changes for the declared modules (pure, no writes).
|
|
266
|
+
*
|
|
267
|
+
* @param {object} input
|
|
268
|
+
* @param {string} input.cwd
|
|
269
|
+
* @param {string[]} input.modules
|
|
270
|
+
* @param {string | null} [input.origin] stored in stagepass.json when set
|
|
271
|
+
* @param {object} input.pkg package.json
|
|
272
|
+
* @param {typeof fs} [fse]
|
|
273
|
+
* @returns {Promise<{ writes: { file: string, content: string, action: string }[], notes: string[] }>}
|
|
274
|
+
*/
|
|
275
|
+
export async function planProjectSync(input, fse = fs) {
|
|
276
|
+
const { cwd, modules, pkg } = input;
|
|
277
|
+
/** @type {{ file: string, content: string, action: string }[]} */
|
|
278
|
+
const writes = [];
|
|
279
|
+
/** @type {string[]} */
|
|
280
|
+
const notes = [];
|
|
281
|
+
const modulesDependency = Boolean(
|
|
282
|
+
pkg?.dependencies?.['@stagepass/modules'] || pkg?.devDependencies?.['@stagepass/modules']
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
// stagepass.json
|
|
286
|
+
const sp = await readStagepassJson(cwd, fse);
|
|
287
|
+
if (sp.exists && !sp.data) {
|
|
288
|
+
notes.push(`stagepass.json skipped: ${sp.error}`);
|
|
289
|
+
} else {
|
|
290
|
+
/** @type {Record<string, unknown>} */
|
|
291
|
+
let data;
|
|
292
|
+
if (sp.data) {
|
|
293
|
+
data = { ...sp.data, modules };
|
|
294
|
+
} else {
|
|
295
|
+
data = typeof pkg?.name === 'string' ? { name: pkg.name, modules } : { modules };
|
|
296
|
+
}
|
|
297
|
+
const before = JSON.stringify(sp.data || {});
|
|
298
|
+
if (input.origin) data.origin = input.origin;
|
|
299
|
+
if (!sp.exists) data.version = 1;
|
|
300
|
+
if (JSON.stringify(data) !== before || !sp.exists) {
|
|
301
|
+
const content = `${JSON.stringify(data, null, 2)}\n`;
|
|
302
|
+
writes.push({
|
|
303
|
+
file: 'stagepass.json',
|
|
304
|
+
content,
|
|
305
|
+
action: sp.exists ? `modules: ${formatModules(modules)}` : 'created',
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// AGENTS.md
|
|
311
|
+
const block = buildManagedBlock(modules);
|
|
312
|
+
const agentsPath = path.join(cwd, 'AGENTS.md');
|
|
313
|
+
if (await fse.pathExists(agentsPath)) {
|
|
314
|
+
const raw = await fse.readFile(agentsPath, 'utf8');
|
|
315
|
+
const result = upsertManagedBlock(raw, block);
|
|
316
|
+
if (result.action === 'invalid') {
|
|
317
|
+
notes.push('AGENTS.md skipped: stagepass markers are incomplete; fix or remove them and rerun.');
|
|
318
|
+
} else if (result.action !== 'unchanged') {
|
|
319
|
+
writes.push({
|
|
320
|
+
file: 'AGENTS.md',
|
|
321
|
+
content: result.content,
|
|
322
|
+
action: result.action === 'inserted' ? 'managed block added' : 'managed block refreshed',
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
} else {
|
|
326
|
+
const title = typeof pkg?.name === 'string' ? pkg.name : path.basename(cwd);
|
|
327
|
+
writes.push({
|
|
328
|
+
file: 'AGENTS.md',
|
|
329
|
+
content: `# ${title} (Stagepass project)\n\n${block}\n`,
|
|
330
|
+
action: 'created',
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Cursor rule
|
|
335
|
+
const rulePath = path.join(cwd, CURSOR_RULE_PATH);
|
|
336
|
+
if (await fse.pathExists(rulePath)) {
|
|
337
|
+
const raw = await fse.readFile(rulePath, 'utf8');
|
|
338
|
+
if (raw.includes(BLOCK_BEGIN)) {
|
|
339
|
+
const result = upsertManagedBlock(raw, block);
|
|
340
|
+
if (result.action === 'invalid') {
|
|
341
|
+
notes.push(`${CURSOR_RULE_PATH} skipped: stagepass markers are incomplete.`);
|
|
342
|
+
} else if (result.action !== 'unchanged') {
|
|
343
|
+
writes.push({ file: CURSOR_RULE_PATH, content: result.content, action: 'managed block refreshed' });
|
|
344
|
+
}
|
|
345
|
+
} else {
|
|
346
|
+
notes.push(`${CURSOR_RULE_PATH} exists without stagepass markers; left unchanged.`);
|
|
347
|
+
}
|
|
348
|
+
} else {
|
|
349
|
+
writes.push({ file: CURSOR_RULE_PATH, content: buildCursorRule(modules), action: 'created' });
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// tsconfig / jsconfig
|
|
353
|
+
let sawConfig = false;
|
|
354
|
+
for (const name of ['tsconfig.json', 'jsconfig.json']) {
|
|
355
|
+
const file = path.join(cwd, name);
|
|
356
|
+
if (!(await fse.pathExists(file))) continue;
|
|
357
|
+
sawConfig = true;
|
|
358
|
+
const raw = await fse.readFile(file, 'utf8');
|
|
359
|
+
const result = updateConfigTypes(raw, modules, { modulesDependency });
|
|
360
|
+
if (result.status === 'skipped') {
|
|
361
|
+
const wanted = JSON.stringify(typesForModules(modules, { modulesDependency }));
|
|
362
|
+
notes.push(`${name} ${result.reason}: "types": ${wanted}`);
|
|
363
|
+
} else if (result.status === 'updated') {
|
|
364
|
+
writes.push({ file: name, content: result.content, action: `types: ${result.types.join(', ')}` });
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (!sawConfig) {
|
|
368
|
+
notes.push('No tsconfig.json or jsconfig.json found; types not configured.');
|
|
369
|
+
}
|
|
370
|
+
if (!modulesDependency && modules.length > 0) {
|
|
371
|
+
notes.push('Module types need @stagepass/modules in devDependencies (npm install -D @stagepass/modules).');
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return { writes, notes };
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Write planned changes (skips when dryRun).
|
|
379
|
+
* @param {string} cwd
|
|
380
|
+
* @param {{ file: string, content: string }[]} writes
|
|
381
|
+
* @param {typeof fs} [fse]
|
|
382
|
+
*/
|
|
383
|
+
export async function applyProjectSync(cwd, writes, fse = fs) {
|
|
384
|
+
const root = path.resolve(cwd);
|
|
385
|
+
for (const w of writes) {
|
|
386
|
+
const abs = path.resolve(root, w.file);
|
|
387
|
+
const rel = path.relative(root, abs);
|
|
388
|
+
if (rel.startsWith('..') || path.isAbsolute(rel)) {
|
|
389
|
+
throw new Error(`Refusing to write outside the project: ${w.file}`);
|
|
390
|
+
}
|
|
391
|
+
await fse.ensureDir(path.dirname(abs));
|
|
392
|
+
await fse.writeFile(abs, w.content, 'utf8');
|
|
393
|
+
}
|
|
394
|
+
}
|