{"version":3,"sources":["../../../packages/tools/gulp-manifest-validator/index.ts"],"names":[],"mappings":"","file":"index.d.ts","sourcesContent":["'use strict';\r\n\r\nimport Path from 'path';\r\nimport ajv from 'ajv';\r\nimport pluginError from 'plugin-error';\r\nimport through2 from 'through2';\r\nimport * as util from '../utilities';\r\n\r\nconst PLUGIN_NAME = 'gulp-manifest-validator';\r\n\r\nfunction gulpManifestValidator() {\r\n    return through2.obj(\r\n        function (file, encoding, callback) {\r\n            let manifest;\r\n            // first make sure the file is json.\r\n            try {\r\n                manifest = JSON.parse(file.contents.toString());\r\n            } catch (e) {\r\n                const error = (!e.plugin || (e.plugin !== PLUGIN_NAME)) ?\r\n                    util.extendError(new pluginError({ plugin: PLUGIN_NAME, message: e.message }), e) : e;\r\n                callback(error);\r\n                return;\r\n            }\r\n\r\n            // next try to resolve the schema\r\n            let schemaPath = manifest.$schema;\r\n            if (!schemaPath || typeof schemaPath !== 'string') {\r\n                callback(new pluginError({ plugin: PLUGIN_NAME, message: `Invalid $schema property in manifest: ${file.path}` }));\r\n                return;\r\n            }\r\n            // resolve the absolute path to the schema from the file path and the schemaPath\r\n            schemaPath = Path.resolve(file.base, schemaPath);\r\n            if (schemaPath.endsWith('.json#')) {\r\n                schemaPath = schemaPath.substring(0, schemaPath.length - 1);\r\n            }\r\n\r\n            // fetch the schema\r\n            const schema = require(schemaPath);\r\n\r\n            // use the schema to validate the manifest\r\n            const validator = new ajv({ allErrors: true, verbose: true });\r\n            const validate = validator.compile(schema);\r\n            const valid = validate(manifest);\r\n            if (!valid) {\r\n                let message = 'Invalid manifest';\r\n                if (validate.errors && validate.errors.length > 0) {\r\n                    message = `${validate.errors.length} problems found.\\n\\n`;\r\n                    validate.errors.forEach(e => {\r\n                        message += `location: ${e.dataPath}.${e.propertyName || ''}\\n`;\r\n                        // message += `data: ${JSON.stringify(e.data)}\\n`;\r\n                        message += `error: ${e.message}\\n`;\r\n                        if (e.params) {\r\n                            const keys = Object.keys(e.params);\r\n                            keys.forEach(key => {\r\n                                message += `${key}: ${JSON.stringify(e.params[key])}`;\r\n                            });\r\n                        }\r\n\r\n                        message += '\\n\\n';\r\n\r\n                    });\r\n                }\r\n                callback(new pluginError({ plugin: PLUGIN_NAME, message: `Invalid manifest:\\n${message}` }));\r\n                return;\r\n            }\r\n\r\n            callback();\r\n        });\r\n}\r\n\r\nmodule.exports = gulpManifestValidator;\r\n"]}