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