-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
yarn-registry.js
141 lines (111 loc) · 3.94 KB
/
yarn-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
/* @flow */
import type Reporter from '../reporters/base-reporter.js';
import type RequestManager from '../util/request-manager.js';
import type {ConfigRegistries} from './index.js';
import {YARN_REGISTRY} from '../constants.js';
import NpmRegistry from './npm-registry.js';
import {stringify, parse} from '../lockfile';
import * as fs from '../util/fs.js';
import {version} from '../util/yarn-version.js';
const userHome = require('../util/user-home-dir').default;
const path = require('path');
export const DEFAULTS = {
'version-tag-prefix': 'v',
'version-git-tag': true,
'version-commit-hooks': true,
'version-git-sign': false,
'version-git-message': 'v%s',
'init-version': '1.0.0',
'init-license': 'MIT',
'save-prefix': '^',
'bin-links': true,
'ignore-scripts': false,
'ignore-optional': false,
registry: YARN_REGISTRY,
'strict-ssl': true,
'user-agent': [`yarn/${version}`, 'npm/?', `node/${process.version}`, process.platform, process.arch].join(' '),
};
const RELATIVE_KEYS = ['yarn-offline-mirror', 'cache-folder', 'global-folder', 'offline-cache-folder', 'yarn-path'];
const FOLDER_KEY = ['yarn-offline-mirror', 'cache-folder', 'global-folder', 'offline-cache-folder'];
const npmMap = {
'version-git-sign': 'sign-git-tag',
'version-tag-prefix': 'tag-version-prefix',
'version-git-tag': 'git-tag-version',
'version-commit-hooks': 'commit-hooks',
'version-git-message': 'message',
};
export default class YarnRegistry extends NpmRegistry {
constructor(
cwd: string,
registries: ConfigRegistries,
requestManager: RequestManager,
reporter: Reporter,
enableDefaultRc: boolean,
extraneousRcFiles: Array<string>,
) {
super(cwd, registries, requestManager, reporter, enableDefaultRc, extraneousRcFiles);
this.homeConfigLoc = path.join(userHome, '.yarnrc');
this.homeConfig = {};
}
static filename = 'yarn.json';
homeConfigLoc: string;
homeConfig: Object;
getOption(key: string): mixed {
let val = this.config[key];
// if this isn't set in a yarn config, then use npm
if (typeof val === 'undefined') {
val = this.registries.npm.getOption(npmMap[key]);
}
if (typeof val === 'undefined') {
val = this.registries.npm.getOption(key);
}
// if this isn't set in a yarn config or npm config, then use the default (or undefined)
if (typeof val === 'undefined') {
val = DEFAULTS[key];
}
return val;
}
async loadConfig(): Promise<void> {
const locations = await this.getPossibleConfigLocations('yarnrc', this.reporter);
for (const [isHome, loc, file] of locations) {
const {object: config} = parse(file, loc);
if (isHome) {
this.homeConfig = config;
}
for (const key of RELATIVE_KEYS) {
const valueLoc = config[key];
if (!this.config[key] && valueLoc) {
const resolvedLoc = (config[key] = path.resolve(path.dirname(loc), valueLoc));
if (FOLDER_KEY.includes(key)) {
await fs.mkdirp(resolvedLoc);
}
}
}
// merge with any existing environment variables
const env = config.env;
if (env) {
const existingEnv = this.config.env;
if (existingEnv) {
this.config.env = Object.assign({}, env, existingEnv);
}
}
this.config = Object.assign({}, config, this.config);
}
// default yarn config
this.config = Object.assign({}, DEFAULTS, this.config);
}
async saveHomeConfig(config: Object): Promise<void> {
YarnRegistry.normalizeConfig(config);
for (const key in config) {
const val = config[key];
// if the current config key was taken from home config then update
// the global config
if (this.homeConfig[key] === this.config[key]) {
this.config[key] = val;
}
// update just the home config
this.homeConfig[key] = config[key];
}
await fs.writeFilePreservingEol(this.homeConfigLoc, `${stringify(this.homeConfig)}\n`);
}
}