Skip to content

Commit 6e4e894

Browse files
authored
Merge pull request #5 from bozaigao/master
适配鸿蒙
2 parents 6534379 + e018761 commit 6e4e894

File tree

3 files changed

+155
-2802
lines changed

3 files changed

+155
-2802
lines changed

cli.json

+19-8
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
"defaultCommand": "help",
44
"commands": {
55
"help": {},
6-
76
"login": {},
87
"logout": {},
98
"me": {},
10-
119
"createApp": {
1210
"options": {
1311
"name": {
@@ -33,7 +31,6 @@
3331
}
3432
}
3533
},
36-
3734
"uploadIpa": {},
3835
"uploadApk": {},
3936
"parseIpa": {},
@@ -45,7 +42,6 @@
4542
}
4643
}
4744
},
48-
4945
"publish": {
5046
"options": {
5147
"platform": {
@@ -69,7 +65,6 @@
6965
}
7066
}
7167
},
72-
7368
"update": {
7469
"options": {
7570
"platform": {
@@ -95,7 +90,6 @@
9590
}
9691
}
9792
},
98-
9993
"updateVersionInfo": {
10094
"options": {
10195
"platform": {
@@ -118,7 +112,6 @@
118112
}
119113
}
120114
},
121-
122115
"build": {
123116
"description": "Bundle javascript and copy assets."
124117
},
@@ -201,6 +194,24 @@
201194
}
202195
}
203196
},
197+
"hdiffFromPPK": {
198+
"description": "Create hdiff patch from a Prepare package(.ppk)",
199+
"options": {
200+
"output": {
201+
"default": ".pushy/output/hdiff-${time}.ppk-patch",
202+
"hasValue": true
203+
}
204+
}
205+
},
206+
"hdiffFromApp": {
207+
"description": "Create hdiff patch from a Harmony package(.app)",
208+
"options": {
209+
"output": {
210+
"default": ".pushy/output/hdiff-${time}.app-patch",
211+
"hasValue": true
212+
}
213+
}
214+
},
204215
"hdiffFromIpa": {
205216
"description": "Create hdiff patch from a iOS package(.ipa)",
206217
"options": {
@@ -216,4 +227,4 @@
216227
"default": false
217228
}
218229
}
219-
}
230+
}

src/bundle.js

+136-33
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { spawn, spawnSync } from 'node:child_process';
99
const g2js = require('gradle-to-js/lib/parser');
1010
import os from 'os';
1111
const properties = require('properties');
12+
const path = require('path');
1213

