From efb35f40a1abae482119e083c4286724388161f1 Mon Sep 17 00:00:00 2001 From: iza <59828082+izadoesdev@users.noreply.github.com> Date: Wed, 2 Apr 2025 14:31:49 +0200 Subject: [PATCH 1/3] feat(init): add resources & imports to project on init --- cli-component/src/utils/installer.js | 91 +++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 9 deletions(-) diff --git a/cli-component/src/utils/installer.js b/cli-component/src/utils/installer.js index 4fdcc0f..15c28ed 100644 --- a/cli-component/src/utils/installer.js +++ b/cli-component/src/utils/installer.js @@ -9,14 +9,45 @@ async function detectProjectStructure() { if (await fs.pathExists('./app')) { return './once-ui/components'; } + // Check for Next.js pages directory structure + if (await fs.pathExists('./pages')) { + return './once-ui/components'; + } // Check for src directory structure if (await fs.pathExists('./src')) { - return './src/once-ui/components'; + if (await fs.pathExists('./src/app')) { + return './src/once-ui/components'; + } + if (await fs.pathExists('./src/pages')) { + return './src/once-ui/components'; + } } // Default to root components directory return './once-ui/components'; } +async function detectAppStructure() { + // Check for Next.js app directory structure + if (await fs.pathExists('./app')) { + return './app'; + } + // Check for Next.js pages directory structure + if (await fs.pathExists('./pages')) { + return './pages'; + } + // Check for src directory structure + if (await fs.pathExists('./src')) { + if (await fs.pathExists('./src/app')) { + return './src/app'; + } + if (await fs.pathExists('./src/pages')) { + return './src/pages'; + } + } + // Default to app directory + return './app'; +} + async function installFile(fileName, content, targetDir) { if (content) { await fs.writeFile( @@ -28,6 +59,33 @@ async function installFile(fileName, content, targetDir) { return false; } +async function ensureLayoutImports() { + const appDir = await detectAppStructure(); + const layoutPath = path.join(appDir, 'layout.css'); + + const imports = [ + '@import "@/once-ui/styles/index.scss";', + '@import "@/once-ui/tokens/index.scss";' + ]; + + try { + let content = ''; + if (await fs.pathExists(layoutPath)) { + content = await fs.readFile(layoutPath, 'utf-8'); + } + + // Check if imports already exist + const hasImports = imports.every(imp => content.includes(imp)); + if (!hasImports) { + // Add imports at the beginning of the file + const newContent = imports.join('\n') + '\n\n' + content; + await fs.writeFile(layoutPath, newContent); + } + } catch (err) { + console.error('Failed to update layout.css:', err.message); + } +} + export async function installComponent(componentName, targetDir = null) { // Detect project structure if targetDir not provided if (!targetDir) { @@ -88,6 +146,9 @@ export async function installComponent(componentName, targetDir = null) { // Install config.js file await installConfigFile(); + + // Ensure layout.css has required imports + await ensureLayoutImports(); } catch (error) { throw error; } @@ -153,23 +214,35 @@ async function fetchUseDebounceContent() { } } -async function installConfigFile() { - const resourcesDir = './once-ui/resources'; - await fs.ensureDir(resourcesDir); - const configContent = await fetchConfigContent(); - await installFile('config.js', configContent, resourcesDir); -} - async function fetchConfigContent() { - const GITHUB_CONFIG_URL = 'https://raw.githubusercontent.com/once-ui-system/nextjs-starter/main/src/once-ui/resources/config.js'; + const GITHUB_CONFIG_URL = 'https://raw.githubusercontent.com/once-ui-system/nextjs-starter/main/src/app/resources/config.js'; try { const response = await axios.get(GITHUB_CONFIG_URL); return response.data; } catch (err) { + console.error('Failed to fetch config file:', err.message); return ''; } } +async function installConfigFile() { + const appDir = await detectAppStructure(); + const resourcesDir = path.join(appDir, 'resources'); + + try { + await fs.ensureDir(resourcesDir); + const configContent = await fetchConfigContent(); + if (configContent) { + await installFile('config.js', configContent, resourcesDir); + console.log('āœ“ Config file installed'); + } else { + console.error('Failed to install config file: No content received'); + } + } catch (err) { + console.error('Failed to install config file:', err.message); + } +} + async function installStylesAndTokens() { const stylesDir = './once-ui/styles'; const tokensDir = './once-ui/tokens'; From d82a70fe9e210272324d877a7e2c0ccbd485a1ae Mon Sep 17 00:00:00 2001 From: iza <59828082+izadoesdev@users.noreply.github.com> Date: Wed, 2 Apr 2025 20:55:15 +0200 Subject: [PATCH 2/3] fix(init): remove add components from init --- cli-component/src/commands/init.js | 33 +++++----------------------- cli-component/src/utils/installer.js | 16 ++++++++++++++ 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/cli-component/src/commands/init.js b/cli-component/src/commands/init.js index 4ac6882..f449bf7 100644 --- a/cli-component/src/commands/init.js +++ b/cli-component/src/commands/init.js @@ -1,44 +1,21 @@ -import inquirer from 'inquirer'; -import { components } from '../utils/components.js'; -import { installComponent } from '../utils/installer.js'; +import { installSharedResources } from '../utils/installer.js'; import { success, info } from '../utils/logger.js'; import { createSpinner } from '../utils/spinner.js'; import gradient from 'gradient-string'; import boxen from 'boxen'; export async function init() { - const spinner = createSpinner('Preparing component list...'); + const spinner = createSpinner('Preparing to install shared resources...'); try { - const answers = await inquirer.prompt([ - { - type: 'checkbox', - name: 'components', - message: 'Select components to add:', - choices: components.map(c => ({ - name: c, - value: c - })), - pageSize: 20, - loop: false - } - ]); - - if (answers.components.length === 0) { - info('No components selected'); - return; - } - console.log(boxen( - gradient.morning('\nšŸš€ Adding selected components...\n'), + gradient.morning('\nšŸ“¦ Installing shared resources...\n'), { padding: 1, borderStyle: 'round', borderColor: 'cyan' } )); - for (const component of answers.components) { - await installComponent(component); - } + await installSharedResources(); - success('All components added successfully! šŸŽ‰'); + success('All shared resources installed successfully! šŸŽ‰'); } catch (error) { spinner.fail(`Initialization failed: ${error.message}`); } diff --git a/cli-component/src/utils/installer.js b/cli-component/src/utils/installer.js index 15c28ed..37245c1 100644 --- a/cli-component/src/utils/installer.js +++ b/cli-component/src/utils/installer.js @@ -131,7 +131,14 @@ export async function installComponent(componentName, targetDir = null) { await installComponent(dep, targetDir); } } + } catch (error) { + throw error; + } +} +// New function to install shared resources +export async function installSharedResources() { + try { // Install styles and tokens await installStylesAndTokens(); @@ -228,11 +235,20 @@ async function fetchConfigContent() { async function installConfigFile() { const appDir = await detectAppStructure(); const resourcesDir = path.join(appDir, 'resources'); + const configPath = path.join(resourcesDir, 'config.js'); try { await fs.ensureDir(resourcesDir); + console.log('Downloading config file...'); const configContent = await fetchConfigContent(); + if (configContent) { + const exists = await fs.pathExists(configPath); + if (exists) { + console.log('āœ“ Config file already exists, updating...'); + } else { + console.log('āœ“ Config file downloaded, installing...'); + } await installFile('config.js', configContent, resourcesDir); console.log('āœ“ Config file installed'); } else { From 313d0ca5a52c2974946a67603501353a503c82bc Mon Sep 17 00:00:00 2001 From: iza <59828082+izadoesdev@users.noreply.github.com> Date: Wed, 2 Apr 2025 20:55:35 +0200 Subject: [PATCH 3/3] fix(version): bump version --- cli-component/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli-component/package.json b/cli-component/package.json index 6cece3e..6c8332d 100644 --- a/cli-component/package.json +++ b/cli-component/package.json @@ -1,6 +1,6 @@ { "name": "once-ui-cli", - "version": "3.1.2", + "version": "3.1.3", "description": "CLI tool for managing Once UI components", "type": "module", "bin": {