UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

77 lines (76 loc) 3.27 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPackageConfig = getPackageConfig; exports.getPackagePath = getPackagePath; exports.getLocalPackageConfig = getLocalPackageConfig; exports.downloadRemotePackageConfig = downloadRemotePackageConfig; exports.getRemotePath = getRemotePath; exports.downloadFile = downloadFile; exports.getRemoteFile = getRemoteFile; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const picocolors_1 = __importDefault(require("picocolors")); const constants_1 = require("../constants"); const progress_1 = require("../progress"); const prompter_1 = require("../prompter"); const getMd5_1 = require("./getMd5"); const getProjectPath_1 = require("./getProjectPath"); async function getPackageConfig(packageName, pagination = { index: 0, count: 1 }) { progress_1.progress.setOptions({ step: pagination.index + 1, total: pagination.count, stage: `[${pagination.index + 1}/${pagination.count}] checking ${picocolors_1.default.blue(packageName)}`, }); (0, prompter_1.updateSpinner)(`[${pagination.index + 1}/${pagination.count}] checking package configuration: ${packageName}`); let localConfigPath = getLocalPackageConfig(packageName); if (!localConfigPath) { localConfigPath = await downloadRemotePackageConfig(packageName); } return localConfigPath; } function getPackagePath(packageName) { const projectPath = (0, getProjectPath_1.getProjectPath)(); return path_1.default.join(projectPath, 'node_modules', packageName); } function getLocalPackageConfig(packageName) { const localPackagePath = getPackagePath(packageName); const localConfigPath = path_1.default.join(localPackagePath, constants_1.Constants.CONFIG_FILE_NAME); if (!fs_1.default.existsSync(localConfigPath)) return null; return localConfigPath; } async function downloadRemotePackageConfig(packageName) { const localPackagePath = getPackagePath(packageName); const localConfigPath = path_1.default.join(localPackagePath, constants_1.Constants.CONFIG_FILE_NAME); const remotePath = getRemotePath(packageName); const didDownload = await downloadFile(remotePath, localConfigPath); if (!didDownload) return null; return localConfigPath; } function getRemotePath(packageName, pathTemplate = constants_1.Constants.REMOTE_CONFIG_REPO) { const md5 = (0, getMd5_1.getMd5)(packageName); const md5Path = md5.substring(0, 3).split('').join('/'); return pathTemplate.replace('[package]', md5Path + '/' + packageName .split('/') .map(x => encodeURIComponent(x)) .join('/')); } async function downloadFile(remotePath, localPath) { const configContent = await getRemoteFile(remotePath); if (!configContent) return false; fs_1.default.writeFileSync(localPath, configContent); return true; } async function getRemoteFile(remotePath) { const response = await fetch(remotePath); if (response.status != 200) return null; return response.text(); }