@tanstack/router-generator 1.102.4 → 1.102.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/generator.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import path from 'node:path'
2
2
  import * as fs from 'node:fs'
3
3
  import * as fsp from 'node:fs/promises'
4
- import * as prettier from 'prettier'
5
4
  import {
6
5
  determineInitialRoutePath,
6
+ format,
7
7
  logging,
8
8
  multiSortBy,
9
9
  removeExt,
@@ -76,12 +76,6 @@ export async function generator(config: Config, root: string) {
76
76
 
77
77
  const TYPES_DISABLED = config.disableTypes
78
78
 
79
- const prettierOptions: prettier.Options = {
80
- semi: config.semicolons,
81
- singleQuote: config.quoteStyle === 'single',
82
- parser: 'typescript',
83
- }
84
-
85
79
  let getRouteNodesResult: GetRouteNodesResult
86
80
 
87
81
  if (config.virtualRouteConfig) {
@@ -138,17 +132,22 @@ export async function generator(config: Config, root: string) {
138
132
 
139
133
  if (!routeCode) {
140
134
  const _rootTemplate = ROUTE_TEMPLATE.rootRoute
141
- const replaced = fillTemplate(_rootTemplate.template(), {
135
+ const replaced = await fillTemplate(config, _rootTemplate.template(), {
142
136
  tsrImports: _rootTemplate.imports.tsrImports(),
143
137
  tsrPath: rootPathId,
144
138
  tsrExportStart: _rootTemplate.imports.tsrExportStart(),
145
139
  tsrExportEnd: _rootTemplate.imports.tsrExportEnd(),
146
140
  })
147
141
 
148
- logger.log(`🟡 Creating ${node.fullPath}`)
149
- fs.writeFileSync(
142
+ await writeIfDifferent(
150
143
  node.fullPath,
151
- await prettier.format(replaced, prettierOptions),
144
+ '', // Empty string because the file doesn't exist yet
145
+ replaced,
146
+ {
147
+ beforeWrite: () => {
148
+ logger.log(`🟡 Creating ${node.fullPath}`)
149
+ },
150
+ },
152
151
  )
153
152
  }
154
153
  }
@@ -208,7 +207,8 @@ export async function generator(config: Config, root: string) {
208
207
  if (node.isLazy) {
209
208
  // Check by default check if the user has a specific lazy route template
210
209
  // If not, check if the user has a route template and use that instead
211
- replaced = fillTemplate(
210
+ replaced = await fillTemplate(
211
+ config,
212
212
  (config.customScaffolding?.lazyRouteTemplate ||
213
213
  config.customScaffolding?.routeTemplate) ??
214
214
  tLazyRouteTemplate.template(),
@@ -227,7 +227,8 @@ export async function generator(config: Config, root: string) {
227
227
  !node.isPendingComponent &&
228
228
  !node.isLoader)
229
229
  ) {
230
- replaced = fillTemplate(
230
+ replaced = await fillTemplate(
231
+ config,
231
232
  config.customScaffolding?.routeTemplate ??
232
233
  tRouteTemplate.template(),
233
234
  {
@@ -260,17 +261,11 @@ export async function generator(config: Config, root: string) {
260
261
  )
261
262
  }
262
263
 
263
- await writeIfDifferent(
264
- node.fullPath,
265
- prettierOptions,
266
- routeCode,
267
- replaced,
268
- {
269
- beforeWrite: () => {
270
- logger.log(`🟡 Updating ${node.fullPath}`)
271
- },
264
+ await writeIfDifferent(node.fullPath, routeCode, replaced, {
265
+ beforeWrite: () => {
266
+ logger.log(`🟡 Updating ${node.fullPath}`)
272
267
  },
273
- )
268
+ })
274
269
  }
275
270
 
276
271
  if (
@@ -390,7 +385,8 @@ export async function generator(config: Config, root: string) {
390
385
  const escapedRoutePath = node.routePath?.replaceAll('$', '$$') ?? ''
391
386
 
392
387
  if (!routeCode) {
393
- const replaced = fillTemplate(
388
+ const replaced = await fillTemplate(
389
+ config,
394
390
  config.customScaffolding?.apiTemplate ?? defaultAPIRouteTemplate,
395
391
  {
396
392
  tsrImports:
@@ -401,15 +397,19 @@ export async function generator(config: Config, root: string) {
401
397
  },
402
398
  )
403
399
 
404
- logger.log(`🟡 Creating ${node.fullPath}`)
405
- fs.writeFileSync(
400
+ await writeIfDifferent(
406
401
  node.fullPath,
407
- await prettier.format(replaced, prettierOptions),
402
+ '', // Empty string because the file doesn't exist yet
403
+ replaced,
404
+ {
405
+ beforeWrite: () => {
406
+ logger.log(`🟡 Creating ${node.fullPath}`)
407
+ },
408
+ },
408
409
  )
409
410
  } else {
410
411
  await writeIfDifferent(
411
412
  node.fullPath,
412
- prettierOptions,
413
413
  routeCode,
414
414
  routeCode.replace(
415
415
  /(createAPIFileRoute\(\s*['"])([^\s]*)(['"],?\s*\))/g,
@@ -748,9 +748,8 @@ export async function generator(config: Config, root: string) {
748
748
  // Write the route tree file, if it has changed
749
749
  const routeTreeWriteResult = await writeIfDifferent(
750
750
  path.resolve(config.generatedRouteTree),
751
- prettierOptions,
752
- existingRouteTreeContent,
753
- routeConfigFileContent,
751
+ await format(existingRouteTreeContent, config),
752
+ await format(routeConfigFileContent, config),
754
753
  {
755
754
  beforeWrite: () => {
756
755
  logger.log(`🟡 Updating ${config.generatedRouteTree}`)
package/src/template.ts CHANGED
@@ -1,15 +1,18 @@
1
+ import { format } from './utils'
1
2
  import type { Config } from './config'
2
3
 
3
4
  type TemplateTag = 'tsrImports' | 'tsrPath' | 'tsrExportStart' | 'tsrExportEnd'
4
5
 
5
6
  export function fillTemplate(
7
+ config: Config,
6
8
  template: string,
7
9
  values: Record<TemplateTag, string>,
8
10
  ) {
9
- return template.replace(
11
+ const replaced = template.replace(
10
12
  /%%(\w+)%%/g,
11
13
  (_, key) => values[key as TemplateTag] || '',
12
14
  )
15
+ return format(replaced, config)
13
16
  }
14
17
 
15
18
  type TargetTemplate = {
package/src/utils.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as fs from 'node:fs'
2
2
  import * as prettier from 'prettier'
3
+ import type { Config } from './config'
3
4
 
4
5
  export function multiSortBy<T>(
5
6
  arr: Array<T>,
@@ -123,7 +124,6 @@ export function removeExt(d: string, keepExtension: boolean = false) {
123
124
  * This function writes to a file if the content is different.
124
125
  *
125
126
  * @param filepath The path to the file
126
- * @param prettierOptions Prettier options
127
127
  * @param content Original content
128
128
  * @param incomingContent New content
129
129
  * @param callbacks Callbacks to run before and after writing
@@ -131,26 +131,35 @@ export function removeExt(d: string, keepExtension: boolean = false) {
131
131
  */
132
132
  export async function writeIfDifferent(
133
133
  filepath: string,
134
- prettierOptions: prettier.Options,
135
134
  content: string,
136
135
  incomingContent: string,
137
136
  callbacks?: { beforeWrite?: () => void; afterWrite?: () => void },
138
137
  ): Promise<boolean> {
139
- const [formattedContent, updatedContent] = await Promise.all([
140
- prettier.format(content, prettierOptions),
141
- prettier.format(incomingContent, prettierOptions),
142
- ])
143
-
144
- if (formattedContent !== updatedContent) {
138
+ if (content !== incomingContent) {
145
139
  callbacks?.beforeWrite?.()
146
- fs.writeFileSync(filepath, updatedContent)
140
+ fs.writeFileSync(filepath, incomingContent)
147
141
  callbacks?.afterWrite?.()
148
142
  return true
149
143
  }
150
-
151
144
  return false
152
145
  }
153
146
 
147
+ /**
148
+ * This function formats the source code using the default formatter (Prettier).
149
+ *
150
+ * @param source The content to format
151
+ * @param config The configuration object
152
+ * @returns The formatted content
153
+ */
154
+ export async function format(source: string, config: Config): Promise<string> {
155
+ const prettierOptions: prettier.Config = {
156
+ semi: config.semicolons,
157
+ singleQuote: config.quoteStyle === 'single',
158
+ parser: 'typescript',
159
+ }
160
+ return prettier.format(source, prettierOptions)
161
+ }
162
+
154
163
  /**
155
164
  * This function resets the regex index to 0 so that it can be reused
156
165
  * without having to create a new regex object or worry about the last