Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --stdin flag #296

Merged
merged 10 commits into from
Jul 2, 2023
Merged
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ $ sort-package-json "**/package.json" --check --quiet
$ sort-package-json "**/package.json" --quiet
```

### `--stdin` flag
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not particularly precious about this, but it makes me think; would --file <path>/-F <path> be a better option here - where one can use - to use stdin. This is how git does it (git commit -F my-template or echo foo | git commit -F -).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We plan to use util.parseArgs #283, not sure if it supports that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> util.parseArgs({args: ['-f', '-'], options: {file: {type: 'string', short: 'f' }}})
{ values: [Object: null prototype] { file: '-' }, positionals: [] }

Looks like it does?


To read from `stdin` and output the result to `stdout` use the `--stdin` flag.

```bash
$ cat package.json | sort-package-json --stdin
```

## API

### Install
Expand Down
21 changes: 21 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import fs from 'node:fs'
import sortPackageJson from './index.js'
import Reporter from './reporter.js'

/** @param {import("node:stream").Stream} stream */
async function streamToString(stream) {
// see https://stackoverflow.com/a/63361543/6051261
const chunks = []

for await (const chunk of stream) {
chunks.push(Buffer.from(chunk))
}

return Buffer.concat(chunks).toString('utf-8')
}
danielpza marked this conversation as resolved.
Show resolved Hide resolved

function showVersion() {
const { name, version } = JSON.parse(
fs.readFileSync(new URL('package.json', import.meta.url)),
Expand All @@ -23,6 +35,7 @@ If file/glob is omitted, './package.json' file will be processed.
-q, --quiet Don't output success messages
-h, --help Display this help
-v, --version Display the package version
--stdin Reads package.json from stdin
`,
)
}
Expand Down Expand Up @@ -57,6 +70,10 @@ function sortPackageJsonFiles(patterns, options) {
reporter.printSummary()
}

async function sortPackageJsonFromStdin() {
console.log(sortPackageJson(await streamToString(process.stdin)))
}

function run() {
const cliArguments = process.argv.slice(2)

Expand All @@ -74,6 +91,10 @@ function run() {
return showVersion()
}

if (cliArguments.some((argument) => argument === '--stdin')) {
return sortPackageJsonFromStdin()
}

const patterns = []
let isCheck = false
let shouldBeQuiet = false
Expand Down
5 changes: 5 additions & 0 deletions tests/snapshots/cli.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Generated by [AVA](https://avajs.dev).
-q, --quiet Don't output success messages␊
-h, --help Display this help␊
-v, --version Display the package version␊
--stdin Reads package.json from stdin␊
`,
},
Expand Down Expand Up @@ -52,6 +53,7 @@ Generated by [AVA](https://avajs.dev).
-q, --quiet Don't output success messages␊
-h, --help Display this help␊
-v, --version Display the package version␊
--stdin Reads package.json from stdin␊
`,
},
Expand All @@ -78,6 +80,7 @@ Generated by [AVA](https://avajs.dev).
-q, --quiet Don't output success messages␊
-h, --help Display this help␊
-v, --version Display the package version␊
--stdin Reads package.json from stdin␊
`,
},
Expand Down Expand Up @@ -105,6 +108,7 @@ Generated by [AVA](https://avajs.dev).
-q, --quiet Don't output success messages␊
-h, --help Display this help␊
-v, --version Display the package version␊
--stdin Reads package.json from stdin␊
`,
},
Expand Down Expand Up @@ -202,6 +206,7 @@ Generated by [AVA](https://avajs.dev).
-q, --quiet Don't output success messages␊
-h, --help Display this help␊
-v, --version Display the package version␊
--stdin Reads package.json from stdin␊
`,
},
Expand Down
Binary file modified tests/snapshots/cli.js.snap
Binary file not shown.