Files
privacy.sexy/tests/checks/desktop-runtime-errors/check-desktop-runtime-errors/utils/npm.ts
undergroundwires 0a2a1a026b Refactor build configs and improve CI/CD checks
This commit makes the build process more robust, simplifies
configurations and reduce the risk of incomplete or erroneous
deployments.

- Centralize output directory definitions by introducing
  `dist-dirs.json`.
- Add `verify-build-artifacts` utility to ensure correct build outputs
  and `print-dist-dir` to determine distribution directory.
- Add steps in CI/CD pipeline to verify build artifacts.
- Migrate Electron Builder config from YAML to CJS for capability to
  read JSON.
- Fix `release-site.yaml` failing due to pointing to wrong distribution
  directory, change it to use `print-dist-dir`.
- Improve `check-desktop-runtime-errors` to verify build artifacts for
  more reliable builds. Ensure tests fail and succeed reliably.
- Update `.gitignore` and configure ESLint to use it to define and
  ignore build artifact directories from one place, remove
  `.eslintignore` that does not add anything after this change.
- Keep `"main"` field in `package.json` as `electron-vite` depends on it
  (alex8088/electron-vite#270).
- Improve documentation
2023-09-03 14:50:31 +02:00

132 lines
4.2 KiB
TypeScript

import { join } from 'path';
import { rm, readFile } from 'fs/promises';
import { exists, isDirMissingOrEmpty } from './io';
import { CommandResult, runCommand } from './run-command';
import { LogLevel, die, log } from './log';
import { sleep } from './sleep';
import type { ExecOptions } from 'child_process';
const NPM_INSTALL_MAX_RETRIES = 3;
const NPM_INSTALL_RETRY_DELAY_MS = 5 /* seconds */ * 1000;
export async function ensureNpmProjectDir(projectDir: string): Promise<void> {
if (!projectDir) { throw new Error('missing project directory'); }
if (!await exists(join(projectDir, 'package.json'))) {
die(`\`package.json\` not found in project directory: ${projectDir}`);
}
}
export async function npmInstall(projectDir: string): Promise<void> {
if (!projectDir) { throw new Error('missing project directory'); }
const npmModulesPath = join(projectDir, 'node_modules');
if (!await isDirMissingOrEmpty(npmModulesPath)) {
log(`Directory "${npmModulesPath}" exists and has content. Skipping \`npm install\`.`);
return;
}
log('Starting dependency installation...');
const { error } = await executeWithRetry('npm install --loglevel=error', {
cwd: projectDir,
}, NPM_INSTALL_MAX_RETRIES, NPM_INSTALL_RETRY_DELAY_MS);
if (error) {
die(error);
}
log('Installed dependencies...');
}
export async function npmBuild(
projectDir: string,
buildCommand: string,
distDir: string,
forceRebuild: boolean,
): Promise<void> {
if (!projectDir) { throw new Error('missing project directory'); }
if (!buildCommand) { throw new Error('missing build command'); }
if (!distDir) { throw new Error('missing distribution directory'); }
const isMissingBuild = await isDirMissingOrEmpty(distDir);
if (!isMissingBuild && !forceRebuild) {
log(`Directory "${distDir}" exists and has content. Skipping build: '${buildCommand}'.`);
return;
}
if (forceRebuild) {
log(`Removing directory "${distDir}" for a clean build (triggered by \`--build\` flag).`);
await rm(distDir, { recursive: true, force: true });
}
log('Building project...');
const { error } = await runCommand(buildCommand, {
cwd: projectDir,
});
if (error) {
die(error);
}
if (await isDirMissingOrEmpty(distDir)) {
die(`The desktop application build process did not produce the expected artifacts. The output directory "${distDir}" is empty or missing.`);
}
}
const appNameCache = new Map<string, string>();
export async function getAppName(projectDir: string): Promise<string> {
if (!projectDir) { throw new Error('missing project directory'); }
if (appNameCache.has(projectDir)) {
return appNameCache.get(projectDir);
}
const packageData = await readPackageJsonContents(projectDir);
try {
const packageJson = JSON.parse(packageData);
const name = packageJson.name as string;
if (!name) {
return die(`The \`package.json\` file doesn't specify a name: ${packageData}`);
}
appNameCache.set(projectDir, name);
return name;
} catch (error) {
return die(`Unable to parse \`package.json\`. Error: ${error}\nContent: ${packageData}`);
}
}
async function readPackageJsonContents(projectDir: string): Promise<string> {
if (!projectDir) { throw new Error('missing project directory'); }
const packagePath = join(projectDir, 'package.json');
if (!await exists(packagePath)) {
return die(`\`package.json\` file not found at ${packagePath}`);
}
try {
const packageData = await readFile(packagePath, 'utf8');
return packageData;
} catch (error) {
log(`Error reading \`package.json\` from ${packagePath}.`, LogLevel.Error);
return die(`Error detail: ${error}`);
}
}
async function executeWithRetry(
command: string,
options: ExecOptions,
maxRetries: number,
retryDelayInMs: number,
currentAttempt = 1,
): Promise<CommandResult> {
const result = await runCommand(command, options);
if (!result.error || currentAttempt >= maxRetries) {
return result;
}
log(`Attempt ${currentAttempt} failed. Retrying in ${retryDelayInMs / 1000} seconds...`);
await sleep(retryDelayInMs);
const retryResult = await executeWithRetry(
command,
options,
maxRetries,
retryDelayInMs,
currentAttempt + 1,
);
return retryResult;
}