-
Notifications
You must be signed in to change notification settings - Fork 24.4k
/
saveAssets.js
139 lines (124 loc) · 3.59 KB
/
saveAssets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/
import type {AssetData} from 'metro/src/Assets';
import {
cleanAssetCatalog,
getImageSet,
isCatalogAsset,
writeImageSet,
} from './assetCatalogIOS';
import filterPlatformAssetScales from './filterPlatformAssetScales';
import getAssetDestPathAndroid from './getAssetDestPathAndroid';
import getAssetDestPathIOS from './getAssetDestPathIOS';
import {logger} from '@react-native-community/cli-tools';
import fs from 'fs';
import path from 'path';
type CopiedFiles = {
[src: string]: string,
};
async function saveAssets(
assets: $ReadOnlyArray<AssetData>,
platform: string,
assetsDest?: string,
assetCatalogDest?: string,
): Promise<void> {
if (assetsDest == null) {
logger.warn('Assets destination folder is not set, skipping...');
return;
}
const filesToCopy: CopiedFiles = {};
const getAssetDestPath =
platform === 'android' ? getAssetDestPathAndroid : getAssetDestPathIOS;
const addAssetToCopy = (asset: AssetData) => {
const validScales = new Set(
filterPlatformAssetScales(platform, asset.scales),
);
asset.scales.forEach((scale, idx) => {
if (!validScales.has(scale)) {
return;
}
const src = asset.files[idx];
const dest = path.join(assetsDest, getAssetDestPath(asset, scale));
filesToCopy[src] = dest;
});
};
if (platform === 'ios' && assetCatalogDest != null) {
// Use iOS Asset Catalog for images. This will allow Apple app thinning to
// remove unused scales from the optimized bundle.
const catalogDir = path.join(assetCatalogDest, 'RNAssets.xcassets');
if (!fs.existsSync(catalogDir)) {
logger.error(
`Could not find asset catalog 'RNAssets.xcassets' in ${assetCatalogDest}. Make sure to create it if it does not exist.`,
);
return;
}
logger.info('Adding images to asset catalog', catalogDir);
cleanAssetCatalog(catalogDir);
for (const asset of assets) {
if (isCatalogAsset(asset)) {
const imageSet = getImageSet(
catalogDir,
asset,
filterPlatformAssetScales(platform, asset.scales),
);
writeImageSet(imageSet);
} else {
addAssetToCopy(asset);
}
}
logger.info('Done adding images to asset catalog');
} else {
assets.forEach(addAssetToCopy);
}
return copyAll(filesToCopy);
}
function copyAll(filesToCopy: CopiedFiles) {
const queue = Object.keys(filesToCopy);
if (queue.length === 0) {
return Promise.resolve();
}
logger.info(`Copying ${queue.length} asset files`);
return new Promise<void>((resolve, reject) => {
const copyNext = (error?: Error) => {
if (error) {
reject(error);
return;
}
if (queue.length === 0) {
logger.info('Done copying assets');
resolve();
} else {
// queue.length === 0 is checked in previous branch, so this is string
const src = queue.shift();
const dest = filesToCopy[src];
copy(src, dest, copyNext);
}
};
copyNext();
});
}
function copy(
src: string,
dest: string,
callback: (error: Error) => void,
): void {
const destDir = path.dirname(dest);
fs.mkdir(destDir, {recursive: true}, (err?) => {
if (err) {
callback(err);
return;
}
fs.createReadStream(src)
.pipe(fs.createWriteStream(dest))
.on('finish', callback);
});
}
export default saveAssets;