command-line-config 1.4.1 → 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.
@@ -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,5 +1,21 @@
1
1
  {
2
- "name": "command-line-config",
3
- "version": "1.4.1",
4
- "main": "./index.js"
5
- }
2
+ "name": "command-line-config",
3
+ "version": "2.1.0",
4
+ "main": "./index.js",
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,20 +1,33 @@
1
- Quick and dirty loading/saving of JSON files.
1
+ # Command Line Config
2
+
3
+ ## Install
4
+
5
+ `npm i command-line-config`
6
+ > Breaking change in version 2: ESM style exports
2
7
 
8
+ ## Usage
3
9
 
4
- var settings_access = require('command-line-config')
10
+ Quick and dirty loading/saving of JSON files.
11
+
12
+ ```js
13
+ import * as settings_access from 'command-line-config'
14
+ //or...
15
+ //import { load, save } from 'command-line-config'
5
16
 
6
- var settings = settings_access.load('./some_file.json')
7
- settings.dumb = false
8
- settings_access.save(settings, './some_file.json')
17
+ let settings = settings_access.load('./some_file.json')
18
+ settings.dumb = false
19
+ settings_access.save(settings, './some_file.json')
20
+ ```
9
21
 
10
22
  All paths are interpreted relative to the current working directory.
11
23
 
12
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`
13
25
 
14
-
15
- var settings = settings_access.load()
16
- settings.wat = "WHERE DID YOU COME FROM"
17
- settings_access.save(settings)
26
+ ```js
27
+ let settings = settings_access.load()
28
+ settings.wat = "WHERE DID YOU COME FROM"
29
+ settings_access.save(settings)
30
+ ```
18
31
 
19
32
  Which command-line argument will be interpreted as a file to be opened? The first one to resolve to an actual file, of course!
20
33
 
package/example.js DELETED
@@ -1,16 +0,0 @@
1
- // Test this by running: node example.js config.json
2
-
3
- var config = require('./index.js')
4
-
5
- var obj = config.load()
6
-
7
- console.log(require('util').inspect(obj))
8
-
9
- if (obj) {
10
- obj.wat = !obj.wat
11
-
12
- config.save(obj)
13
- }
14
-
15
- console.log(config.load('package.json'))config.save(obj, 'new_file.json')
16
- console.log(config.load('new_file.json'))
package/example.json DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "because I can": "butts",
3
- "because I say so": "why not?"
4
- }
package/index.js DELETED
@@ -1,41 +0,0 @@
1
- var fs = require('fs')
2
- var resolve = require('path').resolve
3
-
4
- var safe_path = null
5
-
6
- function resolvePath(path) {
7
- return resolve(process.cwd(), path)
8
- }
9
-
10
- // Returns a resolved path based on the string passed in,
11
- // or returns a path based on the first argument passed in that resolves to a real file
12
- function getPathToUse(path) {
13
- if (typeof path !== 'string') {
14
- if (!safe_path) {
15
- safe_path = process.argv.slice(2).map(resolvePath).filter(fs.existsSync).shift() || null
16
- }
17
-
18
- return safe_path
19
- } else {
20
- return resolvePath(path)
21
- }
22
- }
23
-
24
- module.exports = {
25
- load: function(path) {
26
- path = getPathToUse(path)
27
- if (fs.existsSync(path)) {
28
- try {
29
- return require(path)
30
- } catch (e) {
31
- console.error(e.message)
32
- }
33
- }
34
- return false
35
- },
36
- save: function(obj, path) {
37
- var path = getPathToUse(path)
38
- return path ? fs.writeFileSync(path, JSON.stringify(obj, null, "\t")) : false
39
- }
40
- }
41
-