@react-native-oh/react-native-harmony-cli 0.0.25 → 0.0.26-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/.prettierrc +6 -6
- package/dist/assetResolver.js +1 -23
- package/dist/codegen/core/CodeGenerator.js +2 -0
- package/dist/codegen/core/UberSchema.js +35 -9
- package/dist/codegen/core/index.js +1 -1
- package/dist/codegen/generators/{ComponentCodeGenerator.js → ComponentCodeGeneratorArkTS.js} +12 -4
- package/dist/codegen/generators/ComponentCodeGeneratorCAPI.js +79 -0
- package/dist/codegen/generators/GlueCodeGenerator.js +60 -0
- package/dist/codegen/generators/NativeModuleCodeGenerator.js +14 -6
- package/dist/codegen/generators/UberGeneratorV1.js +49 -0
- package/dist/codegen/generators/UberGeneratorV2.js +60 -0
- package/dist/codegen/generators/index.js +5 -2
- package/dist/codegen/templates/BaseComponentInstanceHTemplate.js +103 -0
- package/dist/codegen/templates/ComponentDescriptorHTemplate.js +42 -36
- package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +45 -36
- package/dist/codegen/templates/ComponentUtilsTSTemplate.js +135 -127
- package/dist/codegen/templates/GeneratedPackageHTemplate.js +101 -0
- package/dist/codegen/templates/IndexTSTemplate.js +23 -5
- package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +88 -82
- package/dist/codegen/templates/TurboModuleCppTemplate.js +26 -18
- package/dist/codegen/templates/TurboModuleHTemplate.js +27 -17
- package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +37 -27
- package/dist/codegen/templates/index.js +1 -0
- package/dist/commands/codegen-harmony.js +99 -14
- package/dist/core/AbsolutePath.js +6 -0
- package/dist/core/CodegenConfig.js +8 -4
- package/dist/core/PackageJSON.js +8 -3
- package/dist/core/ProjectDependenciesManager.js +2 -2
- package/jest.config.js +5 -5
- package/package.json +57 -56
- package/react-native.config.js +10 -10
- package/src/assetResolver.ts +21 -44
- package/src/codegen/core/CodeGenerator.ts +9 -0
- package/src/codegen/core/CodegenError.ts +3 -3
- package/src/codegen/core/EventUtils.ts +18 -18
- package/src/codegen/core/TypeAnnotationToTS.test.ts +75 -75
- package/src/codegen/core/TypeAnnotationToTS.ts +188 -188
- package/src/codegen/core/UberSchema.ts +142 -99
- package/src/codegen/core/index.ts +5 -5
- package/src/codegen/generators/{ComponentCodeGenerator.ts → ComponentCodeGeneratorArkTS.ts} +112 -98
- package/src/codegen/generators/ComponentCodeGeneratorCAPI.ts +114 -0
- package/src/codegen/generators/GlueCodeGenerator.ts +109 -0
- package/src/codegen/generators/NativeModuleCodeGenerator.ts +115 -95
- package/src/codegen/generators/UberGeneratorV1.ts +64 -0
- package/src/codegen/generators/UberGeneratorV2.ts +91 -0
- package/src/codegen/generators/index.ts +6 -3
- package/src/codegen/index.ts +2 -2
- package/src/codegen/templates/BaseComponentInstanceHTemplate.ts +116 -0
- package/src/codegen/templates/ComponentDescriptorHTemplate.ts +55 -49
- package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +93 -85
- package/src/codegen/templates/ComponentUtilsTSTemplate.ts +210 -202
- package/src/codegen/templates/GeneratedPackageHTemplate.ts +107 -0
- package/src/codegen/templates/IndexTSTemplate.ts +53 -33
- package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +130 -124
- package/src/codegen/templates/TurboModuleCppTemplate.ts +54 -47
- package/src/codegen/templates/TurboModuleHTemplate.ts +35 -26
- package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +90 -81
- package/src/codegen/templates/index.ts +9 -8
- package/src/commands/bundle-harmony.ts +203 -203
- package/src/commands/codegen-harmony.ts +272 -143
- package/src/commands/create-metro-host-constants.ts +48 -48
- package/src/commands/index.ts +6 -6
- package/src/commands/pack-harmony.ts +121 -121
- package/src/commands/unpack-harmony.ts +231 -231
- package/src/commands/verify-package-harmony.ts +106 -106
- package/src/core/AbsolutePath.ts +50 -43
- package/src/core/CodegenConfig.ts +62 -54
- package/src/core/DescriptiveError.ts +61 -61
- package/src/core/OHPackageJSON5.ts +23 -23
- package/src/core/PackageJSON.ts +75 -63
- package/src/core/ProjectDependenciesManager.ts +77 -77
- package/src/core/ValidationError.ts +1 -1
- package/src/core/ValueObject.ts +2 -2
- package/src/core/fs.ts +31 -31
- package/src/core/index.ts +9 -9
- package/src/index.ts +20 -20
- package/src/io/Logger.ts +68 -68
- package/src/io/TarGz.ts +26 -26
- package/src/io/fsUtils.ts +24 -24
- package/src/io/index.ts +3 -3
- package/src/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.ts +28 -28
- package/src/package-verifier/checks/HarmonyDirExistsCheck.ts +24 -24
- package/src/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.ts +18 -18
- package/src/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.ts +47 -47
- package/src/package-verifier/checks/index.ts +4 -4
- package/src/package-verifier/index.ts +2 -2
- package/src/package-verifier/types.ts +12 -12
- package/src/package-verifier/verify-package.ts +117 -117
- package/tsconfig.build.json +6 -6
- package/tsconfig.json +2 -2
- package/dist/codegen/core/Codegen.js +0 -35
- package/dist/codegen/generators/PackageCodeGenerator.js +0 -52
- package/src/codegen/core/Codegen.ts +0 -44
- package/src/codegen/generators/PackageCodeGenerator.ts +0 -73
|
@@ -1,203 +1,203 @@
|
|
|
1
|
-
import { Command } from '@react-native-community/cli-types';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import fse from 'fs-extra';
|
|
4
|
-
import Metro from 'metro';
|
|
5
|
-
import { RunBuildOptions as BuildOptions } from 'metro';
|
|
6
|
-
import MetroServer from 'metro/src/Server';
|
|
7
|
-
import pathUtils from 'path';
|
|
8
|
-
import { getAssetDestRelativePath } from '../assetResolver';
|
|
9
|
-
import { ConfigT as MetroConfig } from 'metro-config';
|
|
10
|
-
|
|
11
|
-
const ARK_RESOURCE_PATH = './harmony/entry/src/main/resources/rawfile';
|
|
12
|
-
const ASSETS_DEFAULT_DEST_PATH =
|
|
13
|
-
'./harmony/entry/src/main/resources/rawfile/assets';
|
|
14
|
-
|
|
15
|
-
type AssetData = Metro.AssetData;
|
|
16
|
-
type Bundle = { code: string; map: string };
|
|
17
|
-
type Path = string;
|
|
18
|
-
|
|
19
|
-
export const commandBundleHarmony: Command = {
|
|
20
|
-
name: 'bundle-harmony',
|
|
21
|
-
description:
|
|
22
|
-
'Creates JS bundle, creates a special cpp header containing the JS code, copies assets directory to the project.',
|
|
23
|
-
options: [
|
|
24
|
-
{
|
|
25
|
-
name: '--dev [boolean]',
|
|
26
|
-
description: 'If false, warnings are disabled and the bundle is minified',
|
|
27
|
-
parse: (val: string) => val !== 'false',
|
|
28
|
-
default: true,
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
name: '--entry-file <path>',
|
|
32
|
-
description:
|
|
33
|
-
'Path to the root JS file, either absolute or relative to JS root',
|
|
34
|
-
default: 'index.js',
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
name: '--config <path>',
|
|
38
|
-
description: 'Path to the Metro configuration file',
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
name: '--bundle-output <path>',
|
|
42
|
-
description:
|
|
43
|
-
'File name where to store the resulting bundle, ex. /tmp/groups.bundle',
|
|
44
|
-
default: ARK_RESOURCE_PATH + '/bundle.harmony.js',
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
name: '--assets-dest <path>',
|
|
48
|
-
description:
|
|
49
|
-
'Directory name where to store assets referenced in the bundle',
|
|
50
|
-
default: ASSETS_DEFAULT_DEST_PATH,
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
name: '--sourcemap-output <path>',
|
|
54
|
-
description:
|
|
55
|
-
'File name where to store the resulting source map, ex. /tmp/groups.map',
|
|
56
|
-
},
|
|
57
|
-
{
|
|
58
|
-
name: '--minify [boolean]',
|
|
59
|
-
description: 'Allows overriding whether bundle is minified',
|
|
60
|
-
parse: (val: string) => val !== 'false',
|
|
61
|
-
},
|
|
62
|
-
],
|
|
63
|
-
func: async (argv, config, args: any) => {
|
|
64
|
-
const buildOptions: BuildOptions = {
|
|
65
|
-
entry: args.entryFile,
|
|
66
|
-
platform: 'harmony',
|
|
67
|
-
minify: args.minify !== undefined ? args.minify : !args.dev,
|
|
68
|
-
dev: args.dev,
|
|
69
|
-
sourceMap: args.sourcemapOutput,
|
|
70
|
-
sourceMapUrl: args.sourcemapOutput,
|
|
71
|
-
};
|
|
72
|
-
const metroConfig = await loadMetroConfig(args.config);
|
|
73
|
-
const bundle = await createBundle(metroConfig, buildOptions);
|
|
74
|
-
await saveBundle(bundle, args.bundleOutput, args.sourcemapOutput);
|
|
75
|
-
const assets = await retrieveAssetsData(metroConfig, buildOptions);
|
|
76
|
-
copyAssets(assets, args.assetsDest);
|
|
77
|
-
},
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
async function loadMetroConfig(
|
|
81
|
-
configPath: Path | undefined
|
|
82
|
-
): Promise<MetroConfig> {
|
|
83
|
-
return configPath
|
|
84
|
-
? await Metro.loadConfig({ config: configPath })
|
|
85
|
-
: await Metro.loadConfig();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async function createBundle(
|
|
89
|
-
metroConfig: MetroConfig,
|
|
90
|
-
buildOptions: BuildOptions
|
|
91
|
-
): Promise<Bundle> {
|
|
92
|
-
// casting needed as Metro.runBuild returns Promise<{code: string, map: string}>
|
|
93
|
-
// despite being typed as Promise<void>
|
|
94
|
-
return (await Metro.runBuild(metroConfig, buildOptions)) as unknown as Bundle;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async function saveBundle(
|
|
98
|
-
bundle: Bundle,
|
|
99
|
-
bundleOutput: Path,
|
|
100
|
-
sourceMapOutput: Path | undefined
|
|
101
|
-
) {
|
|
102
|
-
await fse.ensureDir(pathUtils.dirname(bundleOutput));
|
|
103
|
-
fs.writeFileSync(bundleOutput, bundle.code);
|
|
104
|
-
console.log(`[CREATED] ${bundleOutput}`);
|
|
105
|
-
if (sourceMapOutput) {
|
|
106
|
-
fs.writeFileSync(sourceMapOutput, bundle.map);
|
|
107
|
-
console.log(`[CREATED] ${sourceMapOutput}`);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
async function retrieveAssetsData(
|
|
112
|
-
metroConfig: MetroConfig,
|
|
113
|
-
buildOptions: BuildOptions
|
|
114
|
-
): Promise<readonly AssetData[]> {
|
|
115
|
-
const metroServer = new MetroServer(metroConfig);
|
|
116
|
-
try {
|
|
117
|
-
return await metroServer.getAssets({
|
|
118
|
-
...MetroServer.DEFAULT_BUNDLE_OPTIONS,
|
|
119
|
-
...buildOptions,
|
|
120
|
-
entryFile: buildOptions.entry,
|
|
121
|
-
bundleType: 'todo',
|
|
122
|
-
});
|
|
123
|
-
} finally {
|
|
124
|
-
metroServer.end();
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
async function copyAssets(
|
|
129
|
-
assetsData: readonly AssetData[],
|
|
130
|
-
assetsDest: Path
|
|
131
|
-
): Promise<void> {
|
|
132
|
-
if (assetsDest == null) {
|
|
133
|
-
console.warn('Assets destination folder is not set, skipping...');
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
await fse.ensureDir(assetsDest);
|
|
137
|
-
const fileDestBySrc: Record<Path, Path> = {};
|
|
138
|
-
for (const asset of assetsData) {
|
|
139
|
-
const idx = getHighestQualityFileIdx(asset);
|
|
140
|
-
fileDestBySrc[asset.files[idx]] = pathUtils.join(
|
|
141
|
-
assetsDest,
|
|
142
|
-
getAssetDestRelativePath(asset)
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
return copyFiles(fileDestBySrc);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
function getHighestQualityFileIdx(assetData: AssetData): number {
|
|
149
|
-
let result = 0;
|
|
150
|
-
let maxScale = -1;
|
|
151
|
-
for (let idx = 0; idx < assetData.scales.length; idx++) {
|
|
152
|
-
const scale = assetData.scales[idx];
|
|
153
|
-
if (scale > maxScale) {
|
|
154
|
-
maxScale = scale;
|
|
155
|
-
result = idx;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
return result;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function copyFiles(fileDestBySrc: Record<Path, Path>) {
|
|
162
|
-
const fileSources = Object.keys(fileDestBySrc);
|
|
163
|
-
if (fileSources.length === 0) {
|
|
164
|
-
return Promise.resolve();
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
console.info(`Copying ${fileSources.length} asset files`);
|
|
168
|
-
return new Promise<void>((resolve, reject) => {
|
|
169
|
-
const copyNext = (error?: Error) => {
|
|
170
|
-
if (error) {
|
|
171
|
-
reject(error);
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
if (fileSources.length === 0) {
|
|
175
|
-
console.info('Done copying assets');
|
|
176
|
-
resolve();
|
|
177
|
-
} else {
|
|
178
|
-
// fileSources.length === 0 is checked in previous branch, so this is string
|
|
179
|
-
const src = fileSources.shift()!;
|
|
180
|
-
const dest = fileDestBySrc[src];
|
|
181
|
-
copyFile(src, dest, copyNext);
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
copyNext();
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
function copyFile(
|
|
189
|
-
src: string,
|
|
190
|
-
dest: string,
|
|
191
|
-
onFinished: (error: Error) => void
|
|
192
|
-
): void {
|
|
193
|
-
const destDir = pathUtils.dirname(dest);
|
|
194
|
-
fs.mkdir(destDir, { recursive: true }, (err?) => {
|
|
195
|
-
if (err) {
|
|
196
|
-
onFinished(err);
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
fs.createReadStream(src)
|
|
200
|
-
.pipe(fs.createWriteStream(dest))
|
|
201
|
-
.on('finish', onFinished);
|
|
202
|
-
});
|
|
203
|
-
}
|
|
1
|
+
import { Command } from '@react-native-community/cli-types';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import fse from 'fs-extra';
|
|
4
|
+
import Metro from 'metro';
|
|
5
|
+
import { RunBuildOptions as BuildOptions } from 'metro';
|
|
6
|
+
import MetroServer from 'metro/src/Server';
|
|
7
|
+
import pathUtils from 'path';
|
|
8
|
+
import { getAssetDestRelativePath } from '../assetResolver';
|
|
9
|
+
import { ConfigT as MetroConfig } from 'metro-config';
|
|
10
|
+
|
|
11
|
+
const ARK_RESOURCE_PATH = './harmony/entry/src/main/resources/rawfile';
|
|
12
|
+
const ASSETS_DEFAULT_DEST_PATH =
|
|
13
|
+
'./harmony/entry/src/main/resources/rawfile/assets';
|
|
14
|
+
|
|
15
|
+
type AssetData = Metro.AssetData;
|
|
16
|
+
type Bundle = { code: string; map: string };
|
|
17
|
+
type Path = string;
|
|
18
|
+
|
|
19
|
+
export const commandBundleHarmony: Command = {
|
|
20
|
+
name: 'bundle-harmony',
|
|
21
|
+
description:
|
|
22
|
+
'Creates JS bundle, creates a special cpp header containing the JS code, copies assets directory to the project.',
|
|
23
|
+
options: [
|
|
24
|
+
{
|
|
25
|
+
name: '--dev [boolean]',
|
|
26
|
+
description: 'If false, warnings are disabled and the bundle is minified',
|
|
27
|
+
parse: (val: string) => val !== 'false',
|
|
28
|
+
default: true,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: '--entry-file <path>',
|
|
32
|
+
description:
|
|
33
|
+
'Path to the root JS file, either absolute or relative to JS root',
|
|
34
|
+
default: 'index.js',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: '--config <path>',
|
|
38
|
+
description: 'Path to the Metro configuration file',
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: '--bundle-output <path>',
|
|
42
|
+
description:
|
|
43
|
+
'File name where to store the resulting bundle, ex. /tmp/groups.bundle',
|
|
44
|
+
default: ARK_RESOURCE_PATH + '/bundle.harmony.js',
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: '--assets-dest <path>',
|
|
48
|
+
description:
|
|
49
|
+
'Directory name where to store assets referenced in the bundle',
|
|
50
|
+
default: ASSETS_DEFAULT_DEST_PATH,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: '--sourcemap-output <path>',
|
|
54
|
+
description:
|
|
55
|
+
'File name where to store the resulting source map, ex. /tmp/groups.map',
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: '--minify [boolean]',
|
|
59
|
+
description: 'Allows overriding whether bundle is minified',
|
|
60
|
+
parse: (val: string) => val !== 'false',
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
func: async (argv, config, args: any) => {
|
|
64
|
+
const buildOptions: BuildOptions = {
|
|
65
|
+
entry: args.entryFile,
|
|
66
|
+
platform: 'harmony',
|
|
67
|
+
minify: args.minify !== undefined ? args.minify : !args.dev,
|
|
68
|
+
dev: args.dev,
|
|
69
|
+
sourceMap: args.sourcemapOutput,
|
|
70
|
+
sourceMapUrl: args.sourcemapOutput,
|
|
71
|
+
};
|
|
72
|
+
const metroConfig = await loadMetroConfig(args.config);
|
|
73
|
+
const bundle = await createBundle(metroConfig, buildOptions);
|
|
74
|
+
await saveBundle(bundle, args.bundleOutput, args.sourcemapOutput);
|
|
75
|
+
const assets = await retrieveAssetsData(metroConfig, buildOptions);
|
|
76
|
+
copyAssets(assets, args.assetsDest);
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
async function loadMetroConfig(
|
|
81
|
+
configPath: Path | undefined
|
|
82
|
+
): Promise<MetroConfig> {
|
|
83
|
+
return configPath
|
|
84
|
+
? await Metro.loadConfig({ config: configPath })
|
|
85
|
+
: await Metro.loadConfig();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function createBundle(
|
|
89
|
+
metroConfig: MetroConfig,
|
|
90
|
+
buildOptions: BuildOptions
|
|
91
|
+
): Promise<Bundle> {
|
|
92
|
+
// casting needed as Metro.runBuild returns Promise<{code: string, map: string}>
|
|
93
|
+
// despite being typed as Promise<void>
|
|
94
|
+
return (await Metro.runBuild(metroConfig, buildOptions)) as unknown as Bundle;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function saveBundle(
|
|
98
|
+
bundle: Bundle,
|
|
99
|
+
bundleOutput: Path,
|
|
100
|
+
sourceMapOutput: Path | undefined
|
|
101
|
+
) {
|
|
102
|
+
await fse.ensureDir(pathUtils.dirname(bundleOutput));
|
|
103
|
+
fs.writeFileSync(bundleOutput, bundle.code);
|
|
104
|
+
console.log(`[CREATED] ${bundleOutput}`);
|
|
105
|
+
if (sourceMapOutput) {
|
|
106
|
+
fs.writeFileSync(sourceMapOutput, bundle.map);
|
|
107
|
+
console.log(`[CREATED] ${sourceMapOutput}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function retrieveAssetsData(
|
|
112
|
+
metroConfig: MetroConfig,
|
|
113
|
+
buildOptions: BuildOptions
|
|
114
|
+
): Promise<readonly AssetData[]> {
|
|
115
|
+
const metroServer = new MetroServer(metroConfig);
|
|
116
|
+
try {
|
|
117
|
+
return await metroServer.getAssets({
|
|
118
|
+
...MetroServer.DEFAULT_BUNDLE_OPTIONS,
|
|
119
|
+
...buildOptions,
|
|
120
|
+
entryFile: buildOptions.entry,
|
|
121
|
+
bundleType: 'todo',
|
|
122
|
+
});
|
|
123
|
+
} finally {
|
|
124
|
+
metroServer.end();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function copyAssets(
|
|
129
|
+
assetsData: readonly AssetData[],
|
|
130
|
+
assetsDest: Path
|
|
131
|
+
): Promise<void> {
|
|
132
|
+
if (assetsDest == null) {
|
|
133
|
+
console.warn('Assets destination folder is not set, skipping...');
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
await fse.ensureDir(assetsDest);
|
|
137
|
+
const fileDestBySrc: Record<Path, Path> = {};
|
|
138
|
+
for (const asset of assetsData) {
|
|
139
|
+
const idx = getHighestQualityFileIdx(asset);
|
|
140
|
+
fileDestBySrc[asset.files[idx]] = pathUtils.join(
|
|
141
|
+
assetsDest,
|
|
142
|
+
getAssetDestRelativePath(asset)
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
return copyFiles(fileDestBySrc);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function getHighestQualityFileIdx(assetData: AssetData): number {
|
|
149
|
+
let result = 0;
|
|
150
|
+
let maxScale = -1;
|
|
151
|
+
for (let idx = 0; idx < assetData.scales.length; idx++) {
|
|
152
|
+
const scale = assetData.scales[idx];
|
|
153
|
+
if (scale > maxScale) {
|
|
154
|
+
maxScale = scale;
|
|
155
|
+
result = idx;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function copyFiles(fileDestBySrc: Record<Path, Path>) {
|
|
162
|
+
const fileSources = Object.keys(fileDestBySrc);
|
|
163
|
+
if (fileSources.length === 0) {
|
|
164
|
+
return Promise.resolve();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
console.info(`Copying ${fileSources.length} asset files`);
|
|
168
|
+
return new Promise<void>((resolve, reject) => {
|
|
169
|
+
const copyNext = (error?: Error) => {
|
|
170
|
+
if (error) {
|
|
171
|
+
reject(error);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (fileSources.length === 0) {
|
|
175
|
+
console.info('Done copying assets');
|
|
176
|
+
resolve();
|
|
177
|
+
} else {
|
|
178
|
+
// fileSources.length === 0 is checked in previous branch, so this is string
|
|
179
|
+
const src = fileSources.shift()!;
|
|
180
|
+
const dest = fileDestBySrc[src];
|
|
181
|
+
copyFile(src, dest, copyNext);
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
copyNext();
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function copyFile(
|
|
189
|
+
src: string,
|
|
190
|
+
dest: string,
|
|
191
|
+
onFinished: (error: Error) => void
|
|
192
|
+
): void {
|
|
193
|
+
const destDir = pathUtils.dirname(dest);
|
|
194
|
+
fs.mkdir(destDir, { recursive: true }, (err?) => {
|
|
195
|
+
if (err) {
|
|
196
|
+
onFinished(err);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
fs.createReadStream(src)
|
|
200
|
+
.pipe(fs.createWriteStream(dest))
|
|
201
|
+
.on('finish', onFinished);
|
|
202
|
+
});
|
|
203
|
+
}
|