source: trip-planner-front/node_modules/ansi-html/bin/ansi-html@ bdd6491

Last change on this file since bdd6491 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.0 KB
Line 
1#!/usr/bin/env node
2
3var ansiHTML = require('../')
4var pkg = require('../package.json')
5var l = console.log
6var w = console.warn
7
8var stdoutFlushed = true
9var readingStdin = false
10
11function logLine (line) {
12 if (!line) {
13 return
14 }
15 line = ansiHTML(line)
16 try {
17 stdoutFlushed = process.stdout.write(line)
18 } catch (e) {}
19}
20
21function safeExit (code) {
22 l('')
23 process.exit(code)
24}
25
26function processStdin (finish) {
27 readingStdin = true
28 var chunks = ''
29 process.stdin.resume()
30 process.stdin.setEncoding('utf-8')
31 process.stdin.on('data', function (chunk) {
32 var lines = chunk.split(/[\r\n]+/g).filter(function (line) {
33 return line
34 })
35 var length = lines.length
36 if (length === 1) {
37 chunks += lines[0]
38 return
39 }
40 if (length > 1) {
41 logLine(chunks + (chunks ? '\n' : '') + lines[0] + '\n')
42 }
43 chunks = lines.pop()
44 length -= 1
45 for (var i = 1; i < length; i++) {
46 logLine(lines[i] + '\n')
47 }
48 })
49 process.stdin.on('end', function () {
50 if (chunks) {
51 logLine(chunks)
52 }
53 finish()
54 })
55}
56
57function stdoutDrain (code) {
58 process.stdout.on('drain', function () {
59 safeExit(code)
60 })
61 if (stdoutFlushed) {
62 safeExit(code)
63 }
64}
65
66function startup (args) {
67 if (args.indexOf('-h') > 0 || args.indexOf('--help') > 0) {
68 l(pkg.name + '@' + pkg.version)
69 l('Usage:')
70 l(' ansi-html [options]')
71 l(' ... | ansi-html [options]')
72 l('Options:')
73 l(' -h, --help print help information')
74 return
75 }
76
77 process.stdout.on('error', function (err) {
78 if (err.code === 'EPIPE') {
79 stdoutDrain(0)
80 } else {
81 w('stdout error:', err)
82 stdoutDrain(1)
83 }
84 })
85
86 processStdin(function () {
87 safeExit(0)
88 })
89}
90
91if (require.main === module) {
92 startup(process.argv)
93}
94
95process.on('SIGINT', function () {
96 if (!readingStdin) {
97 safeExit(1)
98 }
99})
100process.on('SIGQUIT', function () { safeExit(1) })
101process.on('SIGTERM', function () { safeExit(1) })
102process.on('SIGHUP', function () { safeExit(1) })
Note: See TracBrowser for help on using the repository browser.