Skip to content

Commit

Permalink
Merge pull request #2 from mrmlnc/2.0.0
Browse files Browse the repository at this point in the history
2.0.0
  • Loading branch information
mrmlnc committed May 9, 2017
2 parents 9ca969c + 6483fe7 commit 4cf8c84
Show file tree
Hide file tree
Showing 16 changed files with 419 additions and 255 deletions.
172 changes: 2 additions & 170 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,179 +55,11 @@ configProfiler.getConfig('./path/to/current/file').then((result) => {

## API

```js
const ConfigProfiler = require('config-profiler');
const configProfiler = new ConfigProfiler('./path', {});
```

#### setWorkspace(workspace)

Set a new workspace directory path.

```js
configProfiler.setWorkspace('./path/to/workspace');
```

#### setOptions(options)

Set a new options.

```js
configProfiler.setOptions({
configFiles: ['config.json', 'config.js']
});
```

#### getConfig(filepath, [options]) => Promise

Get config for the current file path.

```js
configProfiler.getConfig('./path/to/workspace/index.txt').then((result) => {
// console.log(result);
// { from: './path/to/workspace/config.json', config: { ok: true } }
});
```
See [`/docs/api.md`](docs/api.md).

## Options

#### `settings`

* Type: `Object|String`
* Default: `null`

The settings from the editor or plugin/module options. Can have the object, the name of a predefined config or the path to the config.

```json
{
"predefinedConfigs": { "name": { "ok": true } },
"settings": { "ok": true },
"settings": "name",
"settings": "~/my-module-config.json",
"settings": "../configs/my-module-config.json"
}
```

> **Tip**
>
> Use `~` to refer to HOME directory and `./` or `../` to refer to the current workspace (relative). Also you can use absolute path.
#### `predefinedConfigs`

* Type: `Object`
* Default: `{}`

Predefined configs. Must have the name of the config, which can be called from `options.settings` (as string) or `options.extendsProp` from the `options.settings` (as object) or found config file.

For example, usage with `options.settings`:

```json
{
"predefinedConfigs": { "name": { "ok": true } },
"settings": "name"
}
```

For example, usage with `options.extendsProp` (`extends` by default) from found config file:

```json
{
"extends": "name"
}
```

Predefined config will be merged with config from found config file.

#### `configFiles`

* Type: `String[]`
* Default: `[]`

The names of the config files that can be used as a configs.

#### `parsers`

* Type: `Parser[]`
* Default: `see below`

Parsers that will be apply to the found configs. The parser receive the content from the configuration file and returns an object.

```js
{
options: {
parsers: [
// Before applying the parser we check the pattern for the current file
{ pattern: /.*\.yml$/, parser: (text) => mySuperYamlParser(text) }
]
}
}
```

The `Parser` interface has the following type:

```ts
interface Parser {
pattern: RegExp;
parser: (text: string) => object;
}
```

#### `useEachParser`

* Type: `Boolean`
* Default: `false`

Allow to use each parser to config file. Necessary in the case that a single file can have multiple syntaxes. We just ignore the `pattern` property.

#### `transform`

* Type: `Function`
* Default: `(result) => result`

A function that returns the result. The `result` has the following object:

```js
{
from: './path/to/config/file.json', // Also can be 'settings' or 'predefined'
config: { ok: true }
}
```

#### `packageProp`

* Type: `String`
* Default: `null`

The name of property in the `package.json` file. If you set this option, we'll look for `package.json` file.

#### `extendsProp`

* Type: `String`
* Default: `extends`

Allow to use `extends` property in a configuration file or settings to reference to another configuration file or predefined config.

Use `~` to refer to HOME directory and `./` or `../` to refer to the current workspace (relative). Also you can use absolute path.

> **Tip**
>
> We'll merge config in order from the most deep to the top.
See [`fixtures/extends`](fixtures/extends) directory as an example.

#### `envVariableName`

* Type: `String`
* Default: `null`

Allow to get the path to the configuration file from environment variable.

#### `allowHomeDirectory`

* Type: `Boolean`
* Default: `true`

Allow configs in the HOME directory or not.
See [`/docs/options.md`](docs/options.md).

## Changelog

Expand Down
47 changes: 47 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# API

#### Constructor(workspace: string, options: IOptions)

Create instance of `ConfigProfiler` class.

```js
const ConfigProfiler = require('config-profiler');
const configProfiler = new ConfigProfiler('./path', {});
```

> For more details about the options, see [`ICoreOptions`](options.md#icoreoptions) and [`IChangeableOptions`](options.md#ichangeableoptions).
#### setWorkspace(workspace)

Set a new workspace directory path.

```js
configProfiler.setWorkspace('./path/to/workspace');
```

#### setOptions(options: IOptions)

Set a new options.

```js
configProfiler.setOptions({
configFiles: ['config.json', 'config.js']
});
```

> For more details about the options, see [`ICoreOptions`](options.md#icoreoptions) and [`IChangeableOptions`](options.md#ichangeableoptions).
#### getConfig(filepath, [options: IChangeableOptions]) => Promise

Get config for the current file path.

```js
configProfiler.getConfig('./path/to/workspace/index.txt').then((result) => {
// console.log(result);
// { from: './path/to/workspace/config.json', config: { ok: true } }
});
```

> **Warning**
>
> Here available not all options. See only [`IChangeableOptions`](options.md#ichangeableoptions) options.
159 changes: 159 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# Options

* [`ICoreOptions`](options.md#icoreoptions)
* [`IChangeableOptions`](options.md#ichangeableoptions)

## ICoreOptions

#### `predefinedConfigs`

* Type: `Object`
* Default: `{}`

Predefined configs. Must have the name of the config, which can be called from `options.settings` (as string) or `options.props.extends` from the `options.settings` (as object) or found config file.

For example, usage with `options.settings`:

```json
{
"predefinedConfigs": { "name": { "ok": true } },
"settings": "name"
}
```

For example, usage with `options.props.extends` (`extends` by default) from found config file:

```json
{
"extends": "name"
}
```

Predefined config will be merged with config from found config file.

#### `configFiles`

* Type: `String[]`
* Default: `[]`

The names of the config files that can be used as a configs.

#### `parsers`

* Type: `Parser[]`
* Default: `see below`

Parsers that will be apply to the found configs. The parser receive the content from the configuration file and returns an object.

```js
{
options: {
parsers: [
// Before applying the parser we check the pattern for the current file
{ pattern: /.*\.yml$/, parser: (text) => mySuperYamlParser(text) }
]
}
}
```

The `Parser` interface has the following type:

```ts
interface Parser {
pattern: RegExp;
parser: (text: string) => object;
}
```

#### `envVariableName`

* Type: `String`
* Default: `null`

Allow to get the path to the configuration file from environment variable.

#### `props`

* Type: `Object`
* Default: `{ package: null, extends: 'extends' }`
* Available: `IOptions`

##### `proprs.package`

* Type: `String`
* Default: `extends`

The name of property in the `package.json` file. If you set this option, we'll look for `package.json` file.

##### `props.extends`

* Type: `String`
* Default: `extends`

Allow to use `extends` property in a configuration file or settings to reference to another configuration file or predefined config.

Use `~` to refer to HOME directory and `./` or `../` to refer to the current workspace (relative). Also you can use absolute path.

> **Tip**
>
> We'll merge config in order from the most deep to the top.
See [`fixtures/extends`](../fixtures/extends) directory as an example.

## IChangeableOptions

#### `settings`

* Type: `Object|String`
* Default: `null`

The settings from the editor or plugin/module options. Can have the object, the name of a predefined config or the path to the config.

```json
{
"predefinedConfigs": { "name": { "ok": true } },
"settings": { "ok": true },
"settings": "name",
"settings": "~/my-module-config.json",
"settings": "../configs/my-module-config.json"
}
```

> **Tip**
>
> Use `~` to refer to HOME directory and `./` or `../` to refer to the current workspace (relative). Also you can use absolute path.
#### `useEachParser`

* Type: `Boolean`
* Default: `false`

Allow to use each parser to config file. Necessary in the case that a single file can have multiple syntaxes. We just ignore the `pattern` property.

#### `transform`

* Type: `Function`
* Default: `(result) => result`

A function that returns the result. The `result` has the following object:

```js
{
from: './path/to/config/file.json', // Also can be 'settings' or 'predefined'
config: { ok: true }
}
```

#### `allowHomeDirectory`

* Type: `Boolean`
* Default: `true`

Allow configs in the HOME directory or not.

#### `extendBuildedConfig`

* Type: `Boolean`
* Default: `null`

Merge builded config with passed object.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "config-profiler",
"version": "1.0.1",
"version": "2.0.0",
"description": "Find configuration for the current file from provided path, workspace, package, settings, HOME directory or env variable",
"license": "MIT",
"repository": "mrmlnc/config-profiler",
Expand Down
Loading

0 comments on commit 4cf8c84

Please sign in to comment.