Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/commands/project.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { createInterface } from 'node:readline/promises'
import { ApiClient, requireProject } from '../api.js'
import { writeProject } from '../config.js'
import { info, die, printJson, handleApproval, renderNextActions } from '../util.js'
import { installObserve } from '../observe/install.js'
import { installSkills } from '../ensure-skills.js'

// Interactive name prompt (stderr, so piped stdout stays clean). Enter accepts the default.
async function promptName(question: string, def: string): Promise<string> {
const rl = createInterface({ input: process.stdin, output: process.stderr })
try { return (await rl.question(`${question} [${def}]: `)).trim() } finally { rl.close() }
}

// Best-effort: wire the credential-audit hook into the project (no-op if assets aren't built).
function tryInstallObserve(): void {
try {
Expand All @@ -19,12 +26,31 @@ async function resolveOrg(api: ApiClient, given?: string): Promise<string> {
return orgs[0].id
}

export async function projectCreate(name: string, opts: { org?: string }): Promise<void> {
/** A valid project name from a raw string: lowercase, non-alnum → hyphen, trimmed. */
export function slugifyName(raw: string): string {
return raw.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 40)
}

/** Name resolution so the recommended one-liner (`insta project create`, no arg) is paste-and-run:
* explicit arg wins; else prompt with the cwd basename as default (TTY); else use the basename. */
export async function resolveProjectName(
nameArg: string | undefined,
cwd = process.cwd(),
prompt?: (question: string, def: string) => Promise<string>,
): Promise<string> {
const fromDir = slugifyName(cwd.split('/').filter(Boolean).pop() ?? 'app') || 'app'
if (nameArg) return slugifyName(nameArg)
if (prompt && process.stdin.isTTY) return slugifyName((await prompt('project name', fromDir)) || fromDir) || fromDir
return fromDir
}

export async function projectCreate(name: string | undefined, opts: { org?: string }): Promise<void> {
const api = await ApiClient.load()
const orgId = await resolveOrg(api, opts.org)
const out = await api.request('POST', `/orgs/${orgId}/projects`, { name })
const resolved = await resolveProjectName(name, process.cwd(), promptName)
const out = await api.request('POST', `/orgs/${orgId}/projects`, { name: resolved })
await writeProject({ projectId: out.project.id, orgId, branch: out.defaultBranch.name })
info(`created project ${out.project.id} (${name})`)
info(`created project ${out.project.id} (${resolved})`)
info(` resources: ${out.resources.map((r: any) => r.kind).join(', ')}`)
info(` linked ./.insta/project.json (branch ${out.defaultBranch.name})`)
renderNextActions(out.nextActions)
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ orgCmd.command('create <name>').action(guard((name) => org.orgCreate(name)))

// ---- project ----
const pj = program.command('project').description('Manage projects')
pj.command('create <name>').option('--org <id>', 'org to create under (default: personal)').action(guard((name, o) => project.projectCreate(name, o)))
pj.command('create [name]').option('--org <id>', 'org to create under (default: personal)').action(guard((name, o) => project.projectCreate(name, o)))
pj.command('list').option('--org <id>').option('--json').action(guard((o) => project.projectList(o)))
pj.command('link <id>').description('Link a project to this directory').action(guard((id) => project.projectLink(id)))
pj.command('delete').option('--project <id>').action(guard((o) => project.projectDelete(o)))
Expand Down
22 changes: 22 additions & 0 deletions test/create-name.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { test, expect, afterEach } from 'vitest'
import { slugifyName, resolveProjectName } from '../src/commands/project.js'

const realTTY = process.stdin.isTTY
afterEach(() => { Object.defineProperty(process.stdin, 'isTTY', { value: realTTY, configurable: true }) })

test('slugify makes a valid project name', () => {
expect(slugifyName('My Cool App!')).toBe('my-cool-app')
expect(slugifyName('linkbox')).toBe('linkbox')
})
test('explicit arg wins (slugified)', async () => {
expect(await resolveProjectName('LinkBox', '/x/whatever')).toBe('linkbox')
})
test('no arg, non-TTY → directory basename (so the pasted one-liner runs unedited)', async () => {
Object.defineProperty(process.stdin, 'isTTY', { value: false, configurable: true })
expect(await resolveProjectName(undefined, '/Users/me/my-project')).toBe('my-project')
})
test('no arg, TTY → prompt, Enter accepts the dir-name default', async () => {
Object.defineProperty(process.stdin, 'isTTY', { value: true, configurable: true })
expect(await resolveProjectName(undefined, '/Users/me/cool-thing', async () => '')).toBe('cool-thing')
expect(await resolveProjectName(undefined, '/Users/me/cool-thing', async () => 'chosen name')).toBe('chosen-name')
})
Loading