@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
121 lines (119 loc) • 4.59 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = tslib_1.__importDefault(require("path"));
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-manifest';
/**
* Plugin level function
*/
function gulpPsManifest(options) {
// override options settings if not specified.
options = Object.assign({
manifest: 'src/manifest.json',
removeComments: true
}, options || {});
function findCommand(data, result) {
if (typeof data === 'object') {
if (data.command) {
result.push(data);
return;
}
for (const key in data) {
if (data.hasOwnProperty(key)) {
const next = data[key];
findCommand(next, result);
}
}
}
}
function convert(command, file, content) {
const commentStart = '<#';
const commentEnd = '#>';
const comment = '#';
let script = '##' + command + '##:' + file + '\n';
let skipping = false;
const lines = content.split('\n');
lines.forEach(value => {
let text = value.replace('\r', '');
if (options.removeComments) {
let process = true;
text = text.trim();
if (text.startsWith(commentStart)) {
skipping = true;
}
if (skipping) {
process = false;
if (text.endsWith(commentEnd)) {
skipping = false;
}
}
if (process && !text.startsWith(comment) && text.length > 0) {
script += text + '\n';
}
}
else {
script += text + '\n';
}
});
return script;
}
const collection = {};
return through2_1.default.obj(function (file, enc, callback) {
let error = null;
try {
if (!options.powerShellModuleName) {
throw new Error('gulp-ps-manifest requires "powerShellModuleName" option: PowerShell module name.');
}
const path = path_1.default.parse(file.path);
if (path.ext === '.ps1') {
collection[path.base] = file.contents.toString('utf8');
}
}
catch (e) {
error = (!e.plugin || (e.plugin !== PLUGIN_NAME)) ?
util.extendError(new plugin_error_1.default({ plugin: PLUGIN_NAME, message: e.message }), e) : e;
}
return callback(error);
}, function (callback) {
try {
const manifestObject = JSON.parse(fs_1.default.readFileSync(options.manifest, 'utf8'));
manifestObject.powerShellModuleName = options.powerShellModuleName;
const result = [];
findCommand(manifestObject.entryPoints, result);
for (const item of result) {
let command = item.command;
if (options.prefixName) {
command = command.replace(options.prefixName, '');
}
const name = command + '.ps1';
if (collection[name]) {
item.script = convert(command, name, collection[name]);
item.module = options.powerShellModuleName;
}
else {
throw new Error('gulp-ps-manifest failed to locate the script: ' + command);
}
}
const manifestFile = new vinyl_1.default({
cwd: './',
path: options.manifest,
contents: Buffer.from(JSON.stringify(manifestObject, null, 2), 'utf8')
});
this.push(manifestFile);
}
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 = gulpPsManifest;
//# sourceMappingURL=index.js.map