cli: sort files

This commit is contained in:
Benjamin Canac
2024-04-18 11:42:28 +02:00
parent 30debff9cb
commit 69539a62b4
2 changed files with 25 additions and 10 deletions

View File

@@ -3,6 +3,7 @@ import { resolve } from 'pathe'
import { defineCommand } from 'citty' import { defineCommand } from 'citty'
import { consola } from 'consola' import { consola } from 'consola'
import { splitByCase, upperFirst, camelCase, kebabCase } from 'scule' import { splitByCase, upperFirst, camelCase, kebabCase } from 'scule'
import { appendFile, sortFile } from '../utils.mjs'
import templates from '../templates.mjs' import templates from '../templates.mjs'
export default defineCommand({ export default defineCommand({
@@ -41,17 +42,14 @@ export default defineCommand({
} }
const themePath = resolve(path, 'src/theme/index.ts') const themePath = resolve(path, 'src/theme/index.ts')
const theme = await fsp.readFile(themePath, 'utf-8') const themeContent = `export { default as ${camelCase(name)} } from './${kebabCase(name)}'`
const themeContents = `export { default as ${camelCase(name)} } from './${kebabCase(name)}'` await appendFile(themePath, themeContent)
if (!theme.includes(themeContents)) {
await fsp.writeFile(themePath, theme.trim() + '\n' + themeContents + '\n')
}
const typesPath = resolve(path, 'src/runtime/types/index.d.ts') const typesPath = resolve(path, 'src/runtime/types/index.d.ts')
const types = await fsp.readFile(typesPath, 'utf-8') const typesContent = `export * from '../components/${splitByCase(name).map(p => upperFirst(p)).join('')}.vue'`
const typesContents = `export * from '../components/${splitByCase(name).map(p => upperFirst(p)).join('')}.vue'` await appendFile(typesPath, typesContent)
if (!types.includes(typesContents)) {
await fsp.writeFile(typesPath, types.trim() + '\n' + typesContents + '\n') await sortFile(themePath)
} await sortFile(typesPath)
} }
}) })

17
cli/utils.mjs Normal file
View File

@@ -0,0 +1,17 @@
import { promises as fsp } from 'node:fs'
export async function sortFile(path) {
const file = await fsp.readFile(path, 'utf-8')
const lines = file.trim().split('\n').sort()
await fsp.writeFile(path, lines.join('\n') + '\n')
}
export async function appendFile(path, content) {
const file = await fsp.readFile(path, 'utf-8')
if (!file.includes(content)) {
await fsp.writeFile(path, file.trim() + '\n' + content + '\n')
}
}