UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

85 lines (84 loc) 4.39 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.importGitFolder = importGitFolder; const fs_1 = __importDefault(require("fs")); const glob_1 = require("glob"); const path_1 = __importDefault(require("path")); const picocolors_1 = __importDefault(require("picocolors")); const constants_1 = require("../../../constants"); const prompter_1 = require("../../../prompter"); const getProjectPath_1 = require("../../getProjectPath"); function importGitFolder(projectPath) { try { const gitFolderPath = path_1.default.join(projectPath, constants_1.Constants.GIT_FOLDER_NAME); if (!fs_1.default.existsSync(gitFolderPath)) return null; const files = (0, glob_1.globSync)([projectPath, constants_1.Constants.GIT_FOLDER_NAME, '**/*'].join('/'), { nodir: true, dot: true }); return { id: 'gitFolder', title: 'Git Folder', value: `${files.length} files`, apply: () => setGitFolder(projectPath, files), }; } catch (_e) { return null; } } async function setGitFolder(oldProjectPath, files) { const shouldBackup = fs_1.default.existsSync(path_1.default.join((0, getProjectPath_1.getProjectPath)(), constants_1.Constants.GIT_FOLDER_NAME)); if (shouldBackup) { fs_1.default.renameSync(path_1.default.join((0, getProjectPath_1.getProjectPath)(), constants_1.Constants.GIT_FOLDER_NAME), path_1.default.join((0, getProjectPath_1.getProjectPath)(), constants_1.Constants.GIT_FOLDER_NAME + '.backup')); (0, prompter_1.logMessage)(`backed up ${picocolors_1.default.yellow(constants_1.Constants.GIT_FOLDER_NAME)}`); } (0, prompter_1.startSpinner)(`copying ${picocolors_1.default.yellow(constants_1.Constants.GIT_FOLDER_NAME)} files`); let success = false; try { for (let i = 0; i < files.length; i++) { (0, prompter_1.updateSpinner)(`copying ${picocolors_1.default.yellow('.git')} files (${i + 1}/${files.length})`); const file = files[i]; const relativePath = path_1.default.relative(oldProjectPath, file); const destination = path_1.default.join((0, getProjectPath_1.getProjectPath)(), relativePath); // ensure dir exists await new Promise(r => fs_1.default.mkdir(path_1.default.dirname(destination), { recursive: true }, r)); await new Promise((res, rej) => { fs_1.default.copyFile(file, destination, err => { if (err) rej(err); else res(null); }); }); } success = true; } finally { if (success) { (0, prompter_1.stopSpinner)(`copied ${picocolors_1.default.yellow(files.length)} files`); if (shouldBackup) { (0, prompter_1.startSpinner)('cleaning up'); await new Promise(r => fs_1.default.rm(path_1.default.join((0, getProjectPath_1.getProjectPath)(), constants_1.Constants.GIT_FOLDER_NAME + '.backup'), { recursive: true, force: true, }, r)); (0, prompter_1.stopSpinner)('cleaned up'); } (0, prompter_1.logMessage)(`imported ${picocolors_1.default.yellow(constants_1.Constants.GIT_FOLDER_NAME)}`); } else { (0, prompter_1.stopSpinner)(picocolors_1.default.red('failed to copy files')); await new Promise(r => fs_1.default.rm(path_1.default.join((0, getProjectPath_1.getProjectPath)(), constants_1.Constants.GIT_FOLDER_NAME), { recursive: true, force: true, }, r)); if (shouldBackup) { fs_1.default.renameSync(path_1.default.join((0, getProjectPath_1.getProjectPath)(), constants_1.Constants.GIT_FOLDER_NAME + '.backup'), path_1.default.join((0, getProjectPath_1.getProjectPath)(), constants_1.Constants.GIT_FOLDER_NAME)); (0, prompter_1.logWarning)(`restored ${picocolors_1.default.yellow(constants_1.Constants.GIT_FOLDER_NAME)}`); } (0, prompter_1.logWarning)('please copy .git folder manually'); } } }