ci(gulp): support multi-application samples

This commit is contained in:
Jan Krueger
2021-07-09 16:01:59 +02:00
parent e1307c58d3
commit a39b2c2c3c
2 changed files with 56 additions and 26 deletions

View File

@@ -5,7 +5,7 @@ import { task } from 'gulp';
import { resolve } from 'path';
import { promisify } from 'util';
import { samplePath } from '../config';
import { getDirs } from '../util/task-helpers';
import { containsPackageJson, getDirs } from '../util/task-helpers';
const exec = promisify(childProcess.exec);
@@ -16,36 +16,57 @@ async function executeNpmScriptInSamples(
const directories = getDirs(samplePath);
for await (const dir of directories) {
const dirName = dir.replace(resolve(__dirname, '../../../'), '');
log.info(`Running ${clc.blue(script)} in ${clc.magenta(dirName)}`);
try {
const result = await exec(
`${script} --prefix ${dir} ${appendScript ? '-- ' + appendScript : ''}`,
);
log.info(
`Finished running ${clc.blue(script)} in ${clc.magenta(dirName)}`,
);
if (result.stderr) {
log.error(result.stderr);
// Check if the sample is a multi-application sample
const isSingleApplicationSample = containsPackageJson(dir);
if (!isSingleApplicationSample) {
// Application is a multi-application sample
// Go down into the sub-directories
const subDirs = getDirs(dir);
for (const subDir of subDirs) {
await executeNPMScriptInDirectory(subDir, script, appendScript);
}
if (result.stdout) {
log.error(result.stdout);
}
} catch (err) {
log.error(
`Failed running ${clc.blue(script)} in ${clc.magenta(dirName)}`,
);
if (err.stderr) {
log.error(err.stderr);
}
if (err.stdout) {
log.error(err.stdout);
}
process.exit(1);
} else {
await executeNPMScriptInDirectory(dir, script, appendScript);
}
}
}
/**
* Executes the provided NPM script in the specified directory
* @param dir directory of the application
* @param script script to execute
* @param appendScript additional params appended to the script
*/
async function executeNPMScriptInDirectory(
dir: string,
script: string,
appendScript?: string,
) {
const dirName = dir.replace(resolve(__dirname, '../../../'), '');
log.info(`Running ${clc.blue(script)} in ${clc.magenta(dirName)}`);
try {
const result = await exec(
`${script} --prefix ${dir} ${appendScript ? '-- ' + appendScript : ''}`,
);
log.info(`Finished running ${clc.blue(script)} in ${clc.magenta(dirName)}`);
if (result.stderr) {
log.error(result.stderr);
}
if (result.stdout) {
log.error(result.stdout);
}
} catch (err) {
log.error(`Failed running ${clc.blue(script)} in ${clc.magenta(dirName)}`);
if (err.stderr) {
log.error(err.stderr);
}
if (err.stdout) {
log.error(err.stdout);
}
process.exit(1);
}
}
task('install:samples', async () =>
executeNpmScriptInSamples(
// 'npm ci --no-audit --no-shrinkwrap --no-optional',

View File

@@ -12,3 +12,12 @@ export function getFolders(dir: string) {
export function getDirs(base: string) {
return getFolders(base).map(path => `${base}/${path}`);
}
/**
* Checks if the directory contains a package.json file
* @param dir Path to the dircetory
* @returns True if the directory contains a package.json
*/
export function containsPackageJson(dir: string) {
return readdirSync(dir).some(file => file === 'package.json');
}