Skip to content

Commit e087ff8

Browse files
robingenzclaude
andauthored
feat(doctor): report a Capacitor app that never imports the SDK (#120)
* feat(doctor): report a Capacitor app that never imports the SDK The Capacitor SDK learns that the app rendered from its import, so an app that never imports it confirms no update and rolls every update back. doctor now has an import check on Capacitor: it fails with import_missing when no script or component file under src/ names @hotcodepush/capacitor-live-updates, and it is skipped when there is no src/. init never edits the app's code, so it ends with the import line to add when the import is missing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(init): name the missing SDK import in a notice Since #115 the advice after init's rows is a notice on stderr and the finish is a result, so the import step joins that advice as a notice before the build advice. The test asserts the diagnostics by content instead of a fixed slice of stdout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 89c80ee commit e087ff8

7 files changed

Lines changed: 168 additions & 4 deletions

File tree

‎CLAUDE.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Release-please keeps a release pull request open on `main`, and merging it tags
8484
3. Nothing outside the module names the framework: a command asks the module, never a config file or a path of its own.
8585

8686
Capacitor's module reads `capacitor.config` as text and no native file, `webDir` when the upload runs, and a config with no `webDir` readable as a quoted string makes `--path` required, `missing_parameter` naming the reason; Cordova's reads `config.xml` through `fast-xml-parser` for the file-mode preference alone. The store build's identity comes from the build on every framework, never from a project file: the hook script passes the version and the build number it read from the processed `Info.plist` or the variant. React Native and Capacitor are wired the same way, `init` adding the phase and the Gradle line; Expo's config plugin and Cordova's plugin wire themselves.
87+
Capacitor's `import` check looks for the SDK's package name, quoted, in the script and component files under `src/`, since the import tells the SDK the app rendered. `init` never edits the app's code: after its rows it names the import to add in a notice, `FrameworkWiring`'s `appImportStep`.
8788
React Native's and Expo's have no build output to read and share React Native's release build in `frameworks/react-native-build.ts`: `packageReactNativeBundles` runs the module's bundler and Hermes' compiler per platform into the command's packaging directory, one bundle each, `collectEmbeddedFiles` takes `main.jsbundle` and `assets/` out of the app the Xcode phase points at, the native build passes the identity and `--resource-file-path` in.
8889
The bundler is all the two differ in there: `react-native bundle` with the project's entry file for React Native, `expo export:embed`, which resolves the entry file itself, for Expo.
8990
`--path` is the prepared bundle directory of the one platform `--platform` names, refused without the JavaScript under the name that platform's app loads.

‎src/commands/doctor.test.ts‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { join } from 'node:path';
33
import { afterEach, describe, expect, it, vi } from 'vitest';
44
import {
55
CAPACITOR_APP_GRADLE_FILE_PATH,
6+
CAPACITOR_ENTRY_FILE_PATH,
67
wireCapacitorProject,
78
writeCapacitorProject,
89
} from '../../test/capacitor-project.js';
@@ -120,6 +121,7 @@ describe('doctor', () => {
120121
'app:ok',
121122
'package:ok',
122123
'hook:ok',
124+
'import:ok',
123125
'hosts:ok',
124126
'signing_key:skipped',
125127
'versions:ok',
@@ -129,6 +131,51 @@ describe('doctor', () => {
129131
);
130132
});
131133

134+
it('should fail the import when no file under src/ imports the SDK, naming the line to add', async () => {
135+
const directoryPath = await writeSetUpProject();
136+
writeFileSync(
137+
join(directoryPath, CAPACITOR_ENTRY_FILE_PATH),
138+
"console.log('demo');\n",
139+
);
140+
respondWithSessionAndApp();
141+
142+
await expect(
143+
doctorCommand.action(
144+
{ config: join(directoryPath, 'hotcodepush.json'), json: true },
145+
undefined,
146+
),
147+
).rejects.toBeInstanceOf(ReportedFailureError);
148+
149+
const result = harness.readJson() as DoctorResult;
150+
expect(result.checks.find(({ check }) => check === 'import')).toEqual({
151+
check: 'import',
152+
code: 'import_missing',
153+
manualStep:
154+
"Add \"import '@hotcodepush/capacitor-live-updates';\" to the app's entry file, such as src/main.ts.",
155+
message:
156+
'No file under src/ imports @hotcodepush/capacitor-live-updates, so the app confirms no update.',
157+
status: 'failed',
158+
});
159+
});
160+
161+
it('should skip the import when the project has no src/ directory', async () => {
162+
const directoryPath = await writeSetUpProject();
163+
rmSync(join(directoryPath, 'src'), { force: true, recursive: true });
164+
respondWithSessionAndApp();
165+
166+
await doctorCommand.action(
167+
{ config: join(directoryPath, 'hotcodepush.json'), json: true },
168+
undefined,
169+
);
170+
171+
const result = harness.readJson() as DoctorResult;
172+
expect(result.checks.find(({ check }) => check === 'import')).toEqual({
173+
check: 'import',
174+
message: 'There is no src/ directory to look for the import in.',
175+
status: 'skipped',
176+
});
177+
});
178+
132179
it('should check a Cordova project by its plugin and the build steps it wires', async () => {
133180
const directoryPath = writeCordovaProject({
134181
isPluginInstalled: true,
@@ -212,6 +259,7 @@ describe('doctor', () => {
212259
' Run "hotcodepush init".',
213260
`✗ Hook The wiring is missing from ${CAPACITOR_APP_GRADLE_FILE_PATH}.`,
214261
' Run "hotcodepush init".',
262+
"✓ Import The app's code imports @hotcodepush/capacitor-live-updates.",
215263
'✗ Hosts The CLI cannot reach api (https://api.example.com/health).',
216264
' Check the network, the API URL in config.json and the HOTCODEPUSH_*_BASE_URL variables.',
217265
'– Signing key Code signing is off. Run "hotcodepush signing-key create" to turn it on.',
@@ -241,6 +289,7 @@ describe('doctor', () => {
241289
'session',
242290
'package',
243291
'hook',
292+
'import',
244293
'hosts',
245294
'signing_key',
246295
'versions',
@@ -253,7 +302,7 @@ describe('doctor', () => {
253302
message: 'The credential cannot be checked. fetch failed.',
254303
status: 'failed',
255304
});
256-
expect(result.checks[4]).toMatchObject({
305+
expect(result.checks.find(({ check }) => check === 'hosts')).toMatchObject({
257306
code: 'host_unreachable',
258307
status: 'failed',
259308
});
@@ -283,6 +332,7 @@ describe('doctor', () => {
283332
'app',
284333
'package',
285334
'hook',
335+
'import',
286336
'hosts',
287337
'signing_key',
288338
'versions',

‎src/commands/init.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { S_SUCCESS } from '@clack/prompts';
1111
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
1212
import {
1313
CAPACITOR_APP_GRADLE_FILE_PATH,
14+
CAPACITOR_ENTRY_FILE_PATH,
1415
CAPACITOR_XCODE_PROJECT_FILE_PATH,
1516
readJsonFile,
1617
wireCapacitorProject,
@@ -262,6 +263,35 @@ describe('init', () => {
262263
);
263264
});
264265

266+
it('should name the SDK import as a notice before the build advice when no file under src/ imports it, leaving the file alone', async () => {
267+
const directoryPath = writeProject({
268+
isPackageInstalled: true,
269+
projectConfig: {
270+
appId: DEMO_APP.id,
271+
channel: PRODUCTION_CHANNEL.name,
272+
},
273+
});
274+
await wireCapacitorProject(directoryPath);
275+
const entryFilePath = join(directoryPath, CAPACITOR_ENTRY_FILE_PATH);
276+
writeFileSync(entryFilePath, "console.log('demo');\n");
277+
respondWithSession([ACME_ORGANIZATION]);
278+
respondWithConfiguredApp();
279+
280+
await initCommand.action(
281+
{ config: join(directoryPath, 'hotcodepush.json'), yes: true },
282+
undefined,
283+
);
284+
285+
expect(harness.readDiagnostics()).toBe(
286+
[
287+
"Add \"import '@hotcodepush/capacitor-live-updates';\" to the app's entry file, such as src/main.ts.",
288+
'Build the app natively once and run it, then publish the first release as shown above.',
289+
'',
290+
].join('\n'),
291+
);
292+
expect(readFileSync(entryFilePath, 'utf8')).toBe("console.log('demo');\n");
293+
});
294+
265295
it('should complete a hotcodepush.json that names its channel by id without naming another channel', async () => {
266296
const directoryPath = writeProject({
267297
isPackageInstalled: true,

‎src/commands/init.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,13 @@ export default defineCommand({
183183
privateKeyPath,
184184
projectFiles,
185185
});
186-
printRun(run, { app: account.app, isReleased, options, privateKeyPath });
186+
printRun(run, {
187+
app: account.app,
188+
appImportStep: projectFiles.wiring.appImportStep,
189+
isReleased,
190+
options,
191+
privateKeyPath,
192+
});
187193
if (run.stoppedCode !== undefined) {
188194
throw new ReportedFailureError(run.stoppedCode, run.stoppedStep);
189195
}
@@ -294,11 +300,13 @@ function printRun(
294300
run: InitRun,
295301
{
296302
app,
303+
appImportStep,
297304
isReleased,
298305
options,
299306
privateKeyPath,
300307
}: {
301308
app: App | undefined;
309+
appImportStep: string | undefined;
302310
isReleased: boolean;
303311
options: InitOptions;
304312
privateKeyPath: string | undefined;
@@ -312,6 +320,9 @@ function printRun(
312320
// A blank line separates the rows from the package manager and build output above them.
313321
console.log('');
314322
printOutcomeRows(resolveStepRows(run.steps));
323+
if (appImportStep !== undefined) {
324+
printNotice(appImportStep);
325+
}
315326
if (app !== undefined && status === 'complete') {
316327
printFinish(app, { isReleased, privateKeyPath });
317328
}

‎src/frameworks/capacitor.ts‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { existsSync, readFileSync } from 'node:fs';
2-
import { dirname, join, relative, sep } from 'node:path';
1+
import { existsSync, readdirSync, readFileSync } from 'node:fs';
2+
import { dirname, extname, join, relative, sep } from 'node:path';
33
import {
44
CAPACITOR_PACKAGE_NAME,
55
CAPACITOR_PACKAGE_SPEC,
@@ -33,6 +33,7 @@ import {
3333
} from './xcode-project.js';
3434
import type {
3535
BuildDirectory,
36+
FrameworkCheck,
3637
FrameworkModule,
3738
FrameworkProject,
3839
FrameworkWiring,
@@ -64,6 +65,23 @@ const CAPACITOR_CONFIG_FILE_NAMES = [
6465
'capacitor.config.ts',
6566
];
6667

68+
// The import tells the SDK the app rendered. Without it, the SDK rolls every update back.
69+
const SDK_IMPORT_PATTERN = new RegExp(`['"]${CAPACITOR_PACKAGE_NAME}['"]`);
70+
71+
const SDK_IMPORT_STEP = `Add "import '${CAPACITOR_PACKAGE_NAME}';" to the app's entry file, such as src/main.ts.`;
72+
73+
const WEB_SOURCE_DIRECTORY_NAME = 'src';
74+
75+
const WEB_SOURCE_FILE_EXTENSIONS = new Set([
76+
'.js',
77+
'.jsx',
78+
'.mjs',
79+
'.svelte',
80+
'.ts',
81+
'.tsx',
82+
'.vue',
83+
]);
84+
6785
export const capacitorFramework: FrameworkModule = {
6886
detectionPackageName: '@capacitor/core',
6987
nativeGluePaths: NATIVE_GLUE_PATHS,
@@ -75,6 +93,7 @@ export const capacitorFramework: FrameworkModule = {
7593
nativeProjectPaths: resolveNativeProjectPaths(project.directoryPath),
7694
packageName: CAPACITOR_PACKAGE_NAME,
7795
}),
96+
checkSdkImport(project.directoryPath),
7897
],
7998
hasConfigFile: projectDirectoryPath =>
8099
CAPACITOR_CONFIG_FILE_NAMES.some(fileName =>
@@ -84,6 +103,49 @@ export const capacitorFramework: FrameworkModule = {
84103
resolveWiring,
85104
};
86105

106+
function checkSdkImport(projectDirectoryPath: string): FrameworkCheck {
107+
const sourceDirectoryPath = join(
108+
projectDirectoryPath,
109+
WEB_SOURCE_DIRECTORY_NAME,
110+
);
111+
if (!existsSync(sourceDirectoryPath)) {
112+
return {
113+
check: 'import',
114+
message: `There is no ${WEB_SOURCE_DIRECTORY_NAME}/ directory to look for the import in.`,
115+
status: 'skipped',
116+
};
117+
}
118+
return isSdkImported(sourceDirectoryPath)
119+
? {
120+
check: 'import',
121+
message: `The app's code imports ${CAPACITOR_PACKAGE_NAME}.`,
122+
status: 'ok',
123+
}
124+
: {
125+
check: 'import',
126+
code: 'import_missing',
127+
manualStep: SDK_IMPORT_STEP,
128+
message: `No file under ${WEB_SOURCE_DIRECTORY_NAME}/ imports ${CAPACITOR_PACKAGE_NAME}, so the app confirms no update.`,
129+
status: 'failed',
130+
};
131+
}
132+
133+
function isSdkImported(sourceDirectoryPath: string): boolean {
134+
return readdirSync(sourceDirectoryPath, {
135+
recursive: true,
136+
withFileTypes: true,
137+
})
138+
.filter(
139+
entry =>
140+
entry.isFile() && WEB_SOURCE_FILE_EXTENSIONS.has(extname(entry.name)),
141+
)
142+
.some(entry =>
143+
SDK_IMPORT_PATTERN.test(
144+
readFileSync(join(entry.parentPath, entry.name), 'utf8'),
145+
),
146+
);
147+
}
148+
87149
function readCapacitorConfig(
88150
projectDirectoryPath: string,
89151
): CapacitorConfig | undefined {
@@ -251,6 +313,7 @@ async function resolveWiring(
251313
CAPACITOR_PACKAGE_NAME,
252314
);
253315
return {
316+
appImportStep: checkSdkImport(directoryPath).manualStep,
254317
isPackageInstalled,
255318
nativeFilePaths: resolveNativeFilePaths(located),
256319
packageFilePaths: isPackageInstalled ? [] : ['package.json'],

‎src/frameworks/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export interface FrameworkProject {
6060
}
6161

6262
export interface FrameworkWiring {
63+
// The import the app's code lacks, which `init` names last, since it never edits the person's source files.
64+
appImportStep?: string;
6365
isPackageInstalled: boolean;
6466
nativeFilePaths: string[];
6567
packageFilePaths: string[];

‎test/capacitor-project.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export const CAPACITOR_APP_GRADLE_FILE_PATH = join(
3939
'build.gradle',
4040
);
4141

42+
export const CAPACITOR_ENTRY_FILE_PATH = join('src', 'main.ts');
43+
4244
export const CAPACITOR_XCODE_PROJECT_FILE_PATH = join(
4345
'ios',
4446
'App',
@@ -77,6 +79,11 @@ export function writeCapacitorProject({
7779
}
7880
mkdirSync(join(directoryPath, webDir));
7981
writeFileSync(join(directoryPath, webDir, 'index.html'), '<h1>v1</h1>');
82+
mkdirSync(join(directoryPath, 'src'));
83+
writeFileSync(
84+
join(directoryPath, CAPACITOR_ENTRY_FILE_PATH),
85+
"import '@hotcodepush/capacitor-live-updates';\n",
86+
);
8087
const pbxprojPath = join(directoryPath, CAPACITOR_XCODE_PROJECT_FILE_PATH);
8188
mkdirSync(dirname(pbxprojPath), { recursive: true });
8289
writeFileSync(pbxprojPath, readFileSync(PBXPROJ_FIXTURE_PATH));

0 commit comments

Comments
 (0)