1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var ui = require('../ui');
|
18 | var fs = require('graceful-fs');
|
19 | var mkdirp = require('mkdirp');
|
20 | var path = require('path');
|
21 | var lockFile = require('proper-lockfile');
|
22 | var dprepend = require('../common').dprepend;
|
23 | var readJSONSync = require('../common').readJSONSync;
|
24 | var stringify = require('../common').stringify;
|
25 | var HOME = require('../common').HOME;
|
26 | var dprepend = require('../common').dprepend;
|
27 |
|
28 | function GlobalConfigFile(homePath) {
|
29 | this.globalConfigFile = homePath + path.sep + '.jspm' + path.sep + 'config';
|
30 | this.config = null;
|
31 |
|
32 | this.registries = [];
|
33 |
|
34 |
|
35 |
|
36 | if (process.env.USERPROFILE && HOME !== process.env.USERPROFILE && !fs.existsSync(path.join(HOME, '.jspm')) && fs.existsSync(path.join(process.env.USERPROFILE, '.jspm'))) {
|
37 | var OLD_HOME = process.env.USERPROFILE;
|
38 | var from = path.join(OLD_HOME, '.jspm');
|
39 | var to = path.join(HOME, '.jspm');
|
40 | ui.log('info', 'Migrating global jspm folder from `' + from + '` to `' + to + '`...');
|
41 | try {
|
42 | ui.log('info', 'Copying configuration...');
|
43 | var oldConfig = fs.readFileSync(path.resolve(from, 'config'));
|
44 | mkdirp.sync(to);
|
45 | fs.writeFileSync(path.resolve(to, 'config'), oldConfig);
|
46 | ui.log('ok', 'Migration successful. Note that linked packages will need to be relinked.');
|
47 | }
|
48 | catch (e) {
|
49 | ui.log('err', 'Error migrating to new jspm folder\n' + (e && e.stack || e));
|
50 | }
|
51 | }
|
52 |
|
53 |
|
54 | this.lock();
|
55 |
|
56 |
|
57 | this.config = readJSONSync(this.globalConfigFile);
|
58 |
|
59 |
|
60 | if (!this.config.defaultRegistry && this.config.registry)
|
61 | this.config.defaultRegistry = this.config.registry;
|
62 | if (!this.config.registries && this.config.endpoints)
|
63 | this.config.registries = this.config.endpoints;
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | if (this.config.github) {
|
72 | dprepend(this.config.registries.github, this.config.github);
|
73 | delete this.config.github;
|
74 | }
|
75 |
|
76 |
|
77 | dprepend(this.config, {
|
78 | defaultRegistry: 'jspm',
|
79 | strictSSL: true,
|
80 | registries: {
|
81 | github: {
|
82 | handler: 'jspm-github',
|
83 | remote: 'https://github.jspm.io'
|
84 | },
|
85 | npm: {
|
86 | handler: 'jspm-npm',
|
87 | remote: 'https://npm.jspm.io'
|
88 | },
|
89 | jspm: {
|
90 | handler: 'jspm-registry',
|
91 | remote: 'https://registry.jspm.io'
|
92 | }
|
93 | }
|
94 | });
|
95 |
|
96 | this.save();
|
97 |
|
98 | this.unlock();
|
99 | }
|
100 |
|
101 | GlobalConfigFile.prototype.constructor = GlobalConfigFile;
|
102 |
|
103 | GlobalConfigFile.prototype.save = function() {
|
104 | try {
|
105 | mkdirp.sync(path.dirname(this.globalConfigFile));
|
106 | }
|
107 | catch (e) {
|
108 | if (e.code !== 'EEXIST')
|
109 | throw 'Unable to create jspm system folder\n' + e.stack;
|
110 | }
|
111 | try {
|
112 | this.lock();
|
113 | var existing = readJSONSync(this.globalConfigFile);
|
114 |
|
115 | if (JSON.stringify(existing) != JSON.stringify(this.config)) {
|
116 | fs.writeFileSync(this.globalConfigFile, stringify(this.config));
|
117 | }
|
118 | }
|
119 | catch (e) {
|
120 | ui.log('err', 'Unable to write global configuration file\n' + e.stack);
|
121 | }
|
122 | finally {
|
123 | this.unlock();
|
124 | }
|
125 | };
|
126 |
|
127 | GlobalConfigFile.prototype.set = function(name, val) {
|
128 | var nameParts = name.split('.');
|
129 |
|
130 | var config = this.config;
|
131 | var part;
|
132 | while (nameParts.length > 1) {
|
133 | part = nameParts.shift();
|
134 | config[part] = typeof config[part] === 'object' ? config[part] : {};
|
135 | config = config[part];
|
136 | }
|
137 | if (val !== undefined) {
|
138 | config[nameParts[0]] = val;
|
139 | }
|
140 | else {
|
141 |
|
142 | delete config[nameParts[0]];
|
143 | }
|
144 |
|
145 | this.save();
|
146 | };
|
147 |
|
148 | GlobalConfigFile.prototype.lock = function() {
|
149 | if (!this._unlock) {
|
150 | try {
|
151 | this._unlock = lockFile.lockSync(this.globalConfigFile, {
|
152 | retries: {
|
153 | retries: 10,
|
154 | minTimeout: 20,
|
155 | maxTimeout: 300,
|
156 | randomize: true
|
157 | },
|
158 | realpath: false
|
159 | });
|
160 | } catch (e) {
|
161 | if (e.code === 'ELOCKED')
|
162 | throw 'Unable to lock global config file %' + this.globalConfigFile + '%, not overwriting';
|
163 | }
|
164 | }
|
165 | };
|
166 | GlobalConfigFile.prototype.unlock = function() {
|
167 | if (this._unlock) {
|
168 | this._unlock();
|
169 | this._unlock = undefined;
|
170 | }
|
171 | };
|
172 |
|
173 |
|
174 |
|
175 | process
|
176 | .once('SIGINT', function () {
|
177 | process.exit(1);
|
178 | })
|
179 | .once('SIGTERM', function () {
|
180 | process.exit(1);
|
181 | });
|
182 |
|
183 | module.exports = new GlobalConfigFile(HOME);
|