Skip to content

Commit

Permalink
Merge pull request #131 from core-ds/fix/presets-from-config
Browse files Browse the repository at this point in the history
fix(config): allow to set presets path from config file
  • Loading branch information
heymdall-legal authored Sep 25, 2023
2 parents 5457e3d + 776b849 commit 552fd8f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-planets-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'arui-scripts': patch
---

Исправлена ошибка, не позволявшая задать пресеты через конфиг-файл
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import path from 'path';

import { tryResolve } from '../util/resolve';

import { readConfigFile } from './read-config-file';
import { AppConfigs, AppContext } from './types';

const CWD = process.cwd();
const absoluteSrcPath = path.resolve(CWD, 'src');

export function getDefaultAppConfig(): AppConfigs {
const appPackage = getPackageJson();
const configFile = readConfigFile(CWD);

return {
/// general settings
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions packages/arui-scripts/src/configs/app-configs/read-config-file.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down

0 comments on commit 552fd8f

Please sign in to comment.