-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathcreate-app-task-run-init-script.ts
More file actions
88 lines (78 loc) · 3.04 KB
/
create-app-task-run-init-script.ts
File metadata and controls
88 lines (78 loc) · 3.04 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { log } from '@clack/prompts'
import { join } from 'node:path'
import { ensureTargetPath } from './ensure-target-path'
import { GetArgsResult } from './get-args-result'
import { deleteInitScript, getInitScript, InitScript } from './get-init-script'
import { getPackageJson } from './get-package-json'
import { initScriptVersion } from './init-script-version'
import { searchAndReplace } from './search-and-replace'
import { Task, taskFail } from './vendor/clack-tasks'
import { namesValues } from './vendor/names'
export function createAppTaskRunInitScript(args: GetArgsResult): Task {
return {
enabled: !args.skipInit,
title: 'Running init script',
task: async (result) => {
try {
const init = getInitScript(args.targetDirectory)
if (!init) {
return result({ message: 'Repository does not have an init script' })
}
if (args.verbose) {
log.warn(`Running init script`)
}
await initScriptVersion(init.versions, args.verbose)
if (args.verbose) {
log.warn(`initCheckVersion done`)
}
await initRename(args, init, args.verbose)
if (args.verbose) {
log.warn(`initRename done`)
}
const instructions: string[] = (initInstructions(init) ?? [])
?.filter(Boolean)
.map((msg) => msg.replace('{pm}', args.packageManager))
if (args.verbose) {
log.warn(`initInstructions done`)
}
deleteInitScript(args.targetDirectory)
if (args.verbose) {
log.warn(`deleteInitScript done`)
}
return result({ message: 'Executed init script', instructions })
} catch (error) {
taskFail(`init: Error running init script: ${error}`)
}
},
}
}
async function initRename(args: GetArgsResult, init: InitScript, verbose: boolean) {
const { contents } = getPackageJson(args.targetDirectory)
// Rename template from package.json to project name throughout the whole project
if (contents.name) {
await searchAndReplace(args.targetDirectory, [contents.name], [args.name], false, verbose)
}
// Return early if there are no renames defined in the init script
if (!init?.rename) {
return
}
// Loop through each word in the rename object
for (const from of Object.keys(init.rename)) {
// Get the 'to' property from the rename object
const to = init.rename[from].to.replace('{{name}}', args.name.replace(/-/g, ''))
// Get the name matrix for the 'from' and the 'to' value
const fromNames = namesValues(from)
const toNames = namesValues(to)
for (const path of init.rename[from].paths) {
const targetPath = join(args.targetDirectory, path)
if (!(await ensureTargetPath(targetPath))) {
console.error(`init-script.rename: target does not exist ${targetPath}`)
continue
}
await searchAndReplace(join(args.targetDirectory, path), fromNames, toNames, args.dryRun)
}
}
}
function initInstructions(init: InitScript) {
return init?.instructions?.length === 0 ? [] : init?.instructions
}