Skip to content
Open
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
20 changes: 16 additions & 4 deletions packages/create/src/add-to-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,21 @@ export async function addToApp(
})
const s = environment.spinner()
s.start(`Installing dependencies via ${newOptions.packageManager}...`)
await packageManagerInstall(
const installResult = await packageManagerInstall(
environment,
newOptions.targetDir,
newOptions.packageManager,
undefined,
{ updateLockfile: true },
)
s.stop(`Installed dependencies`)
environment.finishStep('install-dependencies', 'Dependencies installed')
const installMessage = installResult.failed
? `Failed to install dependencies via ${newOptions.packageManager}`
: 'Installed dependencies'
s.stop(installMessage)
environment.finishStep(
'install-dependencies',
installResult.failed ? installMessage : 'Dependencies installed',
)

// Run any post-init special steps for the new add-ons
const postInitSpecialSteps = new Set<string>([])
Expand Down Expand Up @@ -470,5 +476,11 @@ export async function addToApp(
writeConfigFileToEnvironment(environment, newOptions)
environment.finishStep('write-config-file', 'Config file written')

environment.outro(`Add-ons ${addOns.join(', ')} added!`)
const errors = environment.getErrors()
const summary = `Add-ons ${addOns.join(', ')} added!`
environment.outro(
errors.length
? `${summary}\n\nErrors were encountered while adding add-ons:\n\n${errors.join('\n')}`
: summary,
)
}
42 changes: 42 additions & 0 deletions packages/create/tests/add-to-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,13 @@ describe('addToApp', () => {
2,
),
)
const stop = vi.fn()
environment.spinner = () => ({ start: vi.fn(), stop })
await addToApp(environment, ['test'], '/foo', {
forced: true,
intent: false,
})
expect(stop).toHaveBeenCalledWith('Installed dependencies')
environment.finishRun()
expect(output.files).toEqual({
'jack.txt': 'foo',
Expand All @@ -499,6 +503,44 @@ describe('addToApp', () => {
})
})

it('reports dependency installation failure and records the add-on', async () => {
const { environment } = createMemoryEnvironment('/foo')
environment.startRun()
environment.writeFile('/foo/.cta.json', JSON.stringify(fakeCTAJSON))
environment.writeFile('/foo/package.json', JSON.stringify({ name: 'test' }))

const installError =
'Command "npm install" did not run successfully. Please run this manually in your project.'
const execute = environment.execute
environment.execute = async (command, args, cwd) => {
if (command === 'npm' && args[0] === 'install') {
environment.getErrors().push(installError)
return { stdout: '', failed: true }
}
return execute(command, args, cwd)
}
const stop = vi.fn()
environment.spinner = () => ({ start: vi.fn(), stop })
const finishStep = vi.spyOn(environment, 'finishStep')
const outro = vi.spyOn(environment, 'outro')

await addToApp(environment, ['test'], '/foo', {
forced: true,
intent: false,
})

expect(stop).toHaveBeenCalledWith('Failed to install dependencies via npm')
expect(stop).not.toHaveBeenCalledWith('Installed dependencies')
expect(finishStep).toHaveBeenCalledWith(
'install-dependencies',
'Failed to install dependencies via npm',
)
expect(outro).toHaveBeenCalledWith(expect.stringContaining(installError))
expect(JSON.parse(await environment.readFile('/foo/.cta.json'))).toMatchObject({
chosenAddOns: ['test'],
})
})

it('enables Tailwind when an add-on requires it', async () => {
const { environment } = createMemoryEnvironment('/foo')
environment.startRun()
Expand Down