UNPKG

qraft

Version:

A powerful CLI tool to qraft structured project setups from GitHub template repositories

207 lines • 9.61 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.copyCommand = copyCommand; const chalk_1 = __importDefault(require("chalk")); const inquirer_1 = __importDefault(require("inquirer")); const path = __importStar(require("path")); const interactiveMode_1 = require("../interactive/interactiveMode"); async function copyCommand(boxManager, boxName, options) { try { // Use interactive mode if requested if (options.interactive) { const interactiveMode = new interactiveMode_1.InteractiveMode(boxManager); const copyOptions = {}; if (options.registry !== undefined) copyOptions.registry = options.registry; if (options.target !== undefined) copyOptions.target = options.target; if (options.force !== undefined) copyOptions.force = options.force; if (options.nosync !== undefined) copyOptions.nosync = options.nosync; const result = await interactiveMode.copyBox(boxName, copyOptions); if (!result.success) { process.exit(1); } return; } // Non-interactive mode (existing logic) // Parse box reference to validate it exists const boxRef = await boxManager.parseBoxReference(boxName, options.registry); // Check if box exists const boxExists = await boxManager.boxExists(boxRef); if (!boxExists) { console.error(chalk_1.default.red('āŒ Box not found:'), boxName); if (options.registry) { console.error(chalk_1.default.gray(` Registry: ${options.registry}`)); } console.error(chalk_1.default.gray('\nAvailable boxes:')); console.error(chalk_1.default.cyan(' qraft list')); process.exit(1); } // Get box information const boxInfo = await boxManager.getBoxInfo(boxRef); if (!boxInfo) { console.error(chalk_1.default.red('āŒ Failed to get box information')); process.exit(1); } console.log(chalk_1.default.blue.bold(`\nšŸ“¦ Copying ${boxInfo.manifest.name}`)); console.log(chalk_1.default.gray(` ${boxInfo.manifest.description}`)); console.log(chalk_1.default.gray(` Version: ${boxInfo.manifest.version}`)); console.log(chalk_1.default.gray(` Files: ${boxInfo.files.length}`)); // Determine target directory - respect defaultTarget from manifest if no explicit target provided let targetDirectory = options.target || boxInfo.manifest.defaultTarget || process.cwd(); // Interactive mode if (options.interactive) { const answers = await inquirer_1.default.prompt([ { type: 'input', name: 'targetDirectory', message: 'Target directory:', default: targetDirectory, validate: (input) => { if (!input.trim()) { return 'Target directory cannot be empty'; } return true; } }, { type: 'confirm', name: 'force', message: 'Overwrite existing files?', default: options.force || false, when: () => !options.force } ]); targetDirectory = answers.targetDirectory; options.force = answers.force || options.force; } // Resolve target directory targetDirectory = path.resolve(targetDirectory); console.log(chalk_1.default.gray(`\nšŸ“ Target: ${targetDirectory}`)); // Confirm if not in force mode and not interactive if (!options.force && !options.interactive) { const { confirm } = await inquirer_1.default.prompt([ { type: 'confirm', name: 'confirm', message: `Copy ${boxInfo.files.length} files to ${targetDirectory}?`, default: true } ]); if (!confirm) { console.log(chalk_1.default.yellow('āŒ Operation cancelled')); return; } } // Prepare configuration const config = { boxName, targetDirectory, force: options.force || false, interactive: options.interactive || false, boxesDirectory: '', // Not used for GitHub mode nosync: options.nosync ?? false }; // Show progress console.log(chalk_1.default.blue('\nā³ Copying files...')); // Copy the box const result = await boxManager.copyBox(config, options.registry); if (result.success) { console.log(chalk_1.default.green.bold('\nāœ… Success!')); console.log(chalk_1.default.gray(` Copied ${result.copiedFiles?.length || 0} files`)); if (result.skippedFiles && result.skippedFiles.length > 0) { console.log(chalk_1.default.yellow(` Skipped ${result.skippedFiles.length} existing files`)); if (process.env.QRAFT_VERBOSE) { console.log(chalk_1.default.gray('\n Skipped files:')); result.skippedFiles.forEach(file => { console.log(chalk_1.default.gray(` • ${path.relative(targetDirectory, file)}`)); }); } } if (result.copiedFiles && result.copiedFiles.length > 0 && process.env.QRAFT_VERBOSE) { console.log(chalk_1.default.gray('\n Copied files:')); result.copiedFiles.forEach(file => { console.log(chalk_1.default.gray(` • ${path.relative(targetDirectory, file)}`)); }); } console.log(chalk_1.default.gray(`\nšŸ“ Files copied to: ${targetDirectory}`)); // Show sync tracking status if (config.nosync) { console.log(chalk_1.default.yellow('ā„¹ļø No sync tracking (use without --nosync to enable updates)')); } else { console.log(chalk_1.default.green('šŸ“¦ Box tracking enabled in .qraft/ directory')); } // Show next steps if available in manifest if (boxInfo.manifest.postInstall) { console.log(chalk_1.default.blue.bold('\nšŸ“‹ Next Steps:')); boxInfo.manifest.postInstall.forEach((step, index) => { console.log(chalk_1.default.gray(` ${index + 1}. ${step}`)); }); } } else { console.error(chalk_1.default.red.bold('\nāŒ Failed to copy box')); console.error(chalk_1.default.red(result.message)); if (result.error && process.env.QRAFT_VERBOSE) { console.error(chalk_1.default.gray('\nError details:')); console.error(chalk_1.default.gray(result.error.message)); } process.exit(1); } } catch (error) { if (error instanceof Error && error.message.includes('Authentication failed')) { console.error(chalk_1.default.red('\nšŸ” Authentication Error')); console.error(chalk_1.default.gray('This box requires authentication. Set up your GitHub token:')); console.error(chalk_1.default.cyan(' qraft auth login')); } else if (error instanceof Error && error.message.includes('rate limit')) { console.error(chalk_1.default.red('\nā±ļø Rate Limit Exceeded')); console.error(chalk_1.default.gray('GitHub API rate limit exceeded. Set up authentication:')); console.error(chalk_1.default.cyan(' qraft auth login')); } else { throw error; } } } //# sourceMappingURL=copy.js.map