UNPKG

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