-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
file-resolver.js
86 lines (71 loc) · 2.35 KB
/
file-resolver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* @flow */
import path from 'path';
import invariant from 'invariant';
import uuid from 'uuid';
import type {Manifest} from '../../types.js';
import type PackageRequest from '../../package-request.js';
import type {RegistryNames} from '../../registries/index.js';
import {MessageError} from '../../errors.js';
import ExoticResolver from './exotic-resolver.js';
import * as util from '../../util/misc.js';
import * as fs from '../../util/fs.js';
export const FILE_PROTOCOL_PREFIX = 'file:';
export default class FileResolver extends ExoticResolver {
constructor(request: PackageRequest, fragment: string) {
super(request, fragment);
this.loc = util.removePrefix(fragment, FILE_PROTOCOL_PREFIX);
}
loc: string;
static protocol = 'file';
static prefixMatcher = /^\.{1,2}\//;
static isVersion(pattern: string): boolean {
return super.isVersion.call(this, pattern) || this.prefixMatcher.test(pattern) || path.isAbsolute(pattern);
}
async resolve(): Promise<Manifest> {
let loc = this.loc;
if (!path.isAbsolute(loc)) {
loc = path.resolve(this.config.lockfileFolder, loc);
}
if (this.config.linkFileDependencies) {
const registry: RegistryNames = 'npm';
const manifest: Manifest = {_uid: '', name: '', version: '0.0.0', _registry: registry};
manifest._remote = {
type: 'link',
registry,
hash: null,
reference: loc,
};
manifest._uid = manifest.version;
return manifest;
}
if (!await fs.exists(loc)) {
throw new MessageError(this.reporter.lang('doesntExist', loc, this.pattern.split('@')[0]));
}
const manifest: Manifest = await (async () => {
try {
return await this.config.readManifest(loc, this.registry);
} catch (e) {
if (e.code === 'ENOENT') {
return {
// This is just the default, it can be overridden with key of dependencies
name: path.dirname(loc),
version: '0.0.0',
_uid: '0.0.0',
_registry: 'npm',
};
}
throw e;
}
})();
const registry = manifest._registry;
invariant(registry, 'expected registry');
manifest._remote = {
type: 'copy',
registry,
hash: `${uuid.v4()}-${new Date().getTime()}`,
reference: loc,
};
manifest._uid = manifest.version;
return manifest;
}
}