UNPKG

codess

Version:

vscode代码片段管理器,通过 本地包(本地某个文件夹) 或官网的 远程包(代码片段集合) 生成本地项目的 vscode 代码片段配置。

67 lines (66 loc) 2.12 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpP = void 0; exports.request = request; const https_1 = __importDefault(require("https")); const http_1 = __importDefault(require("http")); const ora_1 = __importDefault(require("ora")); class HttpP { constructor(path, type) { this.path = ''; this.type = ''; this.path = path; this.type = type; this.spinner = (0, ora_1.default)({ text: `${type}ing`, }).start(); } getP() { return (n) => { this.spinner.text = `${this.type}ing ${this.path} ${parseInt(n.toString())}%`; }; } fail() { this.spinner.text = `${this.type} ${this.path}`; this.spinner.fail(); } succeed() { this.spinner.text = `${this.type} ${this.path}`; this.spinner.succeed(); } stop() { this.spinner.stop(); } } exports.HttpP = HttpP; function request(op, progress) { return new Promise((resolve, reject) => { let url = op.url; if (!/^https?:\/\//.test(url)) { url = (op.baseURL || '').replace(/\/+$/, '') + '/' + url.replace(/^\/+/, ''); } let req = (/^https:/.test(url) ? https_1.default : http_1.default).request(url, { headers: Object.assign({}, op.headers), method: op.method, timeout: 1000 * 60, }, (res) => { res.setEncoding('utf8'); let d = ''; res.on('data', (data) => { let length = parseInt(res.headers['content-length'] || '0'); d += data; if (length) { progress === null || progress === void 0 ? void 0 : progress((d.length / length) * 100); } }); res.on('end', () => { resolve(d); }); }); req.on('error', reject); req.end(JSON.stringify(op.body)); }); }