Skip to content

Commit

Permalink
File url normalization (#1498)
Browse files Browse the repository at this point in the history
* normalize manifest in FileResolver

* add a comment
  • Loading branch information
xandris authored and bestander committed Nov 27, 2016
1 parent fa16b8b commit 1696029
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 1 deletion.
9 changes: 9 additions & 0 deletions __tests__/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ test.concurrent('root install with optional deps', (): Promise<void> => {
return runInstall({}, 'root-install-with-optional-dependency');
});

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

test.concurrent('install file: protocol', (): Promise<void> => {
return runInstall({noLockfile: true}, 'install-file', async (config) => {
assert.equal(
Expand Down
5 changes: 5 additions & 0 deletions __tests__/fixtures/install/install-file-relative/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"sub-a": "file:sub/sub-a"
}
}
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,4 @@
{
"name": "root-a",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "root-b",
"version": "1.0.0",
"dependencies": {
"root-a": "file:../root-a"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "sub-a",
"version": "1.0.0",
"dependencies": {
"root-b": "file:../../root-b"
}
}
40 changes: 39 additions & 1 deletion src/resolvers/exotics/file-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import * as fs from '../../util/fs.js';
const invariant = require('invariant');
const path = require('path');

type Dependencies = {
[key: string]: string
};

export default class FileResolver extends ExoticResolver {
constructor(request: PackageRequest, fragment: string) {
super(request, fragment);
Expand Down Expand Up @@ -42,6 +46,40 @@ export default class FileResolver extends ExoticResolver {

manifest._uid = manifest.version;

return manifest;
// Normalize relative paths; if anything changes, make a copy of the manifest
const dependencies = this.normalizeDependencyPaths(manifest.dependencies, loc);
const optionalDependencies = this.normalizeDependencyPaths(manifest.optionalDependencies, loc);

if (dependencies !== manifest.dependencies || optionalDependencies !== manifest.optionalDependencies) {
const _manifest = Object.assign({}, manifest);
if (dependencies != null) {
_manifest.dependencies = dependencies;
}
if (optionalDependencies != null) {
_manifest.optionalDependencies = optionalDependencies;
}
return _manifest;
} else {
return manifest;
}
}

normalizeDependencyPaths(section: ?Dependencies, loc: string): ?Dependencies {
if (section == null) {
return section;
}

let temp = section;

for (const [k, v] of util.entries(section)) {
if (typeof v === 'string' && v.startsWith('file:') && !path.isAbsolute(v)) {
if (temp === section) {
temp = Object.assign({}, section);
}
temp[k] = `file:${path.relative(this.config.cwd, path.join(loc, util.removePrefix(v, 'file:')))}`;
}
}

return temp;
}
}

0 comments on commit 1696029

Please sign in to comment.