Skip to content

Commit

Permalink
feat: configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Sep 5, 2022
1 parent 3959399 commit 0c116be
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
25 changes: 25 additions & 0 deletions README.md
Expand Up @@ -8,6 +8,31 @@ Command-line for creating projects from templates.
npm i -g @sxzz/create
```

## Configuration

The configuration file is located in `$HOME/.config/create-templates.yml`

[TypeScript Schema](https://github.com/sxzz/create/blob/main/src/template.ts#L7-L13)

```yaml
- id: lib # An unique id
name: Library
# color: '#008800' # optional
children:
- id: ts
name: TypeScript
color: '#3178c6'
url: 'git@github.com:sxzz/node-lib-starter.git' # remote URL or local path
- id: webapp
name: Web App
url: xxxxx
```

## TODO

- modify name in `package.json` and `README.md`
- update deps

## Sponsors

<p align="center">
Expand Down
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -24,18 +24,21 @@
"lint": "eslint . --ext .vue,.js,.ts,.jsx,.tsx,.json,.md",
"lint:fix": "pnpm run lint --fix",
"build": "tsup && tsx scripts/postbuild.mts",
"dev": "tsx ./src/index.ts",
"release": "bumpp && pnpm publish",
"prepublishOnly": "pnpm run build"
},
"dependencies": {
"chalk": "^5.0.1",
"consola": "^2.15.3",
"degit": "^2.8.4",
"enquirer": "^2.3.6"
"enquirer": "^2.3.6",
"js-yaml": "^4.1.0"
},
"devDependencies": {
"@sxzz/eslint-config": "^2.4.2",
"@types/degit": "^2.8.3",
"@types/js-yaml": "^4.0.5",
"@types/node": "*",
"bumpp": "^8.2.1",
"eslint": "^8.23.0",
Expand Down
10 changes: 8 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/index.ts
Expand Up @@ -4,12 +4,12 @@ import degit from 'degit'
import consola from 'consola'
import chalk from 'chalk'
import { getColor } from './utils'
import { templates } from './template'
import { loadTemplates } from './template'
import type { Template } from './template'

let currentTemplates = templates

async function run() {
const templates = await loadTemplates()
let currentTemplates = templates
do {
const answer = await enquirer.prompt<{ id: string }>({
type: 'select',
Expand Down
43 changes: 29 additions & 14 deletions src/template.ts
@@ -1,3 +1,9 @@
import { existsSync } from 'node:fs'
import { homedir } from 'node:os'
import path from 'node:path'
import { mkdir, readFile, writeFile } from 'node:fs/promises'
import { load } from 'js-yaml'

export interface Template {
id: string
name: string
Expand All @@ -6,17 +12,26 @@ export interface Template {
url?: string
}

export const templates: Template[] = [
{
id: 'lib',
name: 'Library',
children: [
{
id: 'ts',
name: 'TypeScript',
color: '#3178c6',
url: 'git@github.com:sxzz/node-lib-starter.git',
},
],
},
]
const demoTemplates = `
- id: lib # An unique id
name: Library
# color: '#008800' # optional
children:
- id: ts
name: TypeScript
color: '#3178c6'
url: 'git@github.com:sxzz/node-lib-starter.git' # remote URL or local path
`.trim()

export const loadTemplates = async (): Promise<Template[]> => {
const filePath = path.resolve(homedir(), '.config/create-templates.yml')
if (!existsSync(filePath)) {
mkdir(path.dirname(filePath), { recursive: true }).catch(() => undefined)
writeFile(filePath, demoTemplates)
throw new Error(
`Template configuration not found. A new configuration file is generated, please check ${filePath}`
)
}
const contents = await readFile(filePath, 'utf-8')
return load(contents) as any
}

0 comments on commit 0c116be

Please sign in to comment.