Skip to content

Commit 98806ec

Browse files
committed
refactor: clean up init script utils
1 parent c67b929 commit 98806ec

13 files changed

Lines changed: 201 additions & 103 deletions
Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { log } from '@clack/prompts'
2-
import { join } from 'node:path'
3-
import { ensureTargetPath } from './ensure-target-path'
42
import { GetArgsResult } from './get-args-result'
5-
import { deleteInitScript, getInitScript, InitScript } from './get-init-script'
63
import { getPackageJson } from './get-package-json'
4+
import { initScriptDelete } from './init-script-delete'
5+
import { initScriptInstructions } from './init-script-instructions'
6+
import { initScriptKey } from './init-script-key'
7+
import { initScriptRename } from './init-script-rename'
78
import { initScriptVersion } from './init-script-version'
8-
import { searchAndReplace } from './search-and-replace'
99
import { Task, taskFail } from './vendor/clack-tasks'
10-
import { namesValues } from './vendor/names'
1110

1211
export function createAppTaskRunInitScript(args: GetArgsResult): Task {
1312
return {
1413
enabled: !args.skipInit,
1514
title: 'Running init script',
1615
task: async (result) => {
1716
try {
18-
const init = getInitScript(args.targetDirectory)
17+
const { contents: packageJson } = getPackageJson(args.targetDirectory)
18+
const init = packageJson[initScriptKey]
1919
if (!init) {
2020
return result({ message: 'Repository does not have an init script' })
2121
}
@@ -27,62 +27,20 @@ export function createAppTaskRunInitScript(args: GetArgsResult): Task {
2727
if (args.verbose) {
2828
log.warn(`initCheckVersion done`)
2929
}
30-
await initRename(args, init, args.verbose)
30+
await initScriptRename(args, init.rename, args.verbose)
3131
if (args.verbose) {
3232
log.warn(`initRename done`)
3333
}
3434

35-
const instructions: string[] = (initInstructions(init) ?? [])
35+
const instructions: string[] = initScriptInstructions(init.instructions, args.verbose)
3636
?.filter(Boolean)
3737
.map((msg) => msg.replace('{pm}', args.packageManager))
3838

39-
if (args.verbose) {
40-
log.warn(`initInstructions done`)
41-
}
42-
deleteInitScript(args.targetDirectory)
43-
if (args.verbose) {
44-
log.warn(`deleteInitScript done`)
45-
}
39+
initScriptDelete(args)
4640
return result({ message: 'Executed init script', instructions })
4741
} catch (error) {
4842
taskFail(`init: Error running init script: ${error}`)
4943
}
5044
},
5145
}
5246
}
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-
}

src/utils/get-init-script.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/utils/get-package-json.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { existsSync, readFileSync } from 'node:fs'
22
import { z } from 'zod'
33
import { getPackageJsonPath } from './get-package-json-path'
4+
import { initScriptKey } from './init-script-key'
5+
import { InitScriptSchema } from './init-script-schema'
46

