Skip to content

Commit

Permalink
CLI: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Feb 7, 2024
1 parent 322fafe commit ae58873
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 40 deletions.
36 changes: 9 additions & 27 deletions packages/create-opub-app/create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand All @@ -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';
Expand All @@ -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();
}
Expand Down
29 changes: 25 additions & 4 deletions packages/create-opub-app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -20,9 +22,18 @@ process.on('SIGTERM', handleSigTerm);
const initOptions = initCli(packageJson);

async function run(): Promise<void> {
/**
* 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);
}
}

/**
Expand Down Expand Up @@ -64,7 +75,7 @@ async function run(): Promise<void> {
? 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')}`
);
Expand All @@ -87,16 +98,26 @@ async function run(): Promise<void> {
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
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/create-opub-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-opub-app",
"version": "0.1.1",
"version": "0.1.2",
"publishConfig": {
"access": "public"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/create-opub-app/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ` ____ _ ___ ____ _ _
/ ___|_ __ ___ __ _| |_ ___ / _ \\| _ \\ _ _| |__ / \\ _ __ _ __
| | | '__/ _ \\/ \_\` | __/ _ \\ | | | | |_) | | | | '_ \\ / _ \\ | '_ \\| '_ \\
Expand Down
12 changes: 5 additions & 7 deletions packages/create-opub-app/utils/prompts.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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',
});
},
Expand Down
2 changes: 1 addition & 1 deletion packages/opub-ui/package.json
Original file line number Diff line number Diff line change
@@ -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 <tech@civicdatalab.in>",
Expand Down

0 comments on commit ae58873

Please sign in to comment.