Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dynamic-config): use typescript compiler to load typescript configs #48

Merged
merged 5 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions packages/consul-client/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
"max-classes-per-file": false,
"object-literal-sort-keys": false,
"no-console": false,
"no-var-requires": false,
"array-type": [
true,
"generic"
],
"semicolon": [
true,
"never"
],
"quotemark": [
true,
"single",
"avoid-escape"
]
"no-var-requires": false,
"array-type": [
true,
"generic"
],
"semicolon": [
true,
"never"
],
"quotemark": [
true,
"single",
"avoid-escape"
]
}
}
89 changes: 0 additions & 89 deletions packages/dynamic-config/package-lock.json

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

3 changes: 1 addition & 2 deletions packages/dynamic-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@
"lab": "^14.3.1",
"rimraf": "^2.6.2",
"tslint": "^5.8.0",
"typescript": "2.7.x",
"wait-on": "^2.0.2"
},
"dependencies": {
"@creditkarma/consul-client": "^0.4.3",
"@creditkarma/vault-client": "^0.4.3",
"@types/debug": "0.0.30",
"debug": "^3.1.0",
"ts-node": "^4.1.0"
"typescript": "2.7.x"
}
}
2 changes: 2 additions & 0 deletions packages/dynamic-config/src/main/ConfigLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ function getConfigPath(sourceDir: string): string {
async function loadFileWithName(loaders: Array<IFileLoader>, configPath: string, name: string): Promise<IRootConfigValue> {
const configs: Array<object> = await PromiseUtils.valuesForPromises(loaders.map((loader: IFileLoader) => {
const filePath: string = path.resolve(configPath, `${name}.${loader.type}`)

return fileExists(filePath).then(() => {
return loader.load(filePath)

}).catch((err: any) => {
return {}
})
Expand Down
69 changes: 55 additions & 14 deletions packages/dynamic-config/src/main/loaders/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,65 @@
import * as fs from 'fs'
import * as path from 'path'
import * as ts from 'typescript'
import * as vm from 'vm'
import * as logger from '../logger'
import { IFileLoader } from '../types'

export const tsLoader: IFileLoader = {
type: 'ts',
async load(filePath: string): Promise<object> {
require('ts-node').register({
lazy: true,
cache: false,
typeCheck: true,
compilerOptions: {
allowJs: true,
rootDir: '.',
function locateFile(basePath: string, searchPath: string): string {
const resolvedPath: string = path.resolve(path.dirname(basePath), searchPath)
if (fs.existsSync(`${resolvedPath}.ts`)) {
return `${resolvedPath}.ts`
} else {
return `${resolvedPath}.js`
}
}

function loadTypeScript(filePath: string): any {
try {
const contents: Buffer = fs.readFileSync(filePath)
const source: string = contents.toString()
const result: ts.TranspileOutput = ts.transpileModule(source, {})
const sandbox = Object.assign({}, global, {
exports: {},
require(pathToRequire: string) {
if (pathToRequire.startsWith('.') || pathToRequire.startsWith('/')) {
const resolvedFile: string = locateFile(filePath, pathToRequire)

// If the file to include ends in `ts` use our custom machinery to load file
if (path.extname(resolvedFile) === '.ts') {
return loadTypeScript(resolvedFile)

// Else use the default node system, resolving to absolute path to account for our
// shenanigans
} else {
return require(resolvedFile)
}
} else {
return require(pathToRequire)
}
},
})

const configObj = require(filePath)
vm.createContext(sandbox)

if (typeof configObj.default === 'object') {
return configObj.default
vm.runInContext(result.outputText, sandbox , {
displayErrors: true,
})

if ((sandbox.exports as any).default) {
return (sandbox.exports as any).default
} else {
return configObj
return sandbox.exports
}
} catch (err) {
logger.error(`Error parsing typescript config[${filePath}]: `, err)
return {}
}
}

export const tsLoader: IFileLoader = {
type: 'ts',
async load(filePath: string): Promise<object> {
return Promise.resolve(loadTypeScript(filePath))
},
}
5 changes: 5 additions & 0 deletions packages/dynamic-config/src/tests/integration/config/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const project = {
health: {
control: '/typescript',
},
}
16 changes: 12 additions & 4 deletions packages/dynamic-config/src/tests/integration/config/production.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
export const project = {
health: {
control: '/typescript',
},
import { IProjectConfig } from './default'
import { project } from './foo'

const x: IProjectConfig = {
health: {
control: '',
response: '',
},
}

console.log('x: ', x)

export { project }

export const database = {
username: {
_source: 'env',
Expand Down
40 changes: 21 additions & 19 deletions packages/dynamic-config/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@
"max-classes-per-file": false,
"object-literal-sort-keys": false,
"no-console": false,
"no-var-requires": false,
"variable-name": [true,
"check-format",
"allow-leading-underscore"
],
"array-type": [
true,
"generic"
],
"semicolon": [
true,
"never"
],
"quotemark": [
true,
"single",
"avoid-escape"
],
"object-literal-key-quotes": false
"no-var-requires": false,
"only-arrow-functions": false,
"variable-name": [
true,
"check-format",
"allow-leading-underscore"
],
"array-type": [
true,
"generic"
],
"semicolon": [
true,
"never"
],
"quotemark": [
true,
"single",
"avoid-escape"
],
"object-literal-key-quotes": false
}
}
Loading