Open History to see the last commit
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'),
12)
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 }
31}
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
47}