-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.ts
More file actions
112 lines (84 loc) · 2.68 KB
/
init.ts
File metadata and controls
112 lines (84 loc) · 2.68 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { cancel, intro, isCancel, log, outro, spinner, text } from "@clack/prompts"
import Bun from "bun"
intro("🥐 Pastry")
const TEMPLATE_NAME = "pastry"
const TEMPLATE_AUTHOR = "Adel Rodríguez <hey@adel.do>"
const TEMPLATE_GITHUB_USER = "adelrodriguez"
const TEMPLATE_DESCRIPTION = "A simple template to build libraries with Bun"
const name = await text({
message: "What is the name of the project?",
placeholder: TEMPLATE_NAME,
validate: (value) => {
if (value.length === 0) {
return "Project name is required"
}
},
})
if (isCancel(name)) {
cancel("Operation cancelled")
process.exit(0)
}
const author = await text({
defaultValue: TEMPLATE_AUTHOR,
message: "What is the author of the project?",
placeholder: TEMPLATE_AUTHOR,
})
if (isCancel(author)) {
cancel("Operation cancelled")
process.exit(0)
}
const githubUser = await text({
defaultValue: TEMPLATE_GITHUB_USER,
message: "What is the GitHub user of the project?",
placeholder: TEMPLATE_GITHUB_USER,
})
if (isCancel(githubUser)) {
cancel("Operation cancelled")
process.exit(0)
}
const description = await text({
message: "What is the description of the project?",
placeholder: TEMPLATE_DESCRIPTION,
validate: (value) => {
if (value.length === 0) {
return "Description is required"
}
},
})
if (isCancel(description)) {
cancel("Operation cancelled")
process.exit(0)
}
const s = spinner()
const replaceAllText = (value: string, search: string, replacement: string) =>
value.split(search).join(replacement)
s.start("Updating package.json...")
let packageContents = await Bun.file("package.json").text()
packageContents = replaceAllText(packageContents, TEMPLATE_NAME, name)
packageContents = replaceAllText(packageContents, TEMPLATE_AUTHOR, author)
packageContents = replaceAllText(packageContents, TEMPLATE_GITHUB_USER, githubUser)
packageContents = replaceAllText(packageContents, TEMPLATE_DESCRIPTION, description)
const packageJson = JSON.parse(packageContents)
packageJson.version = "0.0.0"
await Bun.write("package.json", `${JSON.stringify(packageJson, null, 2)}\n`)
s.stop("Package.json updated")
s.start("Updating README.md...")
const readme = `
<div align="center">
<h1 align="center">${name}</h1>
<p align="center">
<strong>${description}</strong>
</p>
</div>
Made with [🥐 \`pastry\`](https://github.com/adelrodriguez/pastry)
`
await Bun.write("README.md", readme)
s.stop("README.md updated")
s.start("Remove template files...")
await Bun.$`rm -rf ./docs`
s.message("docs removed")
await Bun.$`rm -rf ./CHANGELOG.md`
s.message("CHANGELOG.md removed")
s.stop("Template files removed")
log.success("✨ Project initialized successfully")
outro("Get to cooking! 🥐")