UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

177 lines (175 loc) 6.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ResJsonConverter = void 0; class ResJsonConverter { options; static openContent = `/* eslint:disable */ /** * @file Source code generated by gulp-resjson. * @version 1.0 */ `; static closeContent = `} `; outputJson; outputDefinition; outputTypescript; outputInterface; jsonCurrent; root; constructor(options) { this.options = options; } get contentDefinition() { return this.outputDefinition.join(''); } get contentTypescript() { return this.outputTypescript.join(''); } get contentInterface() { return this.outputInterface.join(''); } contentReset() { this.outputDefinition = []; this.outputTypescript = []; this.outputInterface = []; this.outputJson = {}; this.jsonCurrent = this.outputJson; } convert(content, validate) { // Remove comments, /* multiline comment */ and // one line comment and "//": "JSON element comment" content = content.replace(/(\/\*([^*]|[\n]|(\*+([^*/]|[\n])))*\*\/+)|( +\/\/.*)|( +\"\/\/\".*)/g, ''); const data = JSON.parse(content); this.root = {}; this.buildObject(data, validate); this.contentReset(); this.traverse([{ name: 'Strings', value: this.root }], 0); } buildObject(data, validate, indexes) { const itemKeys = Object.keys(data); for (const itemKey of itemKeys) { if (this.skipComment(itemKey, validate, itemKeys)) { continue; } if (!data.hasOwnProperty(itemKey)) { continue; } const itemValue = data[itemKey]; if (typeof itemValue !== 'string') { // nested object, so scan inside to add to current. const newIndexes = indexes ? indexes.slice(0) : []; newIndexes.push(itemKey); this.buildObject(itemValue, validate, newIndexes); continue; } let current = this.root; const keys = indexes ? indexes.slice(0) : []; keys.push(...itemKey.split('_')); let count = keys.length; for (const key of keys) { count--; if (count > 0) { if (!current.hasOwnProperty(key)) { current[key] = {}; } current = current[key]; if (typeof current !== 'object') { throw new Error('Resource key already exists: ' + itemKey); } } else { current[key] = itemValue; } } } } skipComment(itemKey, validate, itemKeys) { // remove localization comments if (itemKey.startsWith('//')) { return true; } else if (itemKey.startsWith('_')) { if (validate) { if (!itemKey.endsWith('.comment')) { throw new Error('Resource cannot start with underscore: ' + itemKey); } const found = itemKeys.find(item => itemKey === `_${item}.comment`); if (!found) { throw new Error('Resource comment doesn\'t match: ' + itemKey); } } return true; } return false; } jsonNewValue(name) { const old = this.jsonCurrent; const json = {}; this.jsonCurrent[name] = json; this.jsonCurrent = json; return old; } jsonAddValue(name, value) { this.jsonCurrent[name] = value; } scan(node) { const current = node; const keyItems = []; const dataItems = []; for (const itemKey in current) { if (current.hasOwnProperty(itemKey)) { const itemValue = current[itemKey]; if (typeof itemValue === 'object') { keyItems.push({ name: itemKey, value: itemValue }); } else if (typeof itemValue === 'string') { dataItems.push({ name: itemKey, value: itemValue }); } } } return { keyItems: keyItems, dataItems: dataItems }; } traverse(keyItems, indent) { const indentSpace = ' '; let indentName = ''; for (let i = 0; i < indent; i++) { indentName += indentSpace; } const indentValue = indentName + indentSpace; if (keyItems.length > 0) { for (const item of keyItems) { if (indent === 0) { this.outputDefinition.push(ResJsonConverter.openContent); this.outputTypescript.push(ResJsonConverter.openContent); this.outputInterface.push(ResJsonConverter.openContent); this.outputDefinition.push('export declare module ' + item.name + ' {\r\n'); this.outputTypescript.push('export module ' + item.name + ' {\r\n \'use strict\';\r\n'); this.outputInterface.push('export interface ' + item.name + ' {\r\n'); } else { this.outputDefinition.push(indentName + 'module ' + item.name + ' {\r\n'); this.outputTypescript.push(indentName + 'export module ' + item.name + ' {\r\n'); this.outputInterface.push(indentName + item.name + ': {\r\n'); } const jsonOld = this.jsonNewValue(item.name); const results = this.scan(item.value); for (const item2 of results.dataItems) { this.outputDefinition.push(indentValue + 'const ' + item2.name + ': string;\r\n'); this.outputTypescript.push(indentValue + 'export const ' + item2.name + ' = \'' + item2.value + '\';\r\n'); this.outputInterface.push(indentValue + item2.name + ': string;\r\n'); this.jsonAddValue(item2.name, item2.value); } this.traverse(results.keyItems, ++indent); this.jsonCurrent = jsonOld; this.outputDefinition.push(indentName + '}\r\n'); this.outputTypescript.push(indentName + '}\r\n'); this.outputInterface.push(indentName + '};\r\n'); } } } } exports.ResJsonConverter = ResJsonConverter; //# sourceMappingURL=resjson-convert.js.map