Skip to content

Commit c19a7db

Browse files
committed
fix(e2e): invoke hdiff commands directly
1 parent 40018c7 commit c19a7db

1 file changed

Lines changed: 72 additions & 11 deletions

File tree

Example/e2etest/scripts/prepare-local-update-artifacts.ts

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ import {
99
LOCAL_UPDATE_LABELS,
1010
} from '../e2e/localUpdateConfig';
1111

12+
type DiffCommandRunner = {
13+
hdiff: (options: {
14+
args: [string, string];
15+
options: {
16+
output: string;
17+
customDiff: (oldSource?: Buffer, newSource?: Buffer) => Buffer;
18+
'no-interactive': true;
19+
};
20+
}) => Promise<void>;
21+
hdiffFromApk: (options: {
22+
args: [string, string];
23+
options: {
24+
output: string;
25+
customDiff: (oldSource?: Buffer, newSource?: Buffer) => Buffer;
26+
'no-interactive': true;
27+
};
28+
}) => Promise<void>;
29+
};
30+
1231
const projectRoot = process.cwd();
1332
const platform = process.env.E2E_PLATFORM || 'ios';
1433
const artifactsRoot = path.join(projectRoot, '.e2e-artifacts');
@@ -57,6 +76,10 @@ if (!fs.existsSync(cliEntry)) {
5776
throw new Error(`react-native-update-cli entry not found: ${cliEntry}`);
5877
}
5978

79+
const { diffCommands } = require(path.join(cliRoot, 'lib/exports.js')) as {
80+
diffCommands: DiffCommandRunner;
81+
};
82+
6083
function runPushy(args: string[], cwd: string) {
6184
const cliNodeModules = path.join(cliRoot, 'node_modules');
6285
const projectNodeModules = path.join(projectRoot, 'node_modules');
@@ -116,6 +139,16 @@ function ensureHdiffModule() {
116139
if (!fs.existsSync(modulePath)) {
117140
throw new Error(`Failed to install node-hdiffpatch under: ${projectRoot}`);
118141
}
142+
const hdiffModule = require(modulePath) as {
143+
diff?: (oldSource?: Buffer, newSource?: Buffer) => Buffer;
144+
} & ((oldSource?: Buffer, newSource?: Buffer) => Buffer);
145+
const customDiff = hdiffModule.diff || hdiffModule;
146+
if (typeof customDiff !== 'function') {
147+
throw new Error(
148+
`node-hdiffpatch did not expose a diff function: ${modulePath}`,
149+
);
150+
}
151+
return customDiff;
119152
}
120153

121154
function prepareDir() {
@@ -151,22 +184,49 @@ function verifyGeneratedFile(label: string, filePath: string) {
151184
);
152185
}
153186

154-
function generatePpkDiff(origin: string, next: string, output: string) {
155-
runPushy(
156-
['hdiff', origin, next, '--output', output, '--no-interactive'],
157-
projectRoot,
187+
async function keepProcessAlive<T>(promise: Promise<T>) {
188+
const timer = setInterval(() => {}, 1000);
189+
try {
190+
return await promise;
191+
} finally {
192+
clearInterval(timer);
193+
}
194+
}
195+
196+
async function generatePpkDiff(
197+
origin: string,
198+
next: string,
199+
output: string,
200+
customDiff: (oldSource?: Buffer, newSource?: Buffer) => Buffer,
201+
) {
202+
await keepProcessAlive(
203+
diffCommands.hdiff({
204+
args: [origin, next],
205+
options: {
206+
output,
207+
customDiff,
208+
'no-interactive': true,
209+
},
210+
}),
158211
);
159212
verifyGeneratedFile('ppk diff', output);
160213
}
161214

162-
function generateAndroidPackageDiff(
215+
async function generateAndroidPackageDiff(
163216
apkPath: string,
164217
next: string,
165218
output: string,
219+
customDiff: (oldSource?: Buffer, newSource?: Buffer) => Buffer,
166220
) {
167-
runPushy(
168-
['hdiffFromApk', apkPath, next, '--output', output, '--no-interactive'],
169-
projectRoot,
221+
await keepProcessAlive(
222+
diffCommands.hdiffFromApk({
223+
args: [apkPath, next],
224+
options: {
225+
output,
226+
customDiff,
227+
'no-interactive': true,
228+
},
229+
}),
170230
);
171231
verifyGeneratedFile('package diff', output);
172232
}
@@ -183,10 +243,10 @@ async function main() {
183243
bundleTo('e2e/entry.v2.ts', v2);
184244
bundleTo('e2e/entry.v3.ts', v3);
185245

186-
ensureHdiffModule();
246+
const customDiff = ensureHdiffModule();
187247

188248
console.log('Generating ppk diff...');
189-
generatePpkDiff(v1, v2, ppkDiff);
249+
await generatePpkDiff(v1, v2, ppkDiff, customDiff);
190250

191251
if (platform === 'android') {
192252
const apkPath = path.join(
@@ -206,10 +266,11 @@ async function main() {
206266
artifactsDir,
207267
LOCAL_UPDATE_FILES.packageDiff,
208268
);
209-
generateAndroidPackageDiff(
269+
await generateAndroidPackageDiff(
210270
apkPath,
211271
v3,
212272
packageDiffPath,
273+
customDiff,
213274
);
214275
}
215276

0 commit comments

Comments
 (0)