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

inject-schematic-package-info.js

48 lines1.5 KB

1import fs from 'node:fs'
2import path from 'node:path'
3import { fileURLToPath } from 'node:url'
4​
5const packageDir = path.resolve(
6 path.dirname(fileURLToPath(import.meta.url)),
7 '..',
8)
9const queryPackage = readPackageJson(path.join(packageDir, 'package.json'))
10const devtoolsPackage = readPackageJson(
11 path.resolve(packageDir, '../angular-query-devtools/package.json'),
13​
14const replacements = new Map([
15 ['__ANGULAR_QUERY_PACKAGE_NAME__', queryPackage.name],
16 ['__ANGULAR_QUERY_PACKAGE_VERSION__', queryPackage.version],
17 ['__ANGULAR_QUERY_DEVTOOLS_PACKAGE_NAME__', devtoolsPackage.name],
18 ['__ANGULAR_QUERY_DEVTOOLS_PACKAGE_VERSION__', devtoolsPackage.version],
19])
20​
21for (const schematic of ['ng-add', 'migrate-from-experimental']) {
22 const outputPath = path.join(
23 packageDir,
24 `dist/schematics/${schematic}/index.js`,
25 )
26 let output = fs.readFileSync(outputPath, 'utf8')
27 for (const [placeholder, value] of replacements) {
28 output = output.replaceAll(placeholder, value)
29 }
30 if (output.includes('__ANGULAR_QUERY_')) {
31 throw new Error(`Unresolved package metadata in ${outputPath}.`)
32 }
33 fs.writeFileSync(outputPath, output)
35​
36/**
37 * @param {string} packageJsonPath
38 * @returns {{ name: string; version: string }}
39 */
40function readPackageJson(packageJsonPath) {
41 const value = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
42​
43 if (typeof value.name !== 'string' || typeof value.version !== 'string') {
44 throw new Error(`Invalid package metadata in ${packageJsonPath}.`)
45 }
46​
47 return value