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
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 10 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node
import { globbySync } from 'globby'
import fs from 'node:fs'
import getStdin from 'get-stdin'
keithamus marked this conversation as resolved.
Show resolved Hide resolved
import sortPackageJson from './index.js'
import Reporter from './reporter.js'

Expand All @@ -23,6 +24,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 Read package.json from stdin
`,
)
}
Expand Down Expand Up @@ -57,6 +59,10 @@ function sortPackageJsonFiles(patterns, options) {
reporter.printSummary()
}

async function sortPackageJsonFromStdin() {
process.stdout.write(sortPackageJson(await getStdin()))
}

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

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

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

const patterns = []
let isCheck = false
let shouldBeQuiet = false
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"dependencies": {
"detect-indent": "^7.0.1",
"detect-newline": "^4.0.0",
"get-stdin": "^9.0.0",
"git-hooks-list": "^3.0.0",
"globby": "^13.1.2",
"is-plain-obj": "^4.1.0",
Expand Down
19 changes: 14 additions & 5 deletions tests/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function asItIs(t, { path, options }, excludeTypes = []) {
}
}

async function testCLI(t, { fixtures = [], args, message }) {
async function testCLI(t, { fixtures = [], args, message, stdin }) {
const cwd = tempy.directory()

fixtures = fixtures.map(({ file = 'package.json', content, expect }) => {
Expand All @@ -145,6 +145,7 @@ async function testCLI(t, { fixtures = [], args, message }) {
args,
cwd,
message,
stdin,
})

for (const fixture of fixtures) {
Expand Down Expand Up @@ -172,11 +173,19 @@ async function testCLI(t, { fixtures = [], args, message }) {
)
}

function runCLI({ args = [], cwd = process.cwd() }) {
function runCLI({ args = [], cwd = process.cwd(), stdin }) {
return new Promise((resolve) => {
execFile('node', [cliScript, ...args], { cwd }, (error, stdout, stderr) => {
resolve({ errorCode: error && error.code, stdout, stderr })
})
const cp = execFile(
'node',
[cliScript, ...args],
{ cwd },
(error, stdout, stderr) => {
resolve({ errorCode: error && error.code, stdout, stderr })
},
)
if (stdin) {
cp.stdin.end(stdin)
fisker marked this conversation as resolved.
Show resolved Hide resolved
}
})
}

Expand Down
6 changes: 6 additions & 0 deletions tests/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,9 @@ test('run `cli --quiet` on 1 non-json file', macro.testCLI, {
args: ['*/package.json', '--quiet'],
message: 'Should output error message',
})

test('run `cli --stdin` with input from stdin', macro.testCLI, {
args: ['--stdin'],
message: 'Should output sorted json',
stdin: `{\n "description": "Description",\n "name": "Name"\n}\n`,
danielpza marked this conversation as resolved.
Show resolved Hide resolved
})
25 changes: 25 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 Read 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 Read 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 Read 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 Read 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 Read package.json from stdin␊
`,
},
Expand Down Expand Up @@ -1364,3 +1369,23 @@ Generated by [AVA](https://avajs.dev).
stdout: '',
},
}

## run `cli --stdin` with input from stdin

> Should output sorted json

{
args: [
'--stdin',
],
fixtures: [],
result: {
errorCode: null,
stderr: '',
stdout: `{␊
"name": "Name",␊
"description": "Description"␊
}␊
`,
},
}
Binary file modified tests/snapshots/cli.js.snap
Binary file not shown.