quiesco 0.4.0 → 0.4.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/dist/cli.d.ts +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +2 -0
- package/dist/cli.js.map +1 -1
- package/dist/commands/doctor.d.ts +2 -1
- package/dist/commands/doctor.d.ts.map +1 -1
- package/dist/commands/doctor.js +141 -3
- package/dist/commands/doctor.js.map +1 -1
- package/dist/commands/init.d.ts +2 -10
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +22 -179
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/upgrade.d.ts +8 -0
- package/dist/commands/upgrade.d.ts.map +1 -0
- package/dist/commands/upgrade.js +185 -0
- package/dist/commands/upgrade.js.map +1 -0
- package/dist/setup-steps.d.ts +95 -0
- package/dist/setup-steps.d.ts.map +1 -0
- package/dist/setup-steps.js +289 -0
- package/dist/setup-steps.js.map +1 -0
- package/package.json +2 -2
- package/src/cli.ts +2 -0
- package/src/commands/doctor.ts +145 -4
- package/src/commands/init.ts +23 -199
- package/src/commands/upgrade.ts +189 -0
- package/src/setup-steps.ts +330 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `quiesco init --upgrade [--dry-run] [--platforms …] [--packages <dir>] [--skip-install]` (issues #34, #35):
|
|
3
|
+
* bring a project set up by an older `quiesco init` to this version. Installs every Quiesco package at this
|
|
4
|
+
* CLI's version (`npx quiesco@latest init --upgrade` upgrades), then fixes what older inits wrote and the
|
|
5
|
+
* current one does not: the `paths` line in the cucumber config (#8), tsconfig's allowImportingTsExtensions
|
|
6
|
+
* (#21), app IDs and the config's TODO placeholders (#34), missing e2e scripts, <QuiescoRoot> (#20).
|
|
7
|
+
*
|
|
8
|
+
* Only changes it can make safely; anything else is listed with what to do. Running it again changes
|
|
9
|
+
* nothing. `--dry-run` lists the changes without making them.
|
|
10
|
+
*/
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
|
|
13
|
+
import type { QuiescoConfig } from '@quiesco/core';
|
|
14
|
+
import { loadConfig } from '@quiesco/core';
|
|
15
|
+
|
|
16
|
+
import type { CliContext, CliPlatform } from '../context.js';
|
|
17
|
+
import { PLATFORMS, takeFlag, takeOption } from '../context.js';
|
|
18
|
+
import { installedVersion, isQuiescoPackage } from '../packages.js';
|
|
19
|
+
import { entryFile, registeredRoot, rootSourceFiles, wrapRegisteredRoot } from '../project.js';
|
|
20
|
+
import type { AppJson, InitStep, PackageJson, StepLog } from '../setup-steps.js';
|
|
21
|
+
import { DEV, RUNTIME, WEB_DEV, detectPackageManager, ensureAppIds, ensureScripts, ensureTsconfig, firstScheme, installPackages, printSteps, readJson } from '../setup-steps.js';
|
|
22
|
+
|
|
23
|
+
export interface UpgradeDeps {
|
|
24
|
+
loadConfig: (cwd: string) => Promise<QuiescoConfig>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const defaultUpgradeDeps: UpgradeDeps = {
|
|
28
|
+
loadConfig: async (cwd) => (await loadConfig({ cwd })).config,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const CONFIG_FILES = ['quiesco.config.mts', 'quiesco.config.ts', 'quiesco.config.mjs', 'quiesco.config.js'];
|
|
32
|
+
const CUCUMBER_FILES = ['cucumber.mjs', 'cucumber.js', 'cucumber.cjs', 'cucumber.json', 'cucumber.yaml', 'cucumber.yml'];
|
|
33
|
+
/** The line 0.1.x wrote into cucumber.mjs (issue #8). */
|
|
34
|
+
const OLD_PATHS_LINE = /^[ \t]*paths:[ \t]*\[[ \t]*['"]features\/\*\*\/\*\.feature['"][ \t]*\],?[ \t]*\r?\n/m;
|
|
35
|
+
|
|
36
|
+
/** Quiesco packages to install: the ones the platforms need and every other one the project already has. */
|
|
37
|
+
function packagesToInstall(pkg: PackageJson, platforms: readonly CliPlatform[]): { runtime: string[]; dev: string[] } {
|
|
38
|
+
const needed = [...RUNTIME, ...DEV, ...(platforms.includes('web') ? WEB_DEV : [])].filter(isQuiescoPackage);
|
|
39
|
+
const present = [...Object.keys(pkg.dependencies ?? {}), ...Object.keys(pkg.devDependencies ?? {})].filter(isQuiescoPackage);
|
|
40
|
+
const all = [...new Set([...needed, ...present])];
|
|
41
|
+
// Keep each package in the group it is in; new ones go where init puts them.
|
|
42
|
+
const isRuntime = (n: string): boolean => pkg.dependencies?.[n] !== undefined || (pkg.devDependencies?.[n] === undefined && RUNTIME.includes(n));
|
|
43
|
+
return { runtime: all.filter(isRuntime), dev: all.filter((n) => !isRuntime(n)) };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function fixCucumberPaths(log: StepLog): void {
|
|
47
|
+
const { ctx, steps } = log;
|
|
48
|
+
const rel = CUCUMBER_FILES.find((f) => ctx.exists(path.join(ctx.cwd, f)));
|
|
49
|
+
if (rel === undefined) return;
|
|
50
|
+
const file = path.join(ctx.cwd, rel);
|
|
51
|
+
const text = ctx.readFile(file) ?? '';
|
|
52
|
+
if (OLD_PATHS_LINE.test(text)) {
|
|
53
|
+
if (!log.dryRun) ctx.writeFile(file, text.replace(OLD_PATHS_LINE, ''));
|
|
54
|
+
steps.push({ status: 'updated', what: rel, note: "removed paths: ['features/**/*.feature'] (the default; with it, quiesco test <file> ran every feature)" });
|
|
55
|
+
} else if (/\bpaths\b["']?\s*:/.test(text)) {
|
|
56
|
+
steps.push({ status: 'todo', what: rel, note: 'sets paths: cucumber-js adds them to the files given on the command line, so quiesco test <file> runs them all; remove it unless you need it' });
|
|
57
|
+
} else {
|
|
58
|
+
steps.push({ status: 'skipped', what: rel, note: 'no paths' });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const PLACEHOLDERS: readonly { todo: string; key: string; value: (app: AppJson) => string | undefined }[] = [
|
|
63
|
+
{ todo: 'expo.ios.bundleIdentifier', key: 'bundleId', value: (a) => a.expo?.ios?.bundleIdentifier },
|
|
64
|
+
{ todo: 'expo.android.package', key: 'package', value: (a) => a.expo?.android?.package },
|
|
65
|
+
{ todo: 'expo.scheme', key: 'scheme', value: (a) => firstScheme(a) },
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
/** Replace the placeholders older inits wrote (`'com.example.app', // TODO: expo.android.package`) with app.json's values. */
|
|
69
|
+
function fixConfigPlaceholders(log: StepLog, app: AppJson): void {
|
|
70
|
+
const { ctx, steps } = log;
|
|
71
|
+
const rel = CONFIG_FILES.find((f) => ctx.exists(path.join(ctx.cwd, f)));
|
|
72
|
+
if (rel === undefined) return;
|
|
73
|
+
const file = path.join(ctx.cwd, rel);
|
|
74
|
+
const text = ctx.readFile(file) ?? '';
|
|
75
|
+
const replaced: string[] = [];
|
|
76
|
+
const left: string[] = [];
|
|
77
|
+
const next = text
|
|
78
|
+
.split('\n')
|
|
79
|
+
.map((line) => {
|
|
80
|
+
const p = PLACEHOLDERS.find((x) => line.includes(`// TODO: ${x.todo}`));
|
|
81
|
+
if (p === undefined) return line;
|
|
82
|
+
const value = p.value(app);
|
|
83
|
+
const keyed = new RegExp(`(\\b${p.key}:\\s*)'[^']*'`);
|
|
84
|
+
if (value === undefined || !keyed.test(line)) {
|
|
85
|
+
left.push(p.todo);
|
|
86
|
+
return line;
|
|
87
|
+
}
|
|
88
|
+
replaced.push(`${p.key} '${value}'`);
|
|
89
|
+
return line.replace(keyed, `$1'${value.replace(/'/g, "\\'")}'`).replace(new RegExp(`\\s*// TODO: ${p.todo.replace(/\./g, '\\.')}`), '');
|
|
90
|
+
})
|
|
91
|
+
.join('\n');
|
|
92
|
+
if (replaced.length > 0) {
|
|
93
|
+
if (!log.dryRun) ctx.writeFile(file, next);
|
|
94
|
+
steps.push({ status: 'updated', what: rel, note: `placeholders replaced with app.json's values: ${replaced.join(', ')}` });
|
|
95
|
+
}
|
|
96
|
+
if (left.length > 0) steps.push({ status: 'todo', what: rel, note: `placeholders left (TODO: ${left.join(', ')}): set them to the values your app config uses` });
|
|
97
|
+
if (replaced.length === 0 && left.length === 0) steps.push({ status: 'skipped', what: rel, note: 'no placeholders' });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** <QuiescoRoot> around the component of registerRootComponent(App) (issue #20); other entries are listed. */
|
|
101
|
+
function ensureRoot(log: StepLog): void {
|
|
102
|
+
const { ctx, steps } = log;
|
|
103
|
+
const main = readJson<PackageJson>(ctx, path.join(ctx.cwd, 'package.json'))?.main;
|
|
104
|
+
if (rootSourceFiles(ctx, main).some((f) => (ctx.readFile(f) ?? '').includes('QuiescoRoot'))) {
|
|
105
|
+
steps.push({ status: 'skipped', what: '<QuiescoRoot>', note: 'the app uses it' });
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const entry = entryFile(ctx, main);
|
|
109
|
+
const src = entry === null ? null : ctx.readFile(entry);
|
|
110
|
+
const wrapped = entry === null || src === null ? null : wrapRegisteredRoot(ctx, entry, src);
|
|
111
|
+
if (entry === null || src === null || wrapped === null) {
|
|
112
|
+
steps.push({ status: 'todo', what: '<QuiescoRoot>', note: "wrap the root layout in <QuiescoRoot> (import { QuiescoRoot } from '@quiesco/agent'): the app connects to Quiesco from it" });
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (!log.dryRun) ctx.writeFile(entry, wrapped);
|
|
116
|
+
steps.push({ status: 'updated', what: path.basename(entry), note: `wrapped ${registeredRoot(ctx, entry, src)?.name ?? 'the root component'} in <QuiescoRoot>` });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export async function upgradeCommand(args: readonly string[], ctx: CliContext, deps: UpgradeDeps = defaultUpgradeDeps): Promise<number> {
|
|
120
|
+
let rest = [...args];
|
|
121
|
+
const platformsOpt = takeOption(rest, '--platforms');
|
|
122
|
+
rest = platformsOpt.rest;
|
|
123
|
+
const packagesOpt = takeOption(rest, '--packages');
|
|
124
|
+
rest = packagesOpt.rest;
|
|
125
|
+
const skipInstall = takeFlag(rest, '--skip-install');
|
|
126
|
+
rest = skipInstall.rest;
|
|
127
|
+
const dryRun = takeFlag(rest, '--dry-run');
|
|
128
|
+
rest = dryRun.rest;
|
|
129
|
+
if (rest.length > 0) {
|
|
130
|
+
ctx.err(`quiesco init --upgrade: unknown arguments ${rest.join(' ')}`);
|
|
131
|
+
return 2;
|
|
132
|
+
}
|
|
133
|
+
const pkg = readJson<PackageJson>(ctx, path.join(ctx.cwd, 'package.json'));
|
|
134
|
+
if (pkg === null || (pkg.dependencies?.['expo'] === undefined && pkg.devDependencies?.['expo'] === undefined)) {
|
|
135
|
+
ctx.err(`quiesco init --upgrade: ${ctx.cwd} is not an Expo project (no expo dependency in package.json).`);
|
|
136
|
+
return 1;
|
|
137
|
+
}
|
|
138
|
+
let platforms: CliPlatform[];
|
|
139
|
+
if (platformsOpt.value !== undefined) {
|
|
140
|
+
const bad = platformsOpt.value.split(',').filter((p) => !(PLATFORMS as readonly string[]).includes(p));
|
|
141
|
+
if (bad.length > 0) {
|
|
142
|
+
ctx.err(`quiesco init --upgrade: unknown platform ${bad.join(', ')}`);
|
|
143
|
+
return 2;
|
|
144
|
+
}
|
|
145
|
+
platforms = platformsOpt.value.split(',') as CliPlatform[];
|
|
146
|
+
} else {
|
|
147
|
+
try {
|
|
148
|
+
const config = await deps.loadConfig(ctx.cwd);
|
|
149
|
+
platforms = PLATFORMS.filter((p) => config.apps[p] !== undefined);
|
|
150
|
+
} catch (e) {
|
|
151
|
+
ctx.err(`quiesco init --upgrade: cannot read quiesco.config (${(e as Error).message.split('\n')[0] ?? ''}).`);
|
|
152
|
+
ctx.err('Run quiesco init for a new setup, or name the platforms: quiesco init --upgrade --platforms ios,android,web');
|
|
153
|
+
return 1;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const steps: InitStep[] = [];
|
|
158
|
+
const log: StepLog = { ctx, steps, dryRun: dryRun.present };
|
|
159
|
+
|
|
160
|
+
// 1. Every Quiesco package at this CLI's version.
|
|
161
|
+
const { runtime, dev } = packagesToInstall(pkg, platforms);
|
|
162
|
+
const current = [...runtime, ...dev].map((n) => installedVersion(ctx, n));
|
|
163
|
+
if (packagesOpt.value === undefined && current.every((v) => v === ctx.version)) {
|
|
164
|
+
steps.push({ status: 'skipped', what: 'versions', note: `${current.length} Quiesco packages at ${ctx.version}` });
|
|
165
|
+
} else {
|
|
166
|
+
const ok = await installPackages(log, {
|
|
167
|
+
pm: detectPackageManager(ctx),
|
|
168
|
+
runtime,
|
|
169
|
+
dev,
|
|
170
|
+
...(packagesOpt.value !== undefined ? { packagesDir: packagesOpt.value } : {}),
|
|
171
|
+
skipInstall: skipInstall.present,
|
|
172
|
+
});
|
|
173
|
+
if (!ok) return 1;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// 2. What older inits wrote.
|
|
177
|
+
fixCucumberPaths(log);
|
|
178
|
+
ensureTsconfig(log);
|
|
179
|
+
const app = ensureAppIds(log, platforms);
|
|
180
|
+
fixConfigPlaceholders(log, app);
|
|
181
|
+
ensureScripts(log, platforms);
|
|
182
|
+
ensureRoot(log);
|
|
183
|
+
|
|
184
|
+
printSteps(ctx, steps);
|
|
185
|
+
ctx.out('');
|
|
186
|
+
if (dryRun.present) ctx.out('Dry run: nothing was changed. Run quiesco init --upgrade without --dry-run to make these changes.');
|
|
187
|
+
else ctx.out(`Upgraded to Quiesco ${ctx.version}${steps.some((s) => s.status === 'todo') ? '; the lines marked ! need you' : ''}. Check the setup with: npx quiesco doctor`);
|
|
188
|
+
return 0;
|
|
189
|
+
}
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The steps of `quiesco init` that `quiesco init --upgrade` repeats on an existing project: installing the
|
|
3
|
+
* Quiesco packages at this CLI's version, app IDs in app.json, tsconfig.json and the e2e scripts. Each step
|
|
4
|
+
* records what it did as an InitStep and, in a dry run, only says what it would do.
|
|
5
|
+
*/
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
|
|
9
|
+
import type { CliContext, CliPlatform } from './context.js';
|
|
10
|
+
import { installedVersion, isQuiescoPackage } from './packages.js';
|
|
11
|
+
|
|
12
|
+
export type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun';
|
|
13
|
+
|
|
14
|
+
export interface PackageJson {
|
|
15
|
+
name?: string;
|
|
16
|
+
main?: string;
|
|
17
|
+
scripts?: Record<string, string>;
|
|
18
|
+
dependencies?: Record<string, string>;
|
|
19
|
+
devDependencies?: Record<string, string>;
|
|
20
|
+
overrides?: Record<string, string>;
|
|
21
|
+
resolutions?: Record<string, string>;
|
|
22
|
+
[key: string]: unknown;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface AppJson {
|
|
26
|
+
expo?: {
|
|
27
|
+
name?: string;
|
|
28
|
+
slug?: string;
|
|
29
|
+
scheme?: string | string[];
|
|
30
|
+
plugins?: unknown[];
|
|
31
|
+
ios?: { bundleIdentifier?: string; [key: string]: unknown };
|
|
32
|
+
android?: { package?: string; [key: string]: unknown };
|
|
33
|
+
[key: string]: unknown;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface InitStep {
|
|
38
|
+
status: 'created' | 'updated' | 'skipped' | 'ran' | 'todo';
|
|
39
|
+
what: string;
|
|
40
|
+
note?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Where steps record what they did; `dryRun`: change nothing, only record what would change. */
|
|
44
|
+
export interface StepLog {
|
|
45
|
+
ctx: CliContext;
|
|
46
|
+
steps: InitStep[];
|
|
47
|
+
dryRun: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Installed into `dependencies`: the app bundles them (stubbed / excluded in production builds). */
|
|
51
|
+
export const RUNTIME = ['@quiesco/agent', '@quiesco/native'];
|
|
52
|
+
/** Installed into `devDependencies`. */
|
|
53
|
+
export const DEV = ['quiesco', '@quiesco/core', '@quiesco/cucumber', '@quiesco/expo-plugin', '@cucumber/cucumber'];
|
|
54
|
+
export const WEB_DEV = ['@quiesco/web', 'playwright'];
|
|
55
|
+
/** Internal packages pulled in by the ones above; only pinned when installing from tarballs. */
|
|
56
|
+
const TRANSITIVE = ['@quiesco/protocol'];
|
|
57
|
+
|
|
58
|
+
/** The app config files Quiesco cannot edit (JavaScript / TypeScript). */
|
|
59
|
+
export const DYNAMIC_APP_CONFIGS = ['app.config.js', 'app.config.ts', 'app.config.mjs', 'app.config.cjs'];
|
|
60
|
+
|
|
61
|
+
export function readJson<T>(ctx: CliContext, file: string): T | null {
|
|
62
|
+
const text = ctx.readFile(file);
|
|
63
|
+
if (text === null) return null;
|
|
64
|
+
try {
|
|
65
|
+
return JSON.parse(text) as T;
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const writeJson = (ctx: CliContext, file: string, value: unknown): void => ctx.writeFile(file, `${JSON.stringify(value, null, 2)}\n`);
|
|
72
|
+
|
|
73
|
+
export function detectPackageManager(ctx: CliContext): PackageManager {
|
|
74
|
+
if (ctx.exists(path.join(ctx.cwd, 'pnpm-lock.yaml'))) return 'pnpm';
|
|
75
|
+
if (ctx.exists(path.join(ctx.cwd, 'yarn.lock'))) return 'yarn';
|
|
76
|
+
if (ctx.exists(path.join(ctx.cwd, 'bun.lock')) || ctx.exists(path.join(ctx.cwd, 'bun.lockb'))) return 'bun';
|
|
77
|
+
return 'npm';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** `@quiesco/agent` → `quiesco-agent-<version>.tgz` (the file name `pnpm pack` / `npm pack` writes). */
|
|
81
|
+
export function findTarballs(dir: string, names: readonly string[], list: (d: string) => string[] = (d) => fs.readdirSync(d)): Map<string, string> {
|
|
82
|
+
const files = list(dir).filter((f) => f.endsWith('.tgz'));
|
|
83
|
+
const found = new Map<string, string>();
|
|
84
|
+
for (const name of names) {
|
|
85
|
+
const base = name.replace(/^@/, '').replace('/', '-');
|
|
86
|
+
const match = files.find((f) => new RegExp(`^${base.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}-\\d[^/]*\\.tgz$`).test(f));
|
|
87
|
+
if (match !== undefined) found.set(name, path.join(dir, match));
|
|
88
|
+
}
|
|
89
|
+
return found;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** An `overrides:` block for pnpm-workspace.yaml (names and paths quoted: `@` starts no plain YAML scalar). */
|
|
93
|
+
function pnpmOverrides(pins: Record<string, string>): string {
|
|
94
|
+
return `overrides:\n${Object.entries(pins).map(([n, f]) => ` ${JSON.stringify(n)}: ${JSON.stringify(f)}\n`).join('')}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function addCommand(pm: PackageManager, dev: boolean, specs: string[]): { command: string; args: string[] } {
|
|
98
|
+
switch (pm) {
|
|
99
|
+
case 'pnpm':
|
|
100
|
+
return { command: 'pnpm', args: ['add', ...(dev ? ['-D'] : []), ...specs] };
|
|
101
|
+
case 'yarn':
|
|
102
|
+
return { command: 'yarn', args: ['add', ...(dev ? ['-D'] : []), ...specs] };
|
|
103
|
+
case 'bun':
|
|
104
|
+
return { command: 'bun', args: ['add', ...(dev ? ['-d'] : []), ...specs] };
|
|
105
|
+
case 'npm':
|
|
106
|
+
return { command: 'npm', args: ['install', ...(dev ? ['--save-dev'] : []), ...specs] };
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface InstallOptions {
|
|
111
|
+
pm: PackageManager;
|
|
112
|
+
runtime: readonly string[];
|
|
113
|
+
dev: readonly string[];
|
|
114
|
+
/** `--packages <dir>`: install from the tarballs in it. */
|
|
115
|
+
packagesDir?: string;
|
|
116
|
+
skipInstall: boolean;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Install `runtime` / `dev` with each Quiesco package at this CLI's version (issue #17: not whatever the
|
|
121
|
+
* package manager's cached registry metadata calls latest), then read the installed versions back.
|
|
122
|
+
* Returns false (after printing why) when it failed; the command should stop then.
|
|
123
|
+
*/
|
|
124
|
+
export async function installPackages(log: StepLog, o: InstallOptions): Promise<boolean> {
|
|
125
|
+
const { ctx, steps } = log;
|
|
126
|
+
let spec = (name: string): string => (isQuiescoPackage(name) ? `${name}@${ctx.version}` : name);
|
|
127
|
+
/** The version each Quiesco package must have after the install. */
|
|
128
|
+
let expected = (_name: string): string => ctx.version;
|
|
129
|
+
if (o.packagesDir !== undefined) {
|
|
130
|
+
const dir = path.resolve(ctx.cwd, o.packagesDir);
|
|
131
|
+
const ours = [...o.runtime, ...o.dev, ...TRANSITIVE].filter(isQuiescoPackage);
|
|
132
|
+
const tarballs = findTarballs(dir, ours);
|
|
133
|
+
const missing = ours.filter((n) => !tarballs.has(n));
|
|
134
|
+
if (missing.length > 0) {
|
|
135
|
+
ctx.err(`quiesco init: no tarball for ${missing.join(', ')} in ${dir}`);
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
spec = (name) => (tarballs.has(name) ? `file:${tarballs.get(name) as string}` : name);
|
|
139
|
+
expected = (name) => /-(\d[^/]*)\.tgz$/.exec(tarballs.get(name) ?? '')?.[1] ?? ctx.version;
|
|
140
|
+
// Their own dependencies on each other ("@quiesco/protocol": "0.0.0") must resolve to the tarballs too.
|
|
141
|
+
// npm refuses an override for a package that is also a direct dependency (EOVERRIDE) and reuses the
|
|
142
|
+
// directly installed tarballs for those ranges anyway: pin only the indirect ones there. pnpm and
|
|
143
|
+
// yarn resolve every dependency on its own, so they need all of them pinned.
|
|
144
|
+
const direct = new Set([...o.runtime, ...o.dev]);
|
|
145
|
+
const pins = Object.fromEntries([...tarballs].filter(([n]) => o.pm !== 'npm' || !direct.has(n)).map(([n, f]) => [n, `file:${f}`]));
|
|
146
|
+
const pinned = `installing from the tarballs in ${o.packagesDir} (${Object.keys(pins).length} indirect ${Object.keys(pins).length === 1 ? 'dependency' : 'dependencies'} pinned)`;
|
|
147
|
+
if (o.pm === 'pnpm') {
|
|
148
|
+
// pnpm 10+ reads overrides from pnpm-workspace.yaml only; the "pnpm" field of package.json is ignored.
|
|
149
|
+
const yamlFile = path.join(ctx.cwd, 'pnpm-workspace.yaml');
|
|
150
|
+
const yaml = ctx.readFile(yamlFile) ?? '';
|
|
151
|
+
if (/^overrides:/m.test(yaml)) {
|
|
152
|
+
ctx.err(`quiesco init: pnpm-workspace.yaml already has overrides; add these and run quiesco init again:\n${pnpmOverrides(pins)}`);
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
if (!log.dryRun) ctx.writeFile(yamlFile, `${yaml}${yaml === '' || yaml.endsWith('\n') ? '' : '\n'}${pnpmOverrides(pins)}`);
|
|
156
|
+
steps.push({ status: 'updated', what: 'pnpm-workspace.yaml', note: pinned });
|
|
157
|
+
} else {
|
|
158
|
+
const pkgFile = path.join(ctx.cwd, 'package.json');
|
|
159
|
+
const current = readJson<PackageJson>(ctx, pkgFile) as PackageJson;
|
|
160
|
+
if (o.pm === 'yarn') current.resolutions = { ...current.resolutions, ...pins };
|
|
161
|
+
else current.overrides = { ...current.overrides, ...pins };
|
|
162
|
+
if (!log.dryRun) writeJson(ctx, pkgFile, current);
|
|
163
|
+
steps.push({ status: 'updated', what: 'package.json', note: pinned });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/** One command per dependency group that has packages (an upgrade may have no runtime ones to change). */
|
|
167
|
+
const commands = (names: (list: readonly string[]) => readonly string[]): { command: string; args: string[] }[] =>
|
|
168
|
+
([[false, o.runtime], [true, o.dev]] as const).filter(([, list]) => names(list).length > 0).map(([dev, list]) => addCommand(o.pm, dev, names(list).map(spec)));
|
|
169
|
+
const installs = commands((list) => list);
|
|
170
|
+
for (const cmd of installs) {
|
|
171
|
+
const line = `${cmd.command} ${cmd.args.join(' ')}`;
|
|
172
|
+
if (o.skipInstall || log.dryRun) {
|
|
173
|
+
steps.push({ status: 'todo', what: 'install', note: line });
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
ctx.out(`$ ${line}`);
|
|
177
|
+
const r = await ctx.exec(cmd.command, cmd.args, { cwd: ctx.cwd });
|
|
178
|
+
if (r.code !== 0) {
|
|
179
|
+
ctx.err(r.stderr.trim() || r.stdout.trim());
|
|
180
|
+
ctx.err(`quiesco init: \`${line}\` failed (exit ${r.code}).`);
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
steps.push({ status: 'ran', what: 'install', note: line });
|
|
184
|
+
}
|
|
185
|
+
if (o.skipInstall || log.dryRun) return true;
|
|
186
|
+
const ours = [...o.runtime, ...o.dev].filter(isQuiescoPackage);
|
|
187
|
+
const wrong = ours.map((name) => ({ name, want: expected(name), got: installedVersion(ctx, name) })).filter((p) => p.got !== p.want);
|
|
188
|
+
if (wrong.length > 0) {
|
|
189
|
+
ctx.err(`quiesco init: the installed Quiesco packages are not the version that was asked for:`);
|
|
190
|
+
for (const p of wrong) ctx.err(` ${p.name}: ${p.got ?? 'not found in node_modules'} (wanted ${p.want})`);
|
|
191
|
+
ctx.err('All Quiesco packages must have the same version as the CLI that writes the project files. The');
|
|
192
|
+
ctx.err("package manager may have used stale registry metadata from its cache, or held a new release back");
|
|
193
|
+
ctx.err('(pnpm minimumReleaseAge). Install them again with the version given:');
|
|
194
|
+
for (const cmd of commands((list) => list.filter(isQuiescoPackage))) ctx.err(` ${cmd.command} ${cmd.args.join(' ')}`);
|
|
195
|
+
ctx.err('then run quiesco init again.');
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
steps.push({ status: 'ran', what: 'versions', note: `${ours.length} Quiesco packages at ${[...new Set(ours.map(expected))].join(', ')}` });
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// ---- app IDs (issue #34) ----
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* IDs for an app.json that has none: `com.anonymous.<slug>` (letters and digits, as Expo's prebuild derives
|
|
206
|
+
* the Android package; for iOS it keeps hyphens, so the two would differ) and the slug as URL scheme.
|
|
207
|
+
*/
|
|
208
|
+
export function defaultAppIds(app: AppJson, fallbackName: string | undefined): { id: string; scheme: string } {
|
|
209
|
+
const base = (app.expo?.slug ?? app.expo?.name ?? fallbackName ?? 'app').toLowerCase();
|
|
210
|
+
let idPart = base.replace(/[^a-z0-9]/g, '');
|
|
211
|
+
if (!/^[a-z]/.test(idPart)) idPart = `app${idPart}`;
|
|
212
|
+
let scheme = base.replace(/[^a-z0-9+.-]/g, '');
|
|
213
|
+
if (!/^[a-z]/.test(scheme)) scheme = `app${scheme}`;
|
|
214
|
+
return { id: `com.anonymous.${idPart}`, scheme };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function firstScheme(app: AppJson): string | undefined {
|
|
218
|
+
const s = app.expo?.scheme;
|
|
219
|
+
return Array.isArray(s) ? s[0] : s;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** The app config is JavaScript / TypeScript: Quiesco cannot read or change it. */
|
|
223
|
+
export function dynamicAppConfig(ctx: CliContext): string | undefined {
|
|
224
|
+
return DYNAMIC_APP_CONFIGS.find((f) => ctx.exists(path.join(ctx.cwd, f)));
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Write the IDs the platforms need into app.json when it has none (issue #34): `ios.bundleIdentifier` for
|
|
229
|
+
* iOS, `android.package` and `scheme` (the dev client's deep link) for Android. Without them the build uses
|
|
230
|
+
* Expo's defaults, which differ per platform and are not what quiesco.config names. Returns app.json as
|
|
231
|
+
* it is now.
|
|
232
|
+
*/
|
|
233
|
+
export function ensureAppIds(log: StepLog, platforms: readonly CliPlatform[]): AppJson {
|
|
234
|
+
const { ctx, steps } = log;
|
|
235
|
+
const appFile = path.join(ctx.cwd, 'app.json');
|
|
236
|
+
const app = readJson<AppJson>(ctx, appFile) ?? {};
|
|
237
|
+
const expo = app.expo;
|
|
238
|
+
if (expo === undefined || !platforms.some((p) => p !== 'web')) return app;
|
|
239
|
+
const dynamic = dynamicAppConfig(ctx);
|
|
240
|
+
const missing: string[] = [];
|
|
241
|
+
if (platforms.includes('ios') && expo.ios?.bundleIdentifier === undefined) missing.push('ios.bundleIdentifier');
|
|
242
|
+
if (platforms.includes('android') && expo.android?.package === undefined) missing.push('android.package');
|
|
243
|
+
if (platforms.includes('android') && firstScheme(app) === undefined) missing.push('scheme');
|
|
244
|
+
if (missing.length === 0) {
|
|
245
|
+
steps.push({ status: 'skipped', what: 'app IDs', note: 'app.json has them' });
|
|
246
|
+
return app;
|
|
247
|
+
}
|
|
248
|
+
if (dynamic !== undefined) {
|
|
249
|
+
steps.push({ status: 'todo', what: 'app IDs', note: `set expo.${missing.join(', expo.')} in ${dynamic} and use the same values in quiesco.config` });
|
|
250
|
+
return app;
|
|
251
|
+
}
|
|
252
|
+
const { id, scheme } = defaultAppIds(app, readJson<PackageJson>(ctx, path.join(ctx.cwd, 'package.json'))?.name);
|
|
253
|
+
const set: string[] = [];
|
|
254
|
+
if (missing.includes('ios.bundleIdentifier')) {
|
|
255
|
+
expo.ios = { ...expo.ios, bundleIdentifier: id };
|
|
256
|
+
set.push(`ios.bundleIdentifier "${id}"`);
|
|
257
|
+
}
|
|
258
|
+
if (missing.includes('android.package')) {
|
|
259
|
+
expo.android = { ...expo.android, package: id };
|
|
260
|
+
set.push(`android.package "${id}"`);
|
|
261
|
+
}
|
|
262
|
+
if (missing.includes('scheme')) {
|
|
263
|
+
expo.scheme = scheme;
|
|
264
|
+
set.push(`scheme "${scheme}"`);
|
|
265
|
+
}
|
|
266
|
+
if (!log.dryRun) writeJson(ctx, appFile, app);
|
|
267
|
+
steps.push({ status: 'updated', what: 'app.json', note: `set ${set.join(', ')}: the build and quiesco.config must name the same app (change them here and in quiesco.config if you like)` });
|
|
268
|
+
return app;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// ---- tsconfig.json (issue #21) ----
|
|
272
|
+
|
|
273
|
+
/** Step files import each other with .ts (Node's type stripping needs it): let tsc accept that. */
|
|
274
|
+
export function ensureTsconfig(log: StepLog): void {
|
|
275
|
+
const { ctx, steps } = log;
|
|
276
|
+
const tsconfigFile = path.join(ctx.cwd, 'tsconfig.json');
|
|
277
|
+
const tsconfigText = ctx.readFile(tsconfigFile);
|
|
278
|
+
if (tsconfigText === null) return;
|
|
279
|
+
let tsconfig: { compilerOptions?: Record<string, unknown> } | null;
|
|
280
|
+
try {
|
|
281
|
+
tsconfig = JSON.parse(tsconfigText) as { compilerOptions?: Record<string, unknown> };
|
|
282
|
+
} catch {
|
|
283
|
+
tsconfig = null; // comments or trailing commas: leave the file to the user
|
|
284
|
+
}
|
|
285
|
+
if (tsconfig?.compilerOptions?.['allowImportingTsExtensions'] === true || /"allowImportingTsExtensions"\s*:\s*true/.test(tsconfigText)) {
|
|
286
|
+
steps.push({ status: 'skipped', what: 'tsconfig.json', note: 'already allows .ts imports' });
|
|
287
|
+
} else if (tsconfig === null || typeof tsconfig !== 'object' || Array.isArray(tsconfig)) {
|
|
288
|
+
steps.push({ status: 'todo', what: 'tsconfig.json', note: 'add "allowImportingTsExtensions": true to compilerOptions (step files import each other with .ts)' });
|
|
289
|
+
} else {
|
|
290
|
+
tsconfig.compilerOptions = { ...tsconfig.compilerOptions, allowImportingTsExtensions: true };
|
|
291
|
+
if (!log.dryRun) writeJson(ctx, tsconfigFile, tsconfig);
|
|
292
|
+
steps.push({ status: 'updated', what: 'tsconfig.json', note: 'allowImportingTsExtensions: step files import each other with .ts' });
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// ---- scripts ----
|
|
297
|
+
|
|
298
|
+
export function e2eScripts(platforms: readonly CliPlatform[]): Record<string, string> {
|
|
299
|
+
return {
|
|
300
|
+
'e2e:start': 'EXPO_PUBLIC_QUIESCO=1 expo start',
|
|
301
|
+
...(platforms.includes('web') ? { 'e2e:web': 'EXPO_PUBLIC_QUIESCO=1 expo export --platform web && quiesco test --platform web' } : {}),
|
|
302
|
+
...(platforms.includes('android') ? { 'e2e:build:android': 'quiesco build android', 'e2e:android': 'quiesco test --platform android' } : {}),
|
|
303
|
+
...(platforms.includes('ios') ? { 'e2e:build:ios': 'quiesco build ios', 'e2e:ios': 'quiesco test --platform ios' } : {}),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Add the e2e scripts that are missing; existing scripts of the same name are left alone. */
|
|
308
|
+
export function ensureScripts(log: StepLog, platforms: readonly CliPlatform[]): void {
|
|
309
|
+
const { ctx, steps } = log;
|
|
310
|
+
const pkgFile = path.join(ctx.cwd, 'package.json');
|
|
311
|
+
const pkg = readJson<PackageJson>(ctx, pkgFile) as PackageJson;
|
|
312
|
+
const scripts = { ...(pkg.scripts ?? {}) };
|
|
313
|
+
const wanted = e2eScripts(platforms);
|
|
314
|
+
const added = Object.keys(wanted).filter((k) => scripts[k] === undefined);
|
|
315
|
+
if (added.length === 0) {
|
|
316
|
+
steps.push({ status: 'skipped', what: 'scripts', note: 'the e2e scripts exist' });
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
for (const k of added) scripts[k] = wanted[k] as string;
|
|
320
|
+
pkg.scripts = scripts;
|
|
321
|
+
if (!log.dryRun) writeJson(ctx, pkgFile, pkg);
|
|
322
|
+
steps.push({ status: 'updated', what: 'package.json', note: `scripts: ${added.join(', ')}` });
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const ICON = { created: '+', updated: '~', skipped: '=', ran: '$', todo: '!' } as const;
|
|
326
|
+
|
|
327
|
+
export function printSteps(ctx: CliContext, steps: readonly InitStep[]): void {
|
|
328
|
+
ctx.out('');
|
|
329
|
+
for (const s of steps) ctx.out(`${ICON[s.status]} ${s.what}${s.note !== undefined ? ` — ${s.note}` : ''}`);
|
|
330
|
+
}
|