Skip to content

Commit

Permalink
Merge pull request #6 from cto-af/update-deps
Browse files Browse the repository at this point in the history
Update dependencies
  • Loading branch information
hildjj committed Oct 31, 2023
2 parents ed3fde5 + 255f8ad commit 2db704b
Show file tree
Hide file tree
Showing 7 changed files with 505 additions and 454 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ module.exports = {
},
},
],
}
};
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [16.x, 18.x, 20.x]
node-version: [16.x, 18.x, 20.x, 21.x]
os: [ubuntu-latest]

runs-on: ${{ matrix.os }}
Expand Down
66 changes: 33 additions & 33 deletions bin/linewrap.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env node
/* eslint-disable no-console */

import {inspect, promisify} from 'util'
import {LineWrap} from '@cto.af/linewrap'
import {fileURLToPath} from 'url'
import fs from 'fs'
import os from 'os'
import {parseArgsWithHelp} from 'minus-h'
import {inspect, promisify} from 'util';
import {LineWrap} from '@cto.af/linewrap';
import {fileURLToPath} from 'url';
import fs from 'fs';
import os from 'os';
import {parseArgsWithHelp} from 'minus-h';

/**
* @type {Parameters<generateHelp>[0]}
Expand Down Expand Up @@ -131,7 +131,7 @@ const config = {
argumentName: '...file',
argumentDescription: 'files to wrap and concatenate. Use "-" for stdin. Default: "-"',
description: 'Wrap some text, either from file, stdin, or given on the command line. Each chunk of text is wrapped independently from one another, and streamed to stdout (or an outFile, if given). Command line arguments with -t/--text are processed before files.',
}
};

/**
* Read stdin to completion with the configured encoding.
Expand All @@ -140,13 +140,13 @@ const config = {
*/
function readStdin(opts, stream) {
// Below, d will be a string
stream.setEncoding(opts.encoding)
stream.setEncoding(opts.encoding);
return new Promise((resolve, reject) => {
let s = ''
stream.on('data', d => (s += d))
stream.on('end', () => resolve(s))
stream.on('error', reject)
})
let s = '';
stream.on('data', d => (s += d));
stream.on('end', () => resolve(s));
stream.on('error', reject);
});
}

const ESCAPES = {
Expand All @@ -156,7 +156,7 @@ const ESCAPES = {
'"': '&quot;',
"'": '&apos;',
'\xA0': '&nbsp;',
}
};

/**
* Escape HTML
Expand All @@ -166,12 +166,12 @@ const ESCAPES = {
* @private
*/
function htmlEscape(str) {
return str.replace(/[&<>\xA0]/g, m => ESCAPES[m])
return str.replace(/[&<>\xA0]/g, m => ESCAPES[m]);
}

const {
exit, stdin, stdout, stderr,
} = process
} = process;

export async function main(
extraConfig,
Expand All @@ -184,10 +184,10 @@ export async function main(
}, {
width: config.options.width.default,
...options,
})
});

if ((values.text.length === 0) && (positionals.length === 0)) {
positionals.push('-')
positionals.push('-');
}

// Always a valid string, due to choices enforcement
Expand All @@ -196,11 +196,11 @@ export async function main(
visible: LineWrap.OVERFLOW_VISIBLE,
clip: LineWrap.OVERFLOW_CLIP,
anywhere: LineWrap.OVERFLOW_ANYWHERE,
}[values.overflow]
}[values.overflow];

const outstream = values.outFile ?
fs.createWriteStream(values.outFile, values.encoding) :
process.stdout // Don't set encoding, will confuse terminal.
process.stdout; // Don't set encoding, will confuse terminal.

/** @type {ConstructorParameters<typeof LineWrap>[0]} */
const opts = {
Expand All @@ -220,39 +220,39 @@ export async function main(
trim: !values.noTrim,
verbose: values.verbose,
width: parseInt(values.width, 10),
}
};
if (typeof values.isNewline === 'string') {
opts.isNewline = (values.isNewline.length === 0) ?
null :
new RegExp(values.isNewline, 'gu')
new RegExp(values.isNewline, 'gu');
}
if (values.verbose) {
process.stdout.write(inspect(opts))
process.stdout.write(inspect(opts));
}
const w = new LineWrap(opts)
const w = new LineWrap(opts);

for (const t of values.text) {
outstream.write(w.wrap(t))
outstream.write(values.newline)
outstream.write(w.wrap(t));
outstream.write(values.newline);
}

for (const f of positionals) {
const t = f === '-' ?
await readStdin(values, process.stdin) :
await fs.promises.readFile(f, values.encoding)
await fs.promises.readFile(f, values.encoding);

outstream.write(w.wrap(t))
outstream.write(values.newline)
outstream.write(w.wrap(t));
outstream.write(values.newline);
}

// Be careful to wait for the file to close, to ensure tests run
// correctly.
await promisify(outstream.end.bind(outstream))()
await promisify(outstream.end.bind(outstream))();
}

if (fileURLToPath(import.meta.url) === process.argv[1]) {
main().catch(e => {
console.error(e)
process.exit(1)
})
console.error(e);
process.exit(1);
});
}

0 comments on commit 2db704b

Please sign in to comment.