Skip to content

Commit

Permalink
feat(*): typescript definition support
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Andersen committed Aug 12, 2019
1 parent 38a3e86 commit c61427f
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 31 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test": "nyc mocha --require babel-core/register",
"test:watch": "npm test -- --watch",
"lint": "standard --verbose | snazzy",
"build": "babel src -d lib",
"build": "babel src -d lib --copy-files",
"postinstall": "npm run install-binary",
"coverage": "nyc report --reporter=lcov",
"precodeclimate": "npm run coverage",
Expand Down Expand Up @@ -48,6 +48,7 @@
],
"dependencies": {
"@hapi/joi": "16.0.0-preview",
"@types/hapi__joi": "^15.0.3",
"bin-build": "^3.0.0",
"download": "^7.1.0",
"is-valid-path": "^0.1.1",
Expand Down
15 changes: 15 additions & 0 deletions src/command.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PartialOptions } from "./options";

export const FILTER_UNDEFINED_ERROR =
'node-jq: invalid filter argument supplied: "undefined"'
export const INPUT_JSON_UNDEFINED_ERROR =
'node-jq: invalid json object argument supplied: "undefined"'
export const INPUT_STRING_ERROR =
'node-jq: invalid json string argument supplied'

interface ICommand {
command: string,
args: string[],
stdin: string
}
export function commandFactory(filter: string, json: any, options?: PartialOptions, jqPath?: string): ICommand
3 changes: 3 additions & 0 deletions src/exec.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as childProcess from "child_process";

export default function(command: string, args: string[], stdin: string, spawnOptions?: childProcess.SpawnOptions): Promise<string>
11 changes: 11 additions & 0 deletions src/jq.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as childProcess from "child_process";
import { PartialOptions } from "./options"

interface run {
(filter: string, json: any, options?: PartialOptions, jqPath?: string, spawnOptions?: childProcess.SpawnOptions): Promise<object | string>
}
export class JQ {
constructor(jqPath?: string, spawnOptions?: childProcess.SpawnOptions);
run: run;
}
export const run: run;
16 changes: 16 additions & 0 deletions src/options.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as Joi from "@hapi/joi";

export const optionsSchema: Joi.SchemaLike
export const preSpawnSchema: Joi.SchemaLike
export const spawnSchema: Joi.SchemaLike
export function parseOptions(options: PartialOptions, filter: string, json: any): any
export const optionDefaults: IOptions;
interface IOptions {
color: boolean,
locations: string[],
output: string,
raw: boolean,
slurp: boolean,
sort: boolean,
}
export type PartialOptions = Partial<IOptions>
8 changes: 8 additions & 0 deletions src/utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const INVALID_PATH_ERROR =
'node-jq: invalid path argument supplied (not a valid path)'
export const INVALID_JSON_PATH_ERROR =
'node-jq: invalid path argument supplied (not a .json file)'

export function isJSONPath(path: any): boolean

export function validateJSONPath(path: any): boolean
44 changes: 14 additions & 30 deletions test/command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,29 @@ import { expect } from 'chai'
import path from 'path'

import { commandFactory } from '../src/command'
import { FILTER_UNDEFINED_ERROR } from '../src/command'
import { INVALID_PATH_ERROR, INVALID_JSON_PATH_ERROR } from '../src/utils'

const PATH_ROOT = path.join(__dirname, '..')
const PATH_ASTERISK_FIXTURE = path.join(PATH_ROOT, 'src', '*.js')
const PATH_FIXTURES = path.join('test', 'fixtures')

const PATH_JSON_FIXTURE = path.join(PATH_FIXTURES, '1.json')
const PATH_JS_FIXTURE = path.join(PATH_FIXTURES, '1.js')
const PATH_LARGE_JSON_FIXTURE = path.join(PATH_FIXTURES, 'large.json')
const PATH_VARIABLE_JSON_FIXTURE = path.join(PATH_FIXTURES, 'var.json')

const FIXTURE_JSON = require('./fixtures/1.json')
const FIXTURE_JSON_STRING = JSON.stringify(FIXTURE_JSON, null, 2)

const FILTER_VALID = '.repository.type'
const FILTER_INVALID = 'invalid'
const FILTER_WITH_VARIABLE =
'[ . as $x | .user[] | {"user": ., "site": $x.site} ]'

const ERROR_INVALID_FILTER = /invalid/

const EMPTY_OPTION_RESULT = {
command: path.join(__dirname, '../bin/jq'),
args: [
FILTER_VALID,
PATH_JSON_FIXTURE
],
stdin: ''
command: path.join(__dirname, '../bin/jq'),
args: [
FILTER_VALID,
PATH_JSON_FIXTURE
],
stdin: ''
}

describe('command', () => {
it('factory should accept undefined options', done => {
try {
const result = commandFactory(FILTER_VALID, PATH_JSON_FIXTURE)
expect(result).to.deep.equal(EMPTY_OPTION_RESULT)
it('factory should accept undefined options', done => {
try {
const result = commandFactory(FILTER_VALID, PATH_JSON_FIXTURE)
expect(result).to.deep.equal(EMPTY_OPTION_RESULT)
done()
} catch (e) {
done(e)
}
})
} catch (e) {
done(e)
}
})
})
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,13 @@
dependencies:
defer-to-connect "^1.0.1"

"@types/hapi__joi@*", "@types/hapi__joi@^15.0.3":
version "15.0.3"
resolved "https://registry.yarnpkg.com/@types/hapi__joi/-/hapi__joi-15.0.3.tgz#eeeca99e3829636975bf39532d6e8279409c78f2"
integrity sha512-kncvQstpAQSjQ+J+tL2oYerjOEepv+yJHKM/JD/NmFZyDcy3rYOBcMJhdHRDjBxuSZu3ipGECszhgtmug9VSuw==
dependencies:
"@types/hapi__joi" "*"

"@types/normalize-package-data@^2.4.0":
version "2.4.0"
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
Expand Down

0 comments on commit c61427f

Please sign in to comment.