@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
247 lines (203 loc) • 8.81 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fancy_log_1 = tslib_1.__importDefault(require("fancy-log"));
const plugin_error_1 = tslib_1.__importDefault(require("plugin-error"));
const through2_1 = tslib_1.__importDefault(require("through2"));
const vinyl_1 = tslib_1.__importDefault(require("vinyl"));
const util = tslib_1.__importStar(require("../utilities"));
const PLUGIN_NAME = 'gulp-ps-module';
function getDefinition(name, version, guid, functionsList, localScripts, externalScripts) {
const definition = `#
# Module manifest for module '{{Name}}'
#
@{
# Script module or binary module file associated with this manifest.
RootModule = '{{Name}}.psm1'
# Version number of this module.
ModuleVersion = '{{Version}}'
# Supported PSEditions
# CompatiblePSEditions = @()
# ID used to uniquely identify this module
GUID = '{{Guid}}'
# Author of this module
Author = 'SME'
# Company or vendor of this module
CompanyName = 'Microsoft'
# Copyright statement for this module
Copyright = '(c) 2018 Microsoft. All rights reserved.'
# Description of the functionality provided by this module
# Description = ''
# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '5.0'
# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ''
# Minimum version of the Windows PowerShell host required by this module
# PowerShellHostVersion = ''
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# CLRVersion = ''
# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''
# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()
# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()
# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @()
# 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.
FunctionsToExport = @(
{{FunctionsList}}
)
# Function attributes: {{FunctionAttributes}}
# 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.
CmdletsToExport = @()
# Variables to export from this module
VariablesToExport = '*'
# 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.
AliasesToExport = @()
# DSC resources to export from this module
# DscResourcesToExport = @()
# List of all modules packaged with this module
# ModuleList = @()
# List of all files packaged with this module
# FileList = @()
# 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.
PrivateData = @{
PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()
# A URL to the license for this module.
# LicenseUri = ''
# A URL to the main website for this project.
# ProjectUri = ''
# A URL to an icon representing this module.
# IconUri = ''
# ReleaseNotes of this module
# ReleaseNotes = ''
} # End of PSData hashtable
} # End of PrivateData hashtable
# HelpInfo URI of this module
# HelpInfoURI = ''
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
}
`;
const attributes = JSON.stringify({ localScripts, externalScripts });
// calling {{Name}} twice since it's used two times on the template.
return definition
.split('\n')
.join('\r\n')
.replace('{{Name}}', name)
.replace('{{Name}}', name)
.replace('{{Version}}', version)
.replace('{{Guid}}', guid)
.replace('{{FunctionsList}}', functionsList)
.replace('{{FunctionAttributes}}', attributes);
}
function gulpPowerShellModule(options) {
//
// (Options):
//
// name.
// name of module: string;
// version.
// version of module: string; (default: 0.0.0)
// guid.
// GUID string of module: string
//
// override options settings if not specified.
options = Object.assign({ version: '0.0.0' }, options || {});
const scriptCollection = {};
const localScripts = [];
const externalScripts = [];
return through2_1.default.obj(
/**
* Transform
*/
function (file, encoding, callback) {
try {
if (!options.name) {
throw new Error('gulp-ps-module requires "name" option: PowerShell module name.');
}
if (!options.guid) {
throw new Error('gulp-ps-module requires "guid" option: PowerShell module identification.');
}
let scriptName = file.relative.replace('.ps1', '');
const lastIndex = scriptName.lastIndexOf('\\');
if (lastIndex > 0) {
// remove any relative folder name.
scriptName = scriptName.substring(lastIndex + 1);
}
if (options.prefixName) {
const hyphenSeparatorIndex = scriptName.indexOf('-');
const prefixName = options.prefixName;
const verb = scriptName.substring(0, hyphenSeparatorIndex);
const target = scriptName.substring(hyphenSeparatorIndex + 1);
scriptName = verb + '-' + prefixName + target;
}
scriptCollection[scriptName] = file.contents.toString();
const external = file.path.indexOf('\\node_modules\\') >= 0;
if (external) {
externalScripts.push(scriptName);
}
else {
localScripts.push(scriptName);
}
return callback();
}
catch (e) {
const error = (!e.plugin || (e.plugin !== PLUGIN_NAME)) ?
util.extendError(new plugin_error_1.default({ plugin: PLUGIN_NAME, message: e.message }), e) : e;
fancy_log_1.default.error(error);
}
},
/**
* Flush
*/
function (callback) {
try {
localScripts.sort();
externalScripts.sort();
let contents = '';
let scriptList = '';
for (const scriptName of localScripts.concat(externalScripts)) {
if (scriptCollection.hasOwnProperty(scriptName)) {
contents += 'function ' + scriptName + ' {\r\n';
contents += scriptCollection[scriptName];
contents += '\r\n}\r\n';
contents += '## [END] ' + scriptName + ' ##\r\n';
scriptList += '\r\n \'' + scriptName + '\',';
}
}
const psm1File = new vinyl_1.default({
cwd: './',
path: options.name + '.psm1',
contents: Buffer.from(contents, 'utf8')
});
this.push(psm1File);
const definition = getDefinition(options.name, options.version, options.guid, scriptList.substring(2, scriptList.length - 1), localScripts, externalScripts);
const psd1File = new vinyl_1.default({
cwd: './',
path: options.name + '.psd1',
contents: Buffer.from(definition, 'utf8')
});
this.push(psd1File);
}
catch (e) {
const error = (!e.plugin || (e.plugin !== PLUGIN_NAME)) ?
util.extendError(new plugin_error_1.default({ plugin: PLUGIN_NAME, message: e.message }), e) : e;
fancy_log_1.default.error(error);
}
callback();
});
}
module.exports = gulpPowerShellModule;
//# sourceMappingURL=index.js.map