UNPKG

directory-schema-validator

Version:

Validate file system structure with an extension of JSON schema.

59 lines (58 loc) 2.25 kB
"use strict"; /** * Copyright 2022 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parse = exports.treeToTreeMap = void 0; const lodash_1 = __importDefault(require("lodash")); const directory_tree_1 = __importDefault(require("directory-tree")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const treeToTreeMap = (tree) => { const treeMap = lodash_1.default.omit(tree, ["children"]); if (tree.children) { treeMap.directories = {}; treeMap.files = {}; for (const child of tree.children) { switch (child.type) { case "directory": treeMap.directories[child.name] = (0, exports.treeToTreeMap)(child); break; case "file": treeMap.files[child.name] = (0, exports.treeToTreeMap)(child); break; } } } return treeMap; }; exports.treeToTreeMap = treeToTreeMap; const parse = (directory, exclude = /(node_modules\/|\.git\/|\.DS_Store|\.idea\/|\.vscode\/)/) => { const attributes = ["size", "type", "extension"]; directory = path_1.default.resolve(directory); if (!fs_1.default.existsSync(directory)) { throw new Error(`Directory ${directory} does not exist`); } const tree = (0, directory_tree_1.default)(directory, { attributes, exclude, normalizePath: true, }); return (0, exports.treeToTreeMap)(tree); }; exports.parse = parse;