UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

286 lines (285 loc) 12.9 kB
"use strict"; 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 }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.copyComposable = copyComposable; exports.copyFile = copyFile; exports.getConfig = getConfig; exports.getConfigForFile = getConfigForFile; exports.getFileInfo = getFileInfo; exports.installPackage = installPackage; exports.processConfig = processConfig; /** * Shared utilities for blocks and components commands * These functions handle downloading and installing Nuxt base components from GitHub */ const axios_1 = __importDefault(require("axios")); const fs = __importStar(require("fs")); const glob = __importStar(require("glob")); const gluegun_1 = require("gluegun"); const path = __importStar(require("path")); const GITHUB_BASE_URL = 'https://raw.githubusercontent.com/lenneTech/nuxt-base-components/main'; const GITHUB_API_URL = 'https://api.github.com/repos/lenneTech/nuxt-base-components/contents'; /** * Copy a composable from GitHub to the local composables directory */ function copyComposable(composable_1, toolbox_1) { return __awaiter(this, arguments, void 0, function* (composable, toolbox, noConfirm = false) { const { print, prompt } = toolbox; const apiUrl = `${GITHUB_BASE_URL}/composables/${composable}.ts`; const response = yield axios_1.default.get(apiUrl); if (response.status === 200) { const sourceCode = response.data; const cwd = process.cwd(); let targetDirectory; if (fs.existsSync(path.resolve(cwd, 'composables'))) { targetDirectory = path.resolve(cwd, 'composables'); } else { const directories = glob.sync('*/composables', { cwd }); if (directories.length > 0) { targetDirectory = path.join(cwd, directories[0]); } else { targetDirectory = cwd; } } // Check if composable already exists if (fs.existsSync(path.join(targetDirectory, `${composable}.ts`))) { print.info(`The composable ${composable} already exists`); return; } if (!noConfirm) { const confirmAdd = yield prompt.confirm(`The composable ${composable} is required. Would you like to add it?`); if (!confirmAdd) { return; } } const targetPath = path.join(targetDirectory, `${composable}.ts`); const spinner = print.spin(`Copy the composable ${composable} to ${targetPath}...`); fs.writeFileSync(targetPath, sourceCode); spinner.succeed(`The composable ${composable} was successfully copied to ${targetPath}`); } else { print.error(`Error retrieving the file from GitHub: ${response.statusText}`); } }); } /** * Copy a file (block or component) from GitHub to local directory */ function copyFile(file_1, toolbox_1, type_1) { return __awaiter(this, arguments, void 0, function* (file, toolbox, type, noConfirm = false) { const { print } = toolbox; const apiUrl = `${GITHUB_BASE_URL}/${type}/${file.name}`; const targetDirName = type === 'blocks' ? 'pages' : 'components'; const config = yield getConfigForFile(file.name, toolbox, type === 'blocks' ? 'block' : 'component'); if (config) { yield processConfig(config, toolbox, type, noConfirm); } const compSpinner = print.spin(`Load ${type.slice(0, -1)} ${file.name} from GitHub...`); const response = yield axios_1.default.get(apiUrl); compSpinner.succeed(`${type === 'blocks' ? 'Block' : 'Component'} ${file.name} successfully loaded from GitHub`); if (response.status === 200) { const sourceCode = response.data; const cwd = process.cwd(); let targetDirectory; if (fs.existsSync(path.resolve(cwd, targetDirName))) { targetDirectory = path.resolve(cwd, targetDirName); } else { const directories = glob.sync(`*/${targetDirName}`, { cwd }); if (directories.length > 0) { targetDirectory = path.join(cwd, directories[0]); } else { targetDirectory = cwd; } } let targetName = file.name; if (type === 'blocks') { targetName = file.name .replace(/([a-z])([A-Z])/g, '$1-$2') .toLowerCase() .replace(/^block-/, ''); } const targetPath = path.join(targetDirectory, targetName); // Check if file already exists if (fs.existsSync(targetPath)) { print.info(`The ${type.slice(0, -1)} ${file.name} already exists`); return targetPath; } if (!fs.existsSync(targetDirectory)) { const targetDirSpinner = print.spin('Creating the target directory...'); fs.mkdirSync(targetDirectory, { recursive: true }); targetDirSpinner.succeed(); } if (file.type === 'dir' || file.name.split('/').length > 1) { const dirName = file.name.split('/')[0]; const dirPath = path.join(targetDirectory, dirName); if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath, { recursive: true }); } } const spinner = print.spin(`Copy the ${type.slice(0, -1)} ${targetName} to ${targetPath}...`); fs.writeFileSync(targetPath, sourceCode); spinner.succeed(`The ${type.slice(0, -1)} ${targetName} was successfully copied to ${targetPath}`); return targetPath; } else { throw new Error(`Error retrieving the file from GitHub: ${response.statusText}`); } }); } /** * Get the config.json from the nuxt-base-components repository */ function getConfig() { return __awaiter(this, void 0, void 0, function* () { const githubApiUrl = `${GITHUB_BASE_URL}/config.json`; const response = yield axios_1.default.get(githubApiUrl); if (response.status === 200) { return response.data; } else { throw new Error(`Error when retrieving the configuration from GitHub: ${response.statusText}`); } }); } /** * Get configuration for a specific block or component */ function getConfigForFile(fileName, toolbox, type) { return __awaiter(this, void 0, void 0, function* () { const { print } = toolbox; const configSpinner = print.spin(`Checking the config for ${type}...`); const data = yield getConfig(); const name = fileName.split('.').slice(0, -1).join('.'); const rootName = name.split('/')[0]; configSpinner.succeed(`Config for ${rootName} loaded successfully`); return data.config[rootName] || {}; }); } /** * Get file info from GitHub API for blocks or components */ function getFileInfo(type, subPath) { return __awaiter(this, void 0, void 0, function* () { const githubApiUrl = `${GITHUB_API_URL}/${type}${subPath ? `/${subPath}` : ''}`; const response = yield axios_1.default.get(githubApiUrl); if (response.status === 200) { return response.data.map((file) => ({ name: file.name, type: file.type, })); } else { throw new Error(`Error when retrieving the file list from GitHub: ${response.statusText}`); } }); } /** * Install an npm package if not already installed */ function installPackage(packageName_1, toolbox_1) { return __awaiter(this, arguments, void 0, function* (packageName, toolbox, noConfirm = false) { const { print, prompt, system } = toolbox; const nameWithoutVersion = packageName.split('@')[0] || packageName; const packageJsonPath = path.resolve(process.cwd(), 'package.json'); const packageJson = gluegun_1.filesystem.read(packageJsonPath, 'json'); const isInstalled = (packageJson.dependencies && packageJson.dependencies[nameWithoutVersion]) || (packageJson.devDependencies && packageJson.devDependencies[nameWithoutVersion]); if (!isInstalled) { if (!noConfirm) { const confirmInstall = yield prompt.confirm(`The package ${nameWithoutVersion} is required. Would you like to install it?`); if (!confirmInstall) { return; } } const { pm } = toolbox; const installSpinner = print.spin(`Install package ${nameWithoutVersion}...`); yield system.run(pm.addPackage(packageName, '--save-exact')); installSpinner.succeed(`Package ${nameWithoutVersion} successfully installed`); } else { print.info(`Package ${nameWithoutVersion} is already installed`); } }); } /** * Process config dependencies (npm packages, composables, components) */ function processConfig(config_1, toolbox_1, type_1) { return __awaiter(this, arguments, void 0, function* (config, toolbox, type, noConfirm = false) { if (config === null || config === void 0 ? void 0 : config.npm) { const npmPackages = config.npm; for (const npmPackage of npmPackages) { yield installPackage(npmPackage, toolbox, noConfirm); } } if (config === null || config === void 0 ? void 0 : config.composables) { const composables = config.composables; for (const composable of composables) { yield copyComposable(composable, toolbox, noConfirm); } } if (config === null || config === void 0 ? void 0 : config.components) { const components = config.components; for (const component of components) { if (component.endsWith('/*')) { const folderName = component.split('/')[0]; const directoryFiles = yield getFileInfo('components', folderName); for (const file of directoryFiles) { yield copyFile({ name: `${folderName}/${file.name}`, type: 'dir' }, toolbox, 'components', noConfirm); } } else { yield copyFile({ name: `${component}.vue`, type: 'file' }, toolbox, 'components', noConfirm); } } } }); }