UNPKG

boats

Version:

Beautiful Open / Async Template System - Write less yaml with BOATS and Nunjucks.

90 lines (89 loc) 4.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const fs_1 = require("fs"); const upath_1 = tslib_1.__importDefault(require("upath")); const js_yaml_1 = tslib_1.__importDefault(require("js-yaml")); const picomatch_1 = tslib_1.__importDefault(require("picomatch")); const removeFileExtension_1 = tslib_1.__importDefault(require("./removeFileExtension")); const buildIndexFromPath_1 = tslib_1.__importDefault(require("./utils/buildIndexFromPath")); const getMethodFromFileName_1 = tslib_1.__importDefault(require("./utils/getMethodFromFileName")); class AutoIndexer { getFiles(dir) { const dirents = (0, fs_1.readdirSync)(dir, { withFileTypes: true }); const files = dirents.map((dirent) => { const res = upath_1.default.resolve(dir, dirent.name); return dirent.isDirectory() ? this.getFiles(res) : res; }); return Array.prototype.concat(...files); } cleanFilePaths(dir, filePaths, indexFile) { return filePaths.map((filePath) => { return filePath !== indexFile && filePath.replace(dir, ''); }); } createChannelString(boatsrc, cleanPath, autoChannelIndexerOptions) { let pathWithoutExtension = (0, removeFileExtension_1.default)(cleanPath); if (autoChannelIndexerOptions) { for (let i = 0; i < autoChannelIndexerOptions.channelSeparators.length; i++) { const isMatch = (0, picomatch_1.default)(autoChannelIndexerOptions.channelSeparators[i].match, boatsrc.picomatchOptions); if (isMatch(pathWithoutExtension)) { if (pathWithoutExtension[0] === '/') { pathWithoutExtension = pathWithoutExtension.slice(1); } return pathWithoutExtension .split('/') .join(autoChannelIndexerOptions.channelSeparators[i].separator); } } } // the fall back is basic / separator for channels return pathWithoutExtension; } buildPathsYamlString(cleanPaths, boatsrc, channels, components, paths, autoComponentIndexerOptions, autoChannelIndexerOptions) { const indexObject = {}; cleanPaths.forEach((cleanPath) => { if (cleanPath) { const dir = upath_1.default.dirname(cleanPath); const filename = upath_1.default.basename(cleanPath); const method = (0, getMethodFromFileName_1.default)(filename); if (paths) { indexObject[dir] = indexObject[dir] || {}; indexObject[dir][method] = { $ref: `.${cleanPath}` }; } if (channels) { indexObject[this.createChannelString(boatsrc, cleanPath, autoChannelIndexerOptions)] = { $ref: `.${cleanPath}` }; } if (components) { indexObject[(0, buildIndexFromPath_1.default)({ cleanPath, autoComponentIndexerOptions, enableFancyPluralization: boatsrc.fancyPluralization })] = { $ref: `.${cleanPath}` }; } } }); return js_yaml_1.default.dump(indexObject, { indent: 2 }); } /** * Returns a string from an auto-built yaml file */ getIndexYaml(indexFile, boatsrc, options) { const absoluteIndexFilePath = upath_1.default.join(process.cwd(), indexFile); const dir = upath_1.default.join(process.cwd(), upath_1.default.dirname(indexFile)); const files = this.getFiles(dir).filter((file) => { return ['.yml', '.yaml', '.njk'].indexOf(upath_1.default.extname(file)) !== -1; }); const cleanPaths = this.cleanFilePaths(dir, files, absoluteIndexFilePath); return this.buildPathsYamlString(cleanPaths, boatsrc, options.channels, options.components, options.paths, options.autoComponentIndexerOptions, options.autoChannelIndexerOptions); } } exports.default = new AutoIndexer();