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

option to run jq as detached process #636

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,41 @@ jq.run('.', ['/path/to/file.json'], { output: 'json', sort: true }).then(console
// },
```

### cwd

| Description | Values | Default |
|:--------------------------------:|:----------:|:-------------:|
| Set working dir for `jq` process | valid path | process.cwd() |


```javascript
jq.run('.', ['file.json'], { output: 'json', sort: true }, '/path/to').then(console.log)
// {
// "a": 2,
// "b": 1
// },
```

### detached

| Description | Values | Default |
|:------------------------------------:|:---------------:|:--------:|
| Run `jq` process as detached process | `true`, `false` | `false` |

By default `jq` process will run 'attached' to the main process. That means that any interrupt signal main process receives will be propagated to `jq` process. For example, if main process receives `SIGTERM`, `jq` will also receive it and exit immediately.

However, in some cases you might **not** want `jq` to exit immediately and let it exit normally. For example, if you want to implement a graceful shutdown - main process receives `SIGTERM`, it finishes processing current json file and exits after processing is completed.

To achieve that run `jq` detached and NodeJS will not propagate `SIGTERM` to `jq` process allowing it to run until it completes.

```javascript
jq.run('.', ['file.json'], { output: 'json', sort: true }, undefined, true).then(console.log)
// {
// "a": 2,
// "b": 1
// },
```

## Projects using **node-jq**

- **[atom-jq](https://github.com/sanack/atom-jq)**: an [Atom](https://atom.io/) package for manipulating JSON
Expand Down
2 changes: 1 addition & 1 deletion src/exec.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function(command: string, args: string[], stdin: string, cwd?: string): Promise<string>
export default function(command: string, args: string[], stdin: string, cwd?: string, detached?: boolean): Promise<string>
4 changes: 2 additions & 2 deletions src/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import stripFinalNewline from 'strip-final-newline'

const TEN_MEBIBYTE = 1024 * 1024 * 10

const exec = (command, args, stdin, cwd) => {
const exec = (command, args, stdin, cwd, detached) => {
return new Promise((resolve, reject) => {
let stdout = ''
let stderr = ''

const spawnOptions = { maxBuffer: TEN_MEBIBYTE, cwd, env: {} }
const spawnOptions = { maxBuffer: TEN_MEBIBYTE, cwd, detached, env: {} }

const process = childProcess.spawn(command, args, spawnOptions)

Expand Down
2 changes: 1 addition & 1 deletion src/jq.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { PartialOptions } from "./options"

export function run(filter: string, json: any, options?: PartialOptions, jqPath?: string, cwd?: string): Promise<object | string>
export function run(filter: string, json: any, options?: PartialOptions, jqPath?: string, cwd?: string, detached?: boolean): Promise<object | string>
4 changes: 2 additions & 2 deletions src/jq.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import exec from './exec'
import { commandFactory } from './command'

export const run = (filter, json, options = {}, jqPath, cwd) => {
export const run = (filter, json, options = {}, jqPath, cwd, detached) => {
return new Promise((resolve, reject) => {
const { command, args, stdin } = commandFactory(
filter,
Expand All @@ -10,7 +10,7 @@ export const run = (filter, json, options = {}, jqPath, cwd) => {
jqPath
)

exec(command, args, stdin, cwd)
exec(command, args, stdin, cwd, detached)
.then((stdout) => {
if (options.output === 'json') {
let result
Expand Down
11 changes: 11 additions & 0 deletions src/jq.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,15 @@ describe('jq core', () => {
done(error)
})
})

it('should run as detached', done => {
run(FILTER_VALID, PATH_JSON_FIXTURE, undefined, undefined, undefined, true)
.then(output => {
expect(output).to.equal('"git"')
done()
})
.catch(error => {
done(error)
})
})
})
Loading