UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

66 lines (64 loc) 2.95 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const path_1 = tslib_1.__importDefault(require("path")); const ajv_1 = tslib_1.__importDefault(require("ajv")); const plugin_error_1 = tslib_1.__importDefault(require("plugin-error")); const through2_1 = tslib_1.__importDefault(require("through2")); const util = tslib_1.__importStar(require("../utilities")); const PLUGIN_NAME = 'gulp-manifest-validator'; function gulpManifestValidator() { return through2_1.default.obj(function (file, encoding, callback) { let manifest; // first make sure the file is json. try { manifest = JSON.parse(file.contents.toString()); } catch (e) { const error = (!e.plugin || (e.plugin !== PLUGIN_NAME)) ? util.extendError(new plugin_error_1.default({ plugin: PLUGIN_NAME, message: e.message }), e) : e; callback(error); return; } // next try to resolve the schema let schemaPath = manifest.$schema; if (!schemaPath || typeof schemaPath !== 'string') { callback(new plugin_error_1.default({ plugin: PLUGIN_NAME, message: `Invalid $schema property in manifest: ${file.path}` })); return; } // resolve the absolute path to the schema from the file path and the schemaPath schemaPath = path_1.default.resolve(file.base, schemaPath); if (schemaPath.endsWith('.json#')) { schemaPath = schemaPath.substring(0, schemaPath.length - 1); } // fetch the schema const schema = require(schemaPath); // use the schema to validate the manifest const validator = new ajv_1.default({ allErrors: true, verbose: true }); const validate = validator.compile(schema); const valid = validate(manifest); if (!valid) { let message = 'Invalid manifest'; if (validate.errors && validate.errors.length > 0) { message = `${validate.errors.length} problems found.\n\n`; validate.errors.forEach(e => { message += `location: ${e.dataPath}.${e.propertyName || ''}\n`; // message += `data: ${JSON.stringify(e.data)}\n`; message += `error: ${e.message}\n`; if (e.params) { const keys = Object.keys(e.params); keys.forEach(key => { message += `${key}: ${JSON.stringify(e.params[key])}`; }); } message += '\n\n'; }); } callback(new plugin_error_1.default({ plugin: PLUGIN_NAME, message: `Invalid manifest:\n${message}` })); return; } callback(); }); } module.exports = gulpManifestValidator; //# sourceMappingURL=index.js.map