Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

post-init-script.ts

52 lines1.4 KB

1import { readFileSync } from 'node:fs'
2import { resolve } from 'node:path'
3​
4import { getPackageManagerScriptCommand } from '../package-manager.js'
5​
6import type { Environment, Options } from '../types.js'
7​
8export async function postInitScript(
9 environment: Environment,
10 options: Options,
11) {
12 const packageJsonPath = resolve(options.targetDir, 'package.json')
13​
14 if (!environment.exists(packageJsonPath)) {
15 environment.warn(
16 'Warning',
17 'No package.json found, skipping post-cta-init script',
18 )
19 return
20 }
21​
22 try {
23 const packageJsonContent = readFileSync(packageJsonPath, 'utf-8')
24 const packageJson = JSON.parse(packageJsonContent)
25​
26 if (!packageJson.scripts || !packageJson.scripts['post-cta-init']) {
27 // No post-cta-init script found, skip silently
28 return
29 }
30​
31 environment.startStep({
32 id: 'post-init-script',
33 type: 'command',
34 message: 'Running post-cta-init script...',
35 })
36​
37 const { command, args } = getPackageManagerScriptCommand(
38 options.packageManager,
39 ['post-cta-init'],
40 )
41​
42 await environment.execute(command, args, options.targetDir, {
43 inherit: true,
44 })
45​
46 environment.finishStep('post-init-script', 'Post-cta-init script complete')
47 } catch (error) {
48 environment.error(
49 `Failed to run post-cta-init script: ${error instanceof Error ? error.message : String(error)}`,
50 )
51 }