1314
let bsdiff, hdiff, diff;
1415
try {
@@ -72,32 +73,51 @@ async function runReactNativeBundleCommand(
7273
});
7374
}
7475
}
75-
const bundleCommand = usingExpo ? 'export:embed' : 'bundle';
76-
77-
Array.prototype.push.apply(reactNativeBundleArgs, [
78-
cliPath,
79-
bundleCommand,
80-
'--assets-dest',
81-
outputFolder,
82-
'--bundle-output',
83-
path.join(outputFolder, bundleName),
84-
'--dev',
85-
development,
86-
'--entry-file',
87-
entryFile,
88-
'--platform',
89-
platform,
90-
'--reset-cache',
91-
]);
76+
const bundleCommand = usingExpo ? 'export:embed' : platform === 'harmony' ? 'bundle-harmony' : 'bundle';
77+
if (platform == 'harmony') {
78+
Array.prototype.push.apply(reactNativeBundleArgs, [
79+
cliPath,
80+
bundleCommand,
81+
'--dev',
82+
development,
83+
'--entry-file',
84+
entryFile,
85+
]);
9286

93-
if (sourcemapOutput) {
94-
reactNativeBundleArgs.push('--sourcemap-output', sourcemapOutput);
95-
}
87+
if (sourcemapOutput) {
88+
reactNativeBundleArgs.push('--sourcemap-output', sourcemapOutput);
89+
}
9690

97-
if (config) {
98-
reactNativeBundleArgs.push('--config', config);
91+
if (config) {
92+
reactNativeBundleArgs.push('--config', config);
93+
}
9994
}
100-
95+
else{
96+
Array.prototype.push.apply(reactNativeBundleArgs, [
97+
cliPath,
98+
bundleCommand,
99+
'--assets-dest',
100+
outputFolder,
101+
'--bundle-output',
102+
path.join(outputFolder, bundleName),
103+
'--dev',
104+
development,
105+
'--entry-file',
106+
entryFile,
107+
'--platform',
108+
platform,
109+
'--reset-cache',
110+
]);
111+
112+
if (sourcemapOutput) {
113+
reactNativeBundleArgs.push('--sourcemap-output', sourcemapOutput);
114+
}
115+
116+
if (config) {
117+
reactNativeBundleArgs.push('--config', config);
118+
}
119+
}
120+
101121
const reactNativeBundleProcess = spawn('node', reactNativeBundleArgs);
102122
console.log(
103123
`Running bundle command: node ${reactNativeBundleArgs.join(' ')}`,
@@ -146,6 +166,8 @@ async function runReactNativeBundleCommand(
146166
fs.existsSync('ios/Pods/hermes-engine')
147167
) {
148168
hermesEnabled = true;
169+
}else if (platform === 'harmony') {
170+
await copyHarmonyBundle(outputFolder);
149171
}
150172
if (hermesEnabled) {
151173
await compileHermesByteCode(
@@ -160,6 +182,21 @@ async function runReactNativeBundleCommand(
160182
});
161183
}
162184

185+
async function copyHarmonyBundle(outputFolder) {
186+
const harmonyRawPath = 'harmony/entry/src/main/resources/rawfile';
187+
188+
try {
189+
await fs.ensureDir(outputFolder);
190+
await fs.copy(harmonyRawPath, outputFolder);
191+
192+
console.log(
193+
`Successfully copied from ${harmonyRawPath} to ${outputFolder}`,
194+
);
195+
} catch (error) {
196+
console.error('Error in copyHarmonyBundle:', error);
197+
}
198+
}
199+
163200
function getHermesOSBin() {
164201
if (os.platform() === 'win32') return 'win64-bin';
165202
if (os.platform() === 'darwin') return 'osx-bin';
@@ -335,7 +372,7 @@ async function diffFromPPK(origin, next, output) {
335372
// isFile
336373
originMap[entry.crc32] = entry.fileName;
337374

338-
if (entry.fileName === 'index.bundlejs') {
375+
if (entry.fileName === 'index.bundlejs' || entry.fileName === 'bundle.harmony.js') {
339376
// This is source.
340377
return readEntire(entry, zipFile).then((v) => (originSource = v));
341378
}
@@ -397,6 +434,16 @@ async function diffFromPPK(origin, next, output) {
397434
);
398435
//console.log('End diff');
399436
});
437+
}else if (entry.fileName === 'bundle.harmony.js') {
438+
//console.log('Found bundle');
439+
return readEntire(entry, nextZipfile).then((newSource) => {
440+
//console.log('Begin diff');
441+
zipfile.addBuffer(
442+
diff(originSource, newSource),
443+
'bundle.harmony.js.patch',
444+
);
445+
//console.log('End diff');
446+
});
400447
} else {
401448
// If same file.
402449
const originEntry = originEntries[entry.fileName];
@@ -519,7 +566,17 @@ async function diffFromPackage(
519566
);
520567
//console.log('End diff');
521568
});
522-
} else {
569+
} else if (entry.fileName === 'bundle.harmony.js') {
570+
//console.log('Found bundle');
571+
return readEntire(entry, nextZipfile).then((newSource) => {
572+
//console.log('Begin diff');
573+
zipfile.addBuffer(
574+
diff(originSource, newSource),
575+
'bundle.harmony.js.patch',
576+
);
577+
//console.log('End diff');
578+
});
579+
}else {
523580
// If same file.
524581
if (originEntries[entry.fileName] === entry.crc32) {
525582
copies[entry.fileName] = '';
@@ -551,22 +608,53 @@ async function diffFromPackage(
551608
await writePromise;
552609
}
553610

554-
function enumZipEntries(zipFn, callback) {
611+
async function enumZipEntries(zipFn, callback, nestedPath = '') {
555612
return new Promise((resolve, reject) => {
556-
openZipFile(zipFn, { lazyEntries: true }, (err, zipfile) => {
613+
openZipFile(zipFn, { lazyEntries: true }, async (err, zipfile) => {
557614
if (err) {
558615
return reject(err);
559616
}
617+
560618
zipfile.on('end', resolve);
561619
zipfile.on('error', reject);
562-
zipfile.on('entry', (entry) => {
563-
const result = callback(entry, zipfile);
564-
if (result && typeof result.then === 'function') {
565-
result.then(() => zipfile.readEntry());
566-
} else {
567-
zipfile.readEntry();
620+
zipfile.on('entry', async (entry) => {
621+
const fullPath = nestedPath + entry.fileName;
622+
623+
try {
624+
if (
625+
!entry.fileName.endsWith('/') &&
626+
entry.fileName.toLowerCase().endsWith('.hap')
627+
) {
628+
const tempDir = path.join(os.tmpdir(), 'nested_zip_' + Date.now());
629+
await fs.ensureDir(tempDir);
630+
const tempZipPath = path.join(tempDir, 'temp.zip');
631+
632+
await new Promise((res, rej) => {
633+
zipfile.openReadStream(entry, async (err, readStream) => {
634+
if (err) return rej(err);
635+
const writeStream = fs.createWriteStream(tempZipPath);
636+
readStream.pipe(writeStream);
637+
writeStream.on('finish', res);
638+
writeStream.on('error', rej);
639+
});
640+
});
641+
642+
await enumZipEntries(tempZipPath, callback, fullPath + '/');
643+
644+
await fs.remove(tempDir);
645+
}
646+
647+
const result = callback(entry, zipfile, fullPath);
648+
if (result && typeof result.then === 'function') {
649+
await result;
650+
}
651+
} catch (error) {
652+
console.error('处理文件时出错:', error);
568653
}
654+
655+
zipfile.readEntry();
569656
});
657+
570658
zipfile.readEntry();
571659
});
572660
});
@@ -700,6 +788,21 @@ export const commands = {
700788
console.log(`${realOutput} generated.`);
701789
},
702790

791+
async hdiffFromApp({ args, options }) {
792+
const { origin, next, realOutput } = diffArgsCheck(
793+
args,
794+
options,
795+
'hdiffFromApp',
796+
);
797+
await diffFromPackage(
798+
origin,
799+
next,
800+
realOutput,
801+
'resources/rawfile/bundle.harmony.js',
802+
);
803+
console.log(`${realOutput} generated.`);
804+
},
805+
703806
async diffFromIpa({ args, options }) {
704807
const { origin, next, realOutput } = diffArgsCheck(
705808
args,

0 commit comments

Comments
 (0)