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
2 changes: 1 addition & 1 deletion docs/cli/shortcuts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Command Shortcuts

Package managers that execute scripts from a `package.json` or `deno.(json|jsonc)` file can be shortened when in concurrently.<br/>
Package managers that execute scripts from a `package.json` / `package.json5` or `deno.(json|jsonc)` file can be shortened when in concurrently.<br/>
The following are supported:

| Syntax | Expands to |
Expand Down
44 changes: 44 additions & 0 deletions lib/command-parser/expand-wildcard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ describe('#readPackage()', () => {
name: 'concurrently',
version: '6.4.0',
};
vi.spyOn(fs, 'existsSync').mockImplementation((path: PathOrFileDescriptor) => {
return path === 'package.json';
});
vi.spyOn(fs, 'readFileSync').mockImplementation((path: PathOrFileDescriptor) => {
if (path === 'package.json') {
return JSON.stringify(expectedPackage);
Expand All @@ -118,6 +121,47 @@ describe('#readPackage()', () => {
expect(() => ExpandWildcard.readPackage()).not.toThrow();
expect(ExpandWildcard.readPackage()).toEqual({});
});

it('can read package.json5', () => {
const expectedPackage = {
name: 'concurrently',
version: '6.4.0',
scripts: {
'test:unit': '',
},
};
vi.spyOn(fs, 'existsSync').mockImplementation((path: PathOrFileDescriptor) => {
return path === 'package.json5';
});
vi.spyOn(fs, 'readFileSync').mockImplementation((path: PathOrFileDescriptor) => {
if (path === 'package.json5') {
return `/* comment */\n${JSON.stringify(expectedPackage)}`;
}
return '';
});

const actualReadPackage = ExpandWildcard.readPackage();
expect(actualReadPackage).toEqual(expectedPackage);
});

it('prefers package.json over package.json5', () => {
const expectedPackage = {
name: 'from-json',
version: '1.0.0',
};
vi.spyOn(fs, 'existsSync').mockImplementation((path: PathOrFileDescriptor) => {
return path === 'package.json' || path === 'package.json5';
});
vi.spyOn(fs, 'readFileSync').mockImplementation((path: PathOrFileDescriptor) => {
if (path === 'package.json') {
return JSON.stringify(expectedPackage);
}
return '';
});

const actualReadPackage = ExpandWildcard.readPackage();
expect(actualReadPackage).toEqual(expectedPackage);
});
});

it('returns same command if not an npm run command', () => {
Expand Down
11 changes: 9 additions & 2 deletions lib/command-parser/expand-wildcard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ export class ExpandWildcard implements CommandParser {

static readPackage() {
try {
const json = fs.readFileSync('package.json', { encoding: 'utf-8' });
return JSON.parse(json);
let json: string = '{}';

if (fs.existsSync('package.json')) {
json = fs.readFileSync('package.json', { encoding: 'utf-8' });
} else if (fs.existsSync('package.json5')) {
json = fs.readFileSync('package.json5', { encoding: 'utf-8' });
}

return JSONC.parse(json);
} catch {
return {};
}
Expand Down
Loading