-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(app): Extract utils and add tests
- Loading branch information
1 parent
9cb040b
commit 39821eb
Showing
18 changed files
with
1,398 additions
and
1,129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
#!/usr/bin/env node | ||
|
||
require('../src/createTypeAndJS') | ||
require('../src/createTypeAndJS.js') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/** | ||
* Created by tushar on 30/12/17. | ||
*/ | ||
import {createMergedConfig} from './src/createMergedConfig' | ||
|
||
import {mergeAllConfigs} from './src/mergeAllConfigs' | ||
export const config: Config = mergeAllConfigs(process) | ||
export const config: Config = createMergedConfig(process) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
import {NonConfigEnv} from './configPaths' | ||
|
||
/** | ||
* Check if given path has default.json | ||
*/ | ||
export const checkIfDefaultJson = <T extends NonConfigEnv>( | ||
process: T, | ||
p: string | ||
) => fs.existsSync(path.resolve(process.cwd(), p)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as glob from 'glob' | ||
import {baseConfigPath} from './baseConfigPath' | ||
import {mergeFileConfigsForPath} from './mergeFileConfigsForPath' | ||
import {checkIfDefaultJson} from './checkIfDefaultJson' | ||
import {NonConfigEnv} from './configPaths' | ||
|
||
const baseConfigPathName = baseConfigPath(process) | ||
|
||
/** | ||
* Get paths of config folder | ||
*/ | ||
export const getAllConfigPath: <T extends NonConfigEnv>( | ||
process: T | ||
) => Array<string> = process => | ||
glob.sync(`**/${baseConfigPathName}`, { | ||
ignore: ['node_modules/**'], | ||
cwd: process.cwd() | ||
}) | ||
|
||
/** | ||
* Create merged object of nested configs | ||
*/ | ||
export const createMergedConfig: <T extends NonConfigEnv>( | ||
process: T | ||
) => {[k: string]: Config} = process => { | ||
const configPaths = getAllConfigPath(process) | ||
const mergedConfig: {[k: string]: Config} = {} | ||
configPaths.forEach(p => { | ||
if (checkIfDefaultJson(process, p)) { | ||
mergedConfig[p] = mergeFileConfigsForPath(process, p) | ||
} | ||
}) | ||
return mergedConfig | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import {createMergedConfig} from '../src/createMergedConfig' | ||
import * as assert from 'assert' | ||
import * as path from 'path' | ||
|
||
describe('createMergedConfig', () => { | ||
it('should return nested config', () => { | ||
const process = { | ||
argv: [], | ||
cwd: () => path.resolve(__dirname, 'stub-module'), | ||
env: { | ||
NODE_ENV: undefined, | ||
MAX_RETRIES: 999 | ||
} | ||
} | ||
const actual = createMergedConfig(process) | ||
const expected = { | ||
config: { | ||
type: 'default', | ||
port: 9000, | ||
maxRetries: 999 | ||
}, | ||
'module-1/config': { | ||
type: 'default-module-1', | ||
maxRetries: 999, | ||
module1Props: 'mod1' | ||
} | ||
} | ||
assert.deepEqual(actual, expected) | ||
}) | ||
it('should return nested config for given deployment, env and user', () => { | ||
const process = { | ||
argv: [], | ||
cwd: () => path.resolve(__dirname, 'stub-module'), | ||
env: { | ||
DEPLOYMENT: 'www.example.com', | ||
NODE_ENV: 'production', | ||
USER: 'root', | ||
MAX_RETRIES: 999 | ||
} | ||
} | ||
const actual = createMergedConfig(process) | ||
const expected = { | ||
config: { | ||
type: 'user', | ||
port: 9000, | ||
maxRetries: 999 | ||
}, | ||
'module-1/config': { | ||
type: 'user', | ||
maxRetries: 999, | ||
module1Props: 'mod1-example' | ||
} | ||
} | ||
assert.deepEqual(actual, expected) | ||
}) | ||
it('should return nested config with cli option', () => { | ||
const process = { | ||
argv: ['--port', '3000', '--type', 'cli-type'], | ||
cwd: () => path.resolve(__dirname, 'stub-module'), | ||
env: { | ||
NODE_ENV: undefined, | ||
MAX_RETRIES: 999 | ||
} | ||
} | ||
const actual = createMergedConfig(process) | ||
const expected = { | ||
config: { | ||
type: 'cli-type', | ||
port: 3000, | ||
maxRetries: 999 | ||
}, | ||
'module-1/config': { | ||
type: 'cli-type', | ||
maxRetries: 999, | ||
module1Props: 'mod1', | ||
port: 3000 | ||
} | ||
} | ||
assert.deepEqual(actual, expected) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
{ | ||
"type": "default", | ||
"port": 9000, | ||
"type": "default-module-1", | ||
"maxRetries": "@@MAX_RETRIES", | ||
"module1Props": "mod1" | ||
} |
2 changes: 1 addition & 1 deletion
2
test/stub-module/module-1/config/deployment/www.example.com.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"type": "deployment" | ||
"module1Props": "mod1-example" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.