11open Node
22
33module P = ClackPrompts
4+ module StringPrototype = {
5+ @send external replaceAll : (string , string , string ) => string = "replaceAll"
6+ }
47
58let packageNameRegExp = /^ [a - z0 - 9 - ]+ $/
9+ let resxTemplatePlaceholderName = "resx-template"
610
711let validateProjectName = projectName =>
812 if projectName -> String .trim -> String .length === 0 {
@@ -70,6 +74,72 @@ let getTemplateOptions = () =>
7074 hint : shortDescription ,
7175 })
7276
77+ let rec replaceFileContents = async (remainingFilePaths , ~replaceValue , ~withValue ) =>
78+ switch remainingFilePaths {
79+ | list {} => ()
80+ | list {filePath , ... remainingFilePaths } =>
81+ let fileContents = await Fs .Promises .readFile (filePath )
82+ let updatedFileContents = fileContents -> StringPrototype .replaceAll (replaceValue , withValue )
83+
84+ await Fs .Promises .writeFile (filePath , updatedFileContents )
85+ await replaceFileContents (remainingFilePaths , ~replaceValue , ~withValue )
86+ }
87+
88+ let synchronizeResxTemplateFiles = async (~projectName ) =>
89+ await replaceFileContents (
90+ list {
91+ "package.json" ,
92+ "rescript.json" ,
93+ "README.md" ,
94+ "Dockerfile" ,
95+ "bun.lock" ,
96+ Path .join (["scripts" , "build-sfe.mjs" ]),
97+ Path .join (["src" , "data" , "TemplateContent.res" ]),
98+ },
99+ ~replaceValue = resxTemplatePlaceholderName ,
100+ ~withValue = projectName ,
101+ )
102+
103+ let getPackageManagerName = (packageManager : PackageManagers .packageManager ) =>
104+ switch packageManager {
105+ | Npm => "npm"
106+ | Yarn1 | YarnBerry => "yarn"
107+ | Pnpm => "pnpm"
108+ | Bun => "bun"
109+ }
110+
111+ let showGetStartedNote = async (~templateName , ~projectName ) => {
112+ if templateName === Templates .resxTemplateName {
113+ let packageManagerInfo = await PackageManagers .getPackageManagerInfo ()
114+
115+ switch packageManagerInfo .packageManager {
116+ | Bun =>
117+ P .note (
118+ ~title = "Get started" ,
119+ ~message = ` cd ${projectName}
120+
121+ bun run dev` ,
122+ )
123+ | packageManager =>
124+ P .note (
125+ ~title = "Bun recommended" ,
126+ ~message = ` This ResX template is Bun-centric. You created it with ${packageManager-> getPackageManagerName}, but the generated project should use Bun.
127+
128+ cd ${projectName}
129+ bun install
130+ bun run dev` ,
131+ )
132+ }
133+ } else {
134+ P .note (
135+ ~title = "Get started" ,
136+ ~message = ` cd ${projectName}
137+
138+ # See the project's README.md for more information.` ,
139+ )
140+ }
141+ }
142+
73143let createProject = async (~templateName , ~projectName , ~versions ) => {
74144 let templatePath = CraPaths .getTemplatePath (~templateName )
75145 let projectPath = Path .join2 (Process .cwd (), projectName )
@@ -86,6 +156,9 @@ let createProject = async (~templateName, ~projectName, ~versions) => {
86156 await Fs .Promises .rename ("_gitignore" , ".gitignore" )
87157 await updatePackageJson (~projectName , ~versions )
88158 await updateRescriptJson (~projectName , ~versions )
159+ if templateName === Templates .resxTemplateName {
160+ await synchronizeResxTemplateFiles (~projectName )
161+ }
89162
90163 await RescriptVersions .installVersions (versions )
91164 let _ = await Promisified .ChildProcess .exec ("git init" )
@@ -94,12 +167,7 @@ let createProject = async (~templateName, ~projectName, ~versions) => {
94167 s -> P .Spinner .stop ("Project created." )
95168 }
96169
97- P .note (
98- ~title = "Get started" ,
99- ~message = ` cd ${projectName}
100-
101- # See the project's README.md for more information.` ,
102- )
170+ await showGetStartedNote (~templateName , ~projectName )
103171}
104172
105173let createNewProject = async () => {
0 commit comments