Skip to content

Commit

Permalink
several fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
murrayju committed Jan 18, 2022
1 parent 989f9f9 commit fb69713
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 39 deletions.
19 changes: 9 additions & 10 deletions src/brew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import os from 'os';
import path from 'path';

import { spawn, SpawnOptions } from './cp.js';
import { cmdExists } from './env.js';
import { appendIfMissing } from './fs.js';
import {
appendToEnvProfile,
AppendToEnvProfileOptions,
cmdExists,
ensureProcessPathEnvIncludes,
} from './env.js';

const brewPath = async (): Promise<string> => {
for (const testPath of [
Expand All @@ -28,15 +32,10 @@ const brewPathExists = async () => {
}
};

const appendBrewShellEnv = async () => {
const appendBrewShellEnv = async (opts?: AppendToEnvProfileOptions) => {
const brewDir = path.dirname(await brewPath());
await appendIfMissing(
path.join(os.homedir(), '.zprofile'),
`\neval "$(${brewDir}/brew shellenv)"`,
);
if (!process.env.PATH?.includes(brewDir)) {
process.env.PATH = `${brewDir}:${process.env.PATH}`;
}
await appendToEnvProfile(`\neval "$(${brewDir}/brew shellenv)"`, opts);
ensureProcessPathEnvIncludes(brewDir);
};

export const ensureBrewInstalled = async () => {
Expand Down
44 changes: 31 additions & 13 deletions src/docker.container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,22 @@ export async function unthrottledDockerContainerLs({
log(`Stopping container: <${this.name}>...`);
await this.stop(false);
log(`<${this.name}> container stopped.`);
} catch (e1: any) {
log(`Failed to stop <${this.name}> container: ${e1.message}`);
} catch (e1) {
log(
`Failed to stop <${this.name}> container: ${
e1 instanceof Error && e1.message
}`,
);
try {
log(`Killing container: <${this.name}>...`);
await this.kill(false);
log(`<${this.name}> container killed.`);
} catch (e2: any) {
log(`Failed to kill <${this.name}> container: ${e2.message}`);
} catch (e2) {
log(
`Failed to kill <${this.name}> container: ${
e2 instanceof Error && e2.message
}`,
);
}
}
} else {
Expand Down Expand Up @@ -210,9 +218,11 @@ export async function dockerContainerStop(
const ids = Array.isArray(id) ? id : [id];
try {
await spawn('docker', ['container', 'stop', ...ids]);
} catch (e: any) {
} catch (e) {
if (!ignoreErrors) {
throw new Error(`Failed to stop container(s): ${e.message}`);
throw new Error(
`Failed to stop container(s): ${e instanceof Error && e.message}`,
);
}
}
}
Expand All @@ -224,9 +234,11 @@ export async function dockerContainerKill(
const ids = Array.isArray(id) ? id : [id];
try {
await spawn('docker', ['container', 'kill', ...ids]);
} catch (e: any) {
} catch (e) {
if (!ignoreErrors) {
throw new Error(`Failed to kill container(s): ${e.message}`);
throw new Error(
`Failed to kill container(s): ${e instanceof Error && e.message}`,
);
}
}
}
Expand All @@ -235,8 +247,10 @@ export async function dockerTryStopContainer(id: string | null, name = '') {
if (id) {
try {
await dockerContainerStop(id, false);
} catch (e: any) {
buildLog(`Failed to stop ${name} container: ${e.message}`);
} catch (e) {
buildLog(
`Failed to stop ${name} container: ${e instanceof Error && e.message}`,
);
}
}
}
Expand All @@ -248,13 +262,17 @@ export async function dockerContainerRm(
const ids = Array.isArray(id) ? id : [id];
try {
await spawn('docker', ['container', 'rm', ...ids]);
} catch (e: any) {
} catch (e) {
if (ignoreErrors) {
buildLog(
`Warning (ignored Error): Failed to remove container(s): ${e.message}`,
`Warning (ignored Error): Failed to remove container(s): ${
e instanceof Error && e.message
}`,
);
} else {
throw new Error(`Failed to remove container(s): ${e.message}`);
throw new Error(
`Failed to remove container(s): ${e instanceof Error && e.message}`,
);
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/docker.volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { throttle } from 'lodash-es';

import { spawn } from './cp.js';

type DockerVolumeInspectOutput = Record<string, any>;
type DockerVolumeInspectOutput = Record<string, unknown>;

export interface DockerVolume {
driver: string;
Expand Down Expand Up @@ -110,9 +110,11 @@ export async function dockerVolumeRm(
const ids = Array.isArray(id) ? id : [id];
try {
await spawn('docker', ['volume', 'rm', ...ids]);
} catch (e: any) {
} catch (e) {
if (!ignoreErrors) {
throw new Error(`Failed to remove volume(s): ${e.message}`);
throw new Error(
`Failed to remove volume(s): ${e instanceof Error && e.message}`,
);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/dockerCleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ async function flatten(
const result = await input;
return Array.isArray(result)
? ([] as string[]).concat(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...(await Promise.all<string[][]>(result.map(flatten as any))),
)
: result;
Expand Down
33 changes: 23 additions & 10 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import os from 'os';
import path from 'path';

import { spawn } from './cp.js';
import { appendIfMissing } from './fs.js';
import { appendIfMissing, StringGenFn } from './fs.js';

const boolCache = new Map<string, boolean>();
const cacheBool = (key: string, fn: () => boolean): boolean => {
Expand Down Expand Up @@ -57,7 +57,7 @@ export const shellEnvFile = (): string | null =>
export const whichCmd = async (cmd: string): Promise<null | string> => {
const binPath = (
await spawn('which', [cmd], { captureOutput: true })
).output.trim();
).stdout.trim();
if (!binPath || !(await fs.pathExists(binPath))) {
return null;
}
Expand Down Expand Up @@ -99,20 +99,27 @@ export const readShellEnvVar = async (
}
};

export const appendEnvVarToProfile = async (
name: string,
value: string,
{ envFile = shellEnvFile() }: ReadShellEnvVarOptions = {},
export interface AppendToEnvProfileOptions {
envFile?: string | null;
testContent?: string | StringGenFn;
}

export const appendToEnvProfile = async (
content: string | StringGenFn,
{ envFile = shellEnvFile(), testContent }: AppendToEnvProfileOptions = {},
) => {
if (!envFile) {
throw new Error('envFile must be set');
}
await appendIfMissing(
path.join(os.homedir(), envFile),
`\nexport ${name}=${value}`,
);
await appendIfMissing(envFile, content, testContent);
};

export const appendEnvVarToProfile = async (
name: string,
value: string,
{ envFile = shellEnvFile() }: ReadShellEnvVarOptions = {},
) => appendToEnvProfile(`\nexport ${name}=${value}`, { envFile });

export const ensureEnvVarValue = async (
name: string,
value: string,
Expand All @@ -136,3 +143,9 @@ export const ensureEnvVarSet = async (
process.env[name] = newValue;
}
};

export const ensureProcessPathEnvIncludes = (binPath: string) => {
if (!process.env.PATH?.includes(binPath)) {
process.env.PATH = `${binPath}:${process.env.PATH}`;
}
};
2 changes: 1 addition & 1 deletion src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const copyIfMissing = async (src: string, dest: string) => {
}
};

type StringGenFn = () => string | Promise<string>;
export type StringGenFn = () => string | Promise<string>;

const resolveStringGen = async (
content: string | StringGenFn,
Expand Down
4 changes: 3 additions & 1 deletion src/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ export const filterKeys = <V, K extends string = string>(
*/
export const filterNullish = <T>(
obj: Record<string, T>,
): Record<string, Exclude<T, null | undefined>> => filterKeys(obj) as any;
): Record<string, Exclude<T, null | undefined>> =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
filterKeys(obj) as any;
2 changes: 1 addition & 1 deletion src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface PublishConfiguration extends CreateArtifactOptions {
npmPath?: string; // (optional) Path to npm executable
npmSkipExisting?: boolean; // (optional) if true, don't error when artifact already exists (skip publish)
npmTag?: string; // (optional) tag to apply to npm package
prePublishFn?: () => any; // (optional) Callback to invoke before publishing
prePublishFn?: () => void | Promise<void>; // (optional) Callback to invoke before publishing
}

// tgz the distDir, and copy it to the outDir
Expand Down

0 comments on commit fb69713

Please sign in to comment.