Skip to content

Commit 82c4cda

Browse files
committed
refactor: clean up init script utils
1 parent c67b929 commit 82c4cda

11 files changed

+196
-84
lines changed
Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
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'
6-
import { getPackageJson } from './get-package-json'
3+
import { getInitScript } from './get-init-script'
4+
import { initScriptDelete } from './init-script-delete'
5+
import { initScriptInstructions } from './init-script-instructions'
6+
import { initScriptRename } from './init-script-rename'
77
import { initScriptVersion } from './init-script-version'
8-
import { searchAndReplace } from './search-and-replace'
98
import { Task, taskFail } from './vendor/clack-tasks'
10-
import { namesValues } from './vendor/names'
119

1210
export function createAppTaskRunInitScript(args: GetArgsResult): Task {
1311
return {
@@ -27,62 +25,20 @@ export function createAppTaskRunInitScript(args: GetArgsResult): Task {
2725
if (args.verbose) {
2826
log.warn(`initCheckVersion done`)
2927
}
30-
await initRename(args, init, args.verbose)
28+
await initScriptRename(args, init.rename)
3129
if (args.verbose) {
3230
log.warn(`initRename done`)
3331
}
3432

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

39-
if (args.verbose) {
40-
log.warn(`initInstructions done`)
41-
}
42-
deleteInitScript(args.targetDirectory)
43-
if (args.verbose) {
44-
log.warn(`deleteInitScript done`)
45-
}
37+
initScriptDelete(args)
4638
return result({ message: 'Executed init script', instructions })
4739
} catch (error) {
4840
taskFail(`init: Error running init script: ${error}`)
4941
}
5042
},
5143
}
5244
}
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: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { writeFileSync } from 'node:fs'
2-
import { z } from 'zod'
31
import { getPackageJson } from './get-package-json'
2+
import { InitScript, InitScriptSchema } from './init-script-schema'
43

54
export const initScriptKey = 'create-solana-dapp'
65

@@ -19,33 +18,3 @@ export function getInitScript(targetDirectory: string): InitScript | undefined {
1918
}
2019
return parsed.data
2120
}
22-
23-
export function deleteInitScript(targetDirectory: string) {
24-
const { path, contents } = getPackageJson(targetDirectory)
25-
delete contents[initScriptKey]
26-
writeFileSync(path, JSON.stringify(contents, undefined, 2) + '\n')
27-
}
28-
29-
const InitScriptVersionsSchema = z.object({
30-
adb: z.string().optional(),
31-
anchor: z.string().optional(),
32-
solana: z.string().optional(),
33-
})
34-
35-
const InitScriptSchema = z
36-
.object({
37-
instructions: z.array(z.string()).optional(),
38-
rename: z
39-
.record(
40-
z.object({
41-
to: z.string(),
42-
paths: z.array(z.string()),
43-
}),
44-
)
45-
.optional(),
46-
versions: InitScriptVersionsSchema.optional(),
47-
})
48-
.optional()
49-
50-
export type InitScript = z.infer<typeof InitScriptSchema>
51-
export type InitScriptVersions = z.infer<typeof InitScriptVersionsSchema>

src/utils/init-script-delete.ts

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

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>

src/utils/init-script-version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { log } from '@clack/prompts'
2-
import { InitScriptVersions } from './get-init-script'
32
import { initScriptVersionAdb } from './init-script-version-adb'
43
import { initScriptVersionAnchor } from './init-script-version-anchor'
54
import { initScriptVersionSolana } from './init-script-version-solana'
5+
import { InitScriptVersions } from './init-script-versions-schema'
66

77
export async function initScriptVersion(versions?: InitScriptVersions, verbose = false) {
88
if (!versions) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { z } from 'zod'
2+
3+
export const InitScriptVersionsSchema = z.object({
4+
adb: z.string().optional(),
5+
anchor: z.string().optional(),
6+
solana: z.string().optional(),
7+
})
8+
9+
export type InitScriptVersions = z.infer<typeof InitScriptVersionsSchema>

0 commit comments

Comments
 (0)