Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

template-file.ts

149 lines3.7 KB

1import { resolve, sep } from 'node:path'
2import { render } from 'ejs'
3import { format } from 'prettier'
4​
5import { formatCommand } from './utils.js'
6import {
7 getPackageManagerInstallCommand,
8 getPackageManagerScriptCommand,
9} from './package-manager.js'
10import { relativePath } from './file-helpers.js'
11​
12import type { AddOn, Environment, Options } from './types.js'
13​
14function convertDotFilesAndPaths(path: string) {
15 return path
16 .split(sep)
17 .map((segment) => segment.replace(/^_dot_/, '.'))
18 .join(sep)
20​
21export function createTemplateFile(environment: Environment, options: Options) {
22 function getPackageManagerAddScript(
23 packageName: string,
24 isDev: boolean = false,
25 ) {
26 return formatCommand(
27 getPackageManagerInstallCommand(
28 options.packageManager,
29 packageName,
30 isDev,
31 ),
32 )
33 }
34 function getPackageManagerRunScript(
35 scriptName: string,
36 args: Array<string> = [],
37 ) {
38 return formatCommand(
39 getPackageManagerScriptCommand(options.packageManager, [
40 scriptName,
41 ...args,
42 ]),
43 )
44 }
45​
46 class IgnoreFileError extends Error {
47 constructor() {
48 super('ignoreFile')
49 this.name = 'IgnoreFileError'
50 }
51 }
52​
53 const integrations: Array<Required<AddOn>['integrations'][number]> = []
54 for (const addOn of options.chosenAddOns) {
55 if (addOn.integrations) {
56 for (const integration of addOn.integrations) {
57 integrations.push(integration)
58 }
59 }
60 }
61​
62 const routes: Array<Required<AddOn>['routes'][number]> = []
63 for (const addOn of options.chosenAddOns) {
64 if (addOn.routes) {
65 routes.push(...addOn.routes)
66 }
67 }
68​
69 const addOnEnabled = options.chosenAddOns.reduce<Record<string, boolean>>(
70 (acc, addOn) => {
71 acc[addOn.id] = true
72 return acc
73 },
74 {},
75 )
76​
77 return async function templateFile(file: string, content: string) {
78 const templateValues = {
79 packageManager: options.packageManager,
80 projectName: options.projectName,
81 typescript: options.typescript,
82 tailwind: options.tailwind,
83 js: options.typescript ? 'ts' : 'js',
84 jsx: options.typescript ? 'tsx' : 'jsx',
85 fileRouter: options.mode === 'file-router',
86 codeRouter: options.mode === 'code-router',
87 addOnEnabled,
88 addOns: options.chosenAddOns,
89 integrations,
90 routes,
91​
92 getPackageManagerAddScript,
93 getPackageManagerRunScript,
94​
95 relativePath: (path: string) => relativePath(file, path),
96​
97 ignoreFile: () => {
98 throw new IgnoreFileError()
99 },
100 }
101​
102 let ignoreFile = false
103​
104 if (file.endsWith('.ejs')) {
105 try {
106 content = render(content, templateValues)
107 } catch (error) {
108 if (error instanceof IgnoreFileError) {
109 ignoreFile = true
110 } else {
111 console.error(file, error)
112 environment.error(`EJS error in file ${file}`, error?.toString())
113 process.exit(1)
114 }
115 }
116 }
117​
118 if (ignoreFile) {
119 return
120 }
121​
122 let target = convertDotFilesAndPaths(file.replace('.ejs', ''))
123​
124 let append = false
125 if (target.endsWith('.append')) {
126 append = true
127 target = target.replace('.append', '')
128 }
129​
130 if (target.endsWith('.ts') || target.endsWith('.tsx')) {
131 content = await format(content, {
132 semi: false,
133 singleQuote: true,
134 trailingComma: 'all',
135 parser: 'typescript',
136 })
137 }
138​
139 if (!options.typescript) {
140 target = target.replace(/\.tsx$/, '.jsx').replace(/\.ts$/, '.js')
141 }
142​
143 if (append) {
144 await environment.appendFile(resolve(options.targetDir, target), content)
145 } else {
146 await environment.writeFile(resolve(options.targetDir, target), content)
147 }
148 }