UNPKG

@react-native-paper-abstracted/cli

Version:

React Native Paper Abstracted is a package that allows you to use only the components you need from [React Native Paper](https://reactnativepaper.com). Thus allowing users to keep their app size small, and provides endless customization.

71 lines (70 loc) 2.74 kB
import path from 'path'; import { promises as fs } from 'fs'; import { Octokit } from "octokit"; const octokit = new Octokit({}); const handleGetFolderName = (path) => path.split("/").pop(); const handleCreateFile = async (decodedContent, fileName, folderPath) => { const filePath = path.join(folderPath, fileName); const uint8ArrayContent = new Uint8Array(decodedContent.length); for (let i = 0; i < decodedContent.length; i++) { uint8ArrayContent[i] = decodedContent.charCodeAt(i); } await fs.writeFile(filePath, uint8ArrayContent); }; const handleCreateFilePath = (basePath, relativePath) => { if (relativePath.includes(".")) { const folder = relativePath.split("/").slice(1, -1).join("/"); return path.join(basePath, folder); } else { return path.join(basePath, relativePath); } }; const handleMainDownload = async (path) => { const result = await octokit.request("GET /repos/{owner}/{repo}/contents/{path}", { owner: "babucarr32", repo: "react-native-paper", path: path, headers: { "X-GitHub-Api-Version": "2022-11-28", }, }); return result; }; const handleSaveToFolder = async (targetPath, componentToDownloadPath, progressCallback) => { let processedCount = 0; let totalCount = 0; const recurse = async (myPath) => { const result = await handleMainDownload(myPath); const fileContent = result.data; if (Array.isArray(fileContent)) { totalCount += fileContent.length; await Promise.all(fileContent.map(async (data) => { if (data.type === "dir") { const dirPath = handleCreateFilePath(targetPath, data.path); await fs.mkdir(dirPath, { recursive: true }); await recurse(data.path); } else { await recurse(data.path); } })); } else { processedCount++; const folderPath = handleCreateFilePath(targetPath, fileContent.path); await fs.mkdir(folderPath, { recursive: true }); const decodedContent = atob(fileContent.content); await handleCreateFile(decodedContent, fileContent.name, folderPath); const progress = (processedCount / totalCount) * 100; progressCallback(progress); } }; const folderName = handleGetFolderName(targetPath); const basePath = path.join(process.cwd(), folderName || ''); await fs.mkdir(basePath, { recursive: true }); await recurse(componentToDownloadPath); progressCallback(100); return basePath; }; export { handleSaveToFolder };