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.
|
2018-05-11 19:43:49 +00:00
|
|
|
*
|
|
|
|
* @format
|
2015-10-05 16:15:09 +00:00
|
|
|
*/
|
2018-05-11 19:43:49 +00:00
|
|
|
|
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');
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
function saveAssets(assets, platform, assetsDest) {
|
2015-10-05 16:15:09 +00:00
|
|
|
if (!assetsDest) {
|
|
|
|
console.warn('Assets destination folder is not set, skipping...');
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const getAssetDestPath =
|
|
|
|
platform === 'android' ? getAssetDestPathAndroid : getAssetDestPathIOS;
|
2015-10-05 16:15:09 +00:00
|
|
|
|
|
|
|
const filesToCopy = Object.create(null); // Map src -> dest
|
2018-05-11 19:43:49 +00:00
|
|
|
assets.forEach(asset => {
|
|
|
|
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;
|
2017-08-24 13:22:25 +00:00
|
|
|
});
|
2018-05-11 19:43:49 +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) => {
|
2018-05-11 19:43:49 +00:00
|
|
|
const copyNext = error => {
|
2015-10-05 16:15:09 +00:00
|
|
|
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);
|
|
|
|
}
|
2018-06-06 12:20:40 +00:00
|
|
|
fs.createReadStream(src)
|
2015-10-05 16:15:09 +00:00
|
|
|
.pipe(fs.createWriteStream(dest))
|
|
|
|
.on('finish', callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-21 04:17:17 +00:00
|
|
|
module.exports = saveAssets;
|