Skip to content

Commit

Permalink
feat: better config
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkomarychev committed Nov 10, 2019
1 parent c7e7635 commit 8495c3c
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 23 deletions.
51 changes: 40 additions & 11 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13443,22 +13443,51 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(__webpack_require__(747));
const js_yaml_1 = __importDefault(__webpack_require__(414));
function readConfig(filename) {
const cwd = process.cwd();
console.log('cwd', cwd);
function parseConfig(rawConfig) {
const result = { whitelist: [], blacklist: [], method: undefined };
if (rawConfig && rawConfig.whitelist) {
if (Array.isArray(rawConfig.whitelist)) {
result.whitelist = rawConfig.whitelist;
}
else {
throw new Error('`whitelist` should be an array');
}
}
if (rawConfig && rawConfig.blacklist) {
if (Array.isArray(rawConfig.blacklist)) {
result.blacklist = rawConfig.blacklist;
}
else {
throw new Error('`blacklist` should be an array');
}
}
if (rawConfig && rawConfig.method) {
const allowedString = ['squash', 'merge', 'rebase'];
if (typeof rawConfig.method !== 'string' ||
!allowedString.includes(rawConfig.method)) {
throw new Error(`'method' should be either 'merge', 'rebase', 'squash' or 'undefined', got ${rawConfig.method}`);
}
result.method = rawConfig.method;
}
return result;
}
exports.parseConfig = parseConfig;
function getFileData(filename) {
try {
const data = fs.readFileSync(filename).toString();
const yaml = js_yaml_1.default.safeLoad(data);
return {
whitelist: yaml.whitelist || [],
blacklist: yaml.blacklist || [],
};
return fs.readFileSync(filename).toString();
}
catch (error) {
console.warn('error reading config', error);
return { whitelist: [], blacklist: [] };
console.log(`Did not find config ${filename}`);
return '';
}
}
function readConfig(filename) {
const cwd = process.cwd();
console.log('cwd', cwd);
const data = getFileData(filename);
const yaml = js_yaml_1.default.safeLoad(data);
return parseConfig(yaml);
}
exports.default = readConfig;


Expand Down
45 changes: 44 additions & 1 deletion src/__tests__/readConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
import readConfig from '../readConfig'
import readConfig, { parseConfig } from '../readConfig'
import * as path from 'path'

describe('config', () => {
describe('config sanity', () => {
it.each`
value
${undefined}
${null}
${{}}
`('returns default values when input is $value', ({ value }) => {
expect(parseConfig(value)).toEqual({
whitelist: [],
blacklist: [],
method: undefined,
})
})
it('throws when types mismatch', () => {
expect(() => {
parseConfig({
whitelist: {},
})
}).toThrowError()
expect(() => {
parseConfig({
blacklist: {},
})
}).toThrowError()
expect(() => {
parseConfig({
method: {},
})
}).toThrowError()
expect(() => {
parseConfig({
method: 'unknown string',
})
}).toThrowError()
})
it('assigns method', () => {
expect(parseConfig({ method: 'merge' })).toEqual({
whitelist: [],
blacklist: [],
method: 'merge',
})
})
})
it('provides default config when file is absent', () => {
expect(readConfig(path.join(__dirname, '.mergepal.yml'))).toEqual({
whitelist: [],
Expand Down
55 changes: 44 additions & 11 deletions src/readConfig.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,51 @@
import * as fs from 'fs'
import jsyaml from 'js-yaml'
import { Config } from './types'

export default function readConfig(filename: string) {
const cwd = process.cwd()
console.log('cwd', cwd)
try {
const data = fs.readFileSync(filename).toString()
const yaml = jsyaml.safeLoad(data)
return {
whitelist: yaml.whitelist || [],
blacklist: yaml.blacklist || [],
export function parseConfig(rawConfig: any) {
const result: Config = { whitelist: [], blacklist: [], method: undefined }
if (rawConfig && rawConfig.whitelist) {
if (Array.isArray(rawConfig.whitelist)) {
result.whitelist = rawConfig.whitelist
} else {
throw new Error('`whitelist` should be an array')
}
}
if (rawConfig && rawConfig.blacklist) {
if (Array.isArray(rawConfig.blacklist)) {
result.blacklist = rawConfig.blacklist
} else {
throw new Error('`blacklist` should be an array')
}
}
if (rawConfig && rawConfig.method) {
const allowedString = ['squash', 'merge', 'rebase']
if (
typeof rawConfig.method !== 'string' ||
!allowedString.includes(rawConfig.method)
) {
throw new Error(
`'method' should be either 'merge', 'rebase', 'squash' or 'undefined', got ${rawConfig.method}`,
)
}
result.method = rawConfig.method
}
return result
}

function getFileData(filename: string) {
try {
return fs.readFileSync(filename).toString()
} catch (error) {
console.warn('error reading config', error)
return { whitelist: [], blacklist: [] }
console.log(`Did not find config ${filename}`)
return ''
}
}

export default function readConfig(filename: string) {
const cwd = process.cwd()
console.log('cwd', cwd)
const data = getFileData(filename)
const yaml = jsyaml.safeLoad(data)
return parseConfig(yaml)
}

0 comments on commit 8495c3c

Please sign in to comment.