qiao-config
Advanced tools
Comparing version 0.4.1 to 1.0.0
240
index.js
@@ -1,1 +0,239 @@ | ||
module.exports = require('./lib/qiao-config.js'); | ||
'use strict'; | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
// fs | ||
/** | ||
* write file | ||
* @param {*} filePath | ||
* @param {*} data | ||
*/ | ||
const writeFile = (filePath, data) => { | ||
fs.writeFileSync(filePath, data); | ||
}; | ||
/** | ||
* read file | ||
* @param {*} filePath | ||
* @returns | ||
*/ | ||
const readFile = (filePath) => { | ||
try{ | ||
// not exists write file | ||
if(!isExists(filePath)) writeFile(filePath, ''); | ||
return fs.readFileSync(filePath, {encoding:'utf8'}); | ||
}catch(e){ | ||
return null; | ||
} | ||
}; | ||
// is exists | ||
function isExists(filePath){ | ||
try{ | ||
fs.accessSync(filePath); | ||
return true; | ||
}catch(e){ | ||
return false; | ||
} | ||
} | ||
// io | ||
/** | ||
* clear | ||
* @param {*} filePath | ||
* @returns | ||
*/ | ||
const clear = (filePath) => { | ||
// check | ||
if(!filePath){ | ||
console.log('qiao-config:clear, need path'); | ||
return; | ||
} | ||
// io | ||
try{ | ||
writeFile(filePath, ''); | ||
}catch(e){ | ||
console.log(`qiao-config:clear, write file error ${e.message}`); | ||
} | ||
}; | ||
/** | ||
* all | ||
* @param {*} filePath | ||
* @returns | ||
*/ | ||
const all = (filePath) => { | ||
// check | ||
if(!filePath){ | ||
console.log('qiao-config:all, need path'); | ||
return; | ||
} | ||
let json; | ||
try{ | ||
const jsonStr = readFile(filePath); | ||
json = JSON.parse(jsonStr); | ||
}catch(e){ | ||
json = {}; | ||
} | ||
return json; | ||
}; | ||
/** | ||
* get | ||
* @param {*} filePath | ||
* @param {*} key | ||
* @returns | ||
*/ | ||
const get = (filePath, key) => { | ||
// check | ||
if(!filePath){ | ||
console.log('qiao-config:get, need path'); | ||
return; | ||
} | ||
if(typeof key == 'undefined'){ | ||
console.log('qiao-config:get, need key'); | ||
return; | ||
} | ||
// get | ||
const json = all(filePath); | ||
return json[key]; | ||
}; | ||
/** | ||
* set | ||
* @param {*} filePath | ||
* @param {*} key | ||
* @param {*} value | ||
* @returns | ||
*/ | ||
const set = (filePath, key, value) => { | ||
// check | ||
if(!filePath){ | ||
console.log('qiao-config:set, need path'); | ||
return; | ||
} | ||
if(typeof key == 'undefined'){ | ||
console.log('qiao-config:set, need key'); | ||
return; | ||
} | ||
// set | ||
const json = all(filePath); | ||
json[key] = value; | ||
// io | ||
try{ | ||
writeFile(filePath, JSON.stringify(json)); | ||
}catch(e){ | ||
console.log(`qiao-config:set, write file error ${e.message}`); | ||
} | ||
}; | ||
/** | ||
* del | ||
* @param {*} filePath | ||
* @param {*} key | ||
* @returns | ||
*/ | ||
const del = (filePath, key) => { | ||
// check | ||
if(!filePath){ | ||
console.log('qiao-config:del, need path'); | ||
return; | ||
} | ||
if(typeof key == 'undefined'){ | ||
console.log('qiao-config:del, need key'); | ||
return; | ||
} | ||
// get | ||
const v = get(filePath, key); | ||
if(!v) return; | ||
// del | ||
const json = all(filePath); | ||
delete json[key]; | ||
// io | ||
try{ | ||
writeFile(filePath, JSON.stringify(json)); | ||
}catch(e){ | ||
console.log(`qiao-config:del, write file error ${e.message}`); | ||
} | ||
}; | ||
// data | ||
/** | ||
* db | ||
* @param {*} dbPath | ||
*/ | ||
const db = (dbPath) => { | ||
const obj = {}; | ||
obj.path = dbPath; | ||
// clear | ||
obj.clear = () => { clearDB(obj.path); }; | ||
// all | ||
obj.all = () => { return listDB(obj.path); }; | ||
// config | ||
obj.config = (key, value) => { return configDB(obj.path, key, value); }; | ||
return obj; | ||
}; | ||
// clear db | ||
function clearDB(filePath){ | ||
clear(filePath); | ||
} | ||
// list db | ||
function listDB(filePath){ | ||
return all(filePath); | ||
} | ||
// config db | ||
function configDB(filePath, key, value){ | ||
// remove | ||
if(value === null){ | ||
del(filePath, key); | ||
return; | ||
} | ||
// get | ||
if(typeof value == 'undefined'){ | ||
return get(filePath, key); | ||
} | ||
// set | ||
set(filePath, key, value); | ||
} | ||
// path | ||
/** | ||
* qiao config | ||
*/ | ||
var index = (filePath) => { | ||
// path | ||
const defaultPath = path.resolve(__dirname, './config.json'); | ||
const finalPath = !filePath ? defaultPath : path.resolve(process.cwd(), filePath); | ||
// db | ||
return db(finalPath); | ||
}; | ||
module.exports = index; |
{ | ||
"name": "qiao-config", | ||
"version": "0.4.1", | ||
"description": "json config to local file", | ||
"keywords": [ | ||
"json", | ||
"config", | ||
"local", | ||
"file", | ||
"database" | ||
], | ||
"author": "uikoo9 <uikoo9@qq.com>", | ||
"homepage": "https://code.insistime.com/qiao-config", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/uikoo9/qiao-monorepo.git" | ||
}, | ||
"scripts": {}, | ||
"dependencies": { | ||
"qiao-file": "^1.0.5" | ||
}, | ||
"gitHead": "baba1fabfbcb292ba2217ef7448fee37a6676f40" | ||
"name": "qiao-config", | ||
"version": "1.0.0", | ||
"description": "json config to local file", | ||
"keywords": [ | ||
"json", | ||
"config", | ||
"local", | ||
"file", | ||
"database" | ||
], | ||
"author": "uikoo9 <uikoo9@qq.com>", | ||
"homepage": "https://code.insistime.com/qiao-config", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"module": "src/index.js", | ||
"sideEffets": false, | ||
"files": [ | ||
"src" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/uikoo9/qiao-monorepo.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/uikoo9/qiao-monorepo/issues" | ||
}, | ||
"scripts": { | ||
"build": "rollup --config _config.rollup.js", | ||
"eslint": "eslint . --ext .js -c _config.eslint.js", | ||
"eslintfix": "eslint . --ext .js --fix -c _config.eslint.js", | ||
"test": "jest --config _config.jest.js", | ||
"prepublish": "npm run build && npm run eslintfix && npm run test" | ||
}, | ||
"gitHead": "8744227cfc9c670735e950a01d4b4be02a7fe6a2" | ||
} |
@@ -1,29 +0,25 @@ | ||
# qiao-config | ||
json config to local file | ||
## qiao-config | ||
[![npm version](https://img.shields.io/npm/v/qiao-config.svg?style=flat-square)](https://www.npmjs.org/package/qiao-config) | ||
[![npm downloads](https://img.shields.io/npm/dm/qiao-config.svg?style=flat-square)](https://npm-stat.com/charts.html?package=qiao-config) | ||
## api | ||
### c | ||
```javascript | ||
'use strict'; | ||
nodejs下基于本地文件的config能力 | ||
var q = require('qiao-config'); | ||
## install | ||
```bash | ||
npm i qiao-config | ||
``` | ||
// default path | ||
var _c1 = q.c(); | ||
console.log(_c1); | ||
## db | ||
```javascript | ||
// default | ||
const db = require('qiao-config')(); | ||
// custom path | ||
var _c2 = q.c('../'); | ||
console.log(_c2); | ||
// custom | ||
const db = require('qiao-config')('your path'); | ||
``` | ||
## api | ||
### all | ||
```javascript | ||
'use strict'; | ||
var q = require('qiao-config'); | ||
var _c = q.c(); | ||
var s = _c.all(); | ||
console.log(s); // { test: 'hello' } | ||
db.all(); | ||
``` | ||
@@ -33,9 +29,3 @@ | ||
```javascript | ||
'use strict'; | ||
var q = require('qiao-config'); | ||
var _c = q.c(); | ||
_c.clear(); | ||
console.log(_c.all()); // {} | ||
db.clear(); | ||
``` | ||
@@ -45,22 +35,16 @@ | ||
```javascript | ||
'use strict'; | ||
// get | ||
db.config(key); | ||
var q = require('qiao-config'); | ||
var _c = q.c(); | ||
// set | ||
_c.config('test', 'hello'); | ||
console.log(_c.all()); // { test: 'hello' } | ||
db.config(key, value); | ||
// get | ||
var s = _c.config('test'); | ||
console.log(s); // hello | ||
// del | ||
_c.config('test', null); | ||
console.log(_c.all()); // {} | ||
db.config(key, null); | ||
``` | ||
## version | ||
### 0.0.4.20221118 | ||
1. 1.0.0 | ||
### 0.0.3.20201105 | ||
@@ -67,0 +51,0 @@ 1. c --> config |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11338
0
399
0
1
9
62
2
1
- Removedqiao-file@^1.0.5
- Removedqiao-file@1.0.9(transitive)