Refactor to enforce strictNullChecks

This commit applies `strictNullChecks` to the entire codebase to improve
maintainability and type safety. Key changes include:

- Remove some explicit null-checks where unnecessary.
- Add necessary null-checks.
- Refactor static factory functions for a more functional approach.
- Improve some test names and contexts for better debugging.
- Add unit tests for any additional logic introduced.
- Refactor `createPositionFromRegexFullMatch` to its own function as the
  logic is reused.
- Prefer `find` prefix on functions that may return `undefined` and
  `get` prefix for those that always return a value.
This commit is contained in:
undergroundwires
2023-11-12 22:54:00 +01:00
parent 7ab16ecccb
commit 949fac1a7c
294 changed files with 2477 additions and 2738 deletions

View File

@@ -8,7 +8,7 @@ export async function findByFilePattern(
pattern: string,
directory: string,
projectRootDir: string,
): Promise<ArtifactLocation> {
): Promise<ArtifactLocation | never> {
if (!directory) { throw new Error('Missing directory'); }
if (!pattern) { throw new Error('Missing file pattern'); }
@@ -42,5 +42,5 @@ function escapeRegExp(string: string) {
}
interface ArtifactLocation {
readonly absolutePath?: string;
readonly absolutePath: string;
}

View File

@@ -36,7 +36,10 @@ async function mountDmg(
die(`Failed to mount DMG file at ${dmgFile}.\n${error}`);
}
const mountPathMatch = hdiutilOutput.match(/\/Volumes\/[^\n]+/);
const mountPath = mountPathMatch ? mountPathMatch[0] : null;
if (!mountPathMatch || mountPathMatch.length === 0) {
die(`Could not find mount path from \`hdiutil\` output:\n${hdiutilOutput}`);
}
const mountPath = mountPathMatch[0];
return {
mountPath,
};
@@ -52,7 +55,10 @@ async function findMacAppExecutablePath(
return die(`Failed to find executable path at mount path ${mountPath}\n${error}`);
}
const appFolder = findOutput.trim();
const appName = appFolder.split('/').pop().replace('.app', '');
const appName = appFolder.split('/').pop()?.replace('.app', '');
if (!appName) {
die(`Could not extract app path from \`find\` output: ${findOutput}`);
}
const appPath = `${appFolder}/Contents/MacOS/${appName}`;
if (await exists(appPath)) {
log(`Application is located at ${appPath}`);