57
export function getPackageJson(targetDirectory: string): { path: string; contents: PackageJson } {
68
const path = getPackageJsonPath(targetDirectory)
@@ -26,6 +28,7 @@ const PackageJsonSchema = z
2628
.object({
2729
name: z.string().optional(),
2830
scripts: z.record(z.string()).optional(),
31+
[initScriptKey]: InitScriptSchema.optional(),
2932
})
3033
.passthrough()
3134

src/utils/init-script-delete.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { writeFileSync } from 'node:fs'
2+
import { join } from 'node:path'
3+
import { GetArgsResult } from './get-args-result'
4+
5+
import { initScriptKey } from './init-script-key'
6+
7+
export function initScriptDelete(args: GetArgsResult) {
8+
const packageJson = join(args.targetDirectory, 'package.json')
9+
const contents = require(packageJson)
10+
delete contents[initScriptKey]
11+
writeFileSync(packageJson, JSON.stringify(contents, undefined, 2) + '\n')
12+
if (args.verbose) {
13+
console.log(`initScriptDelete: deleted ${initScriptKey} from package.json`)
14+
}
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { z } from 'zod'
2+
3+
export const InitScriptInstructionsSchema = z.array(z.string())
4+
5+
export type InitScriptInstructions = z.infer<typeof InitScriptInstructionsSchema>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { InitScriptInstructions } from './init-script-instructions-schema'
2+
3+
export function initScriptInstructions(instructions?: InitScriptInstructions, verbose = false): string[] {
4+
if (!instructions || instructions.length === 0) {
5+
if (verbose) {
6+
console.log(`initScriptInstructions: no instructions found`)
7+
}
8+
return []
9+
}
10+
if (verbose) {
11+
console.log(`initScriptInstructions: ${instructions.length} instructions found`)
12+
}
13+
return instructions
14+
}

src/utils/init-script-key.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const initScriptKey = 'create-solana-dapp'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { z } from 'zod'
2+
3+
export const InitScriptRenameSchema = z.record(
4+
z.object({
5+
to: z.string(),
6+
paths: z.array(z.string()),
7+
}),
8+
)
9+
10+
export type InitScriptRename = z.infer<typeof InitScriptRenameSchema>

src/utils/init-script-rename.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { log } from '@clack/prompts'
2+
import { join } from 'node:path'
3+
import { ensureTargetPath } from './ensure-target-path'
4+
import { GetArgsResult } from './get-args-result'
5+
import { getPackageJson } from './get-package-json'
6+
import { InitScriptRename } from './init-script-rename-schema'
7+
import { searchAndReplace } from './search-and-replace'
8+
import { namesValues } from './vendor/names'
9+
10+
export async function initScriptRename(args: GetArgsResult, rename?: InitScriptRename, verbose = false) {
11+
const tag = `initScriptRename`
12+
const { contents } = getPackageJson(args.targetDirectory)
13+
// Rename template from package.json to project name throughout the whole project
14+
if (contents.name) {
15+
if (args.verbose) {
16+
log.warn(`${tag}: renaming template name '${contents.name}' to project name '${args.name}'`)
17+
}
18+
await searchAndReplace(args.targetDirectory, [contents.name], [args.name], false, verbose)
19+
}
20+
21+
// Return early if there are no renames defined in the init script
22+
if (!rename) {
23+
if (args.verbose) {
24+
log.warn(`${tag}: no renames found`)
25+
}
26+
return
27+
}
28+
29+
// Loop through each word in the rename object
30+
for (const from of Object.keys(rename)) {
31+
// Get the 'to' property from the rename object
32+
const to = rename[from].to.replace('{{name}}', args.name.replace(/-/g, ''))
33+
34+
// Get the name matrix for the 'from' and the 'to' value
35+
const fromNames = namesValues(from)
36+
const toNames = namesValues(to)
37+
38+
for (const path of rename[from].paths) {
39+
const targetPath = join(args.targetDirectory, path)
40+
if (!(await ensureTargetPath(targetPath))) {
41+
console.error(`init-script.rename: target does not exist ${targetPath}`)
42+
continue
43+
}
44+
if (args.verbose) {
45+
log.warn(`${tag}: ${targetPath} -> ${fromNames.join('|')} -> ${toNames.join('|')}`)
46+
}
47+
await searchAndReplace(targetPath, fromNames, toNames, args.dryRun, args.verbose)
48+
}
49+
}
50+
51+
if (args.verbose) {
52+
log.warn(`${tag}: done`)
53+
}
54+
}

src/utils/init-script-schema.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { z } from 'zod'
2+
import { InitScriptInstructionsSchema } from './init-script-instructions-schema'
3+
import { InitScriptRenameSchema } from './init-script-rename-schema'
4+
import { InitScriptVersionsSchema } from './init-script-versions-schema'
5+
6+
export const InitScriptSchema = z
7+
.object({
8+
instructions: InitScriptInstructionsSchema.optional(),
9+
rename: InitScriptRenameSchema.optional(),
10+
versions: InitScriptVersionsSchema.optional(),
11+
})
12+
.optional()
13+
14+
export type InitScript = z.infer<typeof InitScriptSchema>

0 commit comments

Comments
 (0)