brek
Version:
46 lines • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.props = exports.generateTypeDef = void 0;
const isLoader_1 = require("./isLoader");
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const indent = (depth) => Array(depth * 4).fill(' ').join('');
/**
* Generate a TypeScript type definition file for the given configuration object.
* If loaders are present, they will be inferred as string.
*/
function generateTypeDef(config) {
return `
import {Config} from "brek";
declare module "brek" {
export interface Config {
${props(config)}
}
}`;
}
exports.generateTypeDef = generateTypeDef;
function props(obj, depth = 2) {
return Object.keys(obj).map((key) => {
const value = obj[key];
switch (typeof value) {
case 'object':
if (Array.isArray(value)) {
const type = typeof value[0];
return `${indent(depth)}'${key}': ${type === 'undefined' ? 'any' : type}[]`;
}
else if ((0, isLoader_1.isLoader)(value)) {
return `${indent(depth)}'${key}': string`;
}
else {
return `${indent(depth)}'${key}': {\n${props(value, depth + 1)}\n${indent(depth)}}`;
}
case 'boolean':
return `${indent(depth)}'${key}': boolean`;
case 'number':
return `${indent(depth)}'${key}': number`;
default:
return `${indent(depth)}'${key}': string`;
}
}).join('\n');
}
exports.props = props;
//# sourceMappingURL=generateTypeDef.js.map