Skip to content

Commit

Permalink
feat(utils): read config by branches
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed Dec 22, 2023
1 parent b5e4d66 commit 602aeeb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 31 deletions.
13 changes: 3 additions & 10 deletions packages/sample/lib/cmd.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ export const command = 'sample';

export const describe = 'Generate code sample files';

export function builder(cli) {
cli.option('config', {
alias: 'c',
describe: 'Config file path',
default: '.bring-it/sample.config.json',
type: 'string',
});
}
export function builder() {}

export function handler(io) {
export function handler() {
import('./lib/sample.mjs')
.then(({ action }) => action(io))
.then(({ action }) => action())
.catch((error) => {
process.exitCode = 1;
console.error(error);
Expand Down
12 changes: 1 addition & 11 deletions packages/sample/lib/lib/config.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { resolve } from 'node:path';

import { readJSON } from '@bring-it/utils';

import { logger } from './utils.mjs';

function checkString(input, name) {
if (typeof input !== 'string') {
throw new TypeError(`${name} must be a string`);
Expand All @@ -22,7 +18,7 @@ function checkArray(input, name, string = true) {
}
}

export function mergeConfig(group) {
export function mergeConfig(group = [{}]) {
checkArray(group, 'group', false);

return group.map(
Expand Down Expand Up @@ -62,9 +58,3 @@ export function mergeConfig(group) {
},
);
}

export function readConfig(configName) {
return readJSON(configName, logger).then(({ group = [{}] }) =>
mergeConfig(group),
);
}
10 changes: 7 additions & 3 deletions packages/sample/lib/lib/sample.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { readConfig } from './config.mjs';
import { readConfig } from '@bring-it/utils';

import { mergeConfig } from './config.mjs';
import { pdf } from './pdf.mjs';
import { picker, scan } from './picker.mjs';
import { logger } from './utils.mjs';

export async function action({ config: configName }) {
const configs = await readConfig(configName);
export async function action() {
const configs = await readConfig('sample', logger).then(({ group }) =>
mergeConfig(group),
);

for (const config of configs) {
const files = await scan(config);
Expand Down
9 changes: 3 additions & 6 deletions packages/sentry/lib/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { rm } from 'node:fs/promises';
import spawn from '@npmcli/promise-spawn';
import { globby } from 'globby';

import { ignore, Logger, readJSON } from '@bring-it/utils/index.mjs';
import { ignore, Logger, readConfig } from '@bring-it/utils';

const logger = new Logger('sentry');

Expand All @@ -14,10 +14,6 @@ function commitHash() {
}).trim();
}

function readConfig(configName) {
return readJSON(configName, logger);
}

function scan({ include }) {
return globby(include, {
ignore,
Expand Down Expand Up @@ -51,7 +47,8 @@ function createCli({ url, org, project, token }) {
export async function action({ mode, name }) {
try {
const { [name || mode]: current, ...all } = await readConfig(
'.bring-it/sentry.config.json',
'sentry',
logger,
);

const {
Expand Down
13 changes: 12 additions & 1 deletion packages/utils/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,20 @@ export const ignore = [
'**/sshd*config',
];

export function readJSON(configName, logger) {
function readJSON(configName, logger) {
return readFile(configName, 'utf8')
.then((text) => JSON.parse(text))
.then((json) => {
const { BRANCH_NAME } = process.env;

if (BRANCH_NAME) {
const { branches, ...rest } = json;

return { ...rest, ...branches[BRANCH_NAME] };
}

return json;
})
.catch((error) => {
logger.warn(error.message);
logger.info('Fallback to default configuration');
Expand Down

0 comments on commit 602aeeb

Please sign in to comment.