@xec-sh/core
Version:
Universal shell execution engine
97 lines • 3.11 kB
JavaScript
export function createSSHExecutionContext(engine, sshOptions, commandConfig = {}) {
const exec = (strings, ...values) => {
let sshEngine = engine.with({
adapter: 'ssh',
adapterOptions: { type: 'ssh', ...sshOptions },
...commandConfig
});
if (commandConfig.retry) {
sshEngine = sshEngine.retry(commandConfig.retry);
}
return sshEngine.run(strings, ...values);
};
const raw = (strings, ...values) => {
let sshEngine = engine.with({
adapter: 'ssh',
adapterOptions: { type: 'ssh', ...sshOptions },
...commandConfig
});
if (commandConfig.retry) {
sshEngine = sshEngine.retry(commandConfig.retry);
}
return sshEngine.raw(strings, ...values);
};
const tunnel = async (options) => {
const adapter = engine.getAdapter('ssh');
if (!adapter) {
throw new Error('SSH adapter not available');
}
await exec `echo "Establishing connection for tunnel"`.quiet();
return adapter.tunnel(options);
};
const uploadFile = async (localPath, remotePath) => {
const adapter = engine.getAdapter('ssh');
if (!adapter) {
throw new Error('SSH adapter not available');
}
await adapter.uploadFile(localPath, remotePath, {
type: 'ssh',
...sshOptions
});
};
const downloadFile = async (remotePath, localPath) => {
const adapter = engine.getAdapter('ssh');
if (!adapter) {
throw new Error('SSH adapter not available');
}
await adapter.downloadFile(remotePath, localPath, {
type: 'ssh',
...sshOptions
});
};
const uploadDirectory = async (localPath, remotePath) => {
const adapter = engine.getAdapter('ssh');
if (!adapter) {
throw new Error('SSH adapter not available');
}
await adapter.uploadDirectory(localPath, remotePath, {
type: 'ssh',
...sshOptions
});
};
const env = (envVars) => createSSHExecutionContext(engine, sshOptions, {
...commandConfig,
env: { ...commandConfig.env, ...envVars }
});
const cd = (dir) => createSSHExecutionContext(engine, sshOptions, {
...commandConfig,
cwd: dir
});
const timeout = (ms) => createSSHExecutionContext(engine, sshOptions, {
...commandConfig,
timeout: ms
});
const shell = (shellValue) => createSSHExecutionContext(engine, sshOptions, {
...commandConfig,
shell: shellValue
});
const retry = (options) => createSSHExecutionContext(engine, sshOptions, {
...commandConfig,
retry: options
});
const context = Object.assign(exec, {
exec,
raw,
tunnel,
uploadFile,
downloadFile,
uploadDirectory,
env,
cd,
timeout,
shell,
retry
});
return context;
}
//# sourceMappingURL=ssh-api.js.map