1
0
Fork 0
js-yaml-schema/src/type/Include.ts

72 lines
1.8 KiB
TypeScript
Raw Normal View History

import { InvalidArgumentError, NotFoundError } from '@apextoaster/js-utils';
import { SAFE_SCHEMA, safeLoad, Schema, Type as YamlType } from 'js-yaml';
2019-11-13 14:01:51 +00:00
import { join } from 'path';
export interface IncludeOptions {
encoding: string;
}
export type IncludeReader = (path: string, options: IncludeOptions) => string;
export interface IncludeSchema {
exists: (path: string) => boolean;
read: IncludeReader;
resolve: (path: string) => string;
schema: Schema;
}
/**
* The schema to be used for included files. This is necessary to work around circular dependency errors.
*
* @public
*/
export const includeSchema: IncludeSchema = {
exists: (path: string) => false,
read: (path: string, encoding: IncludeOptions) => {
throw new Error('read stub');
},
resolve: (path: string) => {
throw new Error('resolve stub');
},
2019-11-13 14:01:51 +00:00
schema: SAFE_SCHEMA,
};
/**
* @internal
*/
2019-11-13 14:01:51 +00:00
export const includeType = new YamlType('!include', {
kind: 'scalar',
resolve(path: string) {
try {
const canonical = resolvePath(path);
// throws in node 11+
if (includeSchema.exists(canonical)) {
2019-11-13 14:01:51 +00:00
return true;
} else {
throw new NotFoundError('included file does not exist');
}
} catch (err) {
throw new NotFoundError('included file does not exist', err);
}
},
construct(path: string): unknown {
try {
return safeLoad(includeSchema.read(resolvePath(path), {
2019-11-13 14:01:51 +00:00
encoding: 'utf-8',
}), {
schema: includeSchema.schema,
});
} catch (err) {
throw new InvalidArgumentError('error including file', err);
2019-11-13 14:01:51 +00:00
}
},
});
export function resolvePath(path: string): string {
if (path[0] === '.') {
return includeSchema.resolve(join(__dirname, path));
2019-11-13 14:01:51 +00:00
} else {
return includeSchema.resolve(path);
2019-11-13 14:01:51 +00:00
}
}