react-native-update-cli 2.19.0 → 2.20.1
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/lib/api.js +4 -1
- package/lib/diff.js +219 -67
- package/lib/native-package.d.ts +17 -0
- package/lib/native-package.js +232 -0
- package/lib/package.js +26 -17
- package/lib/utils/zip-entries.d.ts +1 -0
- package/lib/utils/zip-entries.js +18 -0
- package/package.json +3 -2
- package/src/api.ts +4 -1
- package/src/diff.ts +325 -88
- package/src/native-package.ts +248 -0
- package/src/package.ts +29 -19
- package/src/utils/zip-entries.ts +22 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import * as fs from 'fs-extra';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import {
|
|
5
|
+
type Entry,
|
|
6
|
+
open as openZipFile,
|
|
7
|
+
type ZipFile as YauzlZipFile,
|
|
8
|
+
} from 'yauzl';
|
|
9
|
+
import { ZipFile as YazlZipFile } from 'yazl';
|
|
10
|
+
import type { Platform } from './types';
|
|
11
|
+
|
|
12
|
+
export type NativePackageEntry = {
|
|
13
|
+
diffPath: string;
|
|
14
|
+
kind: 'bundle' | 'resource';
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type NativePackageRule = {
|
|
18
|
+
resolveEntry: (entryName: string) => NativePackageEntry | undefined;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const nativePackageRules: Record<Platform, NativePackageRule> = {
|
|
22
|
+
android: {
|
|
23
|
+
resolveEntry: (entryName) => {
|
|
24
|
+
if (entryName === 'assets/index.android.bundle') {
|
|
25
|
+
return { diffPath: entryName, kind: 'bundle' };
|
|
26
|
+
}
|
|
27
|
+
if (entryName.startsWith('assets/') || entryName.startsWith('res/')) {
|
|
28
|
+
return { diffPath: entryName, kind: 'resource' };
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
ios: {
|
|
33
|
+
resolveEntry: (entryName) => {
|
|
34
|
+
const match = /^Payload\/[^/]+\.app\/(.+)$/.exec(entryName);
|
|
35
|
+
const appPath = match?.[1];
|
|
36
|
+
if (appPath === 'main.jsbundle') {
|
|
37
|
+
return { diffPath: appPath, kind: 'bundle' };
|
|
38
|
+
}
|
|
39
|
+
if (appPath?.startsWith('assets/')) {
|
|
40
|
+
return { diffPath: appPath, kind: 'resource' };
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
harmony: {
|
|
45
|
+
resolveEntry: (entryName) => {
|
|
46
|
+
if (entryName === 'resources/rawfile/bundle.harmony.js') {
|
|
47
|
+
return { diffPath: entryName, kind: 'bundle' };
|
|
48
|
+
}
|
|
49
|
+
if (entryName.startsWith('resources/rawfile/assets/')) {
|
|
50
|
+
return { diffPath: entryName, kind: 'resource' };
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Resolve an archive entry that can be used as a native-package diff origin.
|
|
58
|
+
* Upload extraction and diff indexing deliberately share this function so a
|
|
59
|
+
* file cannot be removed from the uploaded baseline while still being
|
|
60
|
+
* considered by the local diff implementation (or vice versa).
|
|
61
|
+
*/
|
|
62
|
+
export function resolveNativePackageEntry(
|
|
63
|
+
platform: Platform,
|
|
64
|
+
entryName: string,
|
|
65
|
+
): NativePackageEntry | undefined {
|
|
66
|
+
return nativePackageRules[platform].resolveEntry(entryName);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function copyEntryToZip(
|
|
70
|
+
sourceZip: YauzlZipFile,
|
|
71
|
+
entry: Entry,
|
|
72
|
+
outputZip: YazlZipFile,
|
|
73
|
+
): Promise<void> {
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
sourceZip.openReadStream(entry, (error, readStream) => {
|
|
76
|
+
if (error) {
|
|
77
|
+
reject(error);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (!readStream) {
|
|
81
|
+
reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
readStream.once('error', reject);
|
|
86
|
+
readStream.once('end', () => resolve());
|
|
87
|
+
outputZip.addReadStream(readStream, entry.fileName, {
|
|
88
|
+
compress: entry.compressionMethod !== 0,
|
|
89
|
+
mtime: entry.getLastModDate(),
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function extractEntryToFile(
|
|
96
|
+
sourceZip: YauzlZipFile,
|
|
97
|
+
entry: Entry,
|
|
98
|
+
outputPath: string,
|
|
99
|
+
): Promise<void> {
|
|
100
|
+
return new Promise((resolve, reject) => {
|
|
101
|
+
sourceZip.openReadStream(entry, (error, readStream) => {
|
|
102
|
+
if (error) {
|
|
103
|
+
reject(error);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (!readStream) {
|
|
107
|
+
reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const writeStream = fs.createWriteStream(outputPath);
|
|
112
|
+
readStream.once('error', reject);
|
|
113
|
+
writeStream.once('error', reject);
|
|
114
|
+
writeStream.once('close', () => resolve());
|
|
115
|
+
readStream.pipe(writeStream);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function filterNativePackageArchive(
|
|
121
|
+
source: string,
|
|
122
|
+
output: string,
|
|
123
|
+
platform: Platform,
|
|
124
|
+
): Promise<{ bundles: number; entries: number }> {
|
|
125
|
+
await fs.ensureDir(path.dirname(output));
|
|
126
|
+
const scratchDir = await fs.mkdtemp(
|
|
127
|
+
path.join(os.tmpdir(), 'rnu-native-package-entry-'),
|
|
128
|
+
);
|
|
129
|
+
const outputZip = new YazlZipFile();
|
|
130
|
+
const writePromise = new Promise<void>((resolve, reject) => {
|
|
131
|
+
outputZip.outputStream.once('error', reject);
|
|
132
|
+
outputZip.outputStream
|
|
133
|
+
.pipe(fs.createWriteStream(output))
|
|
134
|
+
.once('error', reject)
|
|
135
|
+
.once('close', () => resolve());
|
|
136
|
+
});
|
|
137
|
+
let includedEntries = 0;
|
|
138
|
+
let includedBundles = 0;
|
|
139
|
+
let nestedArchiveIndex = 0;
|
|
140
|
+
|
|
141
|
+
try {
|
|
142
|
+
await new Promise<void>((resolve, reject) => {
|
|
143
|
+
openZipFile(source, { lazyEntries: true }, (openError, sourceZip) => {
|
|
144
|
+
if (openError || !sourceZip) {
|
|
145
|
+
reject(openError ?? new Error(`Unable to open zip file: ${source}`));
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
let settled = false;
|
|
150
|
+
const fail = (error: unknown) => {
|
|
151
|
+
if (settled) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
settled = true;
|
|
155
|
+
sourceZip.close();
|
|
156
|
+
reject(error);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
sourceZip.once('error', fail);
|
|
160
|
+
sourceZip.once('end', () => {
|
|
161
|
+
if (!settled) {
|
|
162
|
+
settled = true;
|
|
163
|
+
resolve();
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
sourceZip.on('entry', async (entry) => {
|
|
167
|
+
try {
|
|
168
|
+
if (entry.fileName.endsWith('/')) {
|
|
169
|
+
sourceZip.readEntry();
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Harmony .app packages contain one or more nested .hap archives.
|
|
174
|
+
// Rebuild those archives recursively and keep their outer path so
|
|
175
|
+
// the slim package has the same nesting as the original package.
|
|
176
|
+
if (
|
|
177
|
+
platform === 'harmony' &&
|
|
178
|
+
entry.fileName.toLowerCase().endsWith('.hap')
|
|
179
|
+
) {
|
|
180
|
+
const index = nestedArchiveIndex++;
|
|
181
|
+
const nestedSource = path.join(scratchDir, `${index}.source.hap`);
|
|
182
|
+
const nestedOutput = path.join(scratchDir, `${index}.slim.hap`);
|
|
183
|
+
await extractEntryToFile(sourceZip, entry, nestedSource);
|
|
184
|
+
const nestedResult = await filterNativePackageArchive(
|
|
185
|
+
nestedSource,
|
|
186
|
+
nestedOutput,
|
|
187
|
+
platform,
|
|
188
|
+
);
|
|
189
|
+
if (nestedResult.entries > 0) {
|
|
190
|
+
outputZip.addFile(nestedOutput, entry.fileName, {
|
|
191
|
+
compress: entry.compressionMethod !== 0,
|
|
192
|
+
mtime: entry.getLastModDate(),
|
|
193
|
+
});
|
|
194
|
+
includedEntries += nestedResult.entries;
|
|
195
|
+
includedBundles += nestedResult.bundles;
|
|
196
|
+
}
|
|
197
|
+
sourceZip.readEntry();
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const resolvedEntry = resolveNativePackageEntry(
|
|
202
|
+
platform,
|
|
203
|
+
entry.fileName,
|
|
204
|
+
);
|
|
205
|
+
if (resolvedEntry) {
|
|
206
|
+
await copyEntryToZip(sourceZip, entry, outputZip);
|
|
207
|
+
includedEntries += 1;
|
|
208
|
+
if (resolvedEntry.kind === 'bundle') {
|
|
209
|
+
includedBundles += 1;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
sourceZip.readEntry();
|
|
213
|
+
} catch (error) {
|
|
214
|
+
fail(error);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
sourceZip.readEntry();
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
outputZip.end();
|
|
223
|
+
await writePromise;
|
|
224
|
+
return { bundles: includedBundles, entries: includedEntries };
|
|
225
|
+
} catch (error) {
|
|
226
|
+
outputZip.end();
|
|
227
|
+
await writePromise.catch(() => {});
|
|
228
|
+
throw error;
|
|
229
|
+
} finally {
|
|
230
|
+
await fs.remove(scratchDir);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Repack a native package with only the bundle and resources required by
|
|
236
|
+
* package-origin diffs. Entry names and nested Harmony HAP structure are kept.
|
|
237
|
+
*/
|
|
238
|
+
export async function createSlimNativePackage(
|
|
239
|
+
source: string,
|
|
240
|
+
output: string,
|
|
241
|
+
platform: Platform,
|
|
242
|
+
): Promise<void> {
|
|
243
|
+
const result = await filterNativePackageArchive(source, output, platform);
|
|
244
|
+
if (result.bundles === 0) {
|
|
245
|
+
await fs.remove(output);
|
|
246
|
+
throw new Error(`Bundle entry not found in native package: ${source}`);
|
|
247
|
+
}
|
|
248
|
+
}
|
package/src/package.ts
CHANGED
|
@@ -4,6 +4,7 @@ import path from 'path';
|
|
|
4
4
|
import Table from 'tty-table';
|
|
5
5
|
import { doDelete, getAllPackages, post, uploadFile } from './api';
|
|
6
6
|
import { getPlatform, getSelectedApp } from './app';
|
|
7
|
+
import { createSlimNativePackage } from './native-package';
|
|
7
8
|
import type { Platform } from './types';
|
|
8
9
|
import {
|
|
9
10
|
getAabInfo,
|
|
@@ -141,26 +142,35 @@ async function uploadNativePackage(
|
|
|
141
142
|
console.log(t('usingCustomVersion', { version: versionName }));
|
|
142
143
|
}
|
|
143
144
|
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
? config.normalizeBuildTime(buildTime)
|
|
147
|
-
: buildTime;
|
|
148
|
-
const uploadBuildTime = normalizeUploadBuildTime(normalizedBuildTime);
|
|
149
|
-
|
|
150
|
-
const { id } = await post(`/app/${appId}/package/create`, {
|
|
151
|
-
name: versionName,
|
|
152
|
-
hash,
|
|
153
|
-
buildTime: uploadBuildTime,
|
|
154
|
-
deps: depVersions,
|
|
155
|
-
commit: await getCommitInfo(),
|
|
156
|
-
});
|
|
157
|
-
console.log(
|
|
158
|
-
t(config.successKey, {
|
|
159
|
-
id,
|
|
160
|
-
version: versionName,
|
|
161
|
-
buildTime: uploadBuildTime,
|
|
162
|
-
}),
|
|
145
|
+
const tempRoot = await fs.mkdtemp(
|
|
146
|
+
path.join(os.tmpdir(), 'rnu-native-package-upload-'),
|
|
163
147
|
);
|
|
148
|
+
const slimPackagePath = path.join(tempRoot, `package${config.extension}`);
|
|
149
|
+
try {
|
|
150
|
+
await createSlimNativePackage(filePath, slimPackagePath, config.platform);
|
|
151
|
+
const { hash } = await uploadFile(slimPackagePath, undefined, appId);
|
|
152
|
+
const normalizedBuildTime = config.normalizeBuildTime
|
|
153
|
+
? config.normalizeBuildTime(buildTime)
|
|
154
|
+
: buildTime;
|
|
155
|
+
const uploadBuildTime = normalizeUploadBuildTime(normalizedBuildTime);
|
|
156
|
+
|
|
157
|
+
const { id } = await post(`/app/${appId}/package/create`, {
|
|
158
|
+
name: versionName,
|
|
159
|
+
hash,
|
|
160
|
+
buildTime: uploadBuildTime,
|
|
161
|
+
deps: depVersions,
|
|
162
|
+
commit: await getCommitInfo(),
|
|
163
|
+
});
|
|
164
|
+
console.log(
|
|
165
|
+
t(config.successKey, {
|
|
166
|
+
id,
|
|
167
|
+
version: versionName,
|
|
168
|
+
buildTime: uploadBuildTime,
|
|
169
|
+
}),
|
|
170
|
+
);
|
|
171
|
+
} finally {
|
|
172
|
+
await fs.remove(tempRoot);
|
|
173
|
+
}
|
|
164
174
|
}
|
|
165
175
|
|
|
166
176
|
export async function listPackage(appId: string) {
|
package/src/utils/zip-entries.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as fs from 'fs-extra';
|
|
2
2
|
import os from 'os';
|
|
3
3
|
import path from 'path';
|
|
4
|
+
import { pipeline } from 'stream/promises';
|
|
4
5
|
import {
|
|
5
6
|
type Entry,
|
|
6
7
|
open as openZipFile,
|
|
@@ -34,6 +35,27 @@ export function readEntry(
|
|
|
34
35
|
});
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
export function writeEntry(
|
|
39
|
+
entry: Entry,
|
|
40
|
+
zipFile: YauzlZipFile,
|
|
41
|
+
outputPath: string,
|
|
42
|
+
): Promise<void> {
|
|
43
|
+
fs.ensureDirSync(path.dirname(outputPath));
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
zipFile.openReadStream(entry, (err, stream) => {
|
|
46
|
+
if (err) {
|
|
47
|
+
reject(err);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (!stream) {
|
|
51
|
+
reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
pipeline(stream, fs.createWriteStream(outputPath)).then(resolve, reject);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
37
59
|
export function readEntryPrefix(
|
|
38
60
|
entry: Entry,
|
|
39
61
|
zipFile: YauzlZipFile,
|