UNPKG

directory-schema-validator

Version:

Validate file system structure with an extension of JSON schema.

45 lines (44 loc) 2.02 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 }); const yargs_1 = __importDefault(require("yargs/yargs")); const helpers_1 = require("yargs/helpers"); const _1 = require("."); const fast_glob_1 = __importDefault(require("fast-glob")); const parse_1 = require("./parse"); const fs_1 = __importDefault(require("fs")); (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv)) .command("validate <schema> <directory>", "validate a directory using a JSON schema", {}, (argv) => { const schema = JSON.parse(fs_1.default.readFileSync(argv.schema, "utf8")); const validator = new _1.Validator(); const valid = validator.validate(schema, argv.directory); if (!valid) { console.error(validator.errors); process.exit(1); } }) .command("shorthand <globs...>", "generate JSON schema from globs", (yargs) => yargs.option("base-path", { type: "string" }), async (argv) => { const files = await (0, fast_glob_1.default)(argv.globs, { dot: false }); console.log(JSON.stringify((0, _1.shorthandToJSONSchema)(files, argv["base-path"]), null, 2)); }) .command("parse <folder>", "parse JSON for folder", {}, async (argv) => { console.log(JSON.stringify((0, parse_1.parse)(argv.folder), null, 2)); }) .help().argv;