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 CHANGED
@@ -1,15 +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'))
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
- 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
- function checkPath(path) {
7
- var path = resolve(process.cwd(), path)
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 (!safe_path) {
16
- safe_path = process.argv.slice(2).reduce(function(memo, argument) {
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 safe_path
19
+ return safePath
22
20
  } else {
23
- return checkPath(path)
21
+ return resolvePath(path)
24
22
  }
25
23
  }
26
24
 
27
- module.exports = {
28
- load: function(path) {
29
- path = getPathToUse(path)
30
- if (path) {
31
- try {
32
- return require(path)
33
- } catch (e) {
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
@@ -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.0",
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).