react-native-integrate
Version:
Automate integration of additional code into React Native projects
57 lines (56 loc) • 3.04 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.importIosAssets = importIosAssets;
const fs_1 = __importDefault(require("fs"));
const glob_1 = require("glob");
const path_1 = __importDefault(require("path"));
const picocolors_1 = __importDefault(require("picocolors"));
const prompter_1 = require("../../../prompter");
const getIosProjectPath_1 = require("../../getIosProjectPath");
const getProjectPath_1 = require("../../getProjectPath");
function importIosAssets(projectPath) {
try {
const iosProjectName = (0, getIosProjectPath_1.getIosProjectName)(projectPath);
const imagesAssets = (0, glob_1.globSync)([projectPath, 'ios', iosProjectName, 'Images.xcassets/**/*'].join('/'), { nodir: true });
const launchScreen = (0, glob_1.globSync)([projectPath, 'ios', iosProjectName, 'LaunchScreen.storyboard'].join('/'), { nodir: true });
if (!imagesAssets.length && !launchScreen.length)
return null;
return {
id: 'iosAssets',
title: 'Ios Assets',
value: 'Images.xcassets, LaunchScreen.storyboard',
apply: () => setIosAssets(projectPath, iosProjectName, imagesAssets, launchScreen[0]),
};
}
catch (_e) {
return null;
}
}
async function setIosAssets(oldProjectPath, oldIosProjectName, assets, launchScreen) {
const iosProjectName = (0, getIosProjectPath_1.getIosProjectName)();
const existingAssets = (0, glob_1.globSync)([(0, getProjectPath_1.getProjectPath)(), 'ios', iosProjectName, 'Images.xcassets/**/*'].join('/'), { nodir: true });
// delete existing image assets
for (const asset of existingAssets) {
await new Promise(r => fs_1.default.unlink(asset, r));
}
(0, prompter_1.logMessage)('deleted existing images');
// copy new assets
for (const asset of assets) {
// get path after ios
const relativePath = path_1.default.relative(path_1.default.join(oldProjectPath, 'ios', oldIosProjectName), asset);
const destination = path_1.default.join(path_1.default.join((0, getProjectPath_1.getProjectPath)(), 'ios', oldIosProjectName), relativePath);
// ensure dir exists
await new Promise(r => fs_1.default.mkdir(path_1.default.dirname(destination), { recursive: true }, r));
// copy file
await new Promise(r => fs_1.default.copyFile(asset, destination, r));
}
(0, prompter_1.logMessage)(`copied ${picocolors_1.default.yellow('Images.xcassets')} from old project`);
if (launchScreen) {
const newPath = path_1.default.join((0, getProjectPath_1.getProjectPath)(), 'ios', iosProjectName, 'LaunchScreen.storyboard');
await new Promise(r => fs_1.default.copyFile(launchScreen, newPath, r));
(0, prompter_1.logMessage)(`copied ${picocolors_1.default.yellow('LaunchScreen.storyboard')} from old project`);
}
}