-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin.js
executable file
·38 lines (32 loc) · 892 Bytes
/
bin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env node
import fs from 'node:fs'
import minimist from 'minimist'
import concat from 'simple-concat'
import insert from './index.js'
const args = minimist(process.argv.slice(2))
const file = args._[0]
if (!file || args.h || args.help) {
console.log(fs.readFileSync(new URL('./usage.txt', import.meta.url), 'utf8'))
process.exit(args.h || args.help ? 0 : 1)
}
const inPlace = args.i || false
const source = fs.createReadStream(file)
concat(process.stdin, function (err, content) {
if (err) throw err
const stream = insert({
content: content.toString('utf8'),
header: args.header,
region: args.region
})
source.pipe(stream)
if (inPlace) {
concat(stream, function (err, result) {
if (err) throw err
fs.writeFile(file, result, function (err) {
if (err) throw err
})
})
} else {
stream.pipe(process.stdout)
}
})