Skip to content

Commit

Permalink
feat: add command to initialize kit
Browse files Browse the repository at this point in the history
  • Loading branch information
recoluan committed May 19, 2024
1 parent b61eaf4 commit 3101b2e
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@vitejs/plugin-vue-jsx": "3.1.0",
"autoprefixer": "10.4.13",
"cac": "6.7.14",
"gitly": "3.0.0",
"naive-ui": "2.34.2",
"postcss": "8.4.21",
"postcss-each": "1.1.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createUseCommand,
createRemoveCommand,
createListCommand,
createInitKitCommand
} from './command/index.js'

// import {getMergedConfig} from './config/getMergedConfig.js'
Expand All @@ -23,6 +24,7 @@ import {
createUseCommand(program)
createRemoveCommand(program)
createListCommand(program)
createInitKitCommand(program)

const reviliCache = await getReviliCache()
if (reviliCache.activeKit) {
Expand Down
98 changes: 98 additions & 0 deletions packages/core/src/node/command/createInitKitCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {CAC} from 'cac'
import fs from 'node:fs'
import path from 'node:path'
import { default as gitly } from 'gitly'
import { inquirer, spinner, chalk } from '@revili/shared/node'

import { CWD } from '../alias.js'
import { consoleUtil } from '../utils/index.js'

export function createInitKitCommand(program: CAC) {
program
.command('create:kit', 'Use kit')
.action(async kit => {
const { kitName, webFramework } = await handleInquirer()
const gitScope = 'revilijs'
const gitRepo = `revili-kit-demo-${webFramework}`

spinner.start(chalk.blue(`[revili] Load file from https://github.com/${gitScope}/${gitRepo}`))

try {
const targetDir = kitName ?? gitRepo
// @ts-ignore
await gitly.default(`${gitScope}/${gitRepo}`, path.join(CWD, targetDir))
spinner.succeed(`[revili] Load file from https://github.com/${gitScope}/${gitRepo}`)


await changePackage(kitName, targetDir)

spinner.stop()

console.log()
consoleUtil.success('[revili] Load successful, enjoy it!')
console.log()
} catch(error) {
spinner.fail(chalk.redBright(`[revili] Load file from git`))
consoleUtil.error(error)
spinner.stop()
}
})
}

interface Answers {
kitName: string
webFramework: 'vue' | 'react'
}

function handleInquirer(): Promise<Answers> {
const questions = [
{
name: 'kitName',
type: 'input',
message: `What's the name of your kit? (.e.g revili-kit-xxx)`
},
{
name: 'webFramework',
type: 'list',
message: `What framework do you want to use in web client?`,
choices: ['vue'],
filter: function (val: any) {
return val.toLowerCase()
}
}
]

return new Promise((resolve, reject) => {
inquirer
.prompt(questions)
.then((answers: any) => {
resolve(answers)
})
.catch((err: any) => {
reject(err)
})
})
}

function changePackage (kitName: string, targetDir: string) {
spinner.start(chalk.blue(`[revili] Edit kit name`))

return new Promise((resolve) => {
const packageJsonPath = `${CWD}/${targetDir}/package.json`
fs.readFile(packageJsonPath, (err, data) => {
if (err) throw err
const _data = JSON.parse(data.toString())
_data.name = kitName
const content = JSON.stringify(_data, null, 2)
fs.writeFile(packageJsonPath, content, (err) => {
if (!err) {
spinner.succeed(chalk.blue(`[revili] Edit kit name`))
resolve('')
} else {
spinner.fail(chalk.blue(`[revili] Edit kit name`))
throw err
}
})
})
})
}
1 change: 1 addition & 0 deletions packages/core/src/node/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './createKitCommands.js'
export * from './createUseCommand.js'
export * from './createRemoveCommand.js'
export * from './createListCommand.js'
export * from './createInitKitCommand.js'
Loading

0 comments on commit 3101b2e

Please sign in to comment.