-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathbase-registry.js
180 lines (145 loc) · 3.98 KB
/
base-registry.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* @flow */
import type Reporter from '../reporters/base-reporter.js';
import type RequestManager, {RequestMethods} from '../util/request-manager.js';
import type Config from '../config.js';
import type {ConfigRegistries} from './index.js';
import {removePrefix} from '../util/misc.js';
const objectPath = require('object-path');
const path = require('path');
export type RegistryRequestOptions = {
method?: RequestMethods,
auth?: Object,
body?: mixed,
buffer?: boolean,
headers?: Object,
process?: Function,
registry?: string,
unfiltered?: boolean,
};
export type CheckOutdatedReturn = Promise<{
wanted: string,
latest: string,
url: string,
}>;
export default class BaseRegistry {
constructor(
cwd: string,
registries: ConfigRegistries,
requestManager: RequestManager,
reporter: Reporter,
enableDefaultRc: boolean,
extraneousRcFiles: Array<string>,
) {
this.reporter = reporter;
this.requestManager = requestManager;
this.registries = registries;
this.config = {};
this.folder = '';
this.token = '';
this.loc = '';
this.cwd = cwd;
this.enableDefaultRc = enableDefaultRc;
this.extraneousRcFiles = extraneousRcFiles;
}
// the filename to use for package metadata
static filename: string;
//
enableDefaultRc: boolean;
extraneousRcFiles: Array<string>;
//
reporter: Reporter;
//
registries: ConfigRegistries;
//
requestManager: RequestManager;
//
token: string;
//
otp: string;
//
cwd: string;
//
config: Object;
// absolute folder name to insert modules
loc: string;
// relative folder name to put these modules
folder: string;
setToken(token: string) {
this.token = token;
}
setOtp(otp: string) {
this.otp = otp;
}
getOption(key: string): mixed {
return this.config[key];
}
getAvailableRegistries(): Array<string> {
const config = this.config;
return Object.keys(config).reduce((registries, option) => {
if (option === 'registry' || option.split(':')[1] === 'registry') {
registries.push(config[option]);
}
return registries;
}, []);
}
loadConfig(): Promise<void> {
return Promise.resolve();
}
checkOutdated(config: Config, name: string, range: string): CheckOutdatedReturn {
return Promise.reject(new Error('unimplemented'));
}
saveHomeConfig(config: Object): Promise<void> {
return Promise.reject(new Error('unimplemented'));
}
request(pathname: string, opts?: RegistryRequestOptions = {}): Promise<*> {
return this.requestManager.request({
url: pathname,
...opts,
});
}
async init(overrides: Object = {}): Promise<void> {
this.mergeEnv('yarn_');
await this.loadConfig();
for (const override of Object.keys(overrides)) {
const val = overrides[override];
if (val !== undefined) {
this.config[override] = val;
}
}
this.loc = path.join(this.cwd, this.folder);
}
static normalizeConfig(config: Object): Object {
for (const key in config) {
config[key] = BaseRegistry.normalizeConfigOption(config[key]);
}
return config;
}
static normalizeConfigOption(val: any): any {
if (val === 'true') {
return true;
} else if (val === 'false') {
return false;
} else {
return val;
}
}
mergeEnv(prefix: string) {
// try environment variables
for (const envKey in process.env) {
let key = envKey.toLowerCase();
// only accept keys prefixed with the prefix
if (key.indexOf(prefix.toLowerCase()) !== 0) {
continue;
}
const val = BaseRegistry.normalizeConfigOption(process.env[envKey]);
// remove config prefix
key = removePrefix(key, prefix.toLowerCase());
// replace dunders with dots
key = key.replace(/__/g, '.');
// replace underscores with dashes ignoring keys that start with an underscore
key = key.replace(/([^_])_/g, '$1-');
// set it via a path
objectPath.set(this.config, key, val);
}
}
}