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> <b>Signature:</b>
```typescript ```typescript
export declare function createInclude(includeOptions: IncludeOptions): YamlType; export declare function createInclude(options: IncludeOptions): YamlType;
``` ```
## Parameters ## Parameters
| Parameter | Type | Description | | Parameter | Type | Description |
| --- | --- | --- | | --- | --- | --- |
| includeOptions | [IncludeOptions](./js-yaml-schema.includeoptions.md) | | | options | [IncludeOptions](./js-yaml-schema.includeoptions.md) | |
<b>Returns:</b> <b>Returns:</b>

View File

@ -8,7 +8,7 @@
| Function | Description | | 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. | | [createSchema(options)](./js-yaml-schema.createschema.md) | Safe schema with additional library types added. |
## Interfaces ## Interfaces

View File

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