Refactor main

pull/268/head
Anton Medvedev 9 months ago
parent 82e6781465
commit 9bc7ab1a3c
No known key found for this signature in database

@ -22,17 +22,10 @@ void async function main() {
await importFxrc(os.homedir())
let fd = 0 // stdin
if (args.length > 0) {
try {
fs.accessSync(args[0], fs.constants.R_OK)
const file = args.shift()
fd = fs.openSync(file, 'r')
} catch (_) {
try {
fs.accessSync(args.at(-1), fs.constants.R_OK)
const file = args.pop()
fd = fs.openSync(file, 'r')
} catch (_) {
}
if (await isFile(args[0])) {
fd = fs.openSync(args.shift(), 'r')
} else if (await isFile(args.at(-1))) {
fd = fs.openSync(args.pop(), 'r')
}
}
const gen = await read(fd)
@ -205,6 +198,12 @@ async function read(fd = 0) {
}()
}
async function isFile(path) {
const fs = await import('node:fs')
const stat = fs.statSync(path, {throwIfNoEntry: false})
return stat !== undefined && stat.isFile()
}
function sleepSync(ms) {
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms)
}

@ -17,6 +17,15 @@ async function run(json, code = '') {
})
}
async function runSimple(code = '') {
const {spawnSync} = await import('node:child_process')
return spawnSync(`node index.js ${code}`, {
stdio: 'pipe',
encoding: 'utf8',
shell: true
})
}
void async function main() {
await test('properly formatted', async t => {
const {stdout} = await run([{'greeting': 'hello world'}])
@ -182,4 +191,14 @@ void async function main() {
const {stdout} = await run('hello,\nworld!', `-rs '.join(" ")'`)
t.equal(stdout, 'hello, world!\n')
})
await test('cli - first arg is file', async t => {
const {stdout} = await runSimple(`package.json .name`)
t.equal(stdout, 'fx\n')
})
await test('cli - last arg is file', async t => {
const {stdout} = await runSimple(`.name package.json`)
t.equal(stdout, 'fx\n')
})
}()

Loading…
Cancel
Save