Skip to content

Commit

Permalink
feat: add --stdin flag (#296)
Browse files Browse the repository at this point in the history
* feat: add --stdin flag

* refactor: use get-stdin package

* chore: fix getStdin call

* chore: add test for --stdin flag

* chore: adjust --stdin flag description

* chore: move get-stream to devDependency

* chore: fix extra line at eof

* refactor: use testCLI for testing --stdin flag

* chore: add test for \r\n

#296 (comment)

* chore: update test for \r\n

#296 (comment)
  • Loading branch information
danielpza committed Jul 2, 2023
1 parent 2789f2f commit cefbbcb
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 5 deletions.
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

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'
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)
}
})
}

Expand Down
12 changes: 12 additions & 0 deletions tests/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,15 @@ 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`,
})

test('run `cli --stdin` with input from stdin with \\r\\n', macro.testCLI, {
args: ['--stdin'],
message: 'The line feed should be CRLF in output',
stdin: `{\r\n "description": "Description",\r\n "name": "Name"\r\n}\r\n`,
})
45 changes: 45 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,43 @@ 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"␊
}␊
`,
},
}

## run `cli --stdin` with input from stdin with \r\n

> The line feed should be CRLF in output
{
args: [
'--stdin',
],
fixtures: [],
result: {
errorCode: null,
stderr: '',
stdout: `{␍␊
"name": "Name",␍␊
"description": "Description"␍␊
}␍␊
`,
},
}
Binary file modified tests/snapshots/cli.js.snap
Binary file not shown.

0 comments on commit cefbbcb

Please sign in to comment.