react-native-update-cli 2.8.0 → 2.8.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/src/bundle.ts CHANGED
@@ -2,48 +2,19 @@ import { spawn, spawnSync } from 'child_process';
2
2
  import path from 'path';
3
3
  import { satisfies } from 'compare-versions';
4
4
  import * as fs from 'fs-extra';
5
- import {
6
- type Entry,
7
- type ZipFile as YauzlZipFile,
8
- open as openZipFile,
9
- } from 'yauzl';
10
5
  import { ZipFile as YazlZipFile } from 'yazl';
11
6
  import { getPlatform } from './app';
12
7
  import { translateOptions } from './utils';
13
8
  import { checkPlugins, question } from './utils';
14
9
  const g2js = require('gradle-to-js/lib/parser');
15
- import os from 'os';
16
- import { npm, yarn } from 'global-dirs';
17
10
  const properties = require('properties');
18
11
  import { addGitIgnore } from './utils/add-gitignore';
19
12
  import { checkLockFiles } from './utils/check-lockfile';
20
- import { isPPKBundleFileName, scriptName, tempDir } from './utils/constants';
13
+ import { tempDir } from './utils/constants';
21
14
  import { depVersions } from './utils/dep-versions';
22
15
  import { t } from './utils/i18n';
23
16
  import { versionCommands } from './versions';
24
17
 
25
- type Diff = (oldSource?: Buffer, newSource?: Buffer) => Buffer;
26
-
27
- const loadDiffModule = (pkgName: string): Diff | undefined => {
28
- const resolvePaths = [process.cwd(), npm.packages, yarn.packages];
29
-
30
- try {
31
- const resolved = require.resolve(pkgName, { paths: resolvePaths });
32
- const mod = require(resolved);
33
- if (mod?.diff) {
34
- return mod.diff as Diff;
35
- }
36
- } catch {}
37
-
38
- return undefined;
39
- };
40
-
41
- let hdiff: Diff | undefined;
42
- hdiff = (loadDiffModule('node-hdiffpatch') as any)?.diff;
43
- let bsdiff: Diff | undefined;
44
- let diff: Diff;
45
- bsdiff = loadDiffModule('node-bsdiff');
46
-
47
18
  async function runReactNativeBundleCommand({
48
19
  bundleName,
49
20
  dev,
@@ -537,353 +508,6 @@ async function pack(dir: string, output: string) {
537
508
  console.log(t('fileGenerated', { file: output }));
538
509
  }
539
510
 
540
- export function readEntry(
541
- entry: Entry,
542
- zipFile: YauzlZipFile,
543
- ): Promise<Buffer> {
544
- const buffers: Buffer[] = [];
545
- return new Promise((resolve, reject) => {
546
- zipFile.openReadStream(entry, (err, stream) => {
547
- stream.on('data', (chunk: Buffer) => {
548
- buffers.push(chunk);
549
- });
550
- stream.on('end', () => {
551
- resolve(Buffer.concat(buffers));
552
- });
553
- stream.on('error', (err) => {
554
- reject(err);
555
- });
556
- });
557
- });
558
- }
559
-
560
- function basename(fn: string) {
561
- const m = /^(.+\/)[^\/]+\/?$/.exec(fn);
562
- return m?.[1];
563
- }
564
-
565
- async function diffFromPPK(origin: string, next: string, output: string) {
566
- fs.ensureDirSync(path.dirname(output));
567
-
568
- const originEntries = {};
569
- const originMap = {};
570
-
571
- let originSource: Buffer | undefined;
572
-
573
- await enumZipEntries(origin, (entry, zipFile) => {
574
- originEntries[entry.fileName] = entry;
575
- if (!/\/$/.test(entry.fileName)) {
576
- // isFile
577
- originMap[entry.crc32] = entry.fileName;
578
-
579
- if (isPPKBundleFileName(entry.fileName)) {
580
- // This is source.
581
- return readEntry(entry, zipFile).then((v) => (originSource = v));
582
- }
583
- }
584
- });
585
-
586
- if (!originSource) {
587
- throw new Error(t('bundleFileNotFound'));
588
- }
589
-
590
- const copies = {};
591
-
592
- const zipfile = new YazlZipFile();
593
-
594
- const writePromise = new Promise((resolve, reject) => {
595
- zipfile.outputStream.on('error', (err) => {
596
- throw err;
597
- });
598
- zipfile.outputStream.pipe(fs.createWriteStream(output)).on('close', () => {
599
- resolve(void 0);
600
- });
601
- });
602
-
603
- const addedEntry = {};
604
-
605
- function addEntry(fn: string) {
606
- //console.log(fn);
607
- if (!fn || addedEntry[fn]) {
608
- return;
609
- }
610
- const base = basename(fn);
611
- if (base) {
612
- addEntry(base);
613
- }
614
- zipfile.addEmptyDirectory(fn);
615
- }
616
-
617
- const newEntries = {};
618
-
619
- await enumZipEntries(next, (entry, nextZipfile) => {
620
- newEntries[entry.fileName] = entry;
621
-
622
- if (/\/$/.test(entry.fileName)) {
623
- // Directory
624
- if (!originEntries[entry.fileName]) {
625
- addEntry(entry.fileName);
626
- }
627
- } else if (isPPKBundleFileName(entry.fileName)) {
628
- //console.log('Found bundle');
629
- return readEntry(entry, nextZipfile).then((newSource) => {
630
- //console.log('Begin diff');
631
- zipfile.addBuffer(
632
- diff(originSource, newSource),
633
- `${entry.fileName}.patch`,
634
- );
635
- //console.log('End diff');
636
- });
637
- } else {
638
- // If same file.
639
- const originEntry = originEntries[entry.fileName];
640
- if (originEntry && originEntry.crc32 === entry.crc32) {
641
- // ignore
642
- return;
643
- }
644
-
645
- // If moved from other place
646
- if (originMap[entry.crc32]) {
647
- const base = basename(entry.fileName);
648
- if (!originEntries[base]) {
649
- addEntry(base);
650
- }
651
- copies[entry.fileName] = originMap[entry.crc32];
652
- return;
653
- }
654
-
655
- // New file.
656
- addEntry(basename(entry.fileName));
657
-
658
- return new Promise((resolve, reject) => {
659
- nextZipfile.openReadStream(entry, (err, readStream) => {
660
- if (err) {
661
- return reject(err);
662
- }
663
- zipfile.addReadStream(readStream, entry.fileName);
664
- readStream.on('end', () => {
665
- //console.log('add finished');
666
- resolve(void 0);
667
- });
668
- });
669
- });
670
- }
671
- });
672
-
673
- const deletes = {};
674
-
675
- for (const k in originEntries) {
676
- if (!newEntries[k]) {
677
- console.log(t('deleteFile', { file: k }));
678
- deletes[k] = 1;
679
- }
680
- }
681
-
682
- //console.log({copies, deletes});
683
- zipfile.addBuffer(
684
- Buffer.from(JSON.stringify({ copies, deletes })),
685
- '__diff.json',
686
- );
687
- zipfile.end();
688
- await writePromise;
689
- }
690
-
691
- async function diffFromPackage(
692
- origin: string,
693
- next: string,
694
- output: string,
695
- originBundleName: string,
696
- transformPackagePath = (v: string) => v,
697
- ) {
698
- fs.ensureDirSync(path.dirname(output));
699
-
700
- const originEntries = {};
701
- const originMap = {};
702
-
703
- let originSource: Buffer | undefined;
704
-
705
- await enumZipEntries(origin, (entry, zipFile) => {
706
- if (!/\/$/.test(entry.fileName)) {
707
- const fn = transformPackagePath(entry.fileName);
708
- if (!fn) {
709
- return;
710
- }
711
-
712
- //console.log(fn);
713
- // isFile
714
- originEntries[fn] = entry.crc32;
715
- originMap[entry.crc32] = fn;
716
-
717
- if (fn === originBundleName) {
718
- // This is source.
719
- return readEntry(entry, zipFile).then((v) => (originSource = v));
720
- }
721
- }
722
- });
723
-
724
- if (!originSource) {
725
- throw new Error(t('bundleFileNotFound'));
726
- }
727
-
728
- const copies = {};
729
-
730
- const zipfile = new YazlZipFile();
731
-
732
- const writePromise = new Promise((resolve, reject) => {
733
- zipfile.outputStream.on('error', (err) => {
734
- throw err;
735
- });
736
- zipfile.outputStream.pipe(fs.createWriteStream(output)).on('close', () => {
737
- resolve(void 0);
738
- });
739
- });
740
-
741
- await enumZipEntries(next, (entry, nextZipfile) => {
742
- if (/\/$/.test(entry.fileName)) {
743
- // Directory
744
- zipfile.addEmptyDirectory(entry.fileName);
745
- } else if (isPPKBundleFileName(entry.fileName)) {
746
- //console.log('Found bundle');
747
- return readEntry(entry, nextZipfile).then((newSource) => {
748
- //console.log('Begin diff');
749
- zipfile.addBuffer(
750
- diff(originSource, newSource),
751
- `${entry.fileName}.patch`,
752
- );
753
- //console.log('End diff');
754
- });
755
- } else {
756
- // If same file.
757
- if (originEntries[entry.fileName] === entry.crc32) {
758
- copies[entry.fileName] = '';
759
- return;
760
- }
761
- // If moved from other place
762
- if (originMap[entry.crc32]) {
763
- copies[entry.fileName] = originMap[entry.crc32];
764
- return;
765
- }
766
-
767
- return new Promise((resolve, reject) => {
768
- nextZipfile.openReadStream(entry, (err, readStream) => {
769
- if (err) {
770
- return reject(err);
771
- }
772
- zipfile.addReadStream(readStream, entry.fileName);
773
- readStream.on('end', () => {
774
- //console.log('add finished');
775
- resolve(void 0);
776
- });
777
- });
778
- });
779
- }
780
- });
781
-
782
- zipfile.addBuffer(Buffer.from(JSON.stringify({ copies })), '__diff.json');
783
- zipfile.end();
784
- await writePromise;
785
- }
786
-
787
- export async function enumZipEntries(
788
- zipFn: string,
789
- callback: (
790
- entry: Entry,
791
- zipFile: YauzlZipFile,
792
- nestedPath?: string,
793
- ) => Promise<any>,
794
- nestedPath = '',
795
- ) {
796
- return new Promise((resolve, reject) => {
797
- openZipFile(
798
- zipFn,
799
- { lazyEntries: true },
800
- async (err: any, zipfile: YauzlZipFile) => {
801
- if (err) {
802
- return reject(err);
803
- }
804
-
805
- zipfile.on('end', resolve);
806
- zipfile.on('error', reject);
807
- zipfile.on('entry', async (entry) => {
808
- const fullPath = nestedPath + entry.fileName;
809
-
810
- try {
811
- if (
812
- !entry.fileName.endsWith('/') &&
813
- entry.fileName.toLowerCase().endsWith('.hap')
814
- ) {
815
- const tempDir = path.join(
816
- os.tmpdir(),
817
- `nested_zip_${Date.now()}`,
818
- );
819
- await fs.ensureDir(tempDir);
820
- const tempZipPath = path.join(tempDir, 'temp.zip');
821
-
822
- await new Promise((res, rej) => {
823
- zipfile.openReadStream(entry, async (err, readStream) => {
824
- if (err) return rej(err);
825
- const writeStream = fs.createWriteStream(tempZipPath);
826
- readStream.pipe(writeStream);
827
- writeStream.on('finish', () => res(void 0));
828
- writeStream.on('error', rej);
829
- });
830
- });
831
-
832
- await enumZipEntries(tempZipPath, callback, `${fullPath}/`);
833
-
834
- await fs.remove(tempDir);
835
- }
836
-
837
- const result = callback(entry, zipfile, fullPath);
838
- if (result && typeof result.then === 'function') {
839
- await result;
840
- }
841
- } catch (error) {
842
- console.error(t('processingError', { error }));
843
- }
844
-
845
- zipfile.readEntry();
846
- });
847
-
848
- zipfile.readEntry();
849
- },
850
- );
851
- });
852
- }
853
-
854
- function diffArgsCheck(args: string[], options: any, diffFn: string) {
855
- const [origin, next] = args;
856
-
857
- if (!origin || !next) {
858
- console.error(t('usageDiff', { command: diffFn }));
859
- process.exit(1);
860
- }
861
-
862
- if (diffFn.startsWith('hdiff')) {
863
- if (!hdiff) {
864
- console.error(t('nodeHdiffpatchRequired', { scriptName }));
865
- process.exit(1);
866
- }
867
- diff = hdiff;
868
- } else {
869
- if (!bsdiff) {
870
- console.error(t('nodeBsdiffRequired', { scriptName }));
871
- process.exit(1);
872
- }
873
- diff = bsdiff;
874
- }
875
- const { output } = translateOptions({
876
- ...options,
877
- tempDir,
878
- });
879
-
880
- return {
881
- origin,
882
- next,
883
- realOutput: output.replace(/\$\{time\}/g, `${Date.now()}`),
884
- };
885
- }
886
-
887
511
  export const bundleCommands = {
888
512
  bundle: async ({ options }) => {
889
513
  const platform = await getPlatform(options.platform);
@@ -1001,110 +625,4 @@ export const bundleCommands = {
1001
625
  }
1002
626
  }
1003
627
  },
1004
-
1005
- async diff({ args, options }) {
1006
- const { origin, next, realOutput } = diffArgsCheck(args, options, 'diff');
1007
-
1008
- await diffFromPPK(origin, next, realOutput);
1009
- console.log(t('diffPackageGenerated', { output: realOutput }));
1010
- },
1011
-
1012
- async hdiff({ args, options }) {
1013
- const { origin, next, realOutput } = diffArgsCheck(args, options, 'hdiff');
1014
-
1015
- await diffFromPPK(origin, next, realOutput);
1016
- console.log(t('diffPackageGenerated', { output: realOutput }));
1017
- },
1018
-
1019
- async diffFromApk({ args, options }) {
1020
- const { origin, next, realOutput } = diffArgsCheck(
1021
- args,
1022
- options,
1023
- 'diffFromApk',
1024
- );
1025
-
1026
- await diffFromPackage(
1027
- origin,
1028
- next,
1029
- realOutput,
1030
- 'assets/index.android.bundle',
1031
- );
1032
- console.log(t('diffPackageGenerated', { output: realOutput }));
1033
- },
1034
-
1035
- async hdiffFromApk({ args, options }) {
1036
- const { origin, next, realOutput } = diffArgsCheck(
1037
- args,
1038
- options,
1039
- 'hdiffFromApk',
1040
- );
1041
-
1042
- await diffFromPackage(
1043
- origin,
1044
- next,
1045
- realOutput,
1046
- 'assets/index.android.bundle',
1047
- );
1048
- console.log(t('diffPackageGenerated', { output: realOutput }));
1049
- },
1050
-
1051
- async diffFromApp({ args, options }) {
1052
- const { origin, next, realOutput } = diffArgsCheck(
1053
- args,
1054
- options,
1055
- 'diffFromApp',
1056
- );
1057
- await diffFromPackage(
1058
- origin,
1059
- next,
1060
- realOutput,
1061
- 'resources/rawfile/bundle.harmony.js',
1062
- );
1063
- console.log(t('diffPackageGenerated', { output: realOutput }));
1064
- },
1065
-
1066
- async hdiffFromApp({ args, options }) {
1067
- const { origin, next, realOutput } = diffArgsCheck(
1068
- args,
1069
- options,
1070
- 'hdiffFromApp',
1071
- );
1072
- await diffFromPackage(
1073
- origin,
1074
- next,
1075
- realOutput,
1076
- 'resources/rawfile/bundle.harmony.js',
1077
- );
1078
- console.log(t('diffPackageGenerated', { output: realOutput }));
1079
- },
1080
-
1081
- async diffFromIpa({ args, options }) {
1082
- const { origin, next, realOutput } = diffArgsCheck(
1083
- args,
1084
- options,
1085
- 'diffFromIpa',
1086
- );
1087
-
1088
- await diffFromPackage(origin, next, realOutput, 'main.jsbundle', (v) => {
1089
- const m = /^Payload\/[^/]+\/(.+)$/.exec(v);
1090
- return m?.[1];
1091
- });
1092
-
1093
- console.log(t('diffPackageGenerated', { output: realOutput }));
1094
- },
1095
-
1096
- async hdiffFromIpa({ args, options }) {
1097
- const { origin, next, realOutput } = diffArgsCheck(
1098
- args,
1099
- options,
1100
- 'hdiffFromIpa',
1101
- );
1102
-
1103
- await diffFromPackage(origin, next, realOutput, 'main.jsbundle', (v) => {
1104
- const m = /^Payload\/[^/]+\/(.+)$/.exec(v);
1105
- return m?.[1];
1106
- });
1107
-
1108
- console.log(t('diffPackageGenerated', { output: realOutput }));
1109
- },
1110
628
  };