From 116fd6fd69a2bd7d361e70c135da1432e20eb45d Mon Sep 17 00:00:00 2001 From: Darlan Alves Date: Mon, 8 Apr 2024 10:43:14 +0200 Subject: [PATCH] feat: add run command --- src/index.spec.ts | 12 +++++++++++- src/index.ts | 14 +++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/index.spec.ts b/src/index.spec.ts index e40bc98..7355f07 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import * as fs from 'fs'; import * as exec from '@cloud-cli/exec'; import sys from './index'; @@ -20,6 +20,16 @@ describe('system commands', () => { expect(exec.exec).toHaveBeenCalledWith('npm', ['i', '@cloud-cli/test']); }); + it('should run a command', async () => { + jest + .spyOn(exec, 'execString') + .mockReset() + .mockImplementationOnce(async () => execOutput); + + await expect(sys.run({ c: 'ls -al' })).resolves.toBe(''); + expect(exec.execString).toHaveBeenCalledWith('ls -al'); + }); + it('should update all modules', async () => { jest .spyOn(exec, 'exec') diff --git a/src/index.ts b/src/index.ts index cd421c5..f69545d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,8 @@ -import { exec } from '@cloud-cli/exec'; -import fs from 'fs'; +import { exec, execString } from '@cloud-cli/exec'; +import * as fs from 'fs'; interface InstallOptions { m: string } +interface RunOptions { c: string } async function update() { `Update dependencies`; @@ -25,6 +26,13 @@ async function restart() { return true; } +async function run({ c }: RunOptions) { + `Run a commmand`; + + const o = await execString(c); + return o.ok ? o.stdout : Promise.reject(o.stderr); +} + function createService() { `Generate a systemd template for Cloudy`; @@ -70,4 +78,4 @@ async function logs(options) { return o.stdout; } -export default { update, install, restart, createService, logs, stats }; \ No newline at end of file +export default { update, install, restart, createService, logs, stats, run }; \ No newline at end of file