Skip to content

Commit a459759

Browse files
ESM only (#568)
Co-authored-by: Pascal Jufer <[email protected]>
1 parent 7985e3a commit a459759

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+259
-320
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ insert_final_newline = true
1010
indent_style = space
1111

1212
# 4 space indentation and max line length of 100 in JavaScript/TypeScript files
13-
[*.{mjs,js,ts}]
13+
[*.{js,ts}]
1414
indent_size = 4
1515
max_line_length = 100

bin/concurrently.spec.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { map } from 'rxjs/operators';
1111
import stringArgv from 'string-argv';
1212
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
1313

14-
import { escapeRegExp } from '../src/utils';
14+
import { escapeRegExp } from '../src/utils.js';
1515

1616
const isWindows = process.platform === 'win32';
1717
const createKillMessage = (prefix: string, signal: 'SIGTERM' | 'SIGINT' | string) => {
@@ -32,7 +32,10 @@ beforeAll(async () => {
3232
entryPoints: [path.join(__dirname, 'concurrently.ts')],
3333
platform: 'node',
3434
bundle: true,
35-
outfile: path.join(tmpDir, 'concurrently.js'),
35+
// it doesn't seem like esbuild is able to change a CJS module to ESM, so target CJS instead.
36+
// https://github.com/evanw/esbuild/issues/1921
37+
format: 'cjs',
38+
outfile: path.join(tmpDir, 'concurrently.cjs'),
3639
});
3740
fs.copyFileSync(path.join(__dirname, '..', 'package.json'), path.join(tmpDir, 'package.json'));
3841
}, 8000);
@@ -50,7 +53,7 @@ afterAll(() => {
5053
*/
5154
const run = (args: string, ctrlcWrapper?: boolean) => {
5255
const spawnFn = ctrlcWrapper ? spawnWithWrapper : spawn;
53-
const child = spawnFn('node', [path.join(tmpDir, 'concurrently.js'), ...stringArgv(args)], {
56+
const child = spawnFn('node', [path.join(tmpDir, 'concurrently.cjs'), ...stringArgv(args)], {
5457
cwd: __dirname,
5558
env: {
5659
...process.env,
@@ -152,30 +155,29 @@ describe('exiting conditions', () => {
152155
});
153156

154157
it('is of success when --success=first and first command to exit succeeds', async () => {
155-
const exit = await run('--success=first "echo foo" "node fixtures/sleep.mjs 0.5 && exit 1"')
158+
const exit = await run('--success=first "echo foo" "node fixtures/sleep.js 0.5 && exit 1"')
156159
.exit;
157160

158161
expect(exit.code).toBe(0);
159162
});
160163

161164
it('is of failure when --success=first and first command to exit fails', async () => {
162-
const exit = await run('--success=first "exit 1" "node fixtures/sleep.mjs 0.5 && echo foo"')
165+
const exit = await run('--success=first "exit 1" "node fixtures/sleep.js 0.5 && echo foo"')
163166
.exit;
164167

165168
expect(exit.code).toBeGreaterThan(0);
166169
});
167170

168171
describe('is of success when --success=last and last command to exit succeeds', () => {
169172
it.each(['--success=last', '-s last'])('%s', async (arg) => {
170-
const exit = await run(`${arg} "exit 1" "node fixtures/sleep.mjs 0.5 && echo foo"`)
171-
.exit;
173+
const exit = await run(`${arg} "exit 1" "node fixtures/sleep.js 0.5 && echo foo"`).exit;
172174

173175
expect(exit.code).toBe(0);
174176
});
175177
});
176178

177179
it('is of failure when --success=last and last command to exit fails', async () => {
178-
const exit = await run('--success=last "echo foo" "node fixtures/sleep.mjs 0.5 && exit 1"')
180+
const exit = await run('--success=last "echo foo" "node fixtures/sleep.js 0.5 && exit 1"')
179181
.exit;
180182

181183
expect(exit.code).toBeGreaterThan(0);
@@ -260,7 +262,7 @@ describe('--hide', () => {
260262
describe('--group', () => {
261263
it('groups output per process', async () => {
262264
const lines = await run(
263-
'--group "echo foo && node fixtures/sleep.mjs 1 && echo bar" "echo baz"',
265+
'--group "echo foo && node fixtures/sleep.js 1 && echo bar" "echo baz"',
264266
).getLogLines();
265267

266268
expect(lines.slice(0, 4)).toEqual([
@@ -334,54 +336,52 @@ describe('--restart-tries', () => {
334336
describe('--kill-others', () => {
335337
describe('kills on success', () => {
336338
it.each(['--kill-others', '-k'])('%s', async (arg) => {
337-
const lines = await run(`${arg} "node fixtures/sleep.mjs 10" "exit 0"`).getLogLines();
339+
const lines = await run(`${arg} "node fixtures/sleep.js 10" "exit 0"`).getLogLines();
338340

339341
expect(lines).toContainEqual(expect.stringContaining('[1] exit 0 exited with code 0'));
340342
expect(lines).toContainEqual(
341343
expect.stringContaining('Sending SIGTERM to other processes'),
342344
);
343345
expect(lines).toContainEqual(
344346
expect.stringMatching(
345-
createKillMessage('[0] node fixtures/sleep.mjs 10', 'SIGTERM'),
347+
createKillMessage('[0] node fixtures/sleep.js 10', 'SIGTERM'),
346348
),
347349
);
348350
});
349351
});
350352

351353
it('kills on failure', async () => {
352-
const lines = await run(
353-
'--kill-others "node fixtures/sleep.mjs 10" "exit 1"',
354-
).getLogLines();
354+
const lines = await run('--kill-others "node fixtures/sleep.js 10" "exit 1"').getLogLines();
355355

356356
expect(lines).toContainEqual(expect.stringContaining('[1] exit 1 exited with code 1'));
357357
expect(lines).toContainEqual(expect.stringContaining('Sending SIGTERM to other processes'));
358358
expect(lines).toContainEqual(
359-
expect.stringMatching(createKillMessage('[0] node fixtures/sleep.mjs 10', 'SIGTERM')),
359+
expect.stringMatching(createKillMessage('[0] node fixtures/sleep.js 10', 'SIGTERM')),
360360
);
361361
});
362362
});
363363

364364
describe('--kill-others-on-fail', () => {
365365
it('does not kill on success', async () => {
366366
const lines = await run(
367-
'--kill-others-on-fail "node fixtures/sleep.mjs 0.5" "exit 0"',
367+
'--kill-others-on-fail "node fixtures/sleep.js 0.5" "exit 0"',
368368
).getLogLines();
369369

370370
expect(lines).toContainEqual(expect.stringContaining('[1] exit 0 exited with code 0'));
371371
expect(lines).toContainEqual(
372-
expect.stringContaining('[0] node fixtures/sleep.mjs 0.5 exited with code 0'),
372+
expect.stringContaining('[0] node fixtures/sleep.js 0.5 exited with code 0'),
373373
);
374374
});
375375

376376
it('kills on failure', async () => {
377377
const lines = await run(
378-
'--kill-others-on-fail "node fixtures/sleep.mjs 10" "exit 1"',
378+
'--kill-others-on-fail "node fixtures/sleep.js 10" "exit 1"',
379379
).getLogLines();
380380

381381
expect(lines).toContainEqual(expect.stringContaining('[1] exit 1 exited with code 1'));
382382
expect(lines).toContainEqual(expect.stringContaining('Sending SIGTERM to other processes'));
383383
expect(lines).toContainEqual(
384-
expect.stringMatching(createKillMessage('[0] node fixtures/sleep.mjs 10', 'SIGTERM')),
384+
expect.stringMatching(createKillMessage('[0] node fixtures/sleep.js 10', 'SIGTERM')),
385385
);
386386
});
387387
});
@@ -482,8 +482,8 @@ describe('--timings', () => {
482482
const tableBottomBorderRegex = /[]+/g;
483483

484484
const timingsTests = {
485-
'shows timings on success': ['node fixtures/sleep.mjs 0.5', 'exit 0'],
486-
'shows timings on failure': ['node fixtures/sleep.mjs 0.75', 'exit 1'],
485+
'shows timings on success': ['node fixtures/sleep.js 0.5', 'exit 0'],
486+
'shows timings on failure': ['node fixtures/sleep.js 0.75', 'exit 1'],
487487
};
488488
it.each(Object.entries(timingsTests))('%s', async (_, commands) => {
489489
const lines = await run(

bin/concurrently.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import yargs from 'yargs';
33
import { hideBin } from 'yargs/helpers';
44

5-
import { assertDeprecated } from '../src/assert';
6-
import * as defaults from '../src/defaults';
7-
import { concurrently } from '../src/index';
8-
import { castArray } from '../src/utils';
9-
import { readPackage } from './read-package';
5+
import { assertDeprecated } from '../src/assert.js';
6+
import * as defaults from '../src/defaults.js';
7+
import { concurrently } from '../src/index.js';
8+
import { castArray } from '../src/utils.js';
9+
import { readPackageJson } from './read-package-json.js';
1010

11-
const version = String(readPackage().version);
11+
const version = String(readPackageJson().version);
1212
const epilogue = `For documentation and more examples, visit:\nhttps://github.com/open-cli-tools/concurrently/tree/v${version}/docs`;
1313

1414
// Clean-up arguments (yargs expects only the arguments after the program name)
File renamed without changes.

bin/read-package-json.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { readFileSync } from 'node:fs';
2+
import { createRequire } from 'node:module';
3+
4+
/**
5+
* Read the package.json file of `concurrently`
6+
*/
7+
export function readPackageJson(): Record<string, unknown> {
8+
let resolver;
9+
try {
10+
resolver = require.resolve;
11+
} catch {
12+
resolver = createRequire(import.meta.url).resolve;
13+
}
14+
const path = resolver('concurrently/package.json');
15+
const content = readFileSync(path, 'utf8');
16+
return JSON.parse(content);
17+
}

bin/read-package.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

eslint.config.mjs renamed to eslint.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export default defineConfig(
1818
...globals.node,
1919
},
2020
ecmaVersion: 2023,
21-
sourceType: 'commonjs',
2221
},
2322
},
2423
eslint.configs.recommended,

index.d.mts

Lines changed: 0 additions & 7 deletions
This file was deleted.

index.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
/*
22
* While in local development, make sure you've run `pnpm run build` first.
33
*/
4+
45
import { concurrently } from './dist/src/index.js';
56

67
export * from './dist/src/index.js';
7-
// @ts-expect-error ignore the usage of `export =` along with `export default`.
8-
// This is seemingly fine, but the file needs to be included in the TS project, which can't be done
9-
// due to importing from `dist`. See https://stackoverflow.com/q/42609768/2083599
10-
export = concurrently;
118
export default concurrently;

index.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22
* While in local development, make sure you've run `pnpm run build` first.
33
*/
44

5-
// eslint-disable-next-line @typescript-eslint/no-require-imports
6-
const concurrently = require('./dist/src/index.js');
5+
import { concurrently } from './dist/src/index.js';
76

8-
// For require()
9-
module.exports = exports = concurrently.concurrently;
10-
11-
// For TS + import syntax; mimics `export default`
12-
exports.default = exports;
13-
14-
Object.assign(exports, concurrently);
7+
export * from './dist/src/index.js';
8+
export default concurrently;

0 commit comments

Comments
 (0)