-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0132488
commit 9e88149
Showing
10 changed files
with
245 additions
and
6 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
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,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" | ||
] | ||
} | ||
} | ||
} |
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,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" | ||
``` |
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,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/" | ||
] | ||
} |
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,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 } | ||
} |
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 @@ | ||
{"foo":"bar"} |
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,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() | ||
}) | ||
}) | ||
}) | ||
}) |
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