1
0
Fork 0

feat(include): add flag option to includes

This commit is contained in:
ssube 2020-08-09 09:44:11 -05:00
parent 6fd7635e44
commit 0e6880bc53
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
3 changed files with 11 additions and 10 deletions

View File

@ -9,14 +9,14 @@ Instantiate an includer with closure over the provided options.
<b>Signature:</b>
```typescript
export declare function createInclude(includeOptions: IncludeOptions): YamlType;
export declare function createInclude(options: IncludeOptions): YamlType;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| includeOptions | [IncludeOptions](./js-yaml-schema.includeoptions.md) | |
| options | [IncludeOptions](./js-yaml-schema.includeoptions.md) | |
<b>Returns:</b>

View File

@ -8,7 +8,7 @@
| Function | Description |
| --- | --- |
| [createInclude(includeOptions)](./js-yaml-schema.createinclude.md) | Instantiate an includer with closure over the provided options. |
| [createInclude(options)](./js-yaml-schema.createinclude.md) | Instantiate an includer with closure over the provided options. |
| [createSchema(options)](./js-yaml-schema.createschema.md) | Safe schema with additional library types added. |
## Interfaces

View File

@ -2,7 +2,8 @@ import { InvalidArgumentError, NotFoundError } from '@apextoaster/js-utils';
import { safeLoad, Schema, Type as YamlType } from 'js-yaml';
export interface ReaderOptions {
encoding: string;
encoding: BufferEncoding;
flag?: string;
}
export type IncludeReader = (path: string, options: ReaderOptions) => string;
@ -19,14 +20,14 @@ export interface IncludeOptions {
* Instantiate an includer with closure over the provided options.
* @public
*/
export function createInclude(includeOptions: IncludeOptions) {
export function createInclude(options: IncludeOptions) {
return new YamlType('!include', {
kind: 'scalar',
resolve(path: string) {
try {
const canonical = includeOptions.resolve(path);
const canonical = options.resolve(path);
// throws in node 11+
if (includeOptions.exists(canonical)) {
if (options.exists(canonical)) {
return true;
} else {
throw new NotFoundError('included file does not exist');
@ -37,11 +38,11 @@ export function createInclude(includeOptions: IncludeOptions) {
},
construct(path: string): unknown {
try {
const abs = includeOptions.resolve(path);
return safeLoad(includeOptions.read(abs, {
const abs = options.resolve(path);
return safeLoad(options.read(abs, {
encoding: 'utf-8',
}), {
schema: includeOptions.schema,
schema: options.schema,
});
} catch (err) {
throw new InvalidArgumentError('error including file', err);