Skip to content

Commit

Permalink
allow to run commands in serial
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Apr 20, 2023
1 parent d6b01e1 commit 6eb5dc7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cp from 'node:child_process'
import cp, { type SpawnOptions } from 'node:child_process'

import { findConfig } from './utils.js'
import { findConfig, asyncSpawn } from './utils.js'
import { download } from './installer.js'

const CLI_COMMANDS = ['completion', 'fmt', 'help', 'list', 'print', 'run', 'tui']
Expand All @@ -10,6 +10,7 @@ const CLI_COMMANDS = ['completion', 'fmt', 'help', 'list', 'print', 'run', 'tui'
* @returns instance of spawned child process instance
*/
export async function runme () {
const spawnOpts: SpawnOptions = { stdio: 'inherit', shell: true, env: process.env }
const binaryPath = await download()
const configFile: Record<string, unknown> = (await findConfig(process.cwd())) || {}
const params = [
Expand All @@ -24,6 +25,19 @@ export async function runme () {
params.unshift('run')
}

/**
* check if more than one markdown cell id was provided
*/
const paramArgs = params.filter((p) => !p.startsWith('--'))
const flags = params.filter((p) => p.startsWith('--'))
if (paramArgs.includes('run') && paramArgs.length > 2) {
const ids = paramArgs.filter((p) => p !== 'run')
for (const id of ids) {
await asyncSpawn(`${binaryPath} run ${id} ${flags.join(' ')}`, spawnOpts)
}
return
}

const command = `${binaryPath} ${params.join(' ')}`
return cp.spawn(command, { stdio: 'inherit', shell: true, env: process.env })
return cp.spawn(command, spawnOpts)
}
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'
import fs from 'node:fs/promises'
import cp, { type SpawnOptions } from 'node:child_process'
import { Transform, type TransformCallback } from 'node:stream'

import { SUPPORTED_RUNME_CONFIGFILE_NAMES } from './constants.js'
Expand Down Expand Up @@ -91,3 +92,12 @@ export async function findConfig(dir: string, depth = Infinity, configFileName =

return findConfig(nextDir, depth - 1)
}

export function asyncSpawn (command: string, args: SpawnOptions) {
let p = cp.spawn(command, args);
return new Promise((resolve) => {
p.stdout?.on("data", (x) => process.stdout.write(x.toString()))
p.stderr?.on("data", (x) => process.stderr.write(x.toString()))
p.on("exit", (code) => resolve(code))
})
}

0 comments on commit 6eb5dc7

Please sign in to comment.