Skip to content

Commit 51c2244

Browse files
committed
feat: throw when repository does not match package name
1 parent 5c2fefd commit 51c2244

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

lib/loader.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,21 @@ internals.createPackageLoader = async (packageName) => {
3636

3737
const repository = internals.parseRepository(packument);
3838

39-
return internals.createRepositoryLoader(repository);
39+
const repositoryLoader = internals.createRepositoryLoader(repository);
40+
41+
return {
42+
...repositoryLoader,
43+
loadFile: async (filename, options) => {
44+
45+
const result = await repositoryLoader.loadFile(filename, options);
46+
47+
if (filename === 'package.json' && result.name !== packageName) {
48+
throw new Error(`${repository} does not contain ${packageName}`);
49+
}
50+
51+
return result;
52+
}
53+
};
4054
}
4155
catch (err) {
4256

@@ -64,7 +78,7 @@ internals.createRepositoryLoader = (repository) => {
6478

6579
return head;
6680
},
67-
loadFile: async (filename) => {
81+
loadFile: async (filename, options) => {
6882

6983
if (parsedRepository.source !== 'github.com') {
7084
throw new Error('Only github.com paths supported, feel free to PR at https://github.com/pkgjs/node-support');
@@ -73,7 +87,7 @@ internals.createRepositoryLoader = (repository) => {
7387
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/${filename}`;
7488

7589
try {
76-
const { payload } = await Wreck.get(url);
90+
const { payload } = await Wreck.get(url, options);
7791

7892
return payload;
7993
}
@@ -108,15 +122,21 @@ internals.createPathLoader = async (path) => {
108122

109123
return simpleGit.revparse(['HEAD']);
110124
},
111-
loadFile: (filename) => {
125+
loadFile: (filename, options = {}) => {
112126

113127
const fullPath = Path.join(path, filename);
114128

115129
if (!Fs.existsSync(fullPath)) {
116130
return;
117131
}
118132

119-
return Fs.readFileSync(fullPath);
133+
const buffer = Fs.readFileSync(fullPath);
134+
135+
if (options.json) {
136+
return JSON.parse(buffer.toString());
137+
}
138+
139+
return buffer;
120140
}
121141
};
122142
};

lib/package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ exports.detect = async ({ path, repository, packageName }) => {
77

88
const { loadFile, getCommit } = await Loader.create({ path, repository, packageName });
99

10-
const packageJson = JSON.parse((await loadFile('package.json')).toString());
10+
const packageJson = await loadFile('package.json', { json: 'force' });
1111

1212
const { name, version, engines } = packageJson;
1313

test/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,25 @@ describe('node-support', () => {
583583
engines: '>=10'
584584
});
585585
});
586+
587+
it('throws when repo package name does not match', async () => {
588+
589+
listRemoteStub
590+
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');
591+
592+
Nock('https://raw.githubusercontent.com')
593+
.get('/pkgjs/node-support/HEAD/package.json')
594+
.reply(200, JSON.stringify({ name: 'something-else' }))
595+
.get('/pkgjs/node-support/HEAD/.travis.yml')
596+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')));
597+
598+
Nock('https://registry.npmjs.org')
599+
.get('/node-support')
600+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json')));
601+
602+
await expect(NodeSupport.detect({ packageName: 'node-support' }))
603+
.to.reject('git+https://github.com/pkgjs/node-support.git does not contain node-support');
604+
});
586605
});
587606
});
588607
});

0 commit comments

Comments
 (0)