massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-json / node_modules / vscode-languageserver / lib / node / files.js
1 "use strict";
2 /* --------------------------------------------------------------------------------------------
3  * Copyright (c) Microsoft Corporation. All rights reserved.
4  * Licensed under the MIT License. See License.txt in the project root for license information.
5  * ------------------------------------------------------------------------------------------ */
6 Object.defineProperty(exports, "__esModule", { value: true });
7 exports.resolveModulePath = exports.FileSystem = exports.resolveGlobalYarnPath = exports.resolveGlobalNodePath = exports.resolve = exports.uriToFilePath = void 0;
8 const url = require("url");
9 const path = require("path");
10 const fs = require("fs");
11 const child_process_1 = require("child_process");
12 /**
13  * @deprecated Use the `vscode-uri` npm module which provides a more
14  * complete implementation of handling VS Code URIs.
15  */
16 function uriToFilePath(uri) {
17     let parsed = url.parse(uri);
18     if (parsed.protocol !== 'file:' || !parsed.path) {
19         return undefined;
20     }
21     let segments = parsed.path.split('/');
22     for (var i = 0, len = segments.length; i < len; i++) {
23         segments[i] = decodeURIComponent(segments[i]);
24     }
25     if (process.platform === 'win32' && segments.length > 1) {
26         let first = segments[0];
27         let second = segments[1];
28         // Do we have a drive letter and we started with a / which is the
29         // case if the first segement is empty (see split above)
30         if (first.length === 0 && second.length > 1 && second[1] === ':') {
31             // Remove first slash
32             segments.shift();
33         }
34     }
35     return path.normalize(segments.join('/'));
36 }
37 exports.uriToFilePath = uriToFilePath;
38 function isWindows() {
39     return process.platform === 'win32';
40 }
41 function resolve(moduleName, nodePath, cwd, tracer) {
42     const nodePathKey = 'NODE_PATH';
43     const app = [
44         'var p = process;',
45         'p.on(\'message\',function(m){',
46         'if(m.c===\'e\'){',
47         'p.exit(0);',
48         '}',
49         'else if(m.c===\'rs\'){',
50         'try{',
51         'var r=require.resolve(m.a);',
52         'p.send({c:\'r\',s:true,r:r});',
53         '}',
54         'catch(err){',
55         'p.send({c:\'r\',s:false});',
56         '}',
57         '}',
58         '});'
59     ].join('');
60     return new Promise((resolve, reject) => {
61         let env = process.env;
62         let newEnv = Object.create(null);
63         Object.keys(env).forEach(key => newEnv[key] = env[key]);
64         if (nodePath && fs.existsSync(nodePath) /* see issue 545 */) {
65             if (newEnv[nodePathKey]) {
66                 newEnv[nodePathKey] = nodePath + path.delimiter + newEnv[nodePathKey];
67             }
68             else {
69                 newEnv[nodePathKey] = nodePath;
70             }
71             if (tracer) {
72                 tracer(`NODE_PATH value is: ${newEnv[nodePathKey]}`);
73             }
74         }
75         newEnv['ELECTRON_RUN_AS_NODE'] = '1';
76         try {
77             let cp = child_process_1.fork('', [], {
78                 cwd: cwd,
79                 env: newEnv,
80                 execArgv: ['-e', app]
81             });
82             if (cp.pid === void 0) {
83                 reject(new Error(`Starting process to resolve node module  ${moduleName} failed`));
84                 return;
85             }
86             cp.on('error', (error) => {
87                 reject(error);
88             });
89             cp.on('message', (message) => {
90                 if (message.c === 'r') {
91                     cp.send({ c: 'e' });
92                     if (message.s) {
93                         resolve(message.r);
94                     }
95                     else {
96                         reject(new Error(`Failed to resolve module: ${moduleName}`));
97                     }
98                 }
99             });
100             let message = {
101                 c: 'rs',
102                 a: moduleName
103             };
104             cp.send(message);
105         }
106         catch (error) {
107             reject(error);
108         }
109     });
110 }
111 exports.resolve = resolve;
112 /**
113  * Resolve the global npm package path.
114  * @deprecated Since this depends on the used package manager and their version the best is that servers
115  * implement this themselves since they know best what kind of package managers to support.
116  * @param tracer the tracer to use
117  */
118 function resolveGlobalNodePath(tracer) {
119     let npmCommand = 'npm';
120     const env = Object.create(null);
121     Object.keys(process.env).forEach(key => env[key] = process.env[key]);
122     env['NO_UPDATE_NOTIFIER'] = 'true';
123     const options = {
124         encoding: 'utf8',
125         env
126     };
127     if (isWindows()) {
128         npmCommand = 'npm.cmd';
129         options.shell = true;
130     }
131     let handler = () => { };
132     try {
133         process.on('SIGPIPE', handler);
134         let stdout = child_process_1.spawnSync(npmCommand, ['config', 'get', 'prefix'], options).stdout;
135         if (!stdout) {
136             if (tracer) {
137                 tracer(`'npm config get prefix' didn't return a value.`);
138             }
139             return undefined;
140         }
141         let prefix = stdout.trim();
142         if (tracer) {
143             tracer(`'npm config get prefix' value is: ${prefix}`);
144         }
145         if (prefix.length > 0) {
146             if (isWindows()) {
147                 return path.join(prefix, 'node_modules');
148             }
149             else {
150                 return path.join(prefix, 'lib', 'node_modules');
151             }
152         }
153         return undefined;
154     }
155     catch (err) {
156         return undefined;
157     }
158     finally {
159         process.removeListener('SIGPIPE', handler);
160     }
161 }
162 exports.resolveGlobalNodePath = resolveGlobalNodePath;
163 /*
164  * Resolve the global yarn pakage path.
165  * @deprecated Since this depends on the used package manager and their version the best is that servers
166  * implement this themselves since they know best what kind of package managers to support.
167  * @param tracer the tracer to use
168  */
169 function resolveGlobalYarnPath(tracer) {
170     let yarnCommand = 'yarn';
171     let options = {
172         encoding: 'utf8'
173     };
174     if (isWindows()) {
175         yarnCommand = 'yarn.cmd';
176         options.shell = true;
177     }
178     let handler = () => { };
179     try {
180         process.on('SIGPIPE', handler);
181         let results = child_process_1.spawnSync(yarnCommand, ['global', 'dir', '--json'], options);
182         let stdout = results.stdout;
183         if (!stdout) {
184             if (tracer) {
185                 tracer(`'yarn global dir' didn't return a value.`);
186                 if (results.stderr) {
187                     tracer(results.stderr);
188                 }
189             }
190             return undefined;
191         }
192         let lines = stdout.trim().split(/\r?\n/);
193         for (let line of lines) {
194             try {
195                 let yarn = JSON.parse(line);
196                 if (yarn.type === 'log') {
197                     return path.join(yarn.data, 'node_modules');
198                 }
199             }
200             catch (e) {
201                 // Do nothing. Ignore the line
202             }
203         }
204         return undefined;
205     }
206     catch (err) {
207         return undefined;
208     }
209     finally {
210         process.removeListener('SIGPIPE', handler);
211     }
212 }
213 exports.resolveGlobalYarnPath = resolveGlobalYarnPath;
214 var FileSystem;
215 (function (FileSystem) {
216     let _isCaseSensitive = undefined;
217     function isCaseSensitive() {
218         if (_isCaseSensitive !== void 0) {
219             return _isCaseSensitive;
220         }
221         if (process.platform === 'win32') {
222             _isCaseSensitive = false;
223         }
224         else {
225             // convert current file name to upper case / lower case and check if file exists
226             // (guards against cases when name is already all uppercase or lowercase)
227             _isCaseSensitive = !fs.existsSync(__filename.toUpperCase()) || !fs.existsSync(__filename.toLowerCase());
228         }
229         return _isCaseSensitive;
230     }
231     FileSystem.isCaseSensitive = isCaseSensitive;
232     function isParent(parent, child) {
233         if (isCaseSensitive()) {
234             return path.normalize(child).indexOf(path.normalize(parent)) === 0;
235         }
236         else {
237             return path.normalize(child).toLowerCase().indexOf(path.normalize(parent).toLowerCase()) === 0;
238         }
239     }
240     FileSystem.isParent = isParent;
241 })(FileSystem = exports.FileSystem || (exports.FileSystem = {}));
242 function resolveModulePath(workspaceRoot, moduleName, nodePath, tracer) {
243     if (nodePath) {
244         if (!path.isAbsolute(nodePath)) {
245             nodePath = path.join(workspaceRoot, nodePath);
246         }
247         return resolve(moduleName, nodePath, nodePath, tracer).then((value) => {
248             if (FileSystem.isParent(nodePath, value)) {
249                 return value;
250             }
251             else {
252                 return Promise.reject(new Error(`Failed to load ${moduleName} from node path location.`));
253             }
254         }).then(undefined, (_error) => {
255             return resolve(moduleName, resolveGlobalNodePath(tracer), workspaceRoot, tracer);
256         });
257     }
258     else {
259         return resolve(moduleName, resolveGlobalNodePath(tracer), workspaceRoot, tracer);
260     }
261 }
262 exports.resolveModulePath = resolveModulePath;
263 //# sourceMappingURL=files.js.map