Skip to content

Commit c001bb2

Browse files
voxsimbestander
authored andcommitted
install package with file: protocol as default if it exists in the filesystem (#2723)
1 parent be9dca8 commit c001bb2

File tree

7 files changed

+48
-2
lines changed

7 files changed

+48
-2
lines changed

__tests__/commands/install/integration.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ test.concurrent('install file: protocol', (): Promise<void> => {
272272
});
273273
});
274274

275+
test.concurrent('install with file: protocol as default', (): Promise<void> => {
276+
return runInstall({noLockfile: true}, 'install-file-as-default', async (config) => {
277+
assert.equal(
278+
await fs.readFile(path.join(config.cwd, 'node_modules', 'foo', 'index.js')),
279+
'foobar\n',
280+
);
281+
});
282+
});
283+
275284
test.concurrent('install everything when flat is enabled', (): Promise<void> => {
276285
return runInstall({noLockfile: true, flat: true}, 'install-file', async (config) => {
277286
assert.equal(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foobar
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "bar",
3+
"version": "0.0.0",
4+
"main": "index.js"
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"foo": "bar"
4+
}
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
"foo@file:bar":
4+
version "0.0.0"

src/package-request.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import {entries} from './util/misc.js';
1414
import * as constants from './constants.js';
1515
import * as versionUtil from './util/version.js';
1616
import * as resolvers from './resolvers/index.js';
17+
import * as fs from './util/fs.js';
1718

19+
const path = require('path');
1820
const invariant = require('invariant');
1921
const semver = require('semver');
2022

@@ -98,7 +100,7 @@ export default class PackageRequest {
98100
*/
99101

100102
async findVersionOnRegistry(pattern: string): Promise<Manifest> {
101-
const {range, name} = PackageRequest.normalizePattern(pattern);
103+
const {range, name} = await this.normalize(pattern);
102104

103105
const exoticResolver = PackageRequest.getExoticResolver(range);
104106
if (exoticResolver) {
@@ -135,6 +137,26 @@ export default class PackageRequest {
135137
}
136138
}
137139

140+
async normalizeRange(pattern: string): Promise<string> {
141+
if (pattern.includes(':') ||
142+
pattern.includes('@') ||
143+
PackageRequest.getExoticResolver(pattern)) {
144+
return Promise.resolve(pattern);
145+
}
146+
147+
if (await fs.exists(path.join(this.config.cwd, pattern))) {
148+
return Promise.resolve(`file:${pattern}`);
149+
}
150+
151+
return Promise.resolve(pattern);
152+
}
153+
154+
async normalize(pattern: string): any {
155+
const {name, range, hasVersion} = PackageRequest.normalizePattern(pattern);
156+
const newRange = await this.normalizeRange(range);
157+
return {name, range: newRange, hasVersion};
158+
}
159+
138160
/**
139161
* Explode and normalize a pattern into it's name and range.
140162
*/

src/util/fs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const access: (path: string, mode?: number) => Promise<void> = promisify(
2222
export const stat: (path: string) => Promise<fs.Stats> = promisify(fs.stat);
2323
export const unlink: (path: string) => Promise<void> = promisify(require('rimraf'));
2424
export const mkdirp: (path: string) => Promise<void> = promisify(require('mkdirp'));
25-
export const exists: (path: string) => Promise<boolean> = promisify(fs.exists, true);
25+
export const exists: (path: string) => Promise<boolean> = promisify(fs.exists, true);
2626
export const lstat: (path: string) => Promise<fs.Stats> = promisify(fs.lstat);
2727
export const chmod: (path: string, mode: number | string) => Promise<void> = promisify(fs.chmod);
2828
export const link: (path: string) => Promise<fs.Stats> = promisify(fs.link);

0 commit comments

Comments
 (0)