refactor(init): use async fs in scaffolding

BREAKING CHANGE: scaffold() now returns a Promise<string> and must
be awaited.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pull/5342/head
Divyansh Singh 1 month ago
parent 479b984a27
commit 38b59f9252

@ -39,7 +39,7 @@ afterAll(async () => {
test.each(variations)('init %s', async (_, { theme, useTs }) => { test.each(variations)('init %s', async (_, { theme, useTs }) => {
const root = getTempRoot() const root = getTempRoot()
await rm(root, { recursive: true, force: true }) await rm(root, { recursive: true, force: true })
scaffold({ root, theme, useTs, injectNpmScripts: false }) await scaffold({ root, theme, useTs, injectNpmScripts: false })
const port = await getPort() const port = await getPort()
const server = await createServer(root, { port }) const server = await createServer(root, { port })

@ -9,10 +9,12 @@ import {
} from '@clack/prompts' } from '@clack/prompts'
import template from 'lodash.template' import template from 'lodash.template'
import fs from 'node:fs' import fs from 'node:fs'
import { mkdir, writeFile } from 'node:fs/promises'
import path from 'node:path' import path from 'node:path'
import { fileURLToPath } from 'node:url' import { fileURLToPath } from 'node:url'
import c from 'picocolors' import c from 'picocolors'
import { slash } from '../shared' import { slash } from '../shared'
import { readFile } from '../utils/fs'
export enum ScaffoldThemeType { export enum ScaffoldThemeType {
Default = 'default theme', Default = 'default theme',
@ -141,10 +143,10 @@ export async function init(root?: string) {
} }
) )
outro(scaffold(options)) outro(await scaffold(options))
} }
export function scaffold({ export async function scaffold({
root: root_ = './', root: root_ = './',
srcDir: srcDir_ = root_, srcDir: srcDir_ = root_,
title = 'My Awesome Project', title = 'My Awesome Project',
@ -178,12 +180,12 @@ export function scaffold({
const pkgPath = path.resolve('package.json') const pkgPath = path.resolve('package.json')
const userPkg = fs.existsSync(pkgPath) const userPkg = fs.existsSync(pkgPath)
? JSON.parse(fs.readFileSync(pkgPath, 'utf8')) ? JSON.parse(await readFile(pkgPath))
: {} : {}
const useMjs = userPkg.type !== 'module' const useMjs = userPkg.type !== 'module'
const renderFile = (file: string) => { const renderFile = async (file: string) => {
const filePath = path.resolve(templateDir, file) const filePath = path.resolve(templateDir, file)
let targetPath = path.resolve(resolvedRoot, file) let targetPath = path.resolve(resolvedRoot, file)
@ -197,11 +199,11 @@ export function scaffold({
targetPath = path.resolve(resolvedSrcDir, file) targetPath = path.resolve(resolvedSrcDir, file)
} }
const content = fs.readFileSync(filePath, 'utf8') const content = await readFile(filePath)
const compiled = template(content)(data) const compiled = template(content)(data)
fs.mkdirSync(path.dirname(targetPath), { recursive: true }) await mkdir(path.dirname(targetPath), { recursive: true })
fs.writeFileSync(targetPath, compiled) await writeFile(targetPath, compiled)
} }
const filesToScaffold = [ const filesToScaffold = [
@ -225,7 +227,7 @@ export function scaffold({
} }
for (const file of filesToScaffold) { for (const file of filesToScaffold) {
renderFile(file) await renderFile(file)
} }
const tips = [] const tips = []
@ -260,7 +262,7 @@ export function scaffold({
scripts[`${prefix}preview`] = `vitepress preview${dir}` scripts[`${prefix}preview`] = `vitepress preview${dir}`
Object.assign(userPkg.scripts || (userPkg.scripts = {}), scripts) Object.assign(userPkg.scripts || (userPkg.scripts = {}), scripts)
fs.writeFileSync(pkgPath, JSON.stringify(userPkg, null, 2)) await writeFile(pkgPath, JSON.stringify(userPkg, null, 2))
return `Done! Now run ${c.cyan(`${pm} run ${prefix}dev`)} and start writing.${tip}` return `Done! Now run ${c.cyan(`${pm} run ${prefix}dev`)} and start writing.${tip}`
} else { } else {

Loading…
Cancel
Save