Skip to content

Commit

Permalink
Merge pull request #8 from karmaniverous:feature/gh-7-support-local-c…
Browse files Browse the repository at this point in the history
…onfig-override

[GH-7] added local config override
  • Loading branch information
karmaniverous authored Aug 12, 2024
2 parents 3b3bf2c + 7f2f744 commit f525010
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .metastructure.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
configPath: ../metastructure-template/src/metastructure.yml
config_path: ../metastructure-template/src/metastructure.yml
3 changes: 2 additions & 1 deletion src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export const configSchema = z
.nullable()
.optional(),
cli_params: cliParamsSchema.optional(),
configPath: z.string().nullable().optional(),
config_path: z.string().nullable().optional(),
config_override_path: z.string().nullable().optional(),
organization: z
.object({
id: z.string().nullable().optional(),
Expand Down
18 changes: 16 additions & 2 deletions src/configFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const resolveConfigPath = async (path?: string) => {

if (await fs.exists(resolvedPath)) {
try {
const { configPath } = parse(
const { config_path: configPath } = parse(
await fs.readFile(resolvedPath, 'utf8'),
) as Partial<Config>;

Expand All @@ -46,7 +46,21 @@ export const readConfig = async (path?: string) => {

const rawConfig = parse(await fs.readFile(configPath, 'utf8')) as Config;

return { rawConfig, configPath };
const pkgDir = (await packageDirectory({ cwd: configPath })) ?? '.';

if (rawConfig.config_override_path) {
const overridePath = resolve(pkgDir, rawConfig.config_override_path);

if (await fs.exists(overridePath)) {
const overrideConfig = parse(
await fs.readFile(overridePath, 'utf8'),
) as Config;

_.merge(rawConfig, overrideConfig);
}
}

return { pkgDir, rawConfig, configPath };
};

export const writeConfig = async (config: Config, configPath: string) => {
Expand Down
9 changes: 2 additions & 7 deletions src/parseConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { Handlebars } from '@karmaniverous/handlebars';
import chalk from 'chalk';
import fs from 'fs-extra';
import _ from 'lodash';
import { dirname, resolve } from 'path';
import { packageDirectory } from 'pkg-dir';
import { resolve } from 'path';
import { inspect } from 'util';
import { parse } from 'yaml';

Expand Down Expand Up @@ -37,7 +36,6 @@ export const parseConfig = async ({
useLocalState,
}: ParseConfigParams) => {
let config: Config;
let configPath: string;
let pkgDir: string;
let rawConfig: Config;

Expand All @@ -46,17 +44,14 @@ export const parseConfig = async ({
process.stdout.write(chalk.black.bold('Parsing config file...'));

// Load & parse config file.
({ rawConfig, configPath } = await readConfig(path));
({ pkgDir, rawConfig } = await readConfig(path));

// Validate workspace.
if (!rawConfig.workspaces?.[workspace]) {
console.log(chalk.red.bold('Unknown workspace!\n'));
throw new Error(`Unknown workspace: ${workspace}`);
}

// Get CLI default overrides.
pkgDir = (await packageDirectory({ cwd: dirname(configPath) })) ?? '.';

let cliDefaultOverrides: CliDefaultOverrides = {};

const rawWorkspace = rawConfig.workspaces[workspace];
Expand Down
4 changes: 1 addition & 3 deletions src/updateConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import chalk from 'chalk';
import { execa } from 'execa';
import _ from 'lodash';
import { resolve } from 'path';
import { packageDirectory } from 'pkg-dir';
import { inspect } from 'util';

import { awsCredentials } from './awsCredentials';
Expand All @@ -27,7 +26,7 @@ export const updateConfig = async ({
process.stdout.write(chalk.black.bold(`Updating config file...`));

// Load config file.
const { rawConfig: config, configPath } = await readConfig(path);
const { pkgDir, rawConfig: config, configPath } = await readConfig(path);

// Validate workspace.
if (!config.workspaces?.[workspace]) {
Expand All @@ -37,7 +36,6 @@ export const updateConfig = async ({
}

// Configure shell client.
const pkgDir = (await packageDirectory({ cwd: configPath })) ?? '.';
const $ = execa({
cwd: resolve(pkgDir, config.workspaces[workspace].path),
env: {
Expand Down

0 comments on commit f525010

Please sign in to comment.