Skip to content

Commit

Permalink
Use @speed/json-extends rather than custom code
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesLMilner committed Sep 24, 2019
1 parent a1f76e5 commit 714b2d9
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 89 deletions.
49 changes: 38 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"typings.json"
],
"dependencies": {
"@speedy/json-extends": "^1.2.0",
"ajv": "6.6.2",
"chalk": "2.4.1",
"configstore": "3.1.2",
Expand Down
59 changes: 8 additions & 51 deletions src/configurationHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { join } from 'path';
import { Config, ConfigurationHelper, ConfigWrapper } from './interfaces';
import * as readlineSync from 'readline-sync';
import * as detectIndent from 'detect-indent';
import { json } from '@speedy/json-extends';

const pkgDir = require('pkg-dir');

const appPath = pkgDir.sync(process.cwd());

export function getDojoRcConfigOption(): string {
Expand Down Expand Up @@ -37,18 +37,20 @@ function parseConfigs(): ConfigWrapper {
if (existsSync(dojoRcPath)) {
try {
const dojoRcFile = readFileSync(dojoRcPath, 'utf8');
const extendedConfig = json.readSync(dojoRcPath);
configWrapper.dojoRcIndent = detectIndent(dojoRcFile).indent;
configWrapper.dojoRcConfig = JSON.parse(dojoRcFile);
configWrapper.dojoRcConfig = extendedConfig;
} catch (error) {
throw Error(chalk.red(`Could not parse the .dojorc file to get config : ${error}`));
throw Error(chalk.red(`Could not parse the .dojorc file to get config: ${error}`));
}
}

if (existsSync(packageJsonPath)) {
try {
const packageJsonFile = readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonFile);
const packageJson: any = json.readSync(packageJsonPath);
configWrapper.packageJsonIndent = detectIndent(packageJsonFile).indent;

configWrapper.packageJsonConfig = packageJson.dojo;
} catch (error) {
throw Error(chalk.red(`Could not parse the package.json file to get config: ${error}`));
Expand Down Expand Up @@ -81,60 +83,15 @@ function writeDojoRcConfig(config: Config, indent: string | number) {
writeFileSync(dojoRcPath, json);
}

function mergeConfigs(config: Config) {
const configs = getExtendingConfigs(config);
let mergedConfig: Config = config;
if (configs.length) {
let baseConfig: Config;
while ((baseConfig = configs.shift())) {
mergedConfig = merge(mergedConfig, baseConfig);
}
}
return mergedConfig;
}

function getExtendingConfigs(config: Config) {
const configs = [];
let extendingConfig = config;
while (typeof extendingConfig.extends === 'string') {
const extendedConfig = JSON.parse(readFileSync(extendingConfig.extends, 'utf8'));
configs.push(extendedConfig);
extendingConfig = extendedConfig;
}
return configs;
}

function merge(extendingObj: { [key: string]: any }, baseObj: { [key: string]: any }) {
Object.keys(baseObj).forEach((prop) => {
if (extendingObj[prop] === undefined) {
extendingObj[prop] = baseObj[prop];
} else if (isObject(baseObj[prop])) {
extendingObj[prop] = merge(extendingObj[prop], baseObj[prop]);
}
});
return extendingObj;
}

function isObject(item: any) {
return item && typeof item === 'object' && !Array.isArray(item);
}

function checkIfConfigExtends(config?: Config) {
if (config && config.extends) {
config = mergeConfigs(config);
}
return config;
}

export function getConfig(): Config | undefined {
const { packageJsonConfig, dojoRcConfig } = parseConfigs();
const hasPackageConfig = typeof packageJsonConfig === 'object';
const hasDojoRcConfig = typeof dojoRcConfig === 'object';

if (!hasDojoRcConfig && hasPackageConfig) {
return checkIfConfigExtends(packageJsonConfig);
return packageJsonConfig;
} else {
return checkIfConfigExtends(dojoRcConfig);
return dojoRcConfig;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function init() {

registerCommands(yargs, mergedCommands);
} catch (err) {
console.log(`Commands are not available: ${err}`);
console.log(`Commands are not available:\n ${err.message}`);
}

try {
Expand Down
3 changes: 2 additions & 1 deletion src/loadCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as globby from 'globby';
import { resolve as pathResolve, join } from 'path';
import { CliConfig, CommandWrapper, GroupMap } from './interfaces';
import configurationHelper from './configurationHelper';
import chalk from 'chalk';

export function isEjected(groupName: string, command: string): boolean {
const config: any = configurationHelper.sandbox(groupName, command).get();
Expand Down Expand Up @@ -72,7 +73,7 @@ export async function loadCommands(paths: string[], load: (path: string) => Comm
}
}
} catch (error) {
error.message = `Failed to load module ${path}\nNested error: ${error.message}`;
error.message = `${chalk.red(`Failed to load module ${path}`)}\n\nNested error:\n ${error.message}`;
reject(error);
}
});
Expand Down
Loading

0 comments on commit 714b2d9

Please sign in to comment.