Skip to content

Commit dbb5617

Browse files
authored
fix: expand wildcards from package.json5 when package.json is missing (#608)
1 parent 9f90a1a commit dbb5617

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

‎docs/cli/shortcuts.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Command Shortcuts
22

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

66
| Syntax | Expands to |

‎lib/command-parser/expand-wildcard.spec.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ describe('#readPackage()', () => {
9999
name: 'concurrently',
100100
version: '6.4.0',
101101
};
102+
vi.spyOn(fs, 'existsSync').mockImplementation((path: PathOrFileDescriptor) => {
103+
return path === 'package.json';
104+
});
102105
vi.spyOn(fs, 'readFileSync').mockImplementation((path: PathOrFileDescriptor) => {
103106
if (path === 'package.json') {
104107
return JSON.stringify(expectedPackage);
@@ -118,6 +121,47 @@ describe('#readPackage()', () => {
118121
expect(() => ExpandWildcard.readPackage()).not.toThrow();
119122
expect(ExpandWildcard.readPackage()).toEqual({});
120123
});
124+
125+
it('can read package.json5', () => {
126+
const expectedPackage = {
127+
name: 'concurrently',
128+
version: '6.4.0',
129+
scripts: {
130+
'test:unit': '',
131+
},
132+
};
133+
vi.spyOn(fs, 'existsSync').mockImplementation((path: PathOrFileDescriptor) => {
134+
return path === 'package.json5';
135+
});
136+
vi.spyOn(fs, 'readFileSync').mockImplementation((path: PathOrFileDescriptor) => {
137+
if (path === 'package.json5') {
138+
return `/* comment */\n${JSON.stringify(expectedPackage)}`;
139+
}
140+
return '';
141+
});
142+
143+
const actualReadPackage = ExpandWildcard.readPackage();
144+
expect(actualReadPackage).toEqual(expectedPackage);
145+
});
146+
147+
it('prefers package.json over package.json5', () => {
148+
const expectedPackage = {
149+
name: 'from-json',
150+
version: '1.0.0',
151+
};
152+
vi.spyOn(fs, 'existsSync').mockImplementation((path: PathOrFileDescriptor) => {
153+
return path === 'package.json' || path === 'package.json5';
154+
});
155+
vi.spyOn(fs, 'readFileSync').mockImplementation((path: PathOrFileDescriptor) => {
156+
if (path === 'package.json') {
157+
return JSON.stringify(expectedPackage);
158+
}
159+
return '';
160+
});
161+
162+
const actualReadPackage = ExpandWildcard.readPackage();
163+
expect(actualReadPackage).toEqual(expectedPackage);
164+
});
121165
});
122166

123167
it('returns same command if not an npm run command', () => {

‎lib/command-parser/expand-wildcard.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ export class ExpandWildcard implements CommandParser {
3232

3333
static readPackage() {
3434
try {
35-
const json = fs.readFileSync('package.json', { encoding: 'utf-8' });
36-
return JSON.parse(json);
35+
let json: string = '{}';
36+
37+
if (fs.existsSync('package.json')) {
38+
json = fs.readFileSync('package.json', { encoding: 'utf-8' });
39+
} else if (fs.existsSync('package.json5')) {
40+
json = fs.readFileSync('package.json5', { encoding: 'utf-8' });
41+
}
42+
43+
return JSONC.parse(json);
3744
} catch {
3845
return {};
3946
}

0 commit comments

Comments
 (0)