2015-10-05 16:15:09 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-10-05 16:15:09 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-02-09 19:06:19 +00:00
|
|
|
const filterPlatformAssetScales = require('./filterPlatformAssetScales');
|
2015-10-05 16:15:09 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const getAssetDestPathAndroid = require('./getAssetDestPathAndroid');
|
|
|
|
const getAssetDestPathIOS = require('./getAssetDestPathIOS');
|
|
|
|
const log = require('../util/log').out('bundle');
|
2015-11-27 15:15:28 +00:00
|
|
|
const mkdirp = require('mkdirp');
|
2015-10-05 16:15:09 +00:00
|
|
|
const path = require('path');
|
|
|
|
|
2015-11-21 04:17:17 +00:00
|
|
|
function saveAssets(
|
|
|
|
assets,
|
2015-10-05 16:15:09 +00:00
|
|
|
platform,
|
|
|
|
assetsDest
|
|
|
|
) {
|
|
|
|
if (!assetsDest) {
|
|
|
|
console.warn('Assets destination folder is not set, skipping...');
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const getAssetDestPath = platform === 'android'
|
|
|
|
? getAssetDestPathAndroid
|
|
|
|
: getAssetDestPathIOS;
|
|
|
|
|
|
|
|
const filesToCopy = Object.create(null); // Map src -> dest
|
2015-11-21 04:17:17 +00:00
|
|
|
assets
|
2017-08-24 13:22:25 +00:00
|
|
|
.forEach(asset => {
|
|
|
|
const validScales = new Set(filterPlatformAssetScales(platform, asset.scales));
|
|
|
|
asset.scales.forEach((scale, idx) => {
|
|
|
|
if (!validScales.has(scale)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-05 16:15:09 +00:00
|
|
|
const src = asset.files[idx];
|
|
|
|
const dest = path.join(assetsDest, getAssetDestPath(asset, scale));
|
|
|
|
filesToCopy[src] = dest;
|
2017-10-10 00:37:08 +00:00
|
|
|
});
|
2017-08-24 13:22:25 +00:00
|
|
|
});
|
2015-10-05 16:15:09 +00:00
|
|
|
|
|
|
|
return copyAll(filesToCopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyAll(filesToCopy) {
|
|
|
|
const queue = Object.keys(filesToCopy);
|
|
|
|
if (queue.length === 0) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
log('Copying ' + queue.length + ' asset files');
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const copyNext = (error) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
if (queue.length === 0) {
|
|
|
|
log('Done copying assets');
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
const src = queue.shift();
|
|
|
|
const dest = filesToCopy[src];
|
|
|
|
copy(src, dest, copyNext);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
copyNext();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function copy(src, dest, callback) {
|
|
|
|
const destDir = path.dirname(dest);
|
2015-11-27 15:15:28 +00:00
|
|
|
mkdirp(destDir, err => {
|
2015-10-05 16:15:09 +00:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
fs.createReadStream(src)
|
|
|
|
.pipe(fs.createWriteStream(dest))
|
|
|
|
.on('finish', callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-21 04:17:17 +00:00
|
|
|
module.exports = saveAssets;
|