UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

240 lines (238 loc) 10.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PsCodeConverter = void 0; class PsCodeConverter { options; static defaultGroup = '!!##default__'; static generatedGroup = '!!##generated__'; static verbs = [ 'Add', 'Clear', 'Close', 'Copy', 'Enter', 'Exit', 'Find', 'Format', 'Get', 'Hide', 'Join', 'Lock', 'Move', 'New', 'Open', 'Optimize', 'Pop', 'Push', 'Redo', 'Remove', 'Rename', 'Reset', 'Resize', 'Search', 'Select', 'Set', 'Show', 'Skip', 'Split', 'Step', 'Switch', 'Undo', 'Unlock', 'Watch', 'Backup', 'Checkpoint', 'Compare', 'Compress', 'Convert', 'ConvertFrom', 'ConvertTo', 'Dismount', 'Edit', 'Expand', 'Export', 'Group', 'Import', 'Initialize', 'Limit', 'Merge', 'Mount', 'Out', 'Publish', 'Restore', 'Save', 'Sync', 'Unpublish', 'Update', 'Approve', 'Assert', 'Complete', 'Confirm', 'Deny', 'Disable', 'Enable', 'Install', 'Invoke', 'Register', 'Request', 'Restart', 'Resume', 'Start', 'Stop', 'Submit', 'Suspend', 'Uninstall', 'Unregister', 'Wait', 'Debug', 'Measure', 'Ping', 'Repair', 'Resolve', 'Test', 'Trace', 'Connect', 'Disconnect', 'Read', 'Receive', 'Send', 'Write', 'Block', 'Grant', 'Protect', 'Revoke', 'Unblock', 'Unprotect', 'Use' ]; static fullNames = [ 'Microsoft.PowerShell.Core\\Get-Help', 'Microsoft.PowerShell.Core\\Out-Default', 'Microsoft.PowerShell.Utility\\Get-FormatData', 'Microsoft.PowerShell.Utility\\Measure-Object', 'Microsoft.PowerShell.Utility\\Select-Object' ]; static removeCommentsTrue = '##RemoveComments=true##'; static removeCommentsFalse = '##RemoveComments=false##'; static skipCheckTrue = '##SkipCheck=true##'; static skipCheckFalse = '##SkipCheck=false##'; static commentStart = '<#'; static commentEnd = '#>'; static comment = '#'; static openContent = `/* eslint:disable */\r /**\r * @file Source code generated by gulp-ps-code.\r * @version 1.1\r */\r export module PowerShellScripts {\r 'use strict'\r `; static closeContent = `}\r `; static hereTextBegin = '@"'; static hereTextEnd = '"@'; buffer; jeaErrorDisplayOnce = true; constructor(options) { this.options = options; } get content() { return this.buffer.join('\r\n'); } contentReset() { this.buffer = []; } generate(groups) { const tsBase = null; this.buffer.push(PsCodeConverter.openContent); this.buffer.push(this.indent(1) + 'export const module = ' + '\'' + this.options.powerShellModuleName + '\';'); if (this.options.resourceName) { this.buffer.push(this.indent(1) + 'export const resourceName = ' + '\'' + this.options.resourceName + '\';'); } if (this.options.prefixName) { this.buffer.push(this.indent(1) + 'export const powerShellPrefix = ' + '\'' + this.options.prefixName + '\';'); } this.addData(groups); this.buffer.push(PsCodeConverter.closeContent); } toJsonName(original) { const name = this.toCommandName(original); return this.replaceAll(name, '-', '_'); } toCommandName(original) { const index = original.indexOf('.ps1'); if (index <= 0) { console.error('Script file must use .ps1 extension.'); return original; } const scriptName = original.substring(0, index); return scriptName; } toPascalName(original) { const publicPath = 'public\\'; const internalPath = 'internal\\'; if (original.startsWith(publicPath)) { original = original.substring(publicPath.length); } else if (original.startsWith(internalPath)) { original = original.substring(internalPath.length); } const name = original[0].toUpperCase() + original.substring(1); let pascal = ''; let upper = true; for (let i = 0; i < name.length; i++) { const char = name.charAt(i); if (char === '-') { upper = true; } else if (char === '\\') { pascal += '_'; } else { pascal += upper ? char.toUpperCase() : char; upper = false; } } return pascal; } regexEscape(str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); } replaceAll(input, searchValue, replaceValue) { return input.replace(new RegExp(this.regexEscape(searchValue), 'g'), replaceValue); } addBeginGroup(name, indent) { this.buffer.push(this.indent(indent) + 'export module ' + name + ' {'); } addEndGroup(name, indent) { this.buffer.push(this.indent(indent) + '}'); } addToContent(name, command, script, indent) { if (this.options.jea) { this.buffer.push(this.indent(indent) + 'export const ' + name + ' = {'); this.buffer.push(this.indent(indent + 1) + 'command: \'' + command + '\','); this.buffer.push(this.indent(indent + 1) + 'script: ' + script + ''); this.buffer.push(this.indent(indent) + '}'); } else { this.buffer.push(this.indent(indent) + 'export const ' + name + ': string = ' + script + ';'); } } addData(groups) { let groupKeys = Object.keys(groups); groupKeys = groupKeys.sort(); for (const groupKey of groupKeys) { const groupName = this.toPascalName(groupKey); let rootGroup = true; if (groupKey !== PsCodeConverter.defaultGroup && groupKey !== PsCodeConverter.generatedGroup) { rootGroup = false; } if (!rootGroup) { this.addBeginGroup(groupName, 1); } const current = groups[groupKey]; const keys = Object.keys(current); for (const key of keys) { let script = ''; const content = current[key]; const name = this.toJsonName(key); const command = this.toCommandName(key); script = '##' + command + '##:' + key + '\n'; let removeComments = this.options.noComments; if (content.indexOf(PsCodeConverter.removeCommentsFalse) > 0) { removeComments = false; } else if (content.indexOf(PsCodeConverter.removeCommentsTrue) > 0) { removeComments = true; } let skipping = false; let skipCheck = false; let hereText = false; const lines = content.split('\n'); lines.forEach((value, index, array) => { let text = value.replace('\r', ''); if (text.endsWith(PsCodeConverter.hereTextBegin)) { hereText = true; script += text + '\n'; } else if (hereText) { if (text === PsCodeConverter.hereTextEnd) { hereText = false; } script += text + '\n'; } else if (removeComments) { let process = true; text = text.trim(); if (text.startsWith(PsCodeConverter.commentStart)) { skipping = true; } if (text.startsWith(PsCodeConverter.skipCheckTrue)) { skipCheck = true; } if (text.startsWith(PsCodeConverter.skipCheckFalse)) { skipCheck = false; } if (skipping) { process = false; if (text.endsWith(PsCodeConverter.commentEnd)) { skipping = false; } } if (process && !text.startsWith(PsCodeConverter.comment) && text.length > 0) { if (!skipCheck && this.options.jea) { const regex = new RegExp(/[^A-Z\\]+(select-object|select|get-formatdata|measure-object|measure|help|get-help|out-default)[^A-Z\\]+/i); const matched = regex.exec(' ' + text); if (matched) { if (this.jeaErrorDisplayOnce) { this.jeaErrorDisplayOnce = false; console.error('JEA Error: use one of full name cmdlet.', PsCodeConverter.fullNames.join(' ')); console.error(' If it scanned unwanted lines, use ##SkipCheck=true## and ##SkipCheck=false## to surround region.'); } console.error(key + ': ' + text); } } script += text + '\n'; } } else { script += text + '\n'; } }); let data = JSON.stringify(script); data = this.replaceAll(data, '\'', '\\u0027'); data = this.replaceAll(data, '<', '\\u003c'); data = this.replaceAll(data, '>', '\\u003e'); data = this.replaceAll(data, '&', '\\u0026'); this.addToContent(name, command, data, rootGroup ? 1 : 2); } if (!rootGroup) { this.addEndGroup(groupName, 1); } } } indent(count) { let pad = ''; for (let i = 0; i < count; i++) { pad += ' '; } return pad; } } exports.PsCodeConverter = PsCodeConverter; //# sourceMappingURL=ps-code-convert.js.map