better error messages

This commit is contained in:
Chris Wanstrath 2026-02-09 16:59:00 -08:00
parent 128afcfef8
commit b447f7d0ca
2 changed files with 32 additions and 11 deletions

View File

@ -1,7 +1,7 @@
import type { App } from '@types'
import { generateTemplates, type TemplateType } from '%templates'
import color from 'kleur'
import { existsSync, mkdirSync, writeFileSync } from 'fs'
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'
import { basename, join } from 'path'
import { del, get, getManifest, HOST, makeAppUrl, post } from '../http'
import { confirm, prompt } from '../prompts'
@ -121,12 +121,26 @@ export async function newApp(name: string | undefined, options: NewAppOptions) {
if (options.bare) template = 'bare'
else if (options.spa) template = 'spa'
const pkgPath = join(appPath, 'package.json')
// If package.json exists, ensure it has scripts.toes and bail
if (existsSync(pkgPath)) {
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'))
if (!pkg.scripts?.toes) {
pkg.scripts = pkg.scripts ?? {}
pkg.scripts.toes = 'bun start'
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
console.log(color.green('✓ Added scripts.toes to package.json'))
}
return
}
if (name && existsSync(appPath)) {
console.error(`Directory already exists: ${name}`)
return
}
const filesToCheck = ['index.tsx', 'package.json', 'tsconfig.json']
const filesToCheck = ['index.tsx', 'tsconfig.json']
const existing = filesToCheck.filter((f) => existsSync(join(appPath, f)))
if (existing.length > 0) {
console.error(`Files already exist: ${existing.join(', ')}`)

View File

@ -7,7 +7,14 @@ import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, unl
import { dirname, join } from 'path'
import { del, download, get, getManifest, handleError, makeUrl, post, put } from '../http'
import { confirm, prompt } from '../prompts'
import { getAppName, isApp, resolveAppName } from '../name'
import { getAppName, getAppPackage, isApp, resolveAppName } from '../name'
function notAppError(): string {
const pkg = getAppPackage()
if (!pkg) return 'No package.json found. Use `toes get <app>` to grab one.'
if (!pkg.scripts?.toes) return 'Missing scripts.toes in package.json. Use `toes new` to add it.'
return 'Not a toes app'
}
interface ManifestDiff {
changed: string[]
@ -59,7 +66,7 @@ export async function getApp(name: string) {
export async function pushApp() {
if (!isApp()) {
console.error('Not a toes app. Use `toes get <app>` to grab one.')
console.error(notAppError())
return
}
@ -147,7 +154,7 @@ export async function pushApp() {
export async function pullApp(options: { force?: boolean } = {}) {
if (!isApp()) {
console.error('Not a toes app. Use `toes get <app>` to grab one.')
console.error(notAppError())
return
}
@ -222,7 +229,7 @@ export async function pullApp(options: { force?: boolean } = {}) {
export async function diffApp() {
if (!isApp()) {
console.error('Not a toes app. Use `toes get <app>` to grab one.')
console.error(notAppError())
return
}
@ -309,7 +316,7 @@ export async function diffApp() {
export async function statusApp() {
if (!isApp()) {
console.error('Not a toes app. Use `toes get <app>` to grab one.')
console.error(notAppError())
return
}
@ -367,7 +374,7 @@ export async function statusApp() {
export async function syncApp() {
if (!isApp()) {
console.error('Not a toes app. Use `toes get <app>` to grab one.')
console.error(notAppError())
return
}
@ -581,7 +588,7 @@ const STASH_BASE = '/tmp/toes-stash'
export async function stashApp() {
if (!isApp()) {
console.error('Not a toes app. Use `toes get <app>` to grab one.')
console.error(notAppError())
return
}
@ -690,7 +697,7 @@ export async function stashListApp() {
export async function stashPopApp() {
if (!isApp()) {
console.error('Not a toes app. Use `toes get <app>` to grab one.')
console.error(notAppError())
return
}
@ -747,7 +754,7 @@ export async function stashPopApp() {
export async function cleanApp(options: { force?: boolean, dryRun?: boolean } = {}) {
if (!isApp()) {
console.error('Not a toes app. Use `toes get <app>` to grab one.')
console.error(notAppError())
return
}