-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cli accepts config and dest arg OR a single arg for config file to be backwards compat * Update README.md
- Loading branch information
Showing
2 changed files
with
62 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,42 @@ | ||
#!/usr/bin/env node | ||
import fs from 'fs' | ||
import path from 'path' | ||
import * as url from 'url' | ||
const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) | ||
const arg = process.argv[2] | ||
const configpath = arg || path.join(__dirname, './config.json') | ||
import { readFileSync, writeFileSync } from 'node:fs' | ||
import { join } from 'node:path' | ||
import { argv, cwd, stderr, stdout } from 'node:process' | ||
import { fileURLToPath } from 'node:url' | ||
import styles from './index.mjs' | ||
const config = fs.readFileSync(configpath, 'utf-8') | ||
process.stdout.write(styles(config)) | ||
|
||
function getArg(key) { | ||
const value = argv.find(a => a.startsWith(`--${key}=`)) | ||
if (!value) return null | ||
return value.replace(`--${key}=`, '') | ||
} | ||
|
||
const here = fileURLToPath(new URL('.', import.meta.url)) | ||
|
||
const configArg = getArg('config') | ||
const outputArg = getArg('output') | ||
const configPath = configArg | ||
? join(cwd(), configArg) | ||
: join(here, './config.json') | ||
|
||
let configBlob | ||
try { | ||
configBlob = readFileSync(configPath, 'utf-8') | ||
} | ||
catch (err) { | ||
stderr.write(`Error reading config file: ${configPath}\n`) | ||
process.exit(1) | ||
} | ||
|
||
if (outputArg) { | ||
const outputPath = join(cwd(), outputArg) | ||
try { | ||
writeFileSync(outputPath, styles(configBlob)) | ||
} | ||
catch (err) { | ||
stderr.write(`Error writing to output file: ${outputPath}\n`) | ||
process.exit(1) | ||
} | ||
} else { | ||
stdout.write(styles(configBlob)) | ||
} |