@eljs/create-preset-structure
Version:
Project structure template powered by @eljs/create.
87 lines (79 loc) • 2.23 kB
text/typescript
import {
getWorkspaces,
gitCommit,
type PackageJson,
readJsonSync,
} from '@eljs/utils'
import inquirer, { type Answers } from 'inquirer'
import autocomplete from 'inquirer-autocomplete-prompt'
import path from 'node:path'
import { $ } from 'zx'
$.verbose = true
const COMMIT_TYPES: string[] = [
'feat',
'fix',
'docs',
'style',
'refactor',
'chore',
'perf',
'test',
'revert',
'merge',
'sync',
]
main()
.then(() => process.exit(0))
.catch(error => {
console.error(`git commit error: ${error.message}`)
process.exit(1)
})
async function main(): Promise<void> {
const rootPath = path.resolve(__dirname, '../')
const workspaces = await getWorkspaces(rootPath)
const pkgNames = workspaces.map(workspace => {
return readJsonSync<PackageJson>(path.resolve(workspace, 'package.json'))
.name as string
})
const questions = [
{
type: 'autocomplete',
name: 'type',
message:
'Please select the commit type(support fuzzy matching and custom input)',
source: function (_: unknown, input: string): Promise<string[]> {
input = input || ''
return new Promise(resolve => {
const fuzzyResult = COMMIT_TYPES.filter(type =>
type.toLowerCase().includes(input.toLowerCase()),
)
resolve(fuzzyResult.length ? fuzzyResult : [input])
})
},
},
{
type: 'autocomplete',
name: 'scope',
message:
'Please select the commit scope(support fuzzy matching and custom input)',
source: function (_: unknown, input: string): Promise<string[]> {
input = input || ''
return new Promise(resolve => {
const fuzzyResult = pkgNames.filter(scope =>
scope.toLowerCase().includes(input.toLowerCase()),
)
resolve(fuzzyResult.length ? fuzzyResult : [input])
})
},
},
{
type: 'input',
name: 'subject',
message: 'Please input the commit subject',
},
]
inquirer.registerPrompt('autocomplete', autocomplete)
inquirer.prompt(questions).then(async (answers: Answers) => {
await gitCommit(`${answers.type}(${answers.scope}): ${answers.subject}`)
})
}