Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install package with file: protocol as default if it exists in the filesystem #2684

Closed
Closed
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
9 changes: 9 additions & 0 deletions __tests__/commands/install/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,15 @@ test.concurrent('install file: protocol', (): Promise<void> => {
});
});

test.concurrent('install with file: protocol as default', (): Promise<void> => {
return runInstall({noLockfile: true}, 'install-file-as-default', async (config) => {
assert.equal(
await fs.readFile(path.join(config.cwd, 'node_modules', 'foo', 'index.js')),
'foobar\n',
);
});
});

test.concurrent('install everything when flat is enabled', (): Promise<void> => {
return runInstall({noLockfile: true, flat: true}, 'install-file', async (config) => {
assert.equal(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foobar
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "bar",
"version": "0.0.0",
"main": "index.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"foo": "bar"
}
}
4 changes: 4 additions & 0 deletions __tests__/fixtures/install/install-file-as-default/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"foo@file:bar":
version "0.0.0"
30 changes: 28 additions & 2 deletions src/package-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import {entries} from './util/misc.js';
import * as constants from './constants.js';
import * as versionUtil from './util/version.js';
import * as resolvers from './resolvers/index.js';
import * as fs from './util/fs.js';

const path = require('path');
const invariant = require('invariant');
const semver = require('semver');

Expand Down Expand Up @@ -98,7 +100,7 @@ export default class PackageRequest {
*/

async findVersionOnRegistry(pattern: string): Promise<Manifest> {
const {range, name} = PackageRequest.normalizePattern(pattern);
const {range, name} = this.normalize(pattern);

const exoticResolver = PackageRequest.getExoticResolver(range);
if (exoticResolver) {
Expand Down Expand Up @@ -135,6 +137,30 @@ export default class PackageRequest {
}
}

/**
* If pattern:
* - contains : it will return because is something like '<protocol>:<something else' or an url
* - is a file it will prepend file:
* otherwise it will return
*/
normalizeRange(pattern: string): string {
if (pattern.includes(':')) {
return pattern;
}

if (fs.existsSync(path.join(this.config.cwd, pattern))) {
return `file:${pattern}`;
}

return pattern;
}

normalize(pattern: string): any {
const {name, range, hasVersion} = PackageRequest.normalizePattern(pattern);
const new_range = this.normalizeRange(range);
return {name, range: new_range, hasVersion};
}

/**
* Explode and normalize a pattern into it's name and range.
*/
Expand Down Expand Up @@ -219,7 +245,7 @@ export default class PackageRequest {

// check if while we were resolving this dep we've already resolved one that satisfies
// the same range
const {range, name} = PackageRequest.normalizePattern(this.pattern);
const {range, name} = this.normalize(this.pattern);
const resolved: ?Manifest = this.resolver.getHighestRangeVersionMatch(name, range);
if (resolved) {
this.reportResolvedRangeMatch(info, resolved);
Expand Down
3 changes: 2 additions & 1 deletion src/util/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ export const access: (path: string, mode?: number) => Promise<void> = promisify(
export const stat: (path: string) => Promise<fs.Stats> = promisify(fs.stat);
export const unlink: (path: string) => Promise<void> = promisify(require('rimraf'));
export const mkdirp: (path: string) => Promise<void> = promisify(require('mkdirp'));
export const exists: (path: string) => Promise<boolean> = promisify(fs.exists, true);
export const exists: (path: string) => Promise<boolean> = promisify(fs.exists, true);
export const lstat: (path: string) => Promise<fs.Stats> = promisify(fs.lstat);
export const chmod: (path: string, mode: number | string) => Promise<void> = promisify(fs.chmod);
export const link: (path: string) => Promise<fs.Stats> = promisify(fs.link);
export const existsSync: (path: string) => boolean = fs.existsSync;

const CONCURRENT_QUEUE_ITEMS = 4;

Expand Down