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

generate-labeler-config.mjs

116 lines3.0 KB

1import * as fs from 'node:fs'
2import * as path from 'node:path'
3import * as prettier from 'prettier'
4​
5/**
6 * Pairs of package labels and their corresponding paths
7 * @typedef {[string, string]} LabelerPair
8 */
9​
10/**
11 * @returns {Array<LabelerPair>} Pairs of package labels and their corresponding paths
12 */
13function readPairsFromFs() {
14 const ignored = new Set(['.DS_Store'])
15​
16 /** @type {Array<LabelerPair>} */
17 const pairs = []
18​
19 // Add subfolders in the packages folder, i.e. packages/**
20 fs.readdirSync(path.resolve('packages'))
21 .filter((folder) => !ignored.has(folder))
22 .forEach((folder) => {
23 // Check if package.json exists for the folder before adding it
24 if (
25 fs.existsSync(
26 path.resolve(path.join('packages', folder, 'package.json')),
27 )
28 ) {
29 pairs.push([`package: ${folder}`, `packages/${folder}/**/*`])
30 } else {
31 console.info(
32 `Skipping \`${folder}\` as it does not have a \`package.json\` file.`,
33 )
34 }
35 })
36​
37 // Sort by package name in alphabetical order
38 pairs.sort((a, b) => a[0].localeCompare(b[0]))
39​
40 return pairs
42​
43/**
44 * @param {Array<LabelerPair>} pairs
45 * @returns {Promise<string>} YAML string for the labeler config
46 */
47async function generateLabelerYaml(pairs) {
48 function s(n = 1) {
49 return ' '.repeat(n)
50 }
51​
52 // Convert the pairs into valid yaml
53 const formattedPairs = pairs
54 .map(([packageLabel, packagePath]) => {
55 const result = [
56 `'${packageLabel}':`,
57 `${s(2)}-${s(1)}changed-files:`,
58 `${s(4)}-${s(1)}any-glob-to-any-file:${s(1)}'${packagePath}'`,
59 ].join('\n')
60​
61 return result
62 })
63 .join('\n')
64​
65 // Get the location of the Prettier config file
66 const prettierConfigPath = await prettier.resolveConfigFile()
67 if (!prettierConfigPath) {
68 throw new Error(
69 'No Prettier config file found. Please ensure you have a Prettier config file in your project.',
70 )
71 }
72 console.info('using prettier config file at:', prettierConfigPath)
73​
74 // Resolve the Prettier config
75 const prettierConfig = await prettier.resolveConfig(prettierConfigPath)
76 console.info('using resolved prettier config:', prettierConfig)
77​
78 // Format the YAML string using Prettier
79 const formattedStr = await prettier.format(formattedPairs, {
80 parser: 'yaml',
81 ...prettierConfig,
82 })
83​
84 return formattedStr
86​
87async function run() {
88 console.info('Generating labeler config...')
89​
90 // Generate the pairs of package labels and their corresponding paths
91 const pairs = readPairsFromFs()
92​
93 // Always add the docs folder
94 pairs.push(['documentation', 'docs/**/*'])
95​
96 // Convert the pairs into valid yaml
97 const yamlStr = await generateLabelerYaml(pairs)
98​
99 // Write to '.github/labeler.yml'
100 const configPath = path.resolve('labeler-config.yml')
101 fs.writeFileSync(configPath, yamlStr, {
102 encoding: 'utf-8',
103 })
104​
105 console.info(`Generated labeler config at \`${configPath}\`!`)
106 return
108​
109try {
110 run().then(() => {
111 process.exit(0)
112 })
113} catch (error) {
114 console.error('Error generating labeler config:', error)
115 process.exit(1)