mirror of https://github.com/YunYouJun/valaxy
feat(cli): support new folder by name, close #563
This commit is contained in:
parent
7f87c55bfa
commit
5efe8ddfdc
|
@ -14,6 +14,7 @@
|
|||
"fuse": "valaxy fuse",
|
||||
"preview-https": "serve dist",
|
||||
"new": "valaxy new",
|
||||
"new:f": "valaxy new -f",
|
||||
"rss": "valaxy rss",
|
||||
"serve": "vite preview"
|
||||
},
|
||||
|
|
|
@ -115,15 +115,19 @@ pnpm add -g valaxy
|
|||
|
||||
:::
|
||||
|
||||
## 文章 {lang="zh-CN"}
|
||||
### 文章 {lang="zh-CN"}
|
||||
|
||||
## Posts {lang="en"}
|
||||
### Posts {lang="en"}
|
||||
|
||||
::: zh-CN
|
||||
|
||||
- `valaxy new <title>`: 在 `pages/posts` 目录下新建标题为 `title` 的帖子(.md)
|
||||
- `-f` 以文件夹的形式创建。
|
||||
|
||||
譬如,`valaxy new your-first-post`,将会在 `pages/posts` 下自动新建 `your-first-post.md` 文件,并附带日期。
|
||||
譬如:
|
||||
|
||||
- `valaxy new your-first-post`,将会在 `pages/posts` 下自动新建 `your-first-post.md` 文件,并附带日期。
|
||||
- `valaxy new -f your-first-post`,将会在 `pages/posts` 下自动新建 `your-first-post/index.md` 文件。
|
||||
|
||||
> 你觉得还可以有其他更常用、更好用的命令?没问题,尽管来 [Issues](https://github.com/YunYouJun/valaxy/issues) 反馈吧!
|
||||
:::
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
<script setup lang="ts">
|
||||
import { useSiteConfig } from 'valaxy'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
|
||||
const siteConfig = useSiteConfig()
|
||||
const { t } = useI18n()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="siteConfig.author.intro" class="site-author-intro" m="t-0 b-4">
|
||||
{{ siteConfig.author.intro }}
|
||||
{{ t(siteConfig.author.intro) }}
|
||||
</div>
|
||||
</template>
|
||||
|
|
|
@ -31,7 +31,7 @@ const licenseHtml = computed(() => {
|
|||
<strong>
|
||||
{{ t('post.copyright.author') + t('symbol.colon') }}
|
||||
</strong>
|
||||
<span>{{ siteConfig.author.name }}</span>
|
||||
<span>{{ t(siteConfig.author.name) }}</span>
|
||||
</li>
|
||||
<li v-if="url" class="post-copyright-link">
|
||||
<strong>
|
||||
|
|
|
@ -3,7 +3,6 @@ import path from 'node:path'
|
|||
import process from 'node:process'
|
||||
import { consola } from 'consola'
|
||||
import fs from 'fs-extra'
|
||||
import { exists } from './utils/fs'
|
||||
|
||||
export async function cleanDist() {
|
||||
const distDir = path.join(process.cwd(), 'dist')
|
||||
|
@ -12,7 +11,7 @@ export async function cleanDist() {
|
|||
consola.box('🧹 Starting clean...')
|
||||
|
||||
// Clean the dist directory
|
||||
if (await exists(distDir)) {
|
||||
if (await fs.exists(distDir)) {
|
||||
consola.info('dist directory exists, removing...')
|
||||
try {
|
||||
await fs.rm(distDir, { recursive: true, force: true })
|
||||
|
@ -28,7 +27,7 @@ export async function cleanDist() {
|
|||
}
|
||||
|
||||
// Clean the cache directory
|
||||
if (await exists(cacheDir)) {
|
||||
if (await fs.exists(cacheDir)) {
|
||||
consola.info('.valaxy cache directory exists, removing...')
|
||||
try {
|
||||
await fs.rm(cacheDir, { recursive: true, force: true })
|
||||
|
|
|
@ -16,6 +16,12 @@ export function registerNewCommand(cli: Argv<object>) {
|
|||
describe: 'The title of the new post',
|
||||
required: true,
|
||||
})
|
||||
.option('folder', {
|
||||
alias: 'f',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
describe: 'Generate a new post in a folder',
|
||||
})
|
||||
.option('path', {
|
||||
alias: 'p',
|
||||
type: 'string',
|
||||
|
@ -35,12 +41,13 @@ export function registerNewCommand(cli: Argv<object>) {
|
|||
.strict()
|
||||
.help()
|
||||
},
|
||||
async ({ title, path, date, layout }) => {
|
||||
async ({ title, folder, path, date, layout }) => {
|
||||
await create({
|
||||
title,
|
||||
date,
|
||||
layout,
|
||||
path,
|
||||
folder,
|
||||
} as CreatePostParams)
|
||||
},
|
||||
)
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
import type { PathLike } from 'node:fs'
|
||||
import { access } from 'node:fs/promises'
|
||||
|
||||
export async function exists(path: PathLike) {
|
||||
try {
|
||||
await access(path)
|
||||
return true
|
||||
}
|
||||
// eslint-disable-next-line unused-imports/no-unused-vars
|
||||
catch (e) {
|
||||
return false
|
||||
}
|
||||
}
|
|
@ -1,12 +1,10 @@
|
|||
import { writeFile } from 'node:fs/promises'
|
||||
import { join, resolve } from 'node:path'
|
||||
import { ensureSuffix } from '@antfu/utils'
|
||||
import { dirname, join, resolve } from 'node:path'
|
||||
import { consola } from 'consola'
|
||||
import { colors } from 'consola/utils'
|
||||
import dayjs from 'dayjs'
|
||||
import { render } from 'ejs'
|
||||
import fs from 'fs-extra'
|
||||
import { defaultPostTemplate, userRoot } from './constants'
|
||||
import { exists } from './fs'
|
||||
|
||||
import { getTemplate } from './scaffold'
|
||||
|
||||
|
@ -15,41 +13,38 @@ export interface CreatePostParams {
|
|||
* generate date in frontmatter
|
||||
*/
|
||||
date?: boolean
|
||||
/**
|
||||
* folder
|
||||
* @zh-CN 生成文件夹
|
||||
*/
|
||||
folder?: boolean
|
||||
path: string
|
||||
layout?: string
|
||||
title: string
|
||||
}
|
||||
|
||||
export async function create(data: CreatePostParams) {
|
||||
const pagesPath = resolve(userRoot, 'pages')
|
||||
const {
|
||||
path,
|
||||
title,
|
||||
} = data
|
||||
const postPath = path || join('posts', title)
|
||||
export async function create(params: CreatePostParams) {
|
||||
const pagesPath = resolve(userRoot, params.path || 'pages')
|
||||
|
||||
let counter = 0
|
||||
|
||||
while (true) {
|
||||
let destinationPath = resolve(pagesPath, postPath)
|
||||
const postFileName = `${params.title}${counter ? `-${counter}` : ''}`
|
||||
const postFilePath = params.folder ? join(postFileName, 'index.md') : `${postFileName}.md`
|
||||
const targetPath = resolve(pagesPath, 'posts', postFilePath)
|
||||
|
||||
if (counter)
|
||||
destinationPath = `${destinationPath}-${counter}`
|
||||
|
||||
destinationPath = ensureSuffix('.md', destinationPath)
|
||||
|
||||
if (!await exists(destinationPath)) {
|
||||
const content = await genLayoutTemplate(data)
|
||||
if (!await fs.exists(targetPath)) {
|
||||
await fs.ensureDir(dirname(targetPath))
|
||||
const content = await genLayoutTemplate(params)
|
||||
try {
|
||||
await writeFile(destinationPath, content, 'utf-8')
|
||||
consola.success(`[valaxy new]: successfully generated file ${colors.magenta(destinationPath)}`)
|
||||
await fs.writeFile(targetPath, content, 'utf-8')
|
||||
consola.success(`[valaxy new]: successfully generated file ${colors.magenta(targetPath)}`)
|
||||
}
|
||||
catch (e) {
|
||||
console.log(e)
|
||||
consola.error(`[valaxy new]: failed to write file ${destinationPath}`)
|
||||
consola.error(`[valaxy new]: failed to write file ${targetPath}`)
|
||||
consola.warn(`You should run ${colors.green('valaxy new')} in your valaxy project root directory.`)
|
||||
}
|
||||
return destinationPath
|
||||
return targetPath
|
||||
}
|
||||
counter++
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { readFile } from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
import fs from 'fs-extra'
|
||||
import { resolveOptions } from '../../options'
|
||||
import { userRoot } from './constants'
|
||||
import { exists } from './fs'
|
||||
|
||||
export async function getTemplate(layout: string): Promise<string | false> {
|
||||
const { clientRoot, themeRoot } = await resolveOptions({ userRoot })
|
||||
|
@ -10,7 +10,7 @@ export async function getTemplate(layout: string): Promise<string | false> {
|
|||
|
||||
for (const root of roots) {
|
||||
const scaffoldPath = path.resolve(root, 'scaffolds', `${layout}.md`)
|
||||
if (await exists(scaffoldPath))
|
||||
if (await fs.exists(scaffoldPath))
|
||||
return readFile(scaffoldPath, 'utf-8')
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue