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");
13 * @deprecated Use the `vscode-uri` npm module which provides a more
14 * complete implementation of handling VS Code URIs.
16 function uriToFilePath(uri) {
17 let parsed = url.parse(uri);
18 if (parsed.protocol !== 'file:' || !parsed.path) {
21 let segments = parsed.path.split('/');
22 for (var i = 0, len = segments.length; i < len; i++) {
23 segments[i] = decodeURIComponent(segments[i]);
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] === ':') {
35 return path.normalize(segments.join('/'));
37 exports.uriToFilePath = uriToFilePath;
38 function isWindows() {
39 return process.platform === 'win32';
41 function resolve(moduleName, nodePath, cwd, tracer) {
42 const nodePathKey = 'NODE_PATH';
45 'p.on(\'message\',function(m){',
49 'else if(m.c===\'rs\'){',
51 'var r=require.resolve(m.a);',
52 'p.send({c:\'r\',s:true,r:r});',
55 'p.send({c:\'r\',s:false});',
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];
69 newEnv[nodePathKey] = nodePath;
72 tracer(`NODE_PATH value is: ${newEnv[nodePathKey]}`);
75 newEnv['ELECTRON_RUN_AS_NODE'] = '1';
77 let cp = child_process_1.fork('', [], {
82 if (cp.pid === void 0) {
83 reject(new Error(`Starting process to resolve node module ${moduleName} failed`));
86 cp.on('error', (error) => {
89 cp.on('message', (message) => {
90 if (message.c === 'r') {
96 reject(new Error(`Failed to resolve module: ${moduleName}`));
111 exports.resolve = resolve;
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
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';
128 npmCommand = 'npm.cmd';
129 options.shell = true;
131 let handler = () => { };
133 process.on('SIGPIPE', handler);
134 let stdout = child_process_1.spawnSync(npmCommand, ['config', 'get', 'prefix'], options).stdout;
137 tracer(`'npm config get prefix' didn't return a value.`);
141 let prefix = stdout.trim();
143 tracer(`'npm config get prefix' value is: ${prefix}`);
145 if (prefix.length > 0) {
147 return path.join(prefix, 'node_modules');
150 return path.join(prefix, 'lib', 'node_modules');
159 process.removeListener('SIGPIPE', handler);
162 exports.resolveGlobalNodePath = resolveGlobalNodePath;
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
169 function resolveGlobalYarnPath(tracer) {
170 let yarnCommand = 'yarn';
175 yarnCommand = 'yarn.cmd';
176 options.shell = true;
178 let handler = () => { };
180 process.on('SIGPIPE', handler);
181 let results = child_process_1.spawnSync(yarnCommand, ['global', 'dir', '--json'], options);
182 let stdout = results.stdout;
185 tracer(`'yarn global dir' didn't return a value.`);
186 if (results.stderr) {
187 tracer(results.stderr);
192 let lines = stdout.trim().split(/\r?\n/);
193 for (let line of lines) {
195 let yarn = JSON.parse(line);
196 if (yarn.type === 'log') {
197 return path.join(yarn.data, 'node_modules');
201 // Do nothing. Ignore the line
210 process.removeListener('SIGPIPE', handler);
213 exports.resolveGlobalYarnPath = resolveGlobalYarnPath;
215 (function (FileSystem) {
216 let _isCaseSensitive = undefined;
217 function isCaseSensitive() {
218 if (_isCaseSensitive !== void 0) {
219 return _isCaseSensitive;
221 if (process.platform === 'win32') {
222 _isCaseSensitive = false;
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());
229 return _isCaseSensitive;
231 FileSystem.isCaseSensitive = isCaseSensitive;
232 function isParent(parent, child) {
233 if (isCaseSensitive()) {
234 return path.normalize(child).indexOf(path.normalize(parent)) === 0;
237 return path.normalize(child).toLowerCase().indexOf(path.normalize(parent).toLowerCase()) === 0;
240 FileSystem.isParent = isParent;
241 })(FileSystem = exports.FileSystem || (exports.FileSystem = {}));
242 function resolveModulePath(workspaceRoot, moduleName, nodePath, tracer) {
244 if (!path.isAbsolute(nodePath)) {
245 nodePath = path.join(workspaceRoot, nodePath);
247 return resolve(moduleName, nodePath, nodePath, tracer).then((value) => {
248 if (FileSystem.isParent(nodePath, value)) {
252 return Promise.reject(new Error(`Failed to load ${moduleName} from node path location.`));
254 }).then(undefined, (_error) => {
255 return resolve(moduleName, resolveGlobalNodePath(tracer), workspaceRoot, tracer);
259 return resolve(moduleName, resolveGlobalNodePath(tracer), workspaceRoot, tracer);
262 exports.resolveModulePath = resolveModulePath;
263 //# sourceMappingURL=files.js.map