From 7bb99bbd13410bef0004b7a80fbf2843c78dc1f1 Mon Sep 17 00:00:00 2001 From: ebedi Date: Mon, 25 May 2026 21:25:34 +0200 Subject: [PATCH] feat[fetch]:added allow to fetch private template --- index.js | 6 ++++++ package-lock.json | 2 -- src/generate.js | 25 +++++++++++++++++++++---- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 7fca8a4..5765547 100755 --- a/index.js +++ b/index.js @@ -137,6 +137,8 @@ async function createAction(projectName, options) { initGit: options.git !== false, enableSecurity: defaults.enableSecurity, noInstall: options.install === false, + repo: options.repo || 'Ebyte-Lab/opusify-templates', + token: options.token || process.env.OPUSIFY_GITHUB_TOKEN || process.env.GITHUB_TOKEN, }; console.log(chalk.green('✔ Using defaults (--yes mode)')); @@ -285,6 +287,8 @@ async function createAction(projectName, options) { initGit: options.git === false ? false : (answers.initGit !== undefined ? answers.initGit : true), enableSecurity: answers.enableSecurity !== undefined ? answers.enableSecurity : true, noInstall: options.install === false, + repo: options.repo || 'Ebyte-Lab/opusify-templates', + token: options.token || process.env.OPUSIFY_GITHUB_TOKEN || process.env.GITHUB_TOKEN, }; console.log('\n' + chalk.green('✔ Configuration collected successfully!')); @@ -375,6 +379,8 @@ program .option('--sidebar', 'Include a sidebar layout') .option('--no-git', 'Skip Git initialization') .option('--no-install', 'Skip npm install') + .option('-r, --repo ', 'Custom GitHub repository (e.g., "username/repo")') + .option('--token ', 'GitHub Personal Access Token for private repositories') .option('-y, --yes', 'Accept all defaults (non-interactive)') .action(createAction); diff --git a/package-lock.json b/package-lock.json index 87f4b5f..67487f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -523,7 +523,6 @@ "integrity": "sha512-UVJyE9MttOsBQIDKw1skb9nAwQuR5wuGD3+82K6JgJlm/Y+KI92oNsMNGZCYdDsVtRHSak0pcV5Dno5+4jh9sw==", "dev": true, "license": "MIT", - "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -826,7 +825,6 @@ "deprecated": "This version is no longer supported. Please see https://eslint.org/version-support for other options.", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@eslint-community/eslint-utils": "^4.2.0", "@eslint-community/regexpp": "^4.6.1", diff --git a/src/generate.js b/src/generate.js index 5b20be7..cf44883 100644 --- a/src/generate.js +++ b/src/generate.js @@ -1,5 +1,6 @@ import fs from 'fs'; import path from 'path'; +import { fileURLToPath } from 'url'; import chalk from 'chalk'; import ora from 'ora'; import tiged from 'tiged'; @@ -7,6 +8,10 @@ import Handlebars from 'handlebars'; import { execSync } from 'child_process'; import { resolveDependencies } from './dependencies.js'; +// Setup __dirname for ES Modules to fix the local template path bug +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + // Register custom Handlebars helpers Handlebars.registerHelper('eq', function (a, b) { return a === b; @@ -29,6 +34,11 @@ function getAllFiles(dirPath, arrayOfFiles = []) { export async function generateProject(config) { console.log(chalk.cyan('\n⚙️ Starting the File Generation Engine...')); + // Inject token into environment for tiged to access private repos + if (config.token) { + process.env.GITHUB_TOKEN = config.token; + } + let projectName = config.projectName; let projectPath = path.join(process.cwd(), projectName); @@ -41,8 +51,10 @@ export async function generateProject(config) { config.projectName = projectName; // Update config with final name // 2. Check for Local vs GitHub + // FIX: Look in the CLI's installation directory, not the user's cwd const localTemplatePath = path.join( - process.cwd(), + __dirname, + '..', // Go up one level from 'src' to reach the root where 'templates' is 'templates', config.template, config.architecture, @@ -60,7 +72,9 @@ export async function generateProject(config) { spinner.succeed(`Files copied to ./${projectName}`); } else { // 🔵 PRODUCTION MODE: Fetch from GitHub - const repoURI = `Ebyte-Lab/opusify-templates/${config.template}/${config.architecture}`; + const targetRepo = config.repo || 'Ebyte-Lab/opusify-templates'; + const repoURI = `${targetRepo}/${config.template}/${config.architecture}`; + const spinner = ora({ text: `Fetching template from GitHub (${repoURI})...`, spinner: 'squareCorners', @@ -72,7 +86,10 @@ export async function generateProject(config) { await emitter.clone(projectPath); spinner.succeed(`Files copied to ./${projectName}`); } catch (fetchError) { - spinner.fail('Failed to fetch template from GitHub.'); + spinner.fail(`Failed to fetch template from GitHub: ${repoURI}`); + if (!config.token) { + console.log(chalk.yellow('\n⚠️ Hint: If this repository is private, you must provide a GitHub token using --token or set the OPUSIFY_GITHUB_TOKEN environment variable.')); + } throw fetchError; } } @@ -143,7 +160,7 @@ export async function generateProject(config) { console.log(chalk.gray('\n⏭️ Skipping Git initialization.')); } - // 7. Final Success Message + // 8. Final Success Message console.log(chalk.magenta(`\n🎉 Project ${projectName} is ready!`)); console.log(chalk.white('\nNext steps:')); console.log(chalk.cyan(` cd ${projectName}`));