Skip to content

Commit

Permalink
install package with file: protocol as default if it exists in the fi…
Browse files Browse the repository at this point in the history
…lesystem
  • Loading branch information
voxsim committed Feb 19, 2017
1 parent 7182302 commit b7cc7d5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 3 deletions.
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} = await 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
*/
async normalizeRange(pattern: string): Promise<string> {
if (pattern.includes(':')) {
return Promise.resolve(pattern);
}

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

return Promise.resolve(pattern);
}

async normalize(pattern: string): any {
const {name, range, hasVersion} = PackageRequest.normalizePattern(pattern);
const new_range = await 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} = await this.normalize(this.pattern);
const resolved: ?Manifest = this.resolver.getHighestRangeVersionMatch(name, range);
if (resolved) {
this.reportResolvedRangeMatch(info, resolved);
Expand Down
2 changes: 1 addition & 1 deletion src/util/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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);
Expand Down

0 comments on commit b7cc7d5

Please sign in to comment.