UNPKG

5.07 kBJavaScriptView Raw
1"use strict";
2var __importStar = (this && this.__importStar) || function (mod) {
3 if (mod && mod.__esModule) return mod;
4 var result = {};
5 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6 result["default"] = mod;
7 return result;
8};
9var __importDefault = (this && this.__importDefault) || function (mod) {
10 return (mod && mod.__esModule) ? mod : { "default": mod };
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13const fs = __importStar(require("fs"));
14const path = __importStar(require("path"));
15const crypto_1 = __importDefault(require("crypto"));
16const url_1 = require("url");
17function getDenoDir() {
18 // ref https://deno.land/manual.html
19 // On Linux/Redox: $XDG_CACHE_HOME/deno or $HOME/.cache/deno
20 // On Windows: %LOCALAPPDATA%/deno (%LOCALAPPDATA% = FOLDERID_LocalAppData)
21 // On macOS: $HOME/Library/Caches/deno
22 // If something fails, it falls back to $HOME/.deno
23 let denoDir = process.env.DENO_DIR;
24 if (denoDir === undefined) {
25 switch (process.platform) {
26 case "win32":
27 denoDir = `${process.env.LOCALAPPDATA}\\deno`;
28 break;
29 case "darwin":
30 denoDir = `${process.env.HOME}/Library/Caches/deno`;
31 break;
32 case "linux":
33 denoDir = process.env.XDG_CACHE_HOME
34 ? `${process.env.XDG_CACHE_HOME}/deno`
35 : `${process.env.HOME}/.cache/deno`;
36 break;
37 default:
38 denoDir = `${process.env.HOME}/.deno`;
39 }
40 }
41 return denoDir;
42}
43exports.getDenoDir = getDenoDir;
44function getDenoDepsDir() {
45 return path.join(getDenoDir(), "deps");
46}
47exports.getDenoDepsDir = getDenoDepsDir;
48function getPluginPath(tsLsHost) {
49 return path.resolve(tsLsHost.getCurrentDirectory(), "node_modules", "typescript-deno-plugin");
50}
51exports.getPluginPath = getPluginPath;
52function getDenoDtsPath(tsLsHost, specifier) {
53 let file = path.resolve(getDenoDir(), specifier);
54 if (fs.existsSync(file)) {
55 return file;
56 }
57 file = path.resolve(getPluginPath(tsLsHost), "lib", specifier);
58 if (fs.existsSync(file)) {
59 return file;
60 }
61 return undefined;
62}
63exports.getDenoDtsPath = getDenoDtsPath;
64function getModuleWithQueryString(moduleName) {
65 let name = moduleName;
66 for (const index = name.indexOf("?"); index !== -1; name = name.substring(index + 1)) {
67 const sub = name.substring(0, index);
68 if (sub.endsWith(".ts") || sub.endsWith(".tsx")) {
69 const cutLength = moduleName.length - name.length;
70 return moduleName.substring(0, index + cutLength) || undefined;
71 }
72 }
73 return undefined;
74}
75exports.getModuleWithQueryString = getModuleWithQueryString;
76function normalizeFilepath(filepath) {
77 return path.normalize(filepath
78 // in Windows, filepath maybe `c:\foo\bar` tut the legal path should be `C:\foo\bar`
79 .replace(/^([a-z]):\\/, (_, $1) => $1.toUpperCase() + ":\\")
80 // There are some paths which are unix style, this style does not work on win32 systems
81 .replace(/\//gm, path.sep));
82}
83exports.normalizeFilepath = normalizeFilepath;
84// ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
85// cover filepath string to regexp string
86// Because the `\` string is included in the path to Windows
87// So we need to translate it once
88// `/^C:\Users\runneradmin\AppData\Local\deno\deps\/` -> `/^C:\\Users\\runneradmin\\AppData\\Local\\deno\\deps\\/`
89function escapeRegExp(str) {
90 return str.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
91}
92exports.escapeRegExp = escapeRegExp;
93function sleep(ms) {
94 return new Promise((resolve) => {
95 setTimeout(() => {
96 resolve();
97 }, ms);
98 });
99}
100exports.sleep = sleep;
101function isHttpURL(str) {
102 if (!str.startsWith("http://") && !str.startsWith("https://")) {
103 return false;
104 }
105 try {
106 new url_1.URL(str);
107 return true;
108 }
109 catch (_a) {
110 return false;
111 }
112}
113exports.isHttpURL = isHttpURL;
114// hash a URL with it's pathname and search
115function hashURL(url) {
116 return crypto_1.default
117 .createHash("sha256")
118 .update(url.pathname + url.search)
119 .digest("hex");
120}
121exports.hashURL = hashURL;
122function isValidDenoDocument(languageID) {
123 return [
124 "javascript",
125 "javascriptreact",
126 "typescript",
127 "typescriptreact",
128 ].includes(languageID);
129}
130exports.isValidDenoDocument = isValidDenoDocument;
131function isUntitledDocument(filename) {
132 // In vscode, tsserver may crash because a temporary document is not saved
133 return /^untitled:/.test(filename);
134}
135exports.isUntitledDocument = isUntitledDocument;
136function pathExistsSync(filepath) {
137 try {
138 fs.statSync(filepath);
139 return true;
140 }
141 catch (err) {
142 return false;
143 }
144}
145exports.pathExistsSync = pathExistsSync;
146//# sourceMappingURL=utils.js.map
\No newline at end of file