|
1 | 1 | import { log } from '@clack/prompts' |
2 | | -import { join } from 'node:path' |
3 | | -import { ensureTargetPath } from './ensure-target-path' |
4 | 2 | import { GetArgsResult } from './get-args-result' |
5 | | -import { deleteInitScript, getInitScript, InitScript } from './get-init-script' |
6 | 3 | import { getPackageJson } from './get-package-json' |
| 4 | +import { initScriptDelete } from './init-script-delete' |
| 5 | +import { initScriptInstructions } from './init-script-instructions' |
| 6 | +import { initScriptRename } from './init-script-rename' |
| 7 | +import { initScriptKey } from './init-script-schema' |
7 | 8 | import { initScriptVersion } from './init-script-version' |
8 | | -import { searchAndReplace } from './search-and-replace' |
9 | 9 | import { Task, taskFail } from './vendor/clack-tasks' |
10 | | -import { namesValues } from './vendor/names' |
11 | 10 |
|
12 | 11 | export function createAppTaskRunInitScript(args: GetArgsResult): Task { |
13 | 12 | return { |
14 | 13 | enabled: !args.skipInit, |
15 | 14 | title: 'Running init script', |
16 | 15 | task: async (result) => { |
17 | 16 | try { |
18 | | - const init = getInitScript(args.targetDirectory) |
| 17 | + const { contents } = getPackageJson(args.targetDirectory) |
| 18 | + const init = contents[initScriptKey] |
19 | 19 | if (!init) { |
20 | | - return result({ message: 'Repository does not have an init script' }) |
| 20 | + return result({ message: 'Init script not found' }) |
21 | 21 | } |
22 | 22 | if (args.verbose) { |
23 | | - log.warn(`Running init script`) |
| 23 | + log.warn(`Init script started`) |
24 | 24 | } |
25 | 25 |
|
26 | 26 | await initScriptVersion(init.versions, args.verbose) |
27 | | - if (args.verbose) { |
28 | | - log.warn(`initCheckVersion done`) |
29 | | - } |
30 | | - await initRename(args, init, args.verbose) |
31 | | - if (args.verbose) { |
32 | | - log.warn(`initRename done`) |
33 | | - } |
34 | 27 |
|
35 | | - const instructions: string[] = (initInstructions(init) ?? []) |
| 28 | + await initScriptRename(args, init.rename, args.verbose) |
| 29 | + |
| 30 | + const instructions: string[] = initScriptInstructions(init.instructions, args.verbose) |
36 | 31 | ?.filter(Boolean) |
37 | 32 | .map((msg) => msg.replace('{pm}', args.packageManager)) |
38 | 33 |
|
39 | | - if (args.verbose) { |
40 | | - log.warn(`initInstructions done`) |
41 | | - } |
42 | | - deleteInitScript(args.targetDirectory) |
43 | | - if (args.verbose) { |
44 | | - log.warn(`deleteInitScript done`) |
45 | | - } |
46 | | - return result({ message: 'Executed init script', instructions }) |
| 34 | + initScriptDelete(args) |
| 35 | + return result({ message: 'Init script done', instructions }) |
47 | 36 | } catch (error) { |
48 | | - taskFail(`init: Error running init script: ${error}`) |
| 37 | + taskFail(`Error running init script: ${error}`) |
49 | 38 | } |
50 | 39 | }, |
51 | 40 | } |
52 | 41 | } |
53 | | - |
54 | | -async function initRename(args: GetArgsResult, init: InitScript, verbose: boolean) { |
55 | | - const { contents } = getPackageJson(args.targetDirectory) |
56 | | - // Rename template from package.json to project name throughout the whole project |
57 | | - if (contents.name) { |
58 | | - await searchAndReplace(args.targetDirectory, [contents.name], [args.name], false, verbose) |
59 | | - } |
60 | | - |
61 | | - // Return early if there are no renames defined in the init script |
62 | | - if (!init?.rename) { |
63 | | - return |
64 | | - } |
65 | | - |
66 | | - // Loop through each word in the rename object |
67 | | - for (const from of Object.keys(init.rename)) { |
68 | | - // Get the 'to' property from the rename object |
69 | | - const to = init.rename[from].to.replace('{{name}}', args.name.replace(/-/g, '')) |
70 | | - |
71 | | - // Get the name matrix for the 'from' and the 'to' value |
72 | | - const fromNames = namesValues(from) |
73 | | - const toNames = namesValues(to) |
74 | | - |
75 | | - for (const path of init.rename[from].paths) { |
76 | | - const targetPath = join(args.targetDirectory, path) |
77 | | - if (!(await ensureTargetPath(targetPath))) { |
78 | | - console.error(`init-script.rename: target does not exist ${targetPath}`) |
79 | | - continue |
80 | | - } |
81 | | - await searchAndReplace(join(args.targetDirectory, path), fromNames, toNames, args.dryRun) |
82 | | - } |
83 | | - } |
84 | | -} |
85 | | - |
86 | | -function initInstructions(init: InitScript) { |
87 | | - return init?.instructions?.length === 0 ? [] : init?.instructions |
88 | | -} |
0 commit comments