-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdependencies.ts
43 lines (38 loc) · 1.6 KB
/
dependencies.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import * as fs from 'fs'
import * as path from 'path'
import * as execa from 'execa'
import { spinner } from '../utils/terminal'
import { hint } from '../utils/messages'
import { PackageJson } from '../utils/directory'
import { askInstallDependencies } from '../utils/prompts'
import { getPackageManager } from '../utils/which-pm'
export function addDevDependecies (projectDirectory: string, packageJson: PackageJson) {
if (!Reflect.has(packageJson, 'devDependencies')) {
packageJson.devDependencies = {}
}
Object.assign(packageJson.devDependencies, {
checkly: 'latest',
'ts-node': 'latest',
typescript: 'latest',
})
fs.writeFileSync(path.join(projectDirectory, 'package.json'), JSON.stringify(packageJson, null, 2))
}
export async function installDependencies (targetDir: string): Promise<void> {
const { installDependencies } = await askInstallDependencies()
if (installDependencies) {
const packageManager = (await getPackageManager())?.name || 'npm'
const installExec = execa(packageManager, ['install'], { cwd: targetDir })
const installSpinner = spinner('Installing packages')
await new Promise<void>((resolve, reject) => {
installExec.stdout?.on('data', function (data: any) {
installSpinner.text = `installing \n${packageManager} ${data}`
})
installExec.on('error', (error: any) => reject(error))
installExec.on('close', () => resolve())
})
installSpinner.text = 'Packages installed successfully'
installSpinner.succeed()
} else {
await hint('No worries.', 'Just remember to install the dependencies after this setup')
}
}