Skip to content

Commit

Permalink
style: format codes
Browse files Browse the repository at this point in the history
  • Loading branch information
w2xi committed Mar 30, 2024
1 parent 4597924 commit 055acb2
Show file tree
Hide file tree
Showing 14 changed files with 1,900 additions and 736 deletions.
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": false,
"singleQuote": true
}
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"editor.tabSize": 2
}
"editor.tabSize": 2
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Install `migi-cli` globally:
```bash
$ npm i @w2xi/migi-cli -g
```

Create an app interactively:

```bash
Expand All @@ -31,13 +31,13 @@ $ migi list

nm - A template for creating a new npm module, preferred node-cli tools
vue-erciyuan-admin - A template designed for those who love anime, based on Vue3 + Vite + UnoCSS

```

## Available Templates

- [nm](https://github.com/migi-templates/nm): nm - A template for creating a new npm module, preferred node-cli tools
- [vue-erciyuan-admin](https://github.com/migi-templates/vue-erciyuan-admin): A template designed for those who love anime, based on Vue3 + Vite + UnoCSS
- [vue-erciyuan-admin](https://github.com/migi-templates/vue-erciyuan-admin): A template designed for those who love anime, based on Vue3 + Vite + UnoCSS

## Options

Expand All @@ -59,4 +59,4 @@ Commands:

## License

[MIT](./LICENSE)
[MIT](./LICENSE)
94 changes: 53 additions & 41 deletions bin/migi-create.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import { Command } from 'commander'
import { promisify } from "util"
import { promisify } from 'node:util'
import { homedir } from 'node:os'
import inquirer from 'inquirer'
import fse from 'fs-extra'
Expand Down Expand Up @@ -45,55 +45,60 @@ if (options.offline) {
}

if (fse.pathExistsSync(destination)) {
inquirer.prompt([
{
type: 'confirm',
message: 'Target directory exists. Continue?',
name: 'ok'
}
]).then(answers => {
if (answers.ok) {
run()
}
}).catch(console.error)
inquirer
.prompt([
{
type: 'confirm',
message: 'Target directory exists. Continue?',
name: 'ok',
},
])
.then((answers) => {
if (answers.ok) {
run()
}
})
.catch(console.error)
} else {
run()
}

async function run() {
const answers: AnswerOptions = Object.create(null)
if (
!options.offline &&
!options.template &&
args.length === 1
) {
await fetchTemplates().then(async (res: GitHubRepo[])=> {
await inquirer.prompt([
{
type: 'list',
name: 'template',
message: 'Select a template:',
choices: res.map((repo: GitHubRepo) => {
return {
name: repo.name,
value: repo.name,
description: repo.description
}
if (!options.offline && !options.template && args.length === 1) {
await fetchTemplates()
.then(async (res: GitHubRepo[]) => {
await inquirer
.prompt([
{
type: 'list',
name: 'template',
message: 'Select a template:',
choices: res.map((repo: GitHubRepo) => {
return {
name: repo.name,
value: repo.name,
description: repo.description,
}
}),
},
])
.then((result) => {
Object.assign(answers, result)
})
}
]).then(result => {
Object.assign(answers, result)
})
}).catch(err => {
console.log('Failed to fetch template list: ' + chalk.red(err.toString()))
process.exit()
})
.catch((err) => {
console.log(
'Failed to fetch template list: ' + chalk.red(err.toString())
)
process.exit()
})
}

setPromptDefault('name', projectName)
setValidateName()

ask().then(result => {
ask().then((result) => {
Object.assign(answers, result)
const opts = getOptions(answers, { projectName })
downloadAndGenerate(opts)
Expand Down Expand Up @@ -121,12 +126,19 @@ function downloadAndGenerate(answers: AnswerOptions) {
console.log()
spinner.succeed('Successful download template!')
generate(templatePath, destination, answers)
}).catch((err: Error) => {
})
.catch((err: Error) => {
console.log()
spinner.fail('Failed to download repo ' + officialTemplate + ': ' + err.message.trim())
spinner.fail(
'Failed to download repo ' +
officialTemplate +
': ' +
err.message.trim()
)
console.log()
}).finally(() => {
})
.finally(() => {
spinner.stop()
})
}
}
}
22 changes: 11 additions & 11 deletions bin/migi-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ process.on('exit', () => {
* List all available repos.
*/

fetchTemplates().then((res: GitHubRepo[]) => {
console.log(' Migi available templates:')
console.log()
res.forEach(repo => {
console.log(
' ' + chalk.blue(repo.name) +
' - ' + repo.description)
fetchTemplates()
.then((res: GitHubRepo[]) => {
console.log(' Migi available templates:')
console.log()
res.forEach((repo) => {
console.log(' ' + chalk.blue(repo.name) + ' - ' + repo.description)
})
})
.catch((err) => {
console.log('Failed to fetch template list: ' + chalk.red(err.toString()))
process.exit(1)
})
}).catch(err => {
console.log('Failed to fetch template list: ' + chalk.red(err.toString()))
process.exit(1)
})
2 changes: 1 addition & 1 deletion bin/migi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ program
.description('Migi CLI')
.command('list', 'list available templates')
.command('create <project-name>', 'create a new project')
.parse(process.argv)
.parse(process.argv)
26 changes: 13 additions & 13 deletions lib/ask.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import inquirer from "inquirer"
import validatePkgName from "validate-npm-package-name"
import type { Question } from "inquirer"
import inquirer from 'inquirer'
import validatePkgName from 'validate-npm-package-name'
import type { Question } from 'inquirer'

export const prompts: Record<string, Question> = {
name: {
name: "name",
type: "string",
message: "Project name",
name: 'name',
type: 'string',
message: 'Project name',
},
version: {
name: 'version',
type: 'string',
message: 'Project version',
default: '0.0.1'
default: '0.0.1',
},
description: {
name: "description",
type: "string",
message: "Project description",
name: 'description',
type: 'string',
message: 'Project description',
},
author: {
name: "author",
type: "string",
message: "Author",
name: 'author',
type: 'string',
message: 'Author',
},
}

Expand Down
23 changes: 12 additions & 11 deletions lib/fetch-templates.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
export default async function fetchTemplates() {
return new Promise((resolve, reject) => {
fetch('https://api.github.com/users/migi-templates/repos')
.then(res => res.json())
.then(res => {
if (Array.isArray(res)) {
resolve(res)
} else {
reject(res.message)
}
}).catch(err => {
reject(err)
})
.then((res) => res.json())
.then((res) => {
if (Array.isArray(res)) {
resolve(res)
} else {
reject(res.message)
}
})
.catch((err) => {
reject(err)
})
})
}
}
40 changes: 20 additions & 20 deletions lib/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ const installDepCommand = {
}

export default async function generate(
templateDir: string,
destination: string,
templateDir: string,
destination: string,
data: AnswerOptions
) {
copy(templateDir, destination)
renderEjs(destination, data)
.then(async () => {
removeTemplateDir(destination)

const detectAgent = await detectPackageManager(destination) || 'npm'
const detectAgent = (await detectPackageManager(destination)) || 'npm'
const [agent] = detectAgent.split('@')
const projectName = path.basename(destination)

console.log()
console.log(chalk.green('Create app successfully!'))
console.log()
Expand All @@ -36,7 +36,8 @@ export default async function generate(
console.log(` cd ${projectName}`)
console.log(` ${installDepCommand[agent]}`)
console.log()
}).catch((err) => {
})
.catch((err) => {
console.error(err)
// remove the generated files
fse.removeSync(destination)
Expand All @@ -52,24 +53,23 @@ function renderEjs(dir: string, data: AnswerOptions) {
}

return new Promise<void>((resolve, reject) => {
const files = fg.sync(
'**/*',
{
cwd: templateDirInside,
dot: true,
ignore: ['**/node_modules/**'],
}
)
const files = fg.sync('**/*', {
cwd: templateDirInside,
dot: true,
ignore: ['**/node_modules/**'],
})
Promise.all(
files.map(file => {
files.map((file) => {
const filepath = path.join(templateDirInside, file)
return renderFile(filepath, data)
})
).then(() => {
resolve()
}).catch((err) => {
reject(err)
})
)
.then(() => {
resolve()
})
.catch((err) => {
reject(err)
})
})
}

Expand Down Expand Up @@ -106,4 +106,4 @@ function removeTemplateDir(dir: string) {
if (fse.pathExistsSync(templateDirInside)) {
fse.removeSync(templateDirInside)
}
}
}
8 changes: 4 additions & 4 deletions lib/git-user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { execSync } from "node:child_process"
import { execSync } from 'node:child_process'

export default function getUser() {
let author
Expand All @@ -11,9 +11,9 @@ export default function getUser() {

author = author ? author.toString().trim() : ''
email = email ? email.toString().trim() : ''

return {
email,
author
author,
}
}
}
Loading

0 comments on commit 055acb2

Please sign in to comment.