command-line-config 1.4.1 → 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 CHANGED
@@ -1,16 +1,18 @@
1
- // Test this by running: node example.js config.json
1
+ // Test this by running: node example.js
2
2
 
3
- var config = require('./index.js')
3
+ import * as commandLineConfig from './index.js'
4
+ import { inspect } from 'node:util'
4
5
 
5
- var obj = config.load()
6
+ let obj = commandLineConfig.load('example.json')
6
7
 
7
- console.log(require('util').inspect(obj))
8
+ console.log(inspect(obj))
8
9
 
9
10
  if (obj) {
10
11
  obj.wat = !obj.wat
11
12
 
12
- config.save(obj)
13
+ commandLineConfig.save(obj)
13
14
  }
14
15
 
15
- console.log(config.load('package.json'))config.save(obj, 'new_file.json')
16
- console.log(config.load('new_file.json'))
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,7 +1,8 @@
1
- var fs = require('fs')
2
- var resolve = require('path').resolve
1
+ import { existsSync, writeFileSync } from 'node:fs'
2
+ import { resolve } from 'path'
3
+ import { readFileSync } from 'node:fs'
3
4
 
4
- var safe_path = null
5
+ let safePath = null
5
6
 
6
7
  function resolvePath(path) {
7
8
  return resolve(process.cwd(), path)
@@ -11,31 +12,29 @@ function resolvePath(path) {
11
12
  // or returns a path based on the first argument passed in that resolves to a real file
12
13
  function getPathToUse(path) {
13
14
  if (typeof path !== 'string') {
14
- if (!safe_path) {
15
- safe_path = process.argv.slice(2).map(resolvePath).filter(fs.existsSync).shift() || null
15
+ if (!safePath) {
16
+ safePath = process.argv.slice(2).map(resolvePath).filter(existsSync).shift() || null
16
17
  }
17
18
 
18
- return safe_path
19
+ return safePath
19
20
  } else {
20
21
  return resolvePath(path)
21
22
  }
22
23
  }
23
24
 
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
- }
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)
33
32
  }
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
33
  }
34
+ return false
40
35
  }
41
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
@@ -0,0 +1,5 @@
1
+ {
2
+ "because I can": "butts",
3
+ "because I say so": "why not?",
4
+ "wat": true
5
+ }
package/package.json CHANGED
@@ -1,5 +1,6 @@
1
1
  {
2
- "name": "command-line-config",
3
- "version": "1.4.1",
4
- "main": "./index.js"
2
+ "name": "command-line-config",
3
+ "version": "2.0.0",
4
+ "main": "./index.js",
5
+ "type": "module"
5
6
  }
package/readme.md CHANGED
@@ -1,23 +1,35 @@
1
- Quick and dirty loading/saving of JSON files.
2
-
1
+ # Command Line Config
3
2
 
4
- var settings_access = require('command-line-config')
3
+ ## Install
4
+ `npm i command-line-config`
5
+ > Breaking change in version 2: ESM style exports
5
6
 
6
- var settings = settings_access.load('./some_file.json')
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
- var settings = settings_access.load()
16
- settings.wat = "WHERE DID YOU COME FROM"
17
- settings_access.save(settings)
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).