This commit is contained in:
Timo Hocker 2020-05-27 17:39:32 +02:00
parent 1d2137feda
commit e5cb4d3dbe
8 changed files with 305 additions and 287 deletions

View File

@ -19,7 +19,7 @@ const { InteractiveOptions } = require ('./dist/lib/index.js');
{ name: 'num', type: 'number', env: 'TEST_NUM' }, { name: 'num', type: 'number', env: 'TEST_NUM' },
{ name: 'arr', type: 'array', env: 'TEST_ARR' }, { name: 'arr', type: 'array', env: 'TEST_ARR' },
{ name: 'fld', type: 'folder', env: 'TEST_FOLDER' } { name: 'fld', type: 'folder', env: 'TEST_FOLDER' }
], { exit_on_interrupt: true }); ], { exit_on_interrupt: true, configs: [ 'test.json' ] });
await reader.parse (); await reader.parse ();
console.log (reader.serialize (true)); console.log (reader.serialize (true));
}) (); }) ();

View File

@ -1,5 +1,9 @@
# Changelog # Changelog
## 1.7.0
parsing config files with [hjson](https://github.com/hjson/hjson-js)
## 1.6.0 ## 1.6.0
option to change the message displayed on invalid input option to change the message displayed on invalid input
@ -19,8 +23,8 @@ new source: config files
## 1.2.0 ## 1.2.0
preset option: preset option: an array of strings to choose from when using one of the string
an array of strings to choose from when using one of the string types types
## 1.1.0 ## 1.1.0

2
Jenkinsfile vendored
View File

@ -5,7 +5,7 @@ pipeline {
VERSION = VersionNumber([ VERSION = VersionNumber([
versionNumberString: versionNumberString:
'${BUILDS_ALL_TIME}', '${BUILDS_ALL_TIME}',
versionPrefix: '1.6.', versionPrefix: '1.7.',
worstResultForIncrement: 'SUCCESS' worstResultForIncrement: 'SUCCESS'
]) ])
} }

View File

@ -1,6 +1,6 @@
# @sapphirecode/console-app # @sapphirecode/console-app
version: 1.6.x version: 1.7.x
read parameters from env, console args or interactively read parameters from env, console args or interactively
@ -30,7 +30,7 @@ const reader = new InteractiveOptions([
description: 'the switch foo', // description in the help page (optional) description: 'the switch foo', // description in the help page (optional)
message: 'should foo be true?', // message when asking interactively (optional) message: 'should foo be true?', // message when asking interactively (optional)
preset: [], // preset choices for string and path types (optional) preset: [], // preset choices for string and path types (optional)
error: 'wrong input' // message to display when the user gives invalid input error: 'wrong input', // message to display when the user gives invalid input
}, },
]); ]);
@ -66,7 +66,10 @@ config files can import other config files with #include. example:
} }
``` ```
the option exit_on_interrupt determines whether an error should be thrown or the process should exit when the user presses control + c in an interactive prompt. config files are parsed using [hjson](https://github.com/hjson/hjson-js)
the option exit_on_interrupt determines whether an error should be thrown or the
process should exit when the user presses control + c in an interactive prompt.
```js ```js
const reader = new InteractiveOptions([], { const reader = new InteractiveOptions([], {
@ -74,7 +77,7 @@ const reader = new InteractiveOptions([], {
env: true, env: true,
interactive: true, interactive: true,
configs: ['json files to search for options'], configs: ['json files to search for options'],
exit_on_interrupt: true exit_on_interrupt: true,
}); });
``` ```

View File

@ -9,6 +9,7 @@
import { dirname, join } from 'path'; import { dirname, join } from 'path';
import fs from 'fs-extra'; import fs from 'fs-extra';
import { run_regex } from '@sapphirecode/utilities'; import { run_regex } from '@sapphirecode/utilities';
import hjson from 'hjson';
import { OptionProcess } from '../Option'; import { OptionProcess } from '../Option';
import { OptionSource } from './OptionSource'; import { OptionSource } from './OptionSource';
@ -37,7 +38,7 @@ export class ConfigSource extends OptionSource {
for (const key of Object.keys (data)) for (const key of Object.keys (data))
obj[key] = data[key]; obj[key] = data[key];
} }
const config = JSON.parse (contents.replace (regex, '')); const config = hjson.parse (contents);
for (const key of Object.keys (config)) for (const key of Object.keys (config))
obj[key] = config[key]; obj[key] = config[key];

View File

@ -13,6 +13,7 @@
"@ava/typescript": "^1.1.1", "@ava/typescript": "^1.1.1",
"@sapphirecode/eslint-config-ts": "^1.1.4", "@sapphirecode/eslint-config-ts": "^1.1.4",
"@types/fs-extra": "^9.0.0", "@types/fs-extra": "^9.0.0",
"@types/hjson": "^2.4.1",
"@types/yargs": "^15.0.5", "@types/yargs": "^15.0.5",
"ava": "^3.8.2", "ava": "^3.8.2",
"eslint": "^7.0.0", "eslint": "^7.0.0",
@ -35,6 +36,7 @@
"@sapphirecode/utilities": "^1.3.4", "@sapphirecode/utilities": "^1.3.4",
"enquirer": "^2.3.5", "enquirer": "^2.3.5",
"fs-extra": "^9.0.0", "fs-extra": "^9.0.0",
"hjson": "^3.2.1",
"yargs": "^15.3.1" "yargs": "^15.3.1"
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -324,6 +324,11 @@
"@types/minimatch" "*" "@types/minimatch" "*"
"@types/node" "*" "@types/node" "*"
"@types/hjson@^2.4.1":
version "2.4.1"
resolved "https://registry.yarnpkg.com/@types/hjson/-/hjson-2.4.1.tgz#a6fc8649777b63f979193d2d5c108a71b3d414d9"
integrity sha512-yXq/C73UHM8GQc6RYJnUXUgxudr2Q9227Iawhkp03YCnfJJTc+6LJnnVLx+UR/Dvw6imO5Q3vpGNmR9IRBI0JQ==
"@types/json-schema@^7.0.3": "@types/json-schema@^7.0.3":
version "7.0.4" version "7.0.4"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339"
@ -1646,6 +1651,11 @@ hasha@^5.0.0:
is-stream "^2.0.0" is-stream "^2.0.0"
type-fest "^0.8.0" type-fest "^0.8.0"
hjson@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/hjson/-/hjson-3.2.1.tgz#20de41dc87fc9a10d1557d0230b0e02afb1b09ac"
integrity sha512-OhhrFMeC7dVuA1xvxuXGTv/yTdhTvbe8hz+3LgVNsfi9+vgz0sF/RrkuX8eegpKaMc9cwYwydImBH6iePoJtdQ==
hosted-git-info@^2.1.4: hosted-git-info@^2.1.4:
version "2.8.8" version "2.8.8"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.8.tgz#7539bd4bc1e0e0a895815a2e0262420b12858488" resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.8.tgz#7539bd4bc1e0e0a895815a2e0262420b12858488"