Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
feat(cli): add select command to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook committed Feb 11, 2024
1 parent 8b88742 commit 0341219
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
98 changes: 98 additions & 0 deletions packages-core/cli/src/commands/select.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { readFileSync } from 'node:fs'
import { join, resolve } from 'node:path'
import { execSync } from 'node:child_process'
import { intro, select } from '@clack/prompts'
import { defineCommand } from 'citty'
import consola from 'consola'
import type { PergelReadme } from '../types'
import { definePergelLoadConfig } from '../core'

export default defineCommand({
meta: {
name: 'Pergel Upgrade',
description: 'Upgrade Pergel CLI',
version: '0.0.1',
},
async run() {
try {
const file = await definePergelLoadConfig()

if (!file.config) {
consola.error('No config file found')
return
}

intro('Oku Pergel Select CLI')

const readmeString = readFileSync(resolve(join(file.config.dir?.pergel, 'README.json')), 'utf-8')
const jsonData: PergelReadme = JSON.parse(readmeString)

const projectNames = Object.keys(jsonData).filter((i) => {
const project = jsonData[i]
if (!project)
return false

for (const key in project) {
if (project[key].cli)
return true
}

return false
})

if (!projectNames.length) {
consola.error('No projects found')
return
}

const selectedProject = await select({
message: 'Select a project',
options: projectNames.map(i => ({
label: i,
value: i,
})),
})

const selectedProjectData = jsonData[selectedProject as string]
const cliModules = Object.keys(selectedProjectData).filter((i) => {
const cli = selectedProjectData[i].cli
if (cli)
return true

return false
})

const selectedModule = await select({
message: 'Select a module',
options: cliModules.map(i => ({
label: i,
value: i,
})),
}) as string

if (selectedModule) {
const selectedCli = await select({
message: 'Select a cli command',
options: Object.keys((selectedProjectData[selectedModule] as any).cli).map(i => ({
label: i,
value: i,
})),
})

const cliData = (selectedProjectData[selectedModule] as any).cli[selectedCli as string]
if (cliData) {
consola.info(`Running ${cliData}`)
execSync(
cliData,
{
stdio: 'inherit',
},
)
}
}
}
catch (error) {
consola.error(error)
}
},
})
1 change: 1 addition & 0 deletions packages-core/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const main = defineCommand({
install: () => import('./commands/install').then(m => m.default),
module: () => import('./commands/module').then(m => m.default),
download: () => import('./commands/download').then(m => m.default),
select: () => import('./commands/select').then(m => m.default),
},
run({ args }) {
if (args.version)
Expand Down
7 changes: 5 additions & 2 deletions packages-core/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export interface ResolvedPergelConfig {
}

export interface PergelReadme {
[moduleName: string]: {
[projectName: string]: {
[projectName: string]: {
[moduleName: string]: {
packageJson?: {
dependencies?: string
devDependencies?: string
Expand All @@ -76,6 +76,9 @@ export interface PergelReadme {
env?: {
[envName: string]: string
}
cli?: {
[cliName: string]: string
}
}
}
}
Expand Down

0 comments on commit 0341219

Please sign in to comment.