UNPKG

@dvhb/react-intl-messages

Version:

Library for parsing source files and extract react-intl messages

58 lines (57 loc) 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const fs = require("fs"); const url_1 = require("url"); const G = require("glob"); const https = require("https"); const querystring = require("querystring"); exports.glob = (pattern, ignore) => new Promise((resolve, reject) => { G(pattern, { ignore }, (err, val) => (err ? reject(err) : resolve(val))); }); exports.readFile = (file) => new Promise((resolve, reject) => { fs.readFile(file, 'utf8', (err, data) => (err ? reject(err) : resolve(data))); }); exports.writeFile = (file, contents) => new Promise((resolve, reject) => { fs.writeFile(file, contents, 'utf8', err => (err ? reject(err) : resolve())); }); exports.posixPath = (fileName) => fileName.replace(/\\/g, '/'); exports.toHash = (array, key) => Array.prototype.reduce.call(array, // @ts-ignore (acc, data, index) => ((acc[key ? data[key] : index] = data), acc), {}); exports.tail = (arr) => (arr.length > 1 ? arr.slice(1) : arr); exports.head = (arr) => arr[0]; exports.request = (_a) => { var { url, body, qs, headers = {} } = _a, rest = tslib_1.__rest(_a, ["url", "body", "qs", "headers"]); const { hostname, pathname } = new url_1.URL(url); const searchParams = qs && querystring.stringify(qs); const path = searchParams ? `${pathname}?${searchParams}` : pathname; const bodyString = JSON.stringify(body); if (body) { headers['Content-Length'] = Buffer.byteLength(bodyString, 'utf8'); } return new Promise((resolve, reject) => { const request = https.request(Object.assign({ headers, hostname, path }, rest), response => { let data = ''; response.on('data', chunk => { data += chunk; }); response.on('end', () => resolve(Object.assign(Object.assign({}, JSON.parse(data)), { headers: response.headers }))); }); if (body) { request.write(bodyString); } request.on('error', err => { reject(err); }); request.end(); }); }; const format = (time) => time.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, '$1'); exports.showError = (message) => console.error('\x1b[31m', message, '\x1b[0m'); exports.showInfo = (message) => console.info(`\x1b[34m[${format(new Date())}]\x1b[0m`, `${message}`); exports.asyncForEach = async (array, callback) => { for (let index = 0; index < array.length; index += 1) { await callback(array[index], index, array); } };