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

inject-schematic-package-info.js

47 lines1.4 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 outputPath = path.join(packageDir, 'dist/schematics/ng-add/index.js')
15let output = fs.readFileSync(outputPath, 'utf8')
16​
17const replacements = new Map([
18 ['__ANGULAR_QUERY_PACKAGE_NAME__', queryPackage.name],
19 ['__ANGULAR_QUERY_DEVTOOLS_PACKAGE_NAME__', devtoolsPackage.name],
20 ['__ANGULAR_QUERY_DEVTOOLS_PACKAGE_VERSION__', devtoolsPackage.version],
21])
22​
23for (const [placeholder, value] of replacements) {
24 if (output.includes(placeholder)) {
25 output = output.replaceAll(placeholder, value)
26 } else if (!output.includes(value)) {
27 throw new Error(
28 `Could not find ${placeholder} or its expected value in ${outputPath}.`,
29 )
30 }
32​
33fs.writeFileSync(outputPath, output)
34​
35/**
36 * @param {string} packageJsonPath
37 * @returns {{ name: string; version: string }}
38 */
39function readPackageJson(packageJsonPath) {
40 const value = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
41​
42 if (typeof value.name !== 'string' || typeof value.version !== 'string') {
43 throw new Error(`Invalid package metadata in ${packageJsonPath}.`)
44 }
45​
46 return value