Skip to content

Commit

Permalink
feat: pull up file api plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Oct 14, 2018
1 parent 0132488 commit 9e88149
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/uniconfig-core/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default class Config {
evaluate () {
const data = this.data = {}
each(this.input.data, (value, key)=> {
if (/^\$.+:.+/.test(value)) {
if (/^\$.+:.*/.test(value)) {
const [sourceName, path] = value.slice(1).split(':')
const source = this.context.source.get(sourceName)

Expand Down
2 changes: 1 addition & 1 deletion packages/uniconfig-core/src/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export interface ISource {
on(event: string, listener: IEventListener): ISource,
emit(event: string, data?: IAny): boolean,

get(path: string): IAny,
get(path?: string): IAny,
has(path: string): boolean,
}

Expand Down
6 changes: 4 additions & 2 deletions packages/uniconfig-core/src/source/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ export default class Source implements ISource {
return this
}

get (path: string): IAny {
get (path?: string): IAny {
this.constructor.assertReady(this)

return get(this.data, path)
return path
? get(this.data, path)
: this.data
}

has (path: string): boolean {
Expand Down
35 changes: 35 additions & 0 deletions packages/uniconfig-plugin-api-file/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"comments": false,
"ignore": [
"interface.js"
],
"presets": [
"@babel/preset-flow"
],
"env": {
"production": {
"presets": [
"@babel/preset-flow",
["@babel/preset-env", {
"modules": false
}]
],
"plugins": [
"@babel/plugin-transform-modules-commonjs",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread"
]
},
"test": {
"presets": [
"@babel/preset-flow",
"@babel/preset-env"
],
"plugins": [
"@babel/plugin-transform-modules-commonjs",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread"
]
}
}
}
44 changes: 44 additions & 0 deletions packages/uniconfig-plugin-api-file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# @qiwi/uniconfig-plugin-api-file

Uniconfig File API plugin

## Install
```bash
npm i @qiwi/uniconfig-plugin-api-file
yarn add @qiwi/uniconfig-plugin-api-fil
```


## Usage
```javascript
import uniconfig, {rollupPlugin} from '@qiwi/uniconfig-core'
import uniconfigFileApiPlugin from '@qiwi/uniconfig-plugin-api-file'
import uniconfigJsonParserPlugin from '@qiwi/uniconfig-plugin-parser-json'

rollupPlugin(uniconfigFileApiPlugin)
rollupPlugin(uniconfigJsonParserPlugin)

const target = './foo.json'
/** foo.json content:
{
"foo": "bar"
}
*/

const config = uniconfig({
data: {
someParam: '$fromFile:foo'
},
source: {
fromFile: {
target,
api: 'file',
parser: 'json'
}
}
}, {
mode: 'sync'
})

config.get('someParam') // "bar"
```
38 changes: 38 additions & 0 deletions packages/uniconfig-plugin-api-file/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@qiwi/uniconfig-plugin-api-file",
"version": "1.16.0",
"description": "Uniconfig file api (fs api) plugin",
"main": "dist/es6/index.js",
"scripts": {
"jest": "BABEL_ENV=test NODE_ENV=test jest -w 1 --detectOpenHandles --config jest.config.json",
"build_es6": "flow-remove-types src/ --out-dir dist/es6/",
"build_es5": "BABEL_ENV=production babel src --out-dir dist/es5/",
"build_bundle": "parcel build dist/es6/index.js --out-dir dist/bundle --experimental-scope-hoisting",
"build": "rm -rf dist && npm run build_es6 && npm run build_es5 && npm run build_bundle"
},
"repository": {
"type": "git",
"url": "git+https://github.com/qiwi/uniconfig.git"
},
"keywords": [
"universal config",
"unified config"
],
"author": "Qiwi <opensource@qiwi.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/qiwi/uniconfig/issues"
},
"homepage": "https://github.com/qiwi/uniconfig#readme",
"dependencies": {
"@qiwi/uniconfig-core": "^1.0.0"
},
"devDependencies": {
"@babel/core": "^7.1.2"
},
"files": [
"README.md",
"CHANGELOG.md",
"dist/"
]
}
48 changes: 48 additions & 0 deletions packages/uniconfig-plugin-api-file/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// @flow

import type {IApi, IAny, IResolve, IReject, IContext, IPlugin} from '../../uniconfig-core/src/interface'
export type IFsOpts = {
encoding: string,
flag?: string
}

export interface IFileApi extends IApi {
readSync (target: string, opts?: ?IFsOpts): IAny,
read (target: string, opts?: ?IFsOpts): Promise<IAny>
}

export const DEFAULT_OPTS: IFsOpts = {
encoding: 'utf8'
}

export const type = 'file'

export const api: IFileApi = {
readSync (target: string, opts?: ?IFsOpts): IAny {
return require('fs').readFileSync(target, processOpts(opts))
},
read (target: string, opts?: ?IFsOpts): Promise<IAny> {
return new Promise((resolve: IResolve, reject: IReject): void => {
require('fs').readFile(target, processOpts(opts), (err: IAny | null, data: IAny) => {
if (err) {
reject(err)
} else {
resolve(data)
}
})
})
}
}

export default ({
rollup(context: IContext): void {
context.api.add(type, api)
},
rollback(context: IContext): void {
context.api.remove(type)
},
}: IPlugin)

export function processOpts (opts?: ?IFsOpts): IFsOpts {
return { ...DEFAULT_OPTS, ...opts }
}
1 change: 1 addition & 0 deletions packages/uniconfig-plugin-api-file/test/foobar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"foo":"bar"}
71 changes: 71 additions & 0 deletions packages/uniconfig-plugin-api-file/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import plugin, {api} from '../src'
import path from 'path'
import {ASYNC, SYNC} from '@qiwi/uniconfig-core/src/source/source'
import {Config, rollupPlugin, rollbackPlugin} from '@qiwi/uniconfig-core/src'

describe('uniconfig-plugin-api-file', () => {
const target = path.resolve(__dirname, './foobar.json')

describe('#readSync', () => {
it('gets file data as string', () => {
expect(api.readSync(target)).toEqual(JSON.stringify({ foo: 'bar' }))
})

it('gets err as result', () => {
expect(() => api.readSync('bazqux')).toThrow('ENOENT: no such file or directory, open \'bazqux\'')
})
})

describe('#read', () => {
it('resolves promise with string', () => {
return expect(api.read(target)).resolves.toEqual(JSON.stringify({ foo: 'bar' }))
})

it('rejects promise with err', () => {
return expect(api.read('bazqux')).rejects.toThrow('ENOENT: no such file or directory, open \'bazqux\'')
})
})

describe('integration', () => {
beforeAll(() => {
rollupPlugin(plugin)
})

afterAll(() => {
rollbackPlugin(plugin)
})

const input = {
data: {
someParam: '$fromFile:'
},
source: {
fromFile: {
target,
api: 'file'
}
}
}

it('sync', () => {
const mode = SYNC
const opts = {mode}
const config = new Config(input, opts)

expect(config.context.source.get('fromFile').get()).toBe('{"foo":"bar"}')
expect(config.get('someParam')).toBe('{"foo":"bar"}')
})

it('async', done => {
const mode = ASYNC
const opts = {mode}
const config = new Config(input, opts)

config.on('CONFIG_READY', () => {
expect(config.context.source.get('fromFile').get()).toBe('{"foo":"bar"}')
expect(config.get('someParam')).toBe('{"foo":"bar"}')
done()
})
})
})
})
4 changes: 2 additions & 2 deletions packages/uniconfig-plugin-json/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import type {IContext, IPlugin, IAny, IParser, IParse} from '../../uniconfig-core/src/interface'

const type = 'json'
export const type = 'json'

export const parse: IParse = (data: string): IAny => JSON.parse(data)

const parser: IParser = {parse}
export const parser: IParser = {parse}

export default ({
rollup(context: IContext): void {
Expand Down

0 comments on commit 9e88149

Please # to comment.