Skip to content

Commit

Permalink
Package CLI code as ES module to allow import()
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Feb 24, 2024
1 parent 495f92d commit c9a0771
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 46 deletions.
11 changes: 11 additions & 0 deletions bin.mjs
@@ -0,0 +1,11 @@
#!/usr/bin/env node

import { UserError, cli, help } from './dist/cli.mjs'

cli(process.stdin, error => {
if (error instanceof UserError) {
if (error.code === UserError.ARGS) console.error(`${help}\n`)
console.error(error.message)
process.exitCode = error.code
} else if (error) throw error
})
6 changes: 5 additions & 1 deletion config/jest.config.js
Expand Up @@ -12,10 +12,14 @@ switch (process.env.npm_lifecycle_event) {
console.log('Testing build output from dist/')
moduleNameMapper = {
'^yaml$': '<rootDir>/dist/index.js',
'^yaml/cli$': '<rootDir>/dist/cli.js',
'^yaml/cli$': '<rootDir>/dist/cli.mjs',
'^yaml/util$': '<rootDir>/dist/util.js',
'^../src/test-events$': '<rootDir>/dist/test-events.js'
}
transform['[/\\\\]dist[/\\\\].*\\.mjs$'] = [
'babel-jest',
{ configFile: './config/babel.config.js' }
]
break

case 'test':
Expand Down
48 changes: 22 additions & 26 deletions config/rollup.node-config.mjs
@@ -1,30 +1,26 @@
import { chmod, stat } from 'node:fs/promises'
import typescript from '@rollup/plugin-typescript'

export default {
input: {
cli: 'src/cli.ts',
index: 'src/index.ts',
'test-events': 'src/test-events.ts',
util: 'src/util.ts'
export default [
{
input: {
index: 'src/index.ts',
'test-events': 'src/test-events.ts',
util: 'src/util.ts'
},
output: {
dir: 'dist',
format: 'cjs',
esModule: false,
preserveModules: true
},
plugins: [typescript()],
treeshake: { moduleSideEffects: false, propertyReadSideEffects: false }
},
output: {
dir: 'dist',
format: 'cjs',
esModule: false,
preserveModules: true
},
external: ['node:path', 'node:util'],
plugins: [
typescript(),
{
async writeBundle() {
// chmod a+x dist/cli.js
const file = 'dist/cli.js'
const prev = await stat(file)
await chmod(file, prev.mode | 0o111)
}
}
],
treeshake: { moduleSideEffects: false, propertyReadSideEffects: false }
}
{
input: 'src/cli.ts',
output: { file: 'dist/cli.mjs' },
external: () => true,
plugins: [typescript()]
}
]
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -18,7 +18,7 @@
],
"type": "commonjs",
"main": "./dist/index.js",
"bin": "./dist/cli.js",
"bin": "./bin.mjs",
"browser": {
"./dist/index.js": "./browser/index.js",
"./dist/util.js": "./browser/dist/util.js",
Expand Down
18 changes: 3 additions & 15 deletions src/cli.ts
@@ -1,5 +1,3 @@
#!/usr/bin/env node

import { resolve } from 'node:path'
import { parseArgs } from 'node:util'

Expand All @@ -12,7 +10,7 @@ import { type Document } from './doc/Document.js'
import { prettifyError } from './errors.js'
import { visit, type visitor } from './visit.js'

const help = `\
export const help = `\
yaml: A command-line YAML processor and inspector
Reads stdin and writes output to stdout and errors & warnings to stderr.
Expand All @@ -34,7 +32,7 @@ Additional options for bare "yaml" command:
--visit, -v Apply a visitor to each document (requires a path to import)
--yaml 1.1 Set the YAML version. (default: 1.2)`

class UserError extends Error {
export class UserError extends Error {
static ARGS = 2
static SINGLE = 3
code: number
Expand All @@ -44,17 +42,7 @@ class UserError extends Error {
}
}

/* istanbul ignore if */
if (require.main === module)
// eslint-disable-next-line @typescript-eslint/no-floating-promises
main(process.stdin, error => {
if (error instanceof UserError) {
console.error(`${help}\n\n${error.message}`)
process.exitCode = error.code
} else if (error) throw error
})

export async function main(
export async function cli(
stdin: NodeJS.ReadableStream,
done: (error?: Error) => void,
argv?: string[]
Expand Down
6 changes: 3 additions & 3 deletions tests/cli.ts
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-floating-promises */

import { Readable } from 'node:stream'
import { main } from 'yaml/cli'
import { cli } from 'yaml/cli'

const [major] = process.versions.node.split('.')
const skip = Number(major) < 20
Expand Down Expand Up @@ -31,7 +31,7 @@ const skip = Number(major) < 20
test(name, done => {
stdout.length = 0
stderr.length = 0
main(
cli(
Readable.from([input]),
error => {
try {
Expand All @@ -56,7 +56,7 @@ const skip = Number(major) < 20
test(name, done => {
stderr.length = 0
let doned = false
main(
cli(
Readable.from([input]),
error => {
if (doned) return
Expand Down

0 comments on commit c9a0771

Please sign in to comment.