UNPKG

datakit

Version:

Simple JavaScript toolkit for data transform across JSON, CSV and YAML.

320 lines 13.3 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.outputData = exports.outputDataToFile = exports.outputDataToStdout = exports.writeStdout = exports.inputData = exports.inputDataFromStdin = exports.readStdin = void 0; var readline = __importStar(require("readline")); var __1 = require(".."); var __2 = require(".."); var __3 = require(".."); var __4 = require(".."); var utils_1 = require("./utils"); // // Reads data from standard input. // function readStdin() { return new Promise(function (resolve, reject) { var rl = readline.createInterface({ input: process.stdin, }); var input = ""; var timeout = undefined; var timedout = false; // // Cancels the timeout, if it's still in progress. // function cancelTimeout() { if (timeout) { clearTimeout(timeout); timeout = undefined; } } // // Starts a new timeout (cancelling any previous timeout). // function startTimeout() { cancelTimeout(); timeout = setTimeout(function () { timedout = true; reject(new Error("Timed out with no input on standard input. Use argument --help to get help about this command.")); }, 1000 * 30); } ; rl.on("line", function (line) { if (timedout) { return; } input += line + '\n'; startTimeout(); }); rl.on("close", function () { if (timedout) { return; } cancelTimeout(); resolve(input); }); startTimeout(); }); } exports.readStdin = readStdin; // // Reads data from standard input in the requested format. // Data format defaults to JSON if none is specified. // function inputDataFromStdin(expectedFormat) { return __awaiter(this, void 0, void 0, function () { var input; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, readStdin()]; case 1: input = _a.sent(); if (expectedFormat === undefined || expectedFormat === "json") { return [2 /*return*/, JSON.parse(input)]; } else if (expectedFormat == "csv") { return [2 /*return*/, (0, __1.fromCsv)(input)]; } else if (expectedFormat === "yaml") { return [2 /*return*/, (0, __1.fromYaml)(input)]; } else { throw new Error("Unexpected data format: ".concat(expectedFormat)); } return [2 /*return*/]; } }); }); } exports.inputDataFromStdin = inputDataFromStdin; // // Input data from a data file or standard input. // function inputData(argv, expectedFormat) { return __awaiter(this, void 0, void 0, function () { var fileName; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(argv.length === 0)) return [3 /*break*/, 2]; return [4 /*yield*/, inputDataFromStdin(expectedFormat)]; case 1: // // Default to reading JSON data from standard input. // return [2 /*return*/, _a.sent()]; case 2: fileName = argv.shift().toLowerCase(); if (!(fileName === "-")) return [3 /*break*/, 4]; return [4 /*yield*/, inputDataFromStdin(expectedFormat)]; case 3: // // Explicitly read JSON data from standard input. // return [2 /*return*/, _a.sent()]; case 4: if (expectedFormat === undefined) { // // Choose data format from the file extension. // expectedFormat = determineFormat(fileName); } if (!(expectedFormat === "json")) return [3 /*break*/, 6]; return [4 /*yield*/, (0, __2.readJson)(fileName)]; case 5: return [2 /*return*/, _a.sent()]; case 6: if (!(expectedFormat === "csv")) return [3 /*break*/, 8]; return [4 /*yield*/, (0, __3.readCsv)(fileName)]; case 7: return [2 /*return*/, _a.sent()]; case 8: if (!(expectedFormat === "yaml")) return [3 /*break*/, 10]; return [4 /*yield*/, (0, __4.readYaml)(fileName)]; case 9: return [2 /*return*/, _a.sent()]; case 10: throw new Error("Unexpected data format: ".concat(expectedFormat)); } }); }); } exports.inputData = inputData; // // Figure out the data format from the file name. // function determineFormat(fileName) { if (fileName.endsWith(".json")) { return "json"; } else if (fileName.endsWith(".csv")) { return "csv"; } else if (fileName.endsWith(".yaml")) { return "yaml"; } else if (fileName.endsWith(".yml")) { return "yaml"; } else { throw new Error("Failed to identify data format from file name: \"".concat(fileName, "\"")); } } // // Writes text to standard output. // function writeStdout(data) { process.stdout.write(data); } exports.writeStdout = writeStdout; // // Write data to standard output in the requested format. // Data format defaults to JSON if none is specified. // function outputDataToStdout(data, expectedFormat, config) { var formattedData; if (expectedFormat === undefined || expectedFormat === "json") { formattedData = JSON.stringify(data, null, 4); } else if (expectedFormat == "csv") { if (!(0, utils_1.isArray)(data)) { throw new Error("To write csv to standard output, expect the data to be an array of records. Instead got \"".concat(typeof data, "\".")); } formattedData = (0, __1.toCsv)(data, config); } else if (expectedFormat === "yaml") { formattedData = (0, __1.toYaml)(data); } else { throw new Error("Unexpected data format: ".concat(expectedFormat)); } writeStdout(formattedData); } exports.outputDataToStdout = outputDataToStdout; // // Outputs data to a name file. // function outputDataToFile(data, fileName, expectedFormat, config) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (expectedFormat === undefined) { // // Choose data format from the file extension. // expectedFormat = determineFormat(fileName); } if (!(expectedFormat === "json")) return [3 /*break*/, 2]; return [4 /*yield*/, (0, __1.writeJson)(fileName, data)]; case 1: _a.sent(); return [3 /*break*/, 7]; case 2: if (!(expectedFormat === "csv")) return [3 /*break*/, 4]; if (!(0, utils_1.isArray)(data)) { throw new Error("To write csv file ".concat(fileName, ", expect the data to be an array of records. Instead got \"").concat(typeof data, "\".")); } return [4 /*yield*/, (0, __1.writeCsv)(fileName, data, config)]; case 3: _a.sent(); return [3 /*break*/, 7]; case 4: if (!(expectedFormat === "yaml")) return [3 /*break*/, 6]; return [4 /*yield*/, (0, __1.writeYaml)(fileName, data)]; case 5: return [2 /*return*/, _a.sent()]; case 6: throw new Error("Unexpected data format: ".concat(expectedFormat)); case 7: return [2 /*return*/]; } }); }); } exports.outputDataToFile = outputDataToFile; // // Outputs data as JSON to a file or to standard output. // function outputData(argv, data, expectedFormat, config) { return __awaiter(this, void 0, void 0, function () { var fileName; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(argv.length === 0)) return [3 /*break*/, 1]; // // Default is to output to stdout. // outputDataToStdout(data, expectedFormat, config); return [3 /*break*/, 4]; case 1: fileName = argv.shift().toLowerCase(); if (!(fileName === "-")) return [3 /*break*/, 2]; // // Explicitly output to stdout. // outputDataToStdout(data, expectedFormat, config); return [3 /*break*/, 4]; case 2: return [4 /*yield*/, outputDataToFile(data, fileName, expectedFormat, config)]; case 3: _a.sent(); _a.label = 4; case 4: return [2 /*return*/]; } }); }); } exports.outputData = outputData; //# sourceMappingURL=io.js.map