@augment-vir/node
Version:
A collection of augments, helpers types, functions, and classes only for Node.js (backend) JavaScript environments.
35 lines (31 loc) • 1.1 kB
text/typescript
import {addSuffix} from '@augment-vir/common';
import {stat} from 'node:fs/promises';
import {runShellCommand} from '../../augments/terminal/shell.js';
/**
* Parameters for `docker.container.copyTo`.
*
* @category Node : Docker : Util
* @category Package : @augment-vir/node
* @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node)
*/
export type CopyToDockerContainerParams = {
hostPath: string;
containerAbsolutePath: string;
containerNameOrId: string;
dockerFlags?: ReadonlyArray<string>;
};
export async function copyToContainer({
containerAbsolutePath,
hostPath,
containerNameOrId,
dockerFlags = [],
}: CopyToDockerContainerParams): Promise<void> {
const isDir = (await stat(hostPath)).isDirectory();
const suffix = isDir ? '/.' : '';
const fullHostPath = addSuffix({value: hostPath, suffix});
const extraInputs: string = dockerFlags.join(' ');
await runShellCommand(
`docker cp ${extraInputs} '${fullHostPath}' '${containerNameOrId}:${containerAbsolutePath}'`,
{rejectOnError: true},
);
}