Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ arguments so we don't have to care about escaping spaces. It can also return
stdout/stderr even when the command fails, or times out. Importantly, it's also
not susceptible to max buffer issues.

Published as both ESM and CommonJS, so `import {exec} from 'teen_process'` and
`const {exec} = require('teen_process')` both work.

## teen_process.exec

Examples:
Expand Down
6 changes: 3 additions & 3 deletions lib/exec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {spawn} from 'node:child_process';
import {quote} from 'shell-quote';
import {formatEnoent} from './helpers';
import {CircularBuffer, MAX_BUFFER_SIZE} from './circular-buffer';
import {formatEnoent} from './helpers.js';
import {CircularBuffer, MAX_BUFFER_SIZE} from './circular-buffer.js';
import type {
TeenProcessExecOptions,
TeenProcessExecResult,
BufferProp,
ExecError,
StreamName,
} from './types';
} from './types.js';

/**
* Spawns a child process and collects its output.
Expand Down
6 changes: 3 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export {spawn} from 'node:child_process';
export {SubProcess} from './subprocess';
export {exec} from './exec';
export {SubProcess} from './subprocess.js';
export {exec} from './exec.js';
export type {
TeenProcessExecOptions,
TeenProcessExecResult,
ExecError,
SubProcessOptions,
} from './types';
} from './types.js';
4 changes: 2 additions & 2 deletions lib/subprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {spawn} from 'node:child_process';
import type {ChildProcess} from 'node:child_process';
import {EventEmitter} from 'node:events';
import {quote} from 'shell-quote';
import {formatEnoent} from './helpers';
import {formatEnoent} from './helpers.js';
import {createInterface} from 'node:readline';
import type {Readable} from 'node:stream';
import type {SubProcessOptions, StartDetector, TIsBufferOpts, StreamName} from './types';
import type {SubProcessOptions, StartDetector, TIsBufferOpts, StreamName} from './types.js';

/**
* A wrapper around Node's spawn that provides event-driven process management.
Expand Down
31 changes: 25 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,39 @@
},
"license": "Apache-2.0",
"author": "Appium Contributors",
"main": "build/lib/index.js",
"types": "build/lib/index.d.ts",
"type": "module",
"main": "./build/cjs/lib/index.js",
"module": "./build/esm/lib/index.js",
"types": "./build/cjs/lib/index.d.ts",
"exports": {
".": {
"import": {
"types": "./build/esm/lib/index.d.ts",
"default": "./build/esm/lib/index.js"
},
"require": {
"types": "./build/cjs/lib/index.d.ts",
"default": "./build/cjs/lib/index.js"
}
},
"./package.json": "./package.json"
},
Comment on lines +27 to +39

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is ok

"bin": {},
"directories": {
"lib": "lib"
},
"files": [
"lib",
"build/lib",
"build/cjs/lib",
"build/cjs/package.json",
"build/esm/lib",
"build/esm/package.json",
"CHANGELOG.md"
],
"scripts": {
"build": "tsc -b",
"clean": "npm run build -- --clean",
"build": "tsc -b tsconfig.esm.json tsconfig.cjs.json",
"postbuild": "node scripts/postbuild.mjs",
"clean": "node -e \"require('node:fs').rmSync('build', {recursive: true, force: true})\"",
"dev": "npm run build -- --watch",
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
Expand All @@ -42,7 +61,7 @@
"format:check": "prettier --check ./lib ./test",
"prepare": "npm run rebuild",
"rebuild": "npm run clean && npm run build",
"test": "node --test \"./build/test/**/*.spec.js\""
"test": "node --test --enable-source-maps --test-timeout=60000 \"./build/esm/test/**/*.spec.js\" \"./test/*.spec.cjs\""
},
"prettier": {
"bracketSpacing": false,
Expand Down
9 changes: 9 additions & 0 deletions scripts/postbuild.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {mkdirSync, writeFileSync} from 'node:fs';

for (const [dir, type] of [
['build/esm', 'module'],
['build/cjs', 'commonjs'],
]) {
mkdirSync(dir, {recursive: true});
writeFileSync(`${dir}/package.json`, `${JSON.stringify({type}, null, 2)}\n`);
}
2 changes: 1 addition & 1 deletion test/circular-buffer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'node:assert/strict';
import {CircularBuffer} from '../lib/circular-buffer';
import {CircularBuffer} from '../lib/circular-buffer.js';
import {describe, it} from 'node:test';

describe('CircularBuffer', function () {
Expand Down
12 changes: 12 additions & 0 deletions test/cjs-interop.spec.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const assert = require('node:assert/strict');
const {test} = require('node:test');
// Requires the package by name (rather than a build path) so this exercises the same
// "exports"/"main" resolution real CommonJS consumers go through.
const teenProcess = require('teen_process');

test('require() interop with the CJS build', async () => {
assert.equal(typeof teenProcess.exec, 'function');
assert.equal(typeof teenProcess.SubProcess, 'function');
const {code} = await teenProcess.exec('true');
assert.equal(code, 0);
});
7 changes: 5 additions & 2 deletions test/exec.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import assert from 'node:assert/strict';
import path from 'node:path';
import {exec} from '../lib';
import {getFixture} from './helpers';
import {fileURLToPath} from 'node:url';
import {exec} from '../lib/index.js';
import {getFixture} from './helpers.js';
import {describe, it, type TestContext} from 'node:test';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

describe('exec', function () {
it('should work with arguments like spawn', async function () {
const cmd = 'ls';
Expand Down
3 changes: 2 additions & 1 deletion test/helpers.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 {fileURLToPath} from 'node:url';

let moduleRoot: string | null = null;

Expand All @@ -16,7 +17,7 @@ export async function getFixture(fix: string): Promise<string> {
}

async function getModuleRoot(): Promise<string> {
let currentDir = path.dirname(path.resolve(__filename));
let currentDir = path.dirname(fileURLToPath(import.meta.url));
let isAtFsRoot = false;
while (!isAtFsRoot) {
const manifestPath = path.join(currentDir, 'package.json');
Expand Down
8 changes: 6 additions & 2 deletions test/subproc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import assert from 'node:assert/strict';
import path from 'node:path';
import {exec, SubProcess} from '../lib';
import {getFixture} from './helpers';
import {fileURLToPath} from 'node:url';
import {exec, SubProcess} from '../lib/index.js';
import {getFixture} from './helpers.js';
import {describe, it, beforeEach, afterEach} from 'node:test';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Windows doesn't understand SIGHUP
const stopSignal = process.platform === 'win32' ? 'SIGTERM' : 'SIGHUP';

Expand Down
10 changes: 10 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "build/cjs",
"module": "CommonJS",
"moduleResolution": "Bundler"
},
"include": ["lib"]
}
7 changes: 7 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "build/esm"
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"include": [
"lib",
"test"
]
],
"exclude": ["test/*.cjs"]
}