UNPKG

@nx-dotnet/utils

Version:

This library was generated with [Nx](https://nx.dev).

152 lines 5.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultConfigValues = void 0; exports.readConfig = readConfig; exports.updateConfig = updateConfig; exports.updateConfigInNxJson = updateConfigInNxJson; exports.readConfigSection = readConfigSection; exports.readConfigFromRCFile = readConfigFromRCFile; exports.readConfigFromNxJson = readConfigFromNxJson; exports.deepMerge = deepMerge; exports.isNxDotnetConfigV1 = isNxDotnetConfigV1; exports.upgradeConfigToV2 = upgradeConfigToV2; const devkit_1 = require("@nx/devkit"); const constants_1 = require("../constants"); const semver_1 = require("semver"); exports.DefaultConfigValues = { solutionFile: '{npmScope}.nx-dotnet.sln', inferProjects: true, nugetPackages: {}, inferredTargets: { build: 'build', lint: 'lint', serve: 'serve', test: 'test', }, ignorePaths: [], tags: ['nx-dotnet'], }; function readConfig(host) { const configFromFile = readConfigFromRCFile(host); const configFromNxJson = readConfigFromNxJson(host); return deepMerge(exports.DefaultConfigValues, isNxDotnetConfigV1(configFromFile) ? upgradeConfigToV2(configFromFile) : configFromFile, isNxDotnetConfigV1(configFromNxJson) ? upgradeConfigToV2(configFromNxJson) : configFromNxJson); } function updateConfig(host, value) { if ((0, semver_1.major)(devkit_1.NX_VERSION) < 17) { (0, devkit_1.writeJson)(host, constants_1.CONFIG_FILE_PATH, value); } else { updateConfigInNxJson(host, value); } } function updateConfigInNxJson(host, value) { const nxJson = (0, devkit_1.readNxJson)(host); if (!nxJson) { throw new Error('nx-dotnet requires nx.json to be present in the workspace'); } nxJson.plugins ?? (nxJson.plugins = []); const pluginIndex = nxJson.plugins.findIndex((p) => typeof p === 'string' ? p === '@nx-dotnet/core' : p.plugin === '@nx-dotnet/core'); if (pluginIndex > -1) { nxJson.plugins[pluginIndex] = { plugin: '@nx-dotnet/core', // Remove cast after next beta options: value, }; } else { throw new Error('nx-dotnet requires @nx-dotnet/core to be present in nx.json'); } (0, devkit_1.writeJson)(host, 'nx.json', nxJson); } function readConfigSection(host, section) { const config = readConfig(host); return config[section] || exports.DefaultConfigValues[section]; } function readConfigFromRCFile(host) { try { if (host) { return (0, devkit_1.readJson)(host, constants_1.CONFIG_FILE_PATH); } else { return (0, devkit_1.readJsonFile)(`${devkit_1.workspaceRoot}/${constants_1.CONFIG_FILE_PATH}`); } } catch { return null; } } function readConfigFromNxJson(host) { if ((0, semver_1.major)(devkit_1.NX_VERSION) < 17) { return null; } try { const nxJson = host ? (0, devkit_1.readNxJson)(host) : (0, devkit_1.readJsonFile)(`${devkit_1.workspaceRoot}/nx.json`); const plugin = nxJson?.plugins?.find((p) => typeof p === 'string' ? p === '@nx-dotnet/core' : p.plugin === '@nx-dotnet/core'); if (!plugin || typeof plugin === 'string') { return null; } else { return plugin.options; } } catch { return null; } } function deepMerge(base, ...objects) { function isObject(obj) { return !!obj && typeof obj === 'object'; } return objects.reduce((agg, obj) => { if (obj === null || obj === undefined) { return agg; } Object.keys(obj).forEach((key) => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const aggVal = agg[key]; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const nextVal = obj[key]; if (Array.isArray(aggVal) && Array.isArray(nextVal)) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore agg[key] = aggVal.concat(...nextVal); } else if (isObject(aggVal) && isObject(nextVal)) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore agg[key] = deepMerge(aggVal, nextVal); } else { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore agg[key] = nextVal; } }); return agg; }, JSON.parse(JSON.stringify(base))); } function isNxDotnetConfigV1(config) { return !!config && 'inferProjectTargets' in config; } function upgradeConfigToV2(config) { const { inferProjectTargets, ...v2compatible } = config; return { ...v2compatible, inferredTargets: inferProjectTargets !== false ? exports.DefaultConfigValues.inferredTargets : false, }; } //# sourceMappingURL=config.js.map