WshConfigStore

The config manager for WSH (Windows Script Host) reads/writes configuration values in a JSON file.

tuckn/WshModeJs basic applications structure

WshBasicApps
  ├─ WshCommander (./dist/module.js)
  ├─ WshConfigStore - This repository (./dist/module.js)
  ├─ WshDotEnv (./dist/module.js)
  ├─ WshLogger (./dist/module.js)
  └─ WshModeJs (./dist/bundle.js)

WshBasicApps can use all the above modules.

Operating environment

Works on JScript in Windows.

Installation

(1) Create a directory of your WSH project.

D:\> mkdir MyWshProject
D:\> cd MyWshProject

(2) Download this ZIP and unzip or Use the following git command.

> git clone https://github.com/tuckn/WshConfigStore.git ./WshModules/WshConfigStore
or
> git submodule add https://github.com/tuckn/WshConfigStore.git ./WshModules/WshConfigStore

(3) Create your JScript (.js) file. For Example,

D:\MyWshProject\
├─ MyScript.js <- Your JScript code will be written in this.
└─ WshModules\
    └─ WshConfigStore\
        └─ dist\
          └─ bundle.js

I recommend JScript (.js) file encoding to be UTF-8 [BOM, CRLF].

(4) Create your WSF packaging scripts file (.wsf).

D:\MyWshProject\
├─ Run.wsf <- WSH entry file
├─ MyScript.js
└─ WshModules\
    └─ WshConfigStore\
        └─ dist\
          └─ bundle.js

And you should include .../dist/bundle.js into the WSF file.
For Example, The content of the above Run.wsf is

<package>
  <job id = "run">
    <script language="JScript" src="./WshModules/WshConfigStore/dist/bundle.js"></script>
    <script language="JScript" src="./MyScript.js"></script>
  </job>
</package>

I recommend this WSH file (.wsf) encoding to be UTF-8 [BOM, CRLF].

Awesome! This WSH configuration allows you to use the following functions in JScript (.\MyScript.js).

Usage

Now the above .\MyScript.js (JScript) can read/write setting.json easily.
The default path is %CD%\.wsh\settings.json.

var conf = new Wsh.ConfigStore();
// Equal with `new Wsh.ConfigStore(null, { dirPath: 'cwd' });`

conf.path;
// Returns: %CD%\.wsh\settings.json

conf.store;
// Returns: An Object in the above settings.json.
// Ex: { objName: { propName: 'value' } }

// Gets the item.
conf.get(); // undefined
conf.get('objName'); // { propName: 'value' }
conf.get('objName.propName'); // 'value'
conf.get('propNameB'); // undefined

// Checks if store has the item.
conf.has('propNameB'); // false

// Sets the item and writes the JSON file.
conf.set('propNameB', 'New Value');
conf.store;
// Returns: { objName: { propName: 'value' }, propNameB: 'New Value' }

// Deletes the item.
conf.has('propNameB'); // true
conf.del('propNameB');
conf.has('propNameB'); // false

To Change Setting-Path

// Default
var conf = new Wsh.ConfigStore();
// Equal with `new Wsh.ConfigStore(null, { dirPath: 'cwd' });`
conf.path; // Returns: %CD%\.wsh\settings.json

// Portable
var conf = new Wsh.ConfigStore(null, { dirPath: 'portable' });
conf.path; // Returns: <Wsh Script Directory>\.wsh\settings.json

// UserProfile
var conf = new Wsh.ConfigStore('myStore', { dirPath: 'userProfile' });
conf.path; // Returns: %USERPROFILE%\myStore.json

// Specifying absolute directory path
var conf = new Wsh.ConfigStore(null, { dirPath: 'C:\\conf' });
conf.path; // Returns: C:\conf\settings.json

// Can use the date literal
var conf = new Wsh.ConfigStore('vals_#{yy-MM}', { dirPath: 'D:\\confs' });
conf.path; // Returns: C:\confs\vals_20-08.json

Together with another WshModeJs Apps

If you want to use it together with other WshModeJs Apps, install as following

> git clone https://github.com/tuckn/WshModeJs.git ./WshModules/WshModeJs
> git clone https://github.com/tuckn/WshCommander.git ./WshModules/WshCommander
> git clone https://github.com/tuckn/WshConfigStore.git ./WshModules/WshConfigStore
or
> git submodule add https://github.com/tuckn/WshModeJs.git ./WshModules/WshModeJs
> git submodule add https://github.com/tuckn/WshCommander.git ./WshModules/WshCommander
> git submodule add https://github.com/tuckn/WshConfigStore.git ./WshModules/WshConfigStore

The definition in the WSF packaging scripts file (.wsf) is as follows.

<package>
  <job id = "run">
    <script language="JScript" src="./WshModules/WshModeJs/dist/bundle.js"></script>
    <script language="JScript" src="./WshModules/WshCommander/dist/module.js"></script>
    <script language="JScript" src="./WshModules/WshConfigStore/dist/module.js"></script>
    <script language="JScript" src="./MyScript.js"></script>
  </job>
</package>

Please note the difference between .../dist/bundle.js and .../dist/module.js.

I recommend using WshBasicApps.
That includes all modules.

Dependency Modules

You can also use the following helper functions in your JScript (.\MyScript.js).

Documentation

See all specifications here and also below.

License

MIT

Copyright (c) 2020 Tuckn