@react-native-oh/react-native-harmony-cli 0.0.15 → 0.0.23

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.
Files changed (85) hide show
  1. package/.prettierrc +6 -6
  2. package/dist/assetResolver.js +23 -5
  3. package/dist/codegen/core/Codegen.js +35 -0
  4. package/dist/codegen/core/CodegenError.js +7 -0
  5. package/dist/codegen/core/EventUtils.js +19 -0
  6. package/dist/codegen/core/TypeAnnotationToTS.js +132 -0
  7. package/dist/codegen/core/UberSchema.js +74 -0
  8. package/dist/codegen/core/index.js +21 -0
  9. package/dist/codegen/generators/ComponentCodeGenerator.js +66 -0
  10. package/dist/codegen/generators/NativeModuleCodeGenerator.js +64 -0
  11. package/dist/codegen/generators/PackageCodeGenerator.js +52 -0
  12. package/dist/codegen/generators/index.js +19 -0
  13. package/dist/codegen/index.js +18 -0
  14. package/dist/codegen/templates/ComponentDescriptorHTemplate.js +55 -0
  15. package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +78 -0
  16. package/dist/codegen/templates/ComponentUtilsTSTemplate.js +181 -0
  17. package/dist/codegen/templates/IndexTSTemplate.js +31 -0
  18. package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +117 -0
  19. package/dist/codegen/templates/TurboModuleCppTemplate.js +45 -0
  20. package/dist/codegen/templates/TurboModuleHTemplate.js +32 -0
  21. package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +64 -0
  22. package/dist/codegen/templates/index.js +24 -0
  23. package/dist/commands/codegen-harmony.js +110 -0
  24. package/dist/commands/create-metro-host-constants.js +61 -0
  25. package/dist/commands/index.js +2 -0
  26. package/dist/commands/pack-harmony.js +4 -1
  27. package/dist/commands/unpack-harmony.js +86 -43
  28. package/dist/core/AbsolutePath.js +41 -0
  29. package/dist/core/CodegenConfig.js +53 -0
  30. package/dist/core/DescriptiveError.js +49 -0
  31. package/dist/core/PackageJSON.js +53 -0
  32. package/dist/core/ProjectDependenciesManager.js +70 -0
  33. package/dist/core/ValueObject.js +2 -0
  34. package/dist/core/fs.js +36 -0
  35. package/dist/core/index.js +7 -0
  36. package/dist/index.js +7 -1
  37. package/dist/io/Logger.js +71 -0
  38. package/dist/io/fsUtils.js +23 -0
  39. package/dist/io/index.js +18 -0
  40. package/jest.config.js +5 -5
  41. package/package.json +52 -47
  42. package/react-native.config.js +10 -10
  43. package/src/assetResolver.ts +44 -25
  44. package/src/codegen/core/Codegen.ts +44 -0
  45. package/src/codegen/core/CodegenError.ts +3 -0
  46. package/src/codegen/core/EventUtils.ts +18 -0
  47. package/src/codegen/core/TypeAnnotationToTS.test.ts +75 -0
  48. package/src/codegen/core/TypeAnnotationToTS.ts +188 -0
  49. package/src/codegen/core/UberSchema.ts +99 -0
  50. package/src/codegen/core/index.ts +5 -0
  51. package/src/codegen/generators/ComponentCodeGenerator.ts +98 -0
  52. package/src/codegen/generators/NativeModuleCodeGenerator.ts +95 -0
  53. package/src/codegen/generators/PackageCodeGenerator.ts +73 -0
  54. package/src/codegen/generators/index.ts +3 -0
  55. package/src/codegen/index.ts +2 -0
  56. package/src/codegen/templates/ComponentDescriptorHTemplate.ts +49 -0
  57. package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +85 -0
  58. package/src/codegen/templates/ComponentUtilsTSTemplate.ts +202 -0
  59. package/src/codegen/templates/IndexTSTemplate.ts +33 -0
  60. package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +124 -0
  61. package/src/codegen/templates/TurboModuleCppTemplate.ts +47 -0
  62. package/src/codegen/templates/TurboModuleHTemplate.ts +26 -0
  63. package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +81 -0
  64. package/src/codegen/templates/index.ts +8 -0
  65. package/src/commands/bundle-harmony.ts +202 -202
  66. package/src/commands/codegen-harmony.ts +143 -0
  67. package/src/commands/create-metro-host-constants.ts +48 -0
  68. package/src/commands/index.ts +5 -3
  69. package/src/commands/pack-harmony.ts +121 -118
  70. package/src/commands/unpack-harmony.ts +231 -156
  71. package/src/core/AbsolutePath.ts +43 -0
  72. package/src/core/CodegenConfig.ts +54 -0
  73. package/src/core/DescriptiveError.ts +61 -0
  74. package/src/core/PackageJSON.ts +63 -0
  75. package/src/core/ProjectDependenciesManager.ts +77 -0
  76. package/src/core/ValidationError.ts +1 -1
  77. package/src/core/ValueObject.ts +2 -0
  78. package/src/core/fs.ts +31 -0
  79. package/src/core/index.ts +8 -1
  80. package/src/index.ts +18 -10
  81. package/src/io/Logger.ts +68 -0
  82. package/src/io/fsUtils.ts +24 -0
  83. package/src/io/index.ts +2 -0
  84. package/tsconfig.build.json +6 -5
  85. package/tsconfig.json +2 -2
@@ -1,118 +1,121 @@
1
- import { Command } from '@react-native-community/cli-types';
2
- import { globSync } from 'glob';
3
- import pathUtils from 'path';
4
- import tar from 'tar';
5
- import fs from 'fs';
6
- import { ValidationError } from '../core';
7
- import ignore, { Ignore } from 'ignore';
8
- import JSON5 from 'json5';
9
-
10
- export const commandPackHarmony: Command = {
11
- name: 'pack-harmony',
12
- description: 'Pack native code. Used for package development.',
13
- options: [
14
- {
15
- name: '--oh-module-path <string>',
16
- description: 'Path to the OH module',
17
- },
18
- {
19
- name: '--harmony-dir-path <string>',
20
- description: 'Path to the harmony dir',
21
- },
22
- {
23
- name: '--package-json-path <string>',
24
- description: 'Path to the package.json',
25
- },
26
- ],
27
- func: async (argv, config, args: any) => {
28
- try {
29
- await pack(
30
- pathUtils.resolve(args.harmonyDirPath),
31
- pathUtils.resolve(args.ohModulePath),
32
- pathUtils.resolve(args.packageJsonPath)
33
- );
34
- } catch (err) {
35
- if (err instanceof ValidationError) {
36
- console.error('[ERROR]', err.message);
37
- process.exit(1);
38
- } else {
39
- throw err;
40
- }
41
- }
42
- },
43
- };
44
-
45
- async function pack(
46
- harmonyDirPath: string,
47
- ohModuleDirPath: string,
48
- packageJSONPath: string
49
- ) {
50
- syncVersions(
51
- packageJSONPath,
52
- pathUtils.join(ohModuleDirPath, 'oh-package.json5')
53
- );
54
- await createTGZFromDir(
55
- ohModuleDirPath,
56
- pathUtils.join(
57
- harmonyDirPath,
58
- `${pathUtils.parse(ohModuleDirPath).name}.tar.gz`
59
- )
60
- );
61
- await createTGZFromDir(
62
- harmonyDirPath,
63
- pathUtils.join(pathUtils.dirname(harmonyDirPath), 'harmony.tar.gz')
64
- );
65
- }
66
-
67
- function syncVersions(
68
- packageJSONFilePath: string,
69
- ohPackageJSONFilePath: string
70
- ) {
71
- const packageJSONFileContent = JSON.parse(
72
- fs.readFileSync(packageJSONFilePath, { encoding: 'utf-8' })
73
- );
74
- if (fs.existsSync(ohPackageJSONFilePath)) {
75
- const ohPackageJSONFileContent = JSON5.parse(
76
- fs.readFileSync(ohPackageJSONFilePath, { encoding: 'utf-8' })
77
- );
78
- ohPackageJSONFileContent['version'] = packageJSONFileContent['version'];
79
- fs.writeFileSync(
80
- ohPackageJSONFilePath,
81
- JSON5.stringify(ohPackageJSONFileContent)
82
- );
83
- }
84
- }
85
-
86
- async function createTGZFromDir(dirPath: string, outputFilePath: string) {
87
- const ig = getIgnoreUtil(pathUtils.join(dirPath, '.tarignore'));
88
- const filesToInclude = globSync('**', {
89
- cwd: dirPath,
90
- nodir: true,
91
- }).filter((path) => !ig.ignores(path));
92
- await createArchive(outputFilePath, dirPath, filesToInclude);
93
- }
94
-
95
- function getIgnoreUtil(tarignorePath: string): Ignore {
96
- if (!fs.existsSync(tarignorePath)) {
97
- return ignore();
98
- }
99
- return ignore().add(fs.readFileSync(tarignorePath).toString());
100
- }
101
-
102
- async function createArchive(
103
- outputFilePath: string,
104
- packageDirPath: string,
105
- filesToInclude: string[]
106
- ) {
107
- if (filesToInclude.length === 0) {
108
- throw new ValidationError('Nothing to pack');
109
- }
110
- await tar.c(
111
- {
112
- gzip: true,
113
- file: outputFilePath,
114
- cwd: packageDirPath,
115
- },
116
- filesToInclude
117
- );
118
- }
1
+ import { Command } from '@react-native-community/cli-types';
2
+ import { globSync } from 'glob';
3
+ import pathUtils from 'path';
4
+ import tar from 'tar';
5
+ import fs from 'fs';
6
+ import { ValidationError } from '../core';
7
+ import ignore, { Ignore } from 'ignore';
8
+ import JSON5 from 'json5';
9
+
10
+ export const commandPackHarmony: Command = {
11
+ name: 'pack-harmony',
12
+ description: 'Pack native code. Used for package development.',
13
+ options: [
14
+ {
15
+ name: '--oh-module-path <string>',
16
+ description: 'Path to the OH module',
17
+ },
18
+ {
19
+ name: '--harmony-dir-path <string>',
20
+ description: 'Path to the harmony dir',
21
+ },
22
+ {
23
+ name: '--package-json-path <string>',
24
+ description: 'Path to the package.json',
25
+ },
26
+ ],
27
+ func: async (argv, config, args: any) => {
28
+ try {
29
+ await pack(
30
+ pathUtils.resolve(args.harmonyDirPath),
31
+ pathUtils.resolve(args.ohModulePath),
32
+ pathUtils.resolve(args.packageJsonPath)
33
+ );
34
+ } catch (err) {
35
+ if (err instanceof ValidationError) {
36
+ console.error('[ERROR]', err.message);
37
+ process.exit(1);
38
+ } else {
39
+ throw err;
40
+ }
41
+ }
42
+ },
43
+ };
44
+
45
+ async function pack(
46
+ harmonyDirPath: string,
47
+ ohModuleDirPath: string,
48
+ packageJSONPath: string
49
+ ) {
50
+ syncVersions(
51
+ packageJSONPath,
52
+ pathUtils.join(ohModuleDirPath, 'oh-package.json5')
53
+ );
54
+ if (!fs.existsSync(harmonyDirPath)) {
55
+ fs.mkdirSync(harmonyDirPath, { recursive: true });
56
+ }
57
+ await createTGZFromDir(
58
+ ohModuleDirPath,
59
+ pathUtils.join(
60
+ harmonyDirPath,
61
+ `${pathUtils.parse(ohModuleDirPath).name}.tar.gz`
62
+ )
63
+ );
64
+ await createTGZFromDir(
65
+ harmonyDirPath,
66
+ pathUtils.join(pathUtils.dirname(harmonyDirPath), 'harmony.tar.gz')
67
+ );
68
+ }
69
+
70
+ function syncVersions(
71
+ packageJSONFilePath: string,
72
+ ohPackageJSONFilePath: string
73
+ ) {
74
+ const packageJSONFileContent = JSON.parse(
75
+ fs.readFileSync(packageJSONFilePath, { encoding: 'utf-8' })
76
+ );
77
+ if (fs.existsSync(ohPackageJSONFilePath)) {
78
+ const ohPackageJSONFileContent = JSON5.parse(
79
+ fs.readFileSync(ohPackageJSONFilePath, { encoding: 'utf-8' })
80
+ );
81
+ ohPackageJSONFileContent['version'] = packageJSONFileContent['version'];
82
+ fs.writeFileSync(
83
+ ohPackageJSONFilePath,
84
+ JSON5.stringify(ohPackageJSONFileContent, null, 2)
85
+ );
86
+ }
87
+ }
88
+
89
+ async function createTGZFromDir(dirPath: string, outputFilePath: string) {
90
+ const ig = getIgnoreUtil(pathUtils.join(dirPath, '.tarignore'));
91
+ const filesToInclude = globSync('**', {
92
+ cwd: dirPath,
93
+ nodir: true,
94
+ }).filter((path) => !ig.ignores(path));
95
+ await createArchive(outputFilePath, dirPath, filesToInclude);
96
+ }
97
+
98
+ function getIgnoreUtil(tarignorePath: string): Ignore {
99
+ if (!fs.existsSync(tarignorePath)) {
100
+ return ignore();
101
+ }
102
+ return ignore().add(fs.readFileSync(tarignorePath).toString());
103
+ }
104
+
105
+ async function createArchive(
106
+ outputFilePath: string,
107
+ packageDirPath: string,
108
+ filesToInclude: string[]
109
+ ) {
110
+ if (filesToInclude.length === 0) {
111
+ throw new ValidationError('Nothing to pack');
112
+ }
113
+ await tar.c(
114
+ {
115
+ gzip: true,
116
+ file: outputFilePath,
117
+ cwd: packageDirPath,
118
+ },
119
+ filesToInclude
120
+ );
121
+ }
@@ -1,156 +1,231 @@
1
- import { Command } from '@react-native-community/cli-types';
2
-
3
- import pathUtils from 'path';
4
- import fs from 'fs';
5
- import zlib from 'zlib';
6
- import tar from 'tar';
7
- import JSON5 from 'json5';
8
- import { ValidationError } from '../core';
9
-
10
- export const commandUnpackHarmony: Command = {
11
- name: 'unpack-harmony',
12
- description: 'Pack native code. Used for package development.',
13
- options: [
14
- {
15
- name: '--node-modules-path <string>',
16
- description: 'Path to node_modules directory',
17
- default: './node_modules',
18
- },
19
- {
20
- name: '--output-dir <string>',
21
- description: 'Output directory to which OH modules should be unpacked to',
22
- default: './harmony/react_native_modules',
23
- },
24
- ],
25
- func: async (argv, config, args: any) => {
26
- try {
27
- await unpack(
28
- pathUtils.resolve(args.nodeModulesPath),
29
- pathUtils.resolve(args.outputDir)
30
- );
31
- } catch (err) {
32
- if (err instanceof ValidationError) {
33
- console.error('[ERROR]', err.message);
34
- process.exit(1);
35
- } else {
36
- throw err;
37
- }
38
- }
39
- },
40
- };
41
-
42
- async function unpack(nodeModulesPath: string, outputDirPath: string) {
43
- const nodeModuleNames = fs.readdirSync(nodeModulesPath);
44
- for (const nodeModuleName of nodeModuleNames) {
45
- const nodeModulePath = pathUtils.join(nodeModulesPath, nodeModuleName);
46
- const nodePackageVersion = getNodePackageVersion(nodeModulePath);
47
- const harmonyArchivePath = pathUtils.join(nodeModulePath, 'harmony.tar.gz');
48
- const pkgHarmonyDirPath = pathUtils.join(nodeModulePath, 'harmony');
49
- const ohModulesInfo = getOhModulesInfo(pkgHarmonyDirPath, outputDirPath);
50
- if (
51
- nodePackageVersion &&
52
- shouldUnpackHarmonyArchive(
53
- harmonyArchivePath,
54
- pkgHarmonyDirPath,
55
- outputDirPath,
56
- nodePackageVersion
57
- )
58
- ) {
59
- console.log(`[UNPACKING] ${nodeModuleName}/harmony.tar.gz`);
60
- await unpackTarGz(harmonyArchivePath, pkgHarmonyDirPath);
61
- for (const ohModuleInfo of ohModulesInfo) {
62
- console.log(
63
- `[UNPACKING] ${nodeModuleName}/harmony/${ohModuleInfo.archiveFileName}`
64
- );
65
- await unpackTarGz(
66
- ohModuleInfo.archivePath,
67
- ohModuleInfo.computedModulePath
68
- );
69
- }
70
- }
71
- }
72
- }
73
-
74
- function shouldUnpackHarmonyArchive(
75
- harmonyArchivePath: string,
76
- pkgHarmonyDirPath: string,
77
- outputDirPath: string,
78
- nodePackageVersion: string
79
- ) {
80
- if (!fs.existsSync(harmonyArchivePath)) return false;
81
- if (!fs.existsSync(pkgHarmonyDirPath)) return true;
82
- const ohModulesInfo = getOhModulesInfo(pkgHarmonyDirPath, outputDirPath);
83
- for (const ohModuleInfo of ohModulesInfo) {
84
- if (
85
- !fs.existsSync(ohModuleInfo.computedModulePath) ||
86
- getOhPackageVersion(ohModuleInfo.computedModulePath) !==
87
- nodePackageVersion
88
- ) {
89
- return true;
90
- }
91
- }
92
- return false;
93
- }
94
-
95
- function getNodePackageVersion(nodeModulePath: string) {
96
- const packageJSONPath = pathUtils.join(nodeModulePath, 'package.json');
97
- if (!fs.existsSync(packageJSONPath)) return null;
98
- const content = fs.readFileSync(packageJSONPath, { encoding: 'utf-8' });
99
- return JSON.parse(content)['version'];
100
- }
101
-
102
- function getOhPackageVersion(modulePath: string) {
103
- const ohPackagePath = pathUtils.join(modulePath, 'oh-package.json5');
104
- if (!fs.existsSync(ohPackagePath)) return null;
105
- const content = fs.readFileSync(ohPackagePath, { encoding: 'utf-8' });
106
- return JSON5.parse(content)['version'];
107
- }
108
-
109
- function getOhModulesInfo(pkgHarmonyDirPath: string, outputDirPath: string) {
110
- const results: {
111
- archivePath: string;
112
- archiveFileName: string;
113
- computedModulePath: string;
114
- computedModuleName: string;
115
- }[] = [];
116
- if (!fs.existsSync(pkgHarmonyDirPath)) return [];
117
- const names = fs.readdirSync(pkgHarmonyDirPath);
118
- for (const name of names) {
119
- if (name.endsWith('.tar.gz')) {
120
- const archivePath = pathUtils.join(pkgHarmonyDirPath, name);
121
- const computedModuleName = pathUtils
122
- .parse(archivePath)
123
- .name.split('.')[0];
124
- const computedModulePath = pathUtils.join(
125
- outputDirPath,
126
- computedModuleName
127
- );
128
- results.push({
129
- archivePath,
130
- archiveFileName: name,
131
- computedModulePath,
132
- computedModuleName,
133
- });
134
- }
135
- }
136
- return results;
137
- }
138
-
139
- function unpackTarGz(archivePath: string, outputDirPath: string) {
140
- if (!fs.existsSync(outputDirPath)) {
141
- fs.mkdirSync(outputDirPath, { recursive: true });
142
- }
143
- const readStream = fs.createReadStream(archivePath);
144
- const writeStream = tar.extract({
145
- cwd: outputDirPath,
146
- });
147
- readStream.pipe(zlib.createGunzip()).pipe(writeStream);
148
- return new Promise((resolve, reject) => {
149
- writeStream.on('finish', () => {
150
- resolve(undefined);
151
- });
152
- writeStream.on('error', (err) => {
153
- reject(err);
154
- });
155
- });
156
- }
1
+ import { Command } from '@react-native-community/cli-types';
2
+ import { Logger } from '../io';
3
+
4
+ import pathUtils from 'path';
5
+ import fs from 'fs';
6
+ import zlib from 'zlib';
7
+ import tar from 'tar';
8
+ import JSON5 from 'json5';
9
+ import {
10
+ ValidationError,
11
+ AbsolutePath,
12
+ ProjectDependenciesManager,
13
+ } from '../core';
14
+
15
+ export const commandUnpackHarmony: Command = {
16
+ name: 'unpack-harmony',
17
+ description: 'Unpacks native code.',
18
+ options: [
19
+ {
20
+ name: '--project-root-path <path>',
21
+ description: 'Relative path to package root',
22
+ default: '.',
23
+ },
24
+ {
25
+ name: '--output-dir <string>',
26
+ description: 'Output directory to which OH modules should be unpacked to',
27
+ default: './harmony/rnoh_modules',
28
+ },
29
+ ],
30
+ func: async (argv, config, args: any) => {
31
+ try {
32
+ await unpack(
33
+ new AbsolutePath(args.projectRootPath),
34
+ new AbsolutePath(args.outputDir)
35
+ );
36
+ } catch (err) {
37
+ if (err instanceof ValidationError) {
38
+ console.error('[ERROR]', err.message);
39
+ process.exit(1);
40
+ } else {
41
+ throw err;
42
+ }
43
+ }
44
+ },
45
+ };
46
+
47
+ async function unpack(
48
+ projectRootPath: AbsolutePath,
49
+ outputDirPath: AbsolutePath
50
+ ) {
51
+ const logger = new Logger();
52
+ const projectDependenciesManager = new ProjectDependenciesManager(
53
+ projectRootPath
54
+ );
55
+ logger.info((styles) => styles.bold(`Unpacking RNOH modules`), {
56
+ prefix: true,
57
+ });
58
+ let unpackedArchivesCount = 0;
59
+ let skippedArchivesCount = 0;
60
+ await projectDependenciesManager.forEachAsync(async (dependency) => {
61
+ const packageJSON = dependency.readPackageJSON();
62
+ const harmonyArchivePath = dependency
63
+ .getRootPath()
64
+ .copyWithNewSegment('harmony.tar.gz');
65
+ const pkgHarmonyDirPath = dependency
66
+ .getRootPath()
67
+ .copyWithNewSegment('harmony');
68
+ const harmonyTarGzPathStr = dependency
69
+ .getRootPath()
70
+ .copyWithNewSegment('harmony.tar.gz')
71
+ .relativeTo(projectRootPath)
72
+ .getValue();
73
+ if (
74
+ packageJSON.version &&
75
+ shouldUnpack(
76
+ harmonyArchivePath,
77
+ pkgHarmonyDirPath,
78
+ outputDirPath,
79
+ packageJSON.version
80
+ )
81
+ ) {
82
+ if (!fs.existsSync(pkgHarmonyDirPath.getValue())) {
83
+ /**
84
+ * After fresh install, harmony.tar.gz wasn't unpacked to harmony. Updating npm package should remove unpacked harmony dir.
85
+ */
86
+ logger.info(
87
+ (styles) =>
88
+ `[${styles.cyan('unpacking')}] ${styles.gray(harmonyTarGzPathStr)}`
89
+ );
90
+ await unpackTarGz(harmonyArchivePath, pkgHarmonyDirPath);
91
+ unpackedArchivesCount++;
92
+ } else {
93
+ skippedArchivesCount++;
94
+ logger.info(
95
+ (styles) =>
96
+ `[${styles.yellow('skipped')}] ${styles.gray(
97
+ harmonyTarGzPathStr
98
+ )}`
99
+ );
100
+ }
101
+ const ohModulesInfo = getOhModulesInfo(pkgHarmonyDirPath, outputDirPath);
102
+ for (const ohModuleInfo of ohModulesInfo) {
103
+ const moduleArchivePathStr = dependency
104
+ .getRootPath()
105
+ .copyWithNewSegment('harmony', ohModuleInfo.archiveFileName)
106
+ .relativeTo(projectRootPath)
107
+ .getValue();
108
+ logger.info(
109
+ (styles) =>
110
+ `[${styles.cyan('unpacking')}] ${styles.gray(moduleArchivePathStr)}`
111
+ );
112
+ unpackedArchivesCount++;
113
+ await unpackTarGz(
114
+ ohModuleInfo.archivePath,
115
+ ohModuleInfo.computedModulePath
116
+ );
117
+ }
118
+ } else {
119
+ if (fs.existsSync(harmonyArchivePath.getValue())) {
120
+ skippedArchivesCount++;
121
+ logger.info(
122
+ (styles) =>
123
+ `[${styles.yellow('skipped')}] ${styles.gray(
124
+ harmonyTarGzPathStr
125
+ )}`
126
+ );
127
+ }
128
+ const ohModulesInfo = getOhModulesInfo(pkgHarmonyDirPath, outputDirPath);
129
+ for (const ohModuleInfo of ohModulesInfo) {
130
+ const moduleArchivePathStr = dependency
131
+ .getRootPath()
132
+ .copyWithNewSegment('harmony', ohModuleInfo.archiveFileName)
133
+ .relativeTo(projectRootPath)
134
+ .getValue();
135
+ skippedArchivesCount++;
136
+ logger.info(
137
+ (styles) =>
138
+ `[${styles.yellow('skipped')}] ${styles.gray(
139
+ moduleArchivePathStr
140
+ )}`
141
+ );
142
+ }
143
+ }
144
+ });
145
+ logger.info(
146
+ (styles) =>
147
+ `\nUnpacked: ${styles.cyan(
148
+ unpackedArchivesCount
149
+ )}, Skipped: ${styles.yellow(skippedArchivesCount)}`
150
+ );
151
+ }
152
+
153
+ function shouldUnpack(
154
+ harmonyArchivePath: AbsolutePath,
155
+ pkgHarmonyDirPath: AbsolutePath,
156
+ outputDirPath: AbsolutePath,
157
+ nodePackageVersion: string
158
+ ) {
159
+ if (!fs.existsSync(harmonyArchivePath.getValue())) return false;
160
+ if (!fs.existsSync(pkgHarmonyDirPath.getValue())) return true;
161
+ const ohModulesInfo = getOhModulesInfo(pkgHarmonyDirPath, outputDirPath);
162
+ for (const ohModuleInfo of ohModulesInfo) {
163
+ if (
164
+ !fs.existsSync(ohModuleInfo.computedModulePath.getValue()) ||
165
+ getOhPackageVersion(ohModuleInfo.computedModulePath) !==
166
+ nodePackageVersion
167
+ ) {
168
+ return true;
169
+ }
170
+ }
171
+ return false;
172
+ }
173
+
174
+ function getOhPackageVersion(modulePath: AbsolutePath) {
175
+ const ohPackagePath = modulePath.copyWithNewSegment('oh-package.json5');
176
+ if (!fs.existsSync(ohPackagePath.getValue())) return null;
177
+ const content = fs.readFileSync(ohPackagePath.getValue(), {
178
+ encoding: 'utf-8',
179
+ });
180
+ return JSON5.parse(content)['version'];
181
+ }
182
+
183
+ function getOhModulesInfo(
184
+ pkgHarmonyDirPath: AbsolutePath,
185
+ outputDirPath: AbsolutePath
186
+ ) {
187
+ const results: {
188
+ archivePath: AbsolutePath;
189
+ archiveFileName: string;
190
+ computedModulePath: AbsolutePath;
191
+ computedModuleName: string;
192
+ }[] = [];
193
+ if (!fs.existsSync(pkgHarmonyDirPath.getValue())) return [];
194
+ const names = fs.readdirSync(pkgHarmonyDirPath.getValue());
195
+ for (const name of names) {
196
+ if (name.endsWith('.tar.gz')) {
197
+ const archivePath = pkgHarmonyDirPath.copyWithNewSegment(name);
198
+ const computedModuleName = pathUtils
199
+ .parse(archivePath.getValue())
200
+ .name.split('.')[0];
201
+ const computedModulePath =
202
+ outputDirPath.copyWithNewSegment(computedModuleName);
203
+ results.push({
204
+ archivePath,
205
+ archiveFileName: name,
206
+ computedModulePath,
207
+ computedModuleName,
208
+ });
209
+ }
210
+ }
211
+ return results;
212
+ }
213
+
214
+ function unpackTarGz(archivePath: AbsolutePath, outputDirPath: AbsolutePath) {
215
+ if (!fs.existsSync(outputDirPath.getValue())) {
216
+ fs.mkdirSync(outputDirPath.getValue(), { recursive: true });
217
+ }
218
+ const readStream = fs.createReadStream(archivePath.getValue());
219
+ const writeStream = tar.extract({
220
+ cwd: outputDirPath.getValue(),
221
+ });
222
+ readStream.pipe(zlib.createGunzip()).pipe(writeStream);
223
+ return new Promise((resolve, reject) => {
224
+ writeStream.on('finish', () => {
225
+ resolve(undefined);
226
+ });
227
+ writeStream.on('error', (err) => {
228
+ reject(err);
229
+ });
230
+ });
231
+ }