2023-03-22 17:22:27 -04:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
2024-11-06 13:40:19 -05:00
|
|
|
import fs from 'node:fs';
|
|
|
|
|
import path from 'node:path';
|
2023-03-22 17:22:27 -04:00
|
|
|
|
2024-11-06 13:40:19 -05:00
|
|
|
import chalk from 'chalk';
|
2023-03-22 17:22:27 -04:00
|
|
|
|
|
|
|
|
function getWorkspaces() {
|
2024-11-06 13:40:19 -05:00
|
|
|
const packageFile = fs.readFileSync('package.json');
|
|
|
|
|
const packageJson = JSON.parse(packageFile, 'utf8');
|
|
|
|
|
|
2023-03-22 17:22:27 -04:00
|
|
|
return packageJson.workspaces;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getPlatformPackagesContainingCommand(scriptName) {
|
|
|
|
|
return getWorkspaces().filter((workspace) => {
|
|
|
|
|
if (!workspace.startsWith('platform/')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 13:40:19 -05:00
|
|
|
const workspacePackageFile = fs.readFileSync(path.join(workspace, 'package.json'));
|
|
|
|
|
const workspacePackageJson = JSON.parse(workspacePackageFile, 'utf8');
|
2023-03-22 17:22:27 -04:00
|
|
|
|
|
|
|
|
return workspacePackageJson?.scripts?.[scriptName];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an array of concurrently commands to run a given script on every platform workspace that contains it.
|
|
|
|
|
*/
|
2024-11-06 13:40:19 -05:00
|
|
|
export function getPlatformCommands(scriptName) {
|
2023-03-22 17:22:27 -04:00
|
|
|
return getPlatformPackagesContainingCommand(scriptName).map((workspace) => ({
|
|
|
|
|
command: `npm:${scriptName} --workspace=${workspace}`,
|
|
|
|
|
name: workspace.substring(workspace.lastIndexOf('/') + 1),
|
|
|
|
|
prefixColor: getColorForWorkspace(workspace),
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const workspaceColors = ['green', 'magenta', 'yellow', 'red', 'blue'];
|
|
|
|
|
function getColorForWorkspace(workspace) {
|
|
|
|
|
const index = getWorkspaces().indexOf(workspace);
|
|
|
|
|
|
|
|
|
|
return index === -1 ? chalk.white : workspaceColors[index % workspaceColors.length];
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-10 11:17:24 -04:00
|
|
|
/**
|
|
|
|
|
* @param {import("concurrently").CloseEvent[]} closeEvents - An array of CloseEvents thrown by concurrently when waiting on a result
|
|
|
|
|
* @param {number} codeOnSignal - Which error code to return when the process is interrupted
|
|
|
|
|
*/
|
2024-11-06 13:40:19 -05:00
|
|
|
export function getExitCode(closeEvents, codeOnSignal = 1) {
|
2023-10-10 11:17:24 -04:00
|
|
|
const exitCode = closeEvents.find((event) => !event.killed && event.exitCode > 0)?.exitCode;
|
|
|
|
|
|
|
|
|
|
if (typeof exitCode === 'string') {
|
|
|
|
|
return codeOnSignal
|
|
|
|
|
} else {
|
|
|
|
|
return exitCode;
|
|
|
|
|
}
|
|
|
|
|
}
|