From 776b849e980470c71c56100f85bd6273a1e508cb Mon Sep 17 00:00:00 2001 From: Aleksandr Kitov Date: Mon, 25 Sep 2023 10:50:05 +0200 Subject: [PATCH] fix(config): allow to set presets path from config file --- .changeset/selfish-planets-stare.md | 5 ++++ .../src/configs/app-configs/get-defaults.ts | 4 +++- .../configs/app-configs/read-config-file.ts | 24 +++++++++++++++++++ .../app-configs/update-with-config-file.ts | 20 ++++------------ 4 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 .changeset/selfish-planets-stare.md create mode 100644 packages/arui-scripts/src/configs/app-configs/read-config-file.ts diff --git a/.changeset/selfish-planets-stare.md b/.changeset/selfish-planets-stare.md new file mode 100644 index 00000000..e35c38fe --- /dev/null +++ b/.changeset/selfish-planets-stare.md @@ -0,0 +1,5 @@ +--- +'arui-scripts': patch +--- + +Исправлена ошибка, не позволявшая задать пресеты через конфиг-файл diff --git a/packages/arui-scripts/src/configs/app-configs/get-defaults.ts b/packages/arui-scripts/src/configs/app-configs/get-defaults.ts index e9f5e588..8cdd055c 100644 --- a/packages/arui-scripts/src/configs/app-configs/get-defaults.ts +++ b/packages/arui-scripts/src/configs/app-configs/get-defaults.ts @@ -3,6 +3,7 @@ import path from 'path'; import { tryResolve } from '../util/resolve'; +import { readConfigFile } from './read-config-file'; import { AppConfigs, AppContext } from './types'; const CWD = process.cwd(); @@ -10,6 +11,7 @@ const absoluteSrcPath = path.resolve(CWD, 'src'); export function getDefaultAppConfig(): AppConfigs { const appPackage = getPackageJson(); + const configFile = readConfigFile(CWD); return { /// general settings @@ -19,7 +21,7 @@ export function getDefaultAppConfig(): AppConfigs { devSourceMaps: 'eval', devServerCors: false, useServerHMR: false, - presets: appPackage.aruiScripts?.presets || null, + presets: configFile?.presets || appPackage?.aruiScripts?.presets || null, proxy: appPackage.proxy || null, // paths diff --git a/packages/arui-scripts/src/configs/app-configs/read-config-file.ts b/packages/arui-scripts/src/configs/app-configs/read-config-file.ts new file mode 100644 index 00000000..78eebc6f --- /dev/null +++ b/packages/arui-scripts/src/configs/app-configs/read-config-file.ts @@ -0,0 +1,24 @@ +import path from 'path'; + +import { tryResolve } from '../util/resolve'; + +export function readConfigFile(cwd: string) { + const appConfigPath = tryResolve(path.join(cwd, '/arui-scripts.config')); + + if (appConfigPath) { + // Мы не можем использовать импорты, нам нужен именно require, потому что мы не знаем заранее не только путь до файла, + // но и то, на каком языке он написан + // eslint-disable-next-line import/no-dynamic-require, global-require, @typescript-eslint/no-var-requires + let appSettings = require(appConfigPath); + + // ts-node импортирует esModules, из них надо вытягивать default именно так + // eslint-disable-next-line no-underscore-dangle + if (appSettings.__esModule) { + appSettings = appSettings.default; + } + + return appSettings; + } + + return null; +} diff --git a/packages/arui-scripts/src/configs/app-configs/update-with-config-file.ts b/packages/arui-scripts/src/configs/app-configs/update-with-config-file.ts index 8a9c5276..9b09157c 100644 --- a/packages/arui-scripts/src/configs/app-configs/update-with-config-file.ts +++ b/packages/arui-scripts/src/configs/app-configs/update-with-config-file.ts @@ -1,26 +1,14 @@ -// TODO: remove eslint-disable-next-line -import path from 'path'; - import merge from 'lodash.merge'; -import { tryResolve } from '../util/resolve'; - +import { readConfigFile } from './read-config-file'; import { AppConfigs, AppContext } from './types'; import { validateSettingsKeys } from './validate-settings-keys'; export function updateWithConfigFile(config: AppConfigs, context: AppContext) { - const appConfigPath = tryResolve(path.join(context.cwd, '/arui-scripts.config')); - - if (appConfigPath) { - // eslint-disable-next-line import/no-dynamic-require, global-require, @typescript-eslint/no-var-requires - let appSettings = require(appConfigPath); + const appSettings = readConfigFile(context.cwd); - // eslint-disable-next-line no-underscore-dangle - if (appSettings.__esModule) { - // ts-node импортирует esModules, из них надо вытягивать default именно так - appSettings = appSettings.default; - } - validateSettingsKeys(config, appSettings, appConfigPath); + if (appSettings) { + validateSettingsKeys(config, appSettings, context.cwd); return merge(config, appSettings); }