From ae58873250537a35931f9c0fee2eaa744a26f33d Mon Sep 17 00:00:00 2001 From: = <=> Date: Wed, 7 Feb 2024 10:33:01 +0530 Subject: [PATCH] CLI: refactor --- packages/create-opub-app/create-app.ts | 36 ++++++--------------- packages/create-opub-app/index.ts | 29 ++++++++++++++--- packages/create-opub-app/package.json | 2 +- packages/create-opub-app/utils/constants.ts | 9 ++++++ packages/create-opub-app/utils/prompts.ts | 12 +++---- packages/opub-ui/package.json | 2 +- 6 files changed, 50 insertions(+), 40 deletions(-) diff --git a/packages/create-opub-app/create-app.ts b/packages/create-opub-app/create-app.ts index 10d3555d..6d12f218 100644 --- a/packages/create-opub-app/create-app.ts +++ b/packages/create-opub-app/create-app.ts @@ -5,8 +5,6 @@ import fse from 'fs-extra'; import { initializeGit } from './utils/git'; import { install } from './utils/install'; -import { isFolderEmpty } from './utils/is-folder-empty'; -import { isWriteable } from './utils/is-writeable'; import { colors, logger } from './utils/logger'; import { nextSteps } from './utils/next-steps'; import { downloadAndExtractRepo, verifyURL } from './utils/repo'; @@ -40,31 +38,15 @@ export async function createApp({ ); console.log(); - const root = path.resolve(projectPath); - - if (!(await isWriteable(path.dirname(root)))) { - console.error( - 'The application path is not writable, please check folder permissions and try again.' - ); - console.error( - 'It is likely you do not have write permissions for this folder.' - ); - process.exit(1); - } - - const appName = path.basename(root); - - await fs.promises.mkdir(root, { recursive: true }); - if (!isFolderEmpty(root, appName)) { - process.exit(1); - } - const repoInfo = await verifyURL(example); + const root = path.resolve(projectPath); console.log(`Creating a new OPub app in ${colors.success(root)}.`); console.log(); - // change current directory to the project directory + // create the project directory + await fs.promises.mkdir(root, { recursive: true }); + // change current directory to the project process.chdir(root); try { @@ -82,14 +64,10 @@ export async function createApp({ const packageJsonPath = path.join(root, 'package.json'); let hasPackageJson = fs.existsSync(packageJsonPath); if (hasPackageJson) { - console.log( - `Installing packages using ${packageManager}. Grab a cup of chai, this might take a while.` - ); - console.log(); - // add changes to package.json const pkgJson = fse.readJSONSync(packageJsonPath); // update the project name in package json + const appName = path.basename(root); pkgJson.name = appName; // add opub-ui as a dependency pkgJson.dependencies['opub-ui'] = 'latest'; @@ -99,6 +77,10 @@ export async function createApp({ // install all packages in the project if (!noInstall) { + console.log( + `Installing packages using ${packageManager}. Grab a cup of chai, this might take a while.` + ); + console.log(); await install(packageManager); console.log(); } diff --git a/packages/create-opub-app/index.ts b/packages/create-opub-app/index.ts index 08e05819..e78de4bb 100644 --- a/packages/create-opub-app/index.ts +++ b/packages/create-opub-app/index.ts @@ -4,12 +4,14 @@ import path from 'path'; import { createApp } from './create-app'; import packageJson from './package.json'; -import { examples } from './utils/constants'; +import { examples, managers } from './utils/constants'; import { initCli } from './utils/initCli'; import { isFolderEmpty } from './utils/is-folder-empty'; +import { isWriteable } from './utils/is-writeable'; import { colors } from './utils/logger'; import { prompts } from './utils/prompts'; import { renderTitle } from './utils/renderTitle'; +import { validateAppName } from './utils/validateAppName'; // terminate process on these signals const handleSigTerm = () => process.exit(0); @@ -20,9 +22,18 @@ process.on('SIGTERM', handleSigTerm); const initOptions = initCli(packageJson); async function run(): Promise { + /** + * Get valid project name + */ let projectPath = initOptions.path; if (typeof projectPath === 'string') { projectPath = projectPath.trim(); + if (validateAppName(projectPath)) { + console.error( + `The project name provided is not valid. Please provide a valid name.` + ); + process.exit(1); + } } /** @@ -64,7 +75,7 @@ async function run(): Promise { ? initOptions.manager.toLowerCase().trim() : null; - if (manager && !['npm', 'pnpm', 'yarn', 'bun'].includes(manager)) { + if (manager && !Object.keys(managers).includes(manager)) { console.error( `The package manager provided is not supported. Please provide a valid manager. We support ${colors.success('d4d')} and ${colors.success('data-exchange')}` ); @@ -87,16 +98,26 @@ async function run(): Promise { const resolvedProjectPath = path.resolve(promptOptions.projectPath); /** - * Verify the project dir is empty or doesn't exist + * Verify the project dir is empty or doesn't exist or is a writable folder */ const root = path.resolve(resolvedProjectPath); const appName = path.basename(root); - const folderExists = fs.existsSync(root); + const folderExists = fs.existsSync(root); if (folderExists && !isFolderEmpty(root, appName)) { process.exit(1); } + if (!(await isWriteable(path.dirname(root)))) { + console.error( + 'The application path is not writable, please check folder permissions and try again.' + ); + console.error( + 'It is likely you do not have write permissions for this folder.' + ); + process.exit(1); + } + /** * Create the app */ diff --git a/packages/create-opub-app/package.json b/packages/create-opub-app/package.json index 4514d55c..0ce585f2 100644 --- a/packages/create-opub-app/package.json +++ b/packages/create-opub-app/package.json @@ -1,6 +1,6 @@ { "name": "create-opub-app", - "version": "0.1.1", + "version": "0.1.2", "publishConfig": { "access": "public" }, diff --git a/packages/create-opub-app/utils/constants.ts b/packages/create-opub-app/utils/constants.ts index 0f573c37..de86588c 100644 --- a/packages/create-opub-app/utils/constants.ts +++ b/packages/create-opub-app/utils/constants.ts @@ -11,6 +11,15 @@ export const examples: { }, }; +export const managers: { + [key: string]: string; +} = { + npm: 'NPM', + pnpm: 'PNPM', + yarn: 'Yarn', + bun: 'Bun', +}; + export const TITLE_TEXT = ` ____ _ ___ ____ _ _ / ___|_ __ ___ __ _| |_ ___ / _ \\| _ \\ _ _| |__ / \\ _ __ _ __ | | | '__/ _ \\/ \_\` | __/ _ \\ | | | | |_) | | | | '_ \\ / _ \\ | '_ \\| '_ \\ diff --git a/packages/create-opub-app/utils/prompts.ts b/packages/create-opub-app/utils/prompts.ts index dfd49c44..da345bad 100644 --- a/packages/create-opub-app/utils/prompts.ts +++ b/packages/create-opub-app/utils/prompts.ts @@ -1,6 +1,6 @@ import * as p from '@clack/prompts'; -import { examples } from './constants'; +import { examples, managers } from './constants'; import { validateAppName } from './validateAppName'; export async function prompts({ @@ -43,12 +43,10 @@ export async function prompts({ manager: () => { return p.select({ message: 'Which package manager will you use?', - options: [ - { value: 'npm', label: 'NPM' }, - { value: 'pnpm', label: 'PNPM' }, - { value: 'yarn', label: 'Yarn' }, - { value: 'bun', label: 'Bun' }, - ], + options: Object.keys(managers).map((item) => ({ + value: item, + label: managers[item], + })), initialValue: 'npm', }); }, diff --git a/packages/opub-ui/package.json b/packages/opub-ui/package.json index 126de354..c919724e 100644 --- a/packages/opub-ui/package.json +++ b/packages/opub-ui/package.json @@ -1,7 +1,7 @@ { "name": "opub-ui", "description": "OPub UI Library", - "version": "0.1.9", + "version": "0.2.0", "private": false, "license": "MIT", "author": "CivicDataLab ",