{"version":3,"sources":["../../../packages/tools/gulp-ps-module/index.ts"],"names":[],"mappings":"","file":"index.d.ts","sourcesContent":["'use strict';\r\n\r\nimport log from 'fancy-log';\r\nimport pluginError from 'plugin-error';\r\nimport through2 from 'through2';\r\nimport Vinyl from 'vinyl';\r\nimport * as util from '../utilities';\r\n\r\nconst PLUGIN_NAME = 'gulp-ps-module';\r\n\r\nfunction getDefinition(\r\n    name: string, version: string, guid: string, functionsList: string, localScripts: string[], externalScripts: string[]) {\r\n    const definition =\r\n        `#\r\n# Module manifest for module '{{Name}}'\r\n#\r\n\r\n@{\r\n\r\n# Script module or binary module file associated with this manifest.\r\nRootModule = '{{Name}}.psm1'\r\n\r\n# Version number of this module.\r\nModuleVersion = '{{Version}}'\r\n\r\n# Supported PSEditions\r\n# CompatiblePSEditions = @()\r\n\r\n# ID used to uniquely identify this module\r\nGUID = '{{Guid}}'\r\n\r\n# Author of this module\r\nAuthor = 'SME'\r\n\r\n# Company or vendor of this module\r\nCompanyName = 'Microsoft'\r\n\r\n# Copyright statement for this module\r\nCopyright = '(c) 2018 Microsoft. All rights reserved.'\r\n\r\n# Description of the functionality provided by this module\r\n# Description = ''\r\n\r\n# Minimum version of the Windows PowerShell engine required by this module\r\nPowerShellVersion = '5.0'\r\n\r\n# Name of the Windows PowerShell host required by this module\r\n# PowerShellHostName = ''\r\n\r\n# Minimum version of the Windows PowerShell host required by this module\r\n# PowerShellHostVersion = ''\r\n\r\n# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.\r\n# DotNetFrameworkVersion = ''\r\n\r\n# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.\r\n# CLRVersion = ''\r\n\r\n# Processor architecture (None, X86, Amd64) required by this module\r\n# ProcessorArchitecture = ''\r\n\r\n# Modules that must be imported into the global environment prior to importing this module\r\n# RequiredModules = @()\r\n\r\n# Assemblies that must be loaded prior to importing this module\r\n# RequiredAssemblies = @()\r\n\r\n# Script files (.ps1) that are run in the caller's environment prior to importing this module.\r\n# ScriptsToProcess = @()\r\n\r\n# Type files (.ps1xml) to be loaded when importing this module\r\n# TypesToProcess = @()\r\n\r\n# Format files (.ps1xml) to be loaded when importing this module\r\n# FormatsToProcess = @()\r\n\r\n# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess\r\nNestedModules = @()\r\n\r\n# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.\r\nFunctionsToExport = @(\r\n{{FunctionsList}}\r\n)\r\n\r\n# Function attributes: {{FunctionAttributes}}\r\n\r\n# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.\r\nCmdletsToExport = @()\r\n\r\n# Variables to export from this module\r\nVariablesToExport = '*'\r\n\r\n# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.\r\nAliasesToExport = @()\r\n\r\n# DSC resources to export from this module\r\n# DscResourcesToExport = @()\r\n\r\n# List of all modules packaged with this module\r\n# ModuleList = @()\r\n\r\n# List of all files packaged with this module\r\n# FileList = @()\r\n\r\n# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.\r\nPrivateData = @{\r\n\r\n    PSData = @{\r\n\r\n        # Tags applied to this module. These help with module discovery in online galleries.\r\n        # Tags = @()\r\n\r\n        # A URL to the license for this module.\r\n        # LicenseUri = ''\r\n\r\n        # A URL to the main website for this project.\r\n        # ProjectUri = ''\r\n\r\n        # A URL to an icon representing this module.\r\n        # IconUri = ''\r\n\r\n        # ReleaseNotes of this module\r\n        # ReleaseNotes = ''\r\n\r\n    } # End of PSData hashtable\r\n\r\n} # End of PrivateData hashtable\r\n\r\n# HelpInfo URI of this module\r\n# HelpInfoURI = ''\r\n\r\n# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.\r\n# DefaultCommandPrefix = ''\r\n\r\n}\r\n\r\n`;\r\n\r\n    const attributes = JSON.stringify({ localScripts, externalScripts });\r\n\r\n    // calling {{Name}} twice since it's used two times on the template.\r\n    return definition\r\n        .split('\\n')\r\n        .join('\\r\\n')\r\n        .replace('{{Name}}', name)\r\n        .replace('{{Name}}', name)\r\n        .replace('{{Version}}', version)\r\n        .replace('{{Guid}}', guid)\r\n        .replace('{{FunctionsList}}', functionsList)\r\n        .replace('{{FunctionAttributes}}', attributes);\r\n}\r\n\r\nfunction gulpPowerShellModule(options) {\r\n    //\r\n    // (Options):\r\n    //\r\n    // name.\r\n    //   name of module: string;\r\n    // version.\r\n    //   version of module: string; (default: 0.0.0)\r\n    // guid.\r\n    //   GUID string of module: string\r\n    //\r\n\r\n    // override options settings if not specified.\r\n    options = Object.assign({ version: '0.0.0' }, options || {});\r\n\r\n    const scriptCollection: any = {};\r\n    const localScripts: string[] = [];\r\n    const externalScripts: string[] = [];\r\n\r\n    return through2.obj(\r\n        /**\r\n         * Transform\r\n         */\r\n        function (file, encoding, callback) {\r\n            try {\r\n                if (!options.name) {\r\n                    throw new Error('gulp-ps-module requires \"name\" option: PowerShell module name.');\r\n                }\r\n\r\n                if (!options.guid) {\r\n                    throw new Error('gulp-ps-module requires \"guid\" option: PowerShell module identification.');\r\n                }\r\n\r\n                let scriptName: string = file.relative.replace('.ps1', '');\r\n                const lastIndex = scriptName.lastIndexOf('\\\\');\r\n                if (lastIndex > 0) {\r\n                    // remove any relative folder name.\r\n                    scriptName = scriptName.substring(lastIndex + 1);\r\n                }\r\n\r\n                if (options.prefixName) {\r\n                    const hyphenSeparatorIndex = scriptName.indexOf('-');\r\n                    const prefixName = options.prefixName;\r\n                    const verb = scriptName.substring(0, hyphenSeparatorIndex);\r\n                    const target = scriptName.substring(hyphenSeparatorIndex + 1);\r\n                    scriptName = verb + '-' + prefixName + target;\r\n                }\r\n\r\n                const lines = file.contents.toString().split('\\n');\r\n                const newLines = [];\r\n                for (let i = 0; i < lines.length; i++) {\r\n                    const line = lines[i];\r\n                    if (line.indexOf('# SIG # Begin signature block') >= 0) {\r\n                        break;\r\n                    }\r\n\r\n                    newLines.push(line);\r\n                }\r\n\r\n                scriptCollection[scriptName] = newLines.join('\\n');\r\n                const external = file.path.indexOf('\\\\node_modules\\\\') >= 0;\r\n                if (external) {\r\n                    externalScripts.push(scriptName);\r\n                } else {\r\n                    localScripts.push(scriptName);\r\n                }\r\n\r\n                return callback();\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                log.error(error);\r\n            }\r\n        },\r\n        /**\r\n         * Flush\r\n         */\r\n        function (callback) {\r\n            try {\r\n                localScripts.sort();\r\n                externalScripts.sort();\r\n\r\n                let contents = '';\r\n                let scriptList = '';\r\n                for (const scriptName of localScripts.concat(externalScripts)) {\r\n                    if (scriptCollection.hasOwnProperty(scriptName)) {\r\n                        contents += 'function ' + scriptName + ' {\\r\\n';\r\n                        contents += scriptCollection[scriptName];\r\n                        contents += '\\r\\n}\\r\\n';\r\n                        contents += '## [END] ' + scriptName + ' ##\\r\\n';\r\n                        scriptList += '\\r\\n    \\'' + scriptName + '\\',';\r\n                    }\r\n                }\r\n\r\n                const psm1File = new Vinyl({\r\n                    cwd: './',\r\n                    path: options.name + '.psm1',\r\n                    contents: Buffer.from(contents, 'utf8')\r\n                });\r\n                this.push(psm1File);\r\n\r\n                const definition = getDefinition(\r\n                    options.name,\r\n                    options.version,\r\n                    options.guid,\r\n                    scriptList.substring(2, scriptList.length - 1),\r\n                    localScripts,\r\n                    externalScripts);\r\n                const psd1File = new Vinyl({\r\n                    cwd: './',\r\n                    path: options.name + '.psd1',\r\n                    contents: Buffer.from(definition, 'utf8')\r\n                });\r\n                this.push(psd1File);\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                log.error(error);\r\n            }\r\n\r\n            callback();\r\n        });\r\n}\r\n\r\nmodule.exports = gulpPowerShellModule;\r\n"]}