forked from yarnpkg/yarn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yarn-registry.js
124 lines (98 loc) 路 3.4 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
/* @flow */
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 from '../lockfile/stringify.js';
import parse from '../lockfile/parse.js';
import * as fs from '../util/fs.js';
const userHome = require('../util/user-home-dir').default;
const path = require('path');
const pkg: { version: string } = require('../../package.json');
export const DEFAULTS = {
'version-tag-prefix': 'v',
'version-git-tag': true,
'version-git-sign': false,
'version-git-message': 'v%s',
'init-version': '1.0.0',
'init-license': 'MIT',
'save-prefix': '^',
'ignore-scripts': false,
'ignore-optional': false,
registry: YARN_REGISTRY,
'strict-ssl': true,
'user-agent': [
`yarn/${pkg.version}`,
'npm/?',
`node/${process.version}`,
process.platform,
process.arch,
].join(' '),
};
const npmMap = {
'version-git-sign': 'sign-git-tag',
'version-tag-prefix': 'tag-version-prefix',
'version-git-tag': 'git-tag-version',
'version-git-message': 'message',
};
export default class YarnRegistry extends NpmRegistry {
constructor(cwd: string, registries: ConfigRegistries, requestManager: RequestManager) {
super(cwd, registries, requestManager);
if (process.env.XDG_CONFIG_HOME) {
this.homeConfigLoc = path.join(process.env.XDG_CONFIG_HOME, '.yarnrc');
} else {
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> {
for (const [isHome, loc, file] of await this.getPossibleConfigLocations('.yarnrc')) {
const config = parse(file, loc);
if (isHome) {
this.homeConfig = config;
}
// normalize offline mirror path relative to the current yarnrc
const offlineLoc = config['yarn-offline-mirror'];
// don't normalize if we already have a mirror path
if (!this.config['yarn-offline-mirror'] && offlineLoc) {
const mirrorLoc = config['yarn-offline-mirror'] = path.resolve(path.dirname(loc), offlineLoc);
await fs.mkdirp(mirrorLoc);
}
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`);
}
}