import fs from 'fs'; import glob from 'glob'; import psCimModule from '@microsoft/windows-admin-center-sdk/tools/gulp-ps-cim'; import psCodeModule from '@microsoft/windows-admin-center-sdk/tools/gulp-ps-code'; import psManifestModule from '@microsoft/windows-admin-center-sdk/tools/gulp-ps-manifest'; import psModuleModule from '@microsoft/windows-admin-center-sdk/tools/gulp-ps-module'; import psResjsonModule from '@microsoft/windows-admin-center-sdk/tools/gulp-ps-resjson'; import { dest, series, src } from 'gulp'; import { gulpConfig } from '../config-data'; import { Config } from './config'; export module PowerShellModule { const config: Config = gulpConfig(); const psCim = psCimModule as any; const psCode = psCodeModule as any; const psManifest = psManifestModule as any; const psModule = psModuleModule as any; const psResjson = psResjsonModule as any; function powershellCim() { return src('src/powershell-cim-config.json') .pipe(psCim()) .pipe(dest('src/generated/scripts/')); } function powershellCode() { const powerShellCodePaths = ['src/resources/scripts/**/*.ps1', 'src/generated/scripts/**/*.ps1']; const validPaths = validatePaths(powerShellCodePaths); if (!validPaths || !validPaths.length) { console.log('[Warning] powershellCode paths are empty.') return new Promise((resolve) => { resolve(true); }); } return src(validPaths) .pipe(psCode({ powerShellModuleName: config.powershell.name, resourceName: config.powershell.skipResjson ? null : config.resjson.resourceName, prefixName: config.powershell.prefixName })) .pipe(dest('src/generated/')); } function powershellModule() { const powerShellModulePaths = []; config.powershell.list.forEach(item => { powerShellModulePaths.push(item + '/resources/scripts/**/*.ps1'); powerShellModulePaths.push(item + '/generated/scripts/**/*.ps1'); }); const validPaths = validatePaths(powerShellModulePaths); if (!validPaths || !validPaths.length) { console.log('[Warning] powershellModule paths are empty.') return new Promise((resolve) => { resolve(true); }); } return src(validPaths) .pipe(psModule(config.powershell)) .pipe(dest('dist-ps/powershell-module/' + config.powershell.name)); } function powershellResjson() { return src(['src/resources/strings/strings.resjson', config.resjson.localePath + '/**/*.resjson']) .pipe(psResjson({ resourceName: config.resjson.resourceName })) .pipe(dest('dist-ps/powershell-module/' + config.powershell.name)); } function powershellManifest(): any { return src(['src/resources/scripts/**/*.ps1']) .pipe(psManifest({ powerShellModuleName: config.powershell.name, prefixName: config.powershell.prefixName })) .pipe(dest('.')); } // The gulp bundle is not aware of which repository has generated scripts or not, so we need to validate the paths. // Otherwise gulp would throw error on empty paths with wildcard. function validatePaths(paths) { let validPaths = []; paths.forEach(path => { const files = glob.sync(path); files.forEach(file => { if (fs.existsSync(file)) { validPaths.push(path); } }); }); return validPaths; } const seriesArray = []; if (!config.powershell.skipCim) { seriesArray.push(powershellCim); } seriesArray.push(powershellCode); if (!config.powershell.skipModule) { seriesArray.push(powershellModule); } if (!config.powershell.skipResjson) { seriesArray.push(powershellResjson); } if (!config.powershell.skipManifest) { seriesArray.push(powershellManifest); } export const powershell = seriesArray.length === 1 ? powershellCode : series(seriesArray); }