react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / bin / js-yaml.js
80684 views#!/usr/bin/env node123'use strict';45/*eslint-disable no-console*/678// stdlib9var fs = require('fs');101112// 3rd-party13var argparse = require('argparse');141516// internal17var yaml = require('..');181920////////////////////////////////////////////////////////////////////////////////212223var cli = new argparse.ArgumentParser({24prog: 'js-yaml',25version: require('../package.json').version,26addHelp: true27});282930cli.addArgument([ '-c', '--compact' ], {31help: 'Display errors in compact mode',32action: 'storeTrue'33});343536// deprecated (not needed after we removed output colors)37// option suppressed, but not completely removed for compatibility38cli.addArgument([ '-j', '--to-json' ], {39help: argparse.Const.SUPPRESS,40dest: 'json',41action: 'storeTrue'42});434445cli.addArgument([ '-t', '--trace' ], {46help: 'Show stack trace on error',47action: 'storeTrue'48});4950cli.addArgument([ 'file' ], {51help: 'File to read, utf-8 encoded without BOM',52nargs: '?',53defaultValue: '-'54});555657////////////////////////////////////////////////////////////////////////////////585960var options = cli.parseArgs();616263////////////////////////////////////////////////////////////////////////////////6465function readFile(filename, encoding, callback) {66if (options.file === '-') {67// read from stdin6869var chunks = [];7071process.stdin.on('data', function (chunk) {72chunks.push(chunk);73});7475process.stdin.on('end', function () {76return callback(null, Buffer.concat(chunks).toString(encoding));77});78} else {79fs.readFile(filename, encoding, callback);80}81}8283readFile(options.file, 'utf8', function (error, input) {84var output, isYaml;8586if (error) {87if (error.code === 'ENOENT') {88console.error('File not found: ' + options.file);89process.exit(2);90}9192console.error(93options.trace && error.stack ||94error.message ||95String(error));9697process.exit(1);98}99100try {101output = JSON.parse(input);102isYaml = false;103} catch (error) {104if (error instanceof SyntaxError) {105try {106output = [];107yaml.loadAll(input, function (doc) { output.push(doc); }, {});108isYaml = true;109110if (0 === output.length) {111output = null;112} else if (1 === output.length) {113output = output[0];114}115} catch (error) {116if (options.trace && error.stack) {117console.error(error.stack);118} else {119console.error(error.toString(options.compact));120}121122process.exit(1);123}124} else {125console.error(126options.trace && error.stack ||127error.message ||128String(error));129130process.exit(1);131}132}133134if (isYaml) {135console.log(JSON.stringify(output, null, ' '));136} else {137console.log(yaml.dump(output));138}139140process.exit(0);141});142143144