Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 3a99d26

Browse files
committed
fix(spawn): better error reporting
1 parent fb13e0d commit 3a99d26

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

src/Environment.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import del from 'del'
88
import glob from 'glob'
99
import mkdirp from 'mkdirp'
1010
import * as pty from 'node-pty'
11-
// @ts-ignore
12-
import spawn from 'await-spawn'
1311
import tmp from 'tmp'
1412
import yaml from 'js-yaml'
1513

14+
import spawn from './spawn'
1615
import * as nix from './nix'
1716

1817
// The home directory for environments

src/nix.ts

+7-14
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import fs from 'fs'
55
import path from 'path'
66

77
import mkdirp from 'mkdirp'
8-
// @ts-ignore
9-
import spawn from 'await-spawn'
108
import { sprintf } from 'sprintf-js'
119

1210
import db from './db'
11+
import spawn from './spawn'
1312

1413
/**
1514
* Create a version string that can be ordered
@@ -46,14 +45,14 @@ mkdirp.sync(profiles)
4645
export async function channel (url: string = 'https://nixos.org/channels/nixpkgs-unstable', name: string = 'nixpkgs-unstable') {
4746
// Update the channel
4847
// nix-channel --add url [name] : Adds a channel named name with URL url to the list of subscribed channels
49-
spawn('nix-channel', ['--add', url, name])
48+
await spawn('nix-channel', ['--add', url, name])
5049
// nix-channel --update [names...] : Downloads the Nix expressions of all subscribed channels (or only those included in names if specified) and
5150
// makes them the default for nix-env operations (by symlinking them from the directory ~/.nix-defexpr).
52-
spawn('nix-channel', ['--update', name])
51+
await spawn('nix-channel', ['--update', name])
5352
// nix-channel --list : Prints the names and URLs of all subscribed channels on standard output.
54-
const list = spawn('nix-channel', ['--list'])
53+
const list = await spawn('nix-channel', ['--list'])
5554
console.log('Updated channel. Current channel list:')
56-
list.stdout.pipe(process.stdout)
55+
console.log(list)
5756
}
5857

5958
/**
@@ -95,13 +94,7 @@ export async function update (channels: string | Array<string> = [], last: boole
9594
['--attr', 'rPackages']
9695
]) {
9796
let allArgs = args.concat(extraArgs)
98-
let query
99-
try {
100-
query = await spawn('nix-env', allArgs)
101-
} catch (error) {
102-
throw new Error(`Running "nix-env ${allArgs.join(' ')}" failed: ${error.stderr}`)
103-
}
104-
let json = query.toString()
97+
let json = await spawn('nix-env', allArgs)
10598
let newPkgs
10699
try {
107100
newPkgs = JSON.parse(json)
@@ -265,7 +258,7 @@ export async function location (env: string): Promise<string> {
265258
const profile = path.join(profiles, env)
266259
if (!fs.existsSync(profile)) return ''
267260
const readlink = await spawn('readlink', ['-f', profile])
268-
return readlink.toString().trim()
261+
return readlink
269262
}
270263

271264
/**

src/spawn.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @ts-ignore
2+
import awaitSpawn from 'await-spawn'
3+
4+
/**
5+
* Spawn a process
6+
*
7+
* A wrapper function around `await-spawn` to provide better error
8+
* reporting.
9+
*
10+
* @param file The executable file to be spawned
11+
* @param args Arguments
12+
*/
13+
export default async function spawn (file: string, args: Array<string>, options: any = {}) {
14+
let buffer
15+
try {
16+
buffer = await awaitSpawn(file, args, options)
17+
} catch (error) {
18+
throw new Error(`Running "${file} ${args.join(' ')}" failed: ${error.code}: ${error.stderr.toString()}`)
19+
}
20+
return buffer.toString().trim()
21+
}

0 commit comments

Comments
 (0)