command-line-config 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +4 -0
- package/dist/index.js +46 -0
- package/package.json +18 -3
- package/readme.md +3 -2
- package/example.js +0 -18
- package/example.json +0 -4
- package/index.js +0 -40
- package/new_file.json +0 -5
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
export type ReadFileOptions = Parameters<typeof readFileSync>[1];
|
|
3
|
+
export declare function load(path?: string, readFileOptions?: ReadFileOptions): unknown | undefined;
|
|
4
|
+
export declare function save(obj: unknown, path?: string): boolean;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { existsSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'path';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
let safePath = null;
|
|
5
|
+
function resolvePath(path) {
|
|
6
|
+
return resolve(process.cwd(), path);
|
|
7
|
+
}
|
|
8
|
+
// Returns a resolved path based on the string passed in,
|
|
9
|
+
// or returns a path based on the first argument passed in that resolves to a real file
|
|
10
|
+
function getPathToUse(path) {
|
|
11
|
+
if (typeof path !== 'string') {
|
|
12
|
+
if (!safePath) {
|
|
13
|
+
safePath = process.argv.slice(2).map(resolvePath).filter(existsSync).shift() || null;
|
|
14
|
+
}
|
|
15
|
+
return safePath;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return resolvePath(path);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function load(path, readFileOptions) {
|
|
22
|
+
const finalPath = getPathToUse(path);
|
|
23
|
+
if (finalPath && existsSync(finalPath)) {
|
|
24
|
+
try {
|
|
25
|
+
// Can return a string or a NonSharedBuffer
|
|
26
|
+
const buffer = readFileSync(finalPath, readFileOptions);
|
|
27
|
+
return JSON.parse(buffer.toString());
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
console.error(e.message);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// Used to return false, but caller expects an object, so something falsy that still fits the bill makes sense. Its either that or letting the error bubble up
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
export function save(obj, path) {
|
|
37
|
+
const resolvedPath = getPathToUse(path);
|
|
38
|
+
if (resolvedPath) {
|
|
39
|
+
// Maybe we should try/catch here and return false if it fails?
|
|
40
|
+
// Load swallows errors, but save doesn't, which is a little weird
|
|
41
|
+
writeFileSync(resolvedPath, JSON.stringify(obj, null, '\t'));
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "command-line-config",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"main": "./index.js",
|
|
5
|
-
"type": "module"
|
|
6
|
-
|
|
5
|
+
"type": "module",
|
|
6
|
+
"devDependencies": {
|
|
7
|
+
"@isoftdata/prettier-config-base": "^1.0.3",
|
|
8
|
+
"@tsconfig/node24": "^24.0.4",
|
|
9
|
+
"@types/node": "^25.2.3",
|
|
10
|
+
"typescript": "^5.9.3"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist/**/*.js",
|
|
14
|
+
"dist/**/*.d.ts"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc",
|
|
18
|
+
"build-watch": "tsc -w"
|
|
19
|
+
},
|
|
20
|
+
"prettier": "@isoftdata/prettier-config-base"
|
|
21
|
+
}
|
package/readme.md
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# Command Line Config
|
|
2
2
|
|
|
3
3
|
## Install
|
|
4
|
+
|
|
4
5
|
`npm i command-line-config`
|
|
5
6
|
> Breaking change in version 2: ESM style exports
|
|
6
7
|
|
|
7
8
|
## Usage
|
|
8
9
|
|
|
9
10
|
Quick and dirty loading/saving of JSON files.
|
|
11
|
+
|
|
10
12
|
```js
|
|
11
13
|
import * as settings_access from 'command-line-config'
|
|
12
14
|
//or...
|
|
@@ -16,6 +18,7 @@ let settings = settings_access.load('./some_file.json')
|
|
|
16
18
|
settings.dumb = false
|
|
17
19
|
settings_access.save(settings, './some_file.json')
|
|
18
20
|
```
|
|
21
|
+
|
|
19
22
|
All paths are interpreted relative to the current working directory.
|
|
20
23
|
|
|
21
24
|
If you don't specify a path to load, the module assumes the file was passed in via the command-line, like so: `node your-script.js config-file.json`
|
|
@@ -31,5 +34,3 @@ Which command-line argument will be interpreted as a file to be opened? The fir
|
|
|
31
34
|
All file-system interactions are done synchronously, which means you most likely only want to be interacting with it when your app launches/terminates (which is the most likely use case anyway).
|
|
32
35
|
|
|
33
36
|
Written for ISoft Data Systems, licensed [WTFPL](http://wtfpl2.com).
|
|
34
|
-
|
|
35
|
-
Moved to GitHub at [SVN revision 27057](http://svn.isoftdata.com/Web/javascript-trunk/command-line-config/?p=27057).
|
package/example.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
// Test this by running: node example.js
|
|
2
|
-
|
|
3
|
-
import * as commandLineConfig from './index.js'
|
|
4
|
-
import { inspect } from 'node:util'
|
|
5
|
-
|
|
6
|
-
let obj = commandLineConfig.load('example.json')
|
|
7
|
-
|
|
8
|
-
console.log(inspect(obj))
|
|
9
|
-
|
|
10
|
-
if (obj) {
|
|
11
|
-
obj.wat = !obj.wat
|
|
12
|
-
|
|
13
|
-
commandLineConfig.save(obj)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
console.log(commandLineConfig.load('package.json'))
|
|
17
|
-
commandLineConfig.save(obj, 'new_file.json')
|
|
18
|
-
console.log(commandLineConfig.load('new_file.json'))
|
package/example.json
DELETED
package/index.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { existsSync, writeFileSync } from 'node:fs'
|
|
2
|
-
import { resolve } from 'path'
|
|
3
|
-
import { readFileSync } from 'node:fs'
|
|
4
|
-
|
|
5
|
-
let safePath = null
|
|
6
|
-
|
|
7
|
-
function resolvePath(path) {
|
|
8
|
-
return resolve(process.cwd(), path)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// Returns a resolved path based on the string passed in,
|
|
12
|
-
// or returns a path based on the first argument passed in that resolves to a real file
|
|
13
|
-
function getPathToUse(path) {
|
|
14
|
-
if (typeof path !== 'string') {
|
|
15
|
-
if (!safePath) {
|
|
16
|
-
safePath = process.argv.slice(2).map(resolvePath).filter(existsSync).shift() || null
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return safePath
|
|
20
|
-
} else {
|
|
21
|
-
return resolvePath(path)
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function load(path, readFileOptions) {
|
|
26
|
-
path = getPathToUse(path)
|
|
27
|
-
if (existsSync(path)) {
|
|
28
|
-
try {
|
|
29
|
-
return JSON.parse(readFileSync(path, readFileOptions))
|
|
30
|
-
} catch (e) {
|
|
31
|
-
console.error(e.message)
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return false
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function save(obj, path) {
|
|
38
|
-
path = getPathToUse(path)
|
|
39
|
-
return path ? writeFileSync(path, JSON.stringify(obj, null, '\t')) : false
|
|
40
|
-
}
|