UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

1 lines 4.94 kB
{"version":3,"sources":["../../../packages/tools/gulp-resjson/index.ts"],"names":[],"mappings":"","file":"index.d.ts","sourcesContent":["'use strict';\r\n\r\nimport { Buffer } from 'buffer';\r\nimport Path from 'path';\r\nimport pluginError from 'plugin-error';\r\nimport through2 from 'through2';\r\nimport Vinyl from 'vinyl';\r\nimport * as util from '../utilities';\r\nimport * as Resjson from './resjson-convert';\r\n\r\nconst PLUGIN_NAME = 'gulp-resjson';\r\n\r\ninterface Options {\r\n /**\r\n * enable to produce xxxx.d.ts file.\r\n * { null, 'module' }\r\n */\r\n definition: string;\r\n\r\n /**\r\n * enable to produce xxxx.ts file.\r\n * { null, 'module', 'interface' }\r\n */\r\n typescript: string;\r\n\r\n /**\r\n * enable to produce xxxx.json file.\r\n */\r\n json: boolean;\r\n\r\n /**\r\n * if set a space characters, it adds formating of JSON.\r\n * it set null, space will be eliminated.\r\n */\r\n jsonSpace: string | number;\r\n\r\n /**\r\n * specify the conversion is Localized file with folder offset.\r\n */\r\n localeOffset: number;\r\n\r\n /**\r\n * src root.\r\n */\r\n srcRoot?: string;\r\n}\r\n\r\nfunction gulpResjson(options: Options) {\r\n // override options settings if not specified.\r\n options = <Options>Object.assign({ definition: null, typescript: null, json: false, jsonSpace: null }, options || {});\r\n\r\n return through2.obj(\r\n /**\r\n * Transform\r\n */\r\n function (file, encoding, callback) {\r\n let error = null;\r\n try {\r\n if (file.isNull()) {\r\n // nothing to do\r\n return callback(null, file);\r\n }\r\n\r\n if (file.isStream()) {\r\n // file.contents is a Stream - https://nodejs.org/api/stream.html\r\n this.emit('error', new pluginError(PLUGIN_NAME, 'Streams not supported!'));\r\n return callback(null, file);\r\n } else if (file.isBuffer()) {\r\n const data = file.contents.toString('utf8');\r\n const converter = new Resjson.ResJsonConverter(options);\r\n converter.convert(data, file.base.indexOf('src\\\\resources') >= 0);\r\n\r\n const path = Path.parse(file.path);\r\n if (options.definition) {\r\n const dtFile = new Vinyl({\r\n cwd: '/',\r\n base: path.dir,\r\n path: path.dir + '/' + path.name + '.d.ts',\r\n contents: Buffer.from(converter.contentDefinition, 'utf8')\r\n });\r\n this.push(dtFile);\r\n }\r\n\r\n if (options.typescript) {\r\n const content = options.typescript === 'interface' ? converter.contentInterface : converter.contentTypescript;\r\n const tsFile = new Vinyl({\r\n cwd: '/',\r\n base: path.dir,\r\n path: path.dir + '/' + path.name + '.ts',\r\n contents: Buffer.from(content, 'utf8')\r\n });\r\n this.push(tsFile);\r\n }\r\n\r\n if (options.json) {\r\n let baseDir = options.srcRoot || path.dir;\r\n let fullDir = path.dir;\r\n if (typeof options.localeOffset === 'number') {\r\n const segments = path.dir.split('\\\\');\r\n segments.length = segments.length - options.localeOffset;\r\n fullDir = segments.join('\\\\');\r\n segments.length = segments.length - 1;\r\n baseDir = segments.join('\\\\');\r\n }\r\n\r\n const content = JSON.stringify(converter.outputJson, null, options.jsonSpace);\r\n const jsonFile = new Vinyl({\r\n cwd: '/',\r\n base: baseDir,\r\n path: fullDir + '\\\\' + path.name + '.json',\r\n contents: Buffer.from(content, 'utf8')\r\n });\r\n this.push(jsonFile);\r\n }\r\n }\r\n } catch (e) {\r\n error = (!e.plugin || (e.plugin !== PLUGIN_NAME)) ?\r\n util.extendError(new pluginError({ plugin: PLUGIN_NAME, message: e.message }), e) : e;\r\n }\r\n\r\n callback(error);\r\n });\r\n}\r\n\r\nmodule.exports = gulpResjson;\r\n"]}