UNPKG

gluestack-ui

Version:

A CLI tool for easily adding components from gluestack to your projects.

177 lines (176 loc) 7.8 kB
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "fs", "path", "fast-glob", "..", "../../config", "./config-types"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._currDir = exports.pathResolver = void 0; exports.checkIfInitialized = checkIfInitialized; exports.getEntryPathAndComponentsPath = getEntryPathAndComponentsPath; exports.getFilePath = getFilePath; exports.getComponentsPath = getComponentsPath; exports.generateMonoRepoConfig = generateMonoRepoConfig; exports.findDirectory = findDirectory; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const fast_glob_1 = __importDefault(require("fast-glob")); const __1 = require(".."); const config_1 = require("../../config"); const config_types_1 = require("./config-types"); const fileExtensions = ['.tsx', '.jsx', '.ts', '.js']; const possibleIndexFiles = ['_app', 'index', 'App']; const possibleDirectories = ['src', 'pages', 'app', 'components']; const _currDir = process.cwd(); exports._currDir = _currDir; const pathResolver = (p) => { return path.resolve(_currDir, p).replace(/\\/g, '/'); }; exports.pathResolver = pathResolver; function findDirectory(rootDir, relativePaths) { for (const relPath of relativePaths) { const dirPath = path.join(rootDir, relPath); if (fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory()) { return dirPath.replace(`${rootDir}/`, ''); } } return ''; } function checkIfInitialized(cwd) { return __awaiter(this, void 0, void 0, function* () { try { const initializeStatus = yield getComponentsPath(cwd); if (initializeStatus.length) { return true; } return false; } catch (error) { return false; } }); } function getComponentsPath(cwd) { return __awaiter(this, void 0, void 0, function* () { const componentsPath = yield fast_glob_1.default.glob(`**/*${config_1.config.providerComponent}/index.tsx`, { cwd: cwd, deep: 8, ignore: config_types_1.PROJECT_SHARED_IGNORE, }); if (!componentsPath.length) { //handle this case when CLI couldn't locate the components path return ''; } const resolvedComponentsPath = componentsPath[0].replace(`/${config_1.config.providerComponent}/index.tsx`, ''); return resolvedComponentsPath; }); } function getEntryPathAndComponentsPath() { let entryPath = []; let componentsPath = []; let FileExists = []; fileExtensions.forEach((ext) => { possibleIndexFiles.map((file) => { if (fs.existsSync(path.join(__1.projectRootPath, `${file}${ext}`).normalize())) { FileExists.push(file); } }); }); // Check if any of the possible index files exist if (FileExists) { FileExists.forEach((file) => { entryPath.push(path.join('.', `${file}.{tsx,jsx,ts,js}`)); }); } // Check if "src", "pages", "app" or "component" directories exist possibleDirectories.forEach((dir) => { if (fs.existsSync(path.join(__1.projectRootPath, dir).normalize())) { entryPath.push(path.join('.', `${dir}/**/*.{tsx,jsx,ts,js}`)); } }); const resolvedPath = config_1.config.writableComponentsPath.split('/'); if (!entryPath.includes(path.join('.', `${resolvedPath[0]}/**/*.{tsx,jsx,ts,js}`))) { componentsPath.push(path.join('.', `${resolvedPath[0]}/**/*.{tsx,jsx,ts,js}`)); } entryPath = [...entryPath, ...componentsPath]; return { entryPath }; } function getFilePath(files) { return __awaiter(this, void 0, void 0, function* () { const filePath = yield fast_glob_1.default.glob(files, { cwd: __1.projectRootPath, deep: 3, ignore: config_types_1.PROJECT_SHARED_IGNORE, }); if (!filePath.length) { return ''; } return filePath[0]; }); } // async function generateConfig(resultConfig: RawConfig) { // const targetPath = path.resolve(projectRootPath, 'gluestack-ui.config.json'); // fs.writeFileSync(targetPath, JSON.stringify(resultConfig, null, 2), 'utf8'); // } function generateMonoRepoConfig() { return __awaiter(this, void 0, void 0, function* () { const componentPath = path.resolve(__1.projectRootPath, config_1.config.writableComponentsPath); const gluestackConfig = { app: { components: componentPath.replace(`${__1.projectRootPath}/`, ''), }, }; const targetPath = path.resolve(__1.projectRootPath, 'gluestack-ui.config.json'); fs.writeFileSync(targetPath, JSON.stringify(gluestackConfig, null, 2), 'utf8'); }); } });