qiao-config
Advanced tools
Comparing version 1.0.0 to 1.0.1
298
index.js
@@ -1,5 +0,5 @@ | ||
'use strict'; | ||
"use strict"; | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var path = require("path"); | ||
var fs = require("fs"); | ||
@@ -11,6 +11,6 @@ // fs | ||
* @param {*} filePath | ||
* @param {*} data | ||
* @param {*} data | ||
*/ | ||
const writeFile = (filePath, data) => { | ||
fs.writeFileSync(filePath, data); | ||
fs.writeFileSync(filePath, data); | ||
}; | ||
@@ -20,25 +20,25 @@ | ||
* read file | ||
* @param {*} filePath | ||
* @returns | ||
* @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; | ||
} | ||
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; | ||
} | ||
function isExists(filePath) { | ||
try { | ||
fs.accessSync(filePath); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
@@ -50,18 +50,18 @@ | ||
* clear | ||
* @param {*} filePath | ||
* @returns | ||
* @param {*} filePath | ||
* @returns | ||
*/ | ||
const clear = (filePath) => { | ||
// check | ||
if(!filePath){ | ||
console.log('qiao-config:clear, need path'); | ||
return; | ||
} | ||
// 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}`); | ||
} | ||
// io | ||
try { | ||
writeFile(filePath, ""); | ||
} catch (e) { | ||
console.log(`qiao-config:clear, write file error ${e.message}`); | ||
} | ||
}; | ||
@@ -71,22 +71,22 @@ | ||
* all | ||
* @param {*} filePath | ||
* @returns | ||
* @param {*} filePath | ||
* @returns | ||
*/ | ||
const all = (filePath) => { | ||
// check | ||
if(!filePath){ | ||
console.log('qiao-config:all, need path'); | ||
return; | ||
} | ||
// check | ||
if (!filePath) { | ||
console.log("qiao-config:all, need path"); | ||
return; | ||
} | ||
let json; | ||
try{ | ||
const jsonStr = readFile(filePath); | ||
let json; | ||
try { | ||
const jsonStr = readFile(filePath); | ||
json = JSON.parse(jsonStr); | ||
}catch(e){ | ||
json = {}; | ||
} | ||
json = JSON.parse(jsonStr); | ||
} catch (e) { | ||
json = {}; | ||
} | ||
return json; | ||
return json; | ||
}; | ||
@@ -96,20 +96,20 @@ | ||
* get | ||
* @param {*} filePath | ||
* @param {*} key | ||
* @returns | ||
* @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; | ||
} | ||
// 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]; | ||
// get | ||
const json = all(filePath); | ||
return json[key]; | ||
}; | ||
@@ -119,28 +119,28 @@ | ||
* set | ||
* @param {*} filePath | ||
* @param {*} key | ||
* @param {*} value | ||
* @returns | ||
* @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; | ||
} | ||
// 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; | ||
// 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}`); | ||
} | ||
// io | ||
try { | ||
writeFile(filePath, JSON.stringify(json)); | ||
} catch (e) { | ||
console.log(`qiao-config:set, write file error ${e.message}`); | ||
} | ||
}; | ||
@@ -150,31 +150,31 @@ | ||
* del | ||
* @param {*} filePath | ||
* @param {*} key | ||
* @returns | ||
* @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; | ||
} | ||
// 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; | ||
// get | ||
const v = get(filePath, key); | ||
if (!v) return; | ||
// del | ||
const json = all(filePath); | ||
delete json[key]; | ||
// 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}`); | ||
} | ||
// io | ||
try { | ||
writeFile(filePath, JSON.stringify(json)); | ||
} catch (e) { | ||
console.log(`qiao-config:del, write file error ${e.message}`); | ||
} | ||
}; | ||
@@ -186,46 +186,52 @@ | ||
* db | ||
* @param {*} dbPath | ||
* @param {*} dbPath | ||
*/ | ||
const db = (dbPath) => { | ||
const obj = {}; | ||
const obj = {}; | ||
obj.path = dbPath; | ||
obj.path = dbPath; | ||
// clear | ||
obj.clear = () => { clearDB(obj.path); }; | ||
// clear | ||
obj.clear = () => { | ||
clearDB(obj.path); | ||
}; | ||
// all | ||
obj.all = () => { return listDB(obj.path); }; | ||
// all | ||
obj.all = () => { | ||
return listDB(obj.path); | ||
}; | ||
// config | ||
obj.config = (key, value) => { return configDB(obj.path, key, value); }; | ||
// config | ||
obj.config = (key, value) => { | ||
return configDB(obj.path, key, value); | ||
}; | ||
return obj; | ||
return obj; | ||
}; | ||
// clear db | ||
function clearDB(filePath){ | ||
clear(filePath); | ||
function clearDB(filePath) { | ||
clear(filePath); | ||
} | ||
// list db | ||
function listDB(filePath){ | ||
return all(filePath); | ||
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); | ||
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); | ||
} | ||
@@ -239,10 +245,12 @@ | ||
var index = (filePath) => { | ||
// path | ||
const defaultPath = path.resolve(__dirname, './config.json'); | ||
const finalPath = !filePath ? defaultPath : path.resolve(process.cwd(), filePath); | ||
// path | ||
const defaultPath = path.resolve(__dirname, "./config.json"); | ||
const finalPath = !filePath | ||
? defaultPath | ||
: path.resolve(process.cwd(), filePath); | ||
// db | ||
return db(finalPath); | ||
// db | ||
return db(finalPath); | ||
}; | ||
module.exports = index; |
{ | ||
"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" | ||
"name": "qiao-config", | ||
"version": "1.0.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", | ||
"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", | ||
"test": "jest --config _config.jest.js" | ||
}, | ||
"gitHead": "10e87c4bb8a4dd7bde6ea4aac9ee50aa2ecb9fc2" | ||
} |
## 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) | ||
nodejs下基于本地文件的config能力 | ||
nodejs 下基于本地文件的 config 能力 | ||
## install | ||
```bash | ||
@@ -13,12 +15,15 @@ npm i qiao-config | ||
## db | ||
```javascript | ||
// default | ||
const db = require('qiao-config')(); | ||
const db = require("qiao-config")(); | ||
// custom | ||
const db = require('qiao-config')('your path'); | ||
const db = require("qiao-config")("your path"); | ||
``` | ||
## api | ||
### all | ||
```javascript | ||
@@ -29,2 +34,3 @@ db.all(); | ||
### clear | ||
```javascript | ||
@@ -35,2 +41,3 @@ db.clear(); | ||
### config | ||
```javascript | ||
@@ -48,6 +55,9 @@ // get | ||
## version | ||
### 0.0.4.20221118 | ||
1. 1.0.0 | ||
### 0.0.3.20201105 | ||
1. c --> config | ||
@@ -57,2 +67,3 @@ 2. custom path | ||
### 0.0.2.20200901 | ||
1. del ok | ||
@@ -63,2 +74,3 @@ 2. c ok | ||
### 0.0.1.20200828 | ||
1. init project | ||
@@ -68,2 +80,2 @@ 2. get ok | ||
4. all ok | ||
5. clear ok | ||
5. clear ok |
182
src/_data.js
// io | ||
import { writeFile, readFile } from './_io.js'; | ||
import { writeFile, readFile } from "./_io.js"; | ||
/** | ||
* clear | ||
* @param {*} filePath | ||
* @returns | ||
* @param {*} filePath | ||
* @returns | ||
*/ | ||
export const clear = (filePath) => { | ||
// check | ||
if(!filePath){ | ||
console.log('qiao-config:clear, need path'); | ||
return; | ||
} | ||
// 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}`); | ||
} | ||
// io | ||
try { | ||
writeFile(filePath, ""); | ||
} catch (e) { | ||
console.log(`qiao-config:clear, write file error ${e.message}`); | ||
} | ||
}; | ||
@@ -26,22 +26,22 @@ | ||
* all | ||
* @param {*} filePath | ||
* @returns | ||
* @param {*} filePath | ||
* @returns | ||
*/ | ||
export const all = (filePath) => { | ||
// check | ||
if(!filePath){ | ||
console.log('qiao-config:all, need path'); | ||
return; | ||
} | ||
// check | ||
if (!filePath) { | ||
console.log("qiao-config:all, need path"); | ||
return; | ||
} | ||
let json; | ||
try{ | ||
const jsonStr = readFile(filePath); | ||
let json; | ||
try { | ||
const jsonStr = readFile(filePath); | ||
json = JSON.parse(jsonStr); | ||
}catch(e){ | ||
json = {}; | ||
} | ||
json = JSON.parse(jsonStr); | ||
} catch (e) { | ||
json = {}; | ||
} | ||
return json; | ||
return json; | ||
}; | ||
@@ -51,20 +51,20 @@ | ||
* get | ||
* @param {*} filePath | ||
* @param {*} key | ||
* @returns | ||
* @param {*} filePath | ||
* @param {*} key | ||
* @returns | ||
*/ | ||
export 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; | ||
} | ||
// 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]; | ||
// get | ||
const json = all(filePath); | ||
return json[key]; | ||
}; | ||
@@ -74,28 +74,28 @@ | ||
* set | ||
* @param {*} filePath | ||
* @param {*} key | ||
* @param {*} value | ||
* @returns | ||
* @param {*} filePath | ||
* @param {*} key | ||
* @param {*} value | ||
* @returns | ||
*/ | ||
export 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; | ||
} | ||
// 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; | ||
// 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}`); | ||
} | ||
// io | ||
try { | ||
writeFile(filePath, JSON.stringify(json)); | ||
} catch (e) { | ||
console.log(`qiao-config:set, write file error ${e.message}`); | ||
} | ||
}; | ||
@@ -105,31 +105,31 @@ | ||
* del | ||
* @param {*} filePath | ||
* @param {*} key | ||
* @returns | ||
* @param {*} filePath | ||
* @param {*} key | ||
* @returns | ||
*/ | ||
export 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; | ||
} | ||
// 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; | ||
// get | ||
const v = get(filePath, key); | ||
if (!v) return; | ||
// del | ||
const json = all(filePath); | ||
delete json[key]; | ||
// 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}`); | ||
} | ||
}; | ||
// io | ||
try { | ||
writeFile(filePath, JSON.stringify(json)); | ||
} catch (e) { | ||
console.log(`qiao-config:del, write file error ${e.message}`); | ||
} | ||
}; |
// fs | ||
import { writeFileSync, readFileSync, accessSync } from 'fs'; | ||
import { writeFileSync, readFileSync, accessSync } from "fs"; | ||
@@ -7,6 +7,6 @@ /** | ||
* @param {*} filePath | ||
* @param {*} data | ||
* @param {*} data | ||
*/ | ||
export const writeFile = (filePath, data) => { | ||
writeFileSync(filePath, data); | ||
writeFileSync(filePath, data); | ||
}; | ||
@@ -16,25 +16,25 @@ | ||
* read file | ||
* @param {*} filePath | ||
* @returns | ||
* @param {*} filePath | ||
* @returns | ||
*/ | ||
export const readFile = (filePath) => { | ||
try{ | ||
// not exists write file | ||
if(!isExists(filePath)) writeFile(filePath, ''); | ||
return readFileSync(filePath, {encoding:'utf8'}); | ||
}catch(e){ | ||
return null; | ||
} | ||
try { | ||
// not exists write file | ||
if (!isExists(filePath)) writeFile(filePath, ""); | ||
return readFileSync(filePath, { encoding: "utf8" }); | ||
} catch (e) { | ||
return null; | ||
} | ||
}; | ||
// is exists | ||
function isExists(filePath){ | ||
try{ | ||
accessSync(filePath); | ||
return true; | ||
}catch(e){ | ||
return false; | ||
} | ||
} | ||
function isExists(filePath) { | ||
try { | ||
accessSync(filePath); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} |
// data | ||
import { get, set, del, clear, all} from './_data.js'; | ||
import { get, set, del, clear, all } from "./_data.js"; | ||
/** | ||
* db | ||
* @param {*} dbPath | ||
* @param {*} dbPath | ||
*/ | ||
const db = (dbPath) => { | ||
const obj = {}; | ||
const obj = {}; | ||
obj.path = dbPath; | ||
obj.path = dbPath; | ||
// clear | ||
obj.clear = () => { clearDB(obj.path); }; | ||
// clear | ||
obj.clear = () => { | ||
clearDB(obj.path); | ||
}; | ||
// all | ||
obj.all = () => { return listDB(obj.path); }; | ||
// all | ||
obj.all = () => { | ||
return listDB(obj.path); | ||
}; | ||
// config | ||
obj.config = (key, value) => { return configDB(obj.path, key, value); }; | ||
// config | ||
obj.config = (key, value) => { | ||
return configDB(obj.path, key, value); | ||
}; | ||
return obj; | ||
return obj; | ||
}; | ||
// clear db | ||
function clearDB(filePath){ | ||
clear(filePath); | ||
function clearDB(filePath) { | ||
clear(filePath); | ||
} | ||
// list db | ||
function listDB(filePath){ | ||
return all(filePath); | ||
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); | ||
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); | ||
} | ||
export default db; | ||
export default db; |
// path | ||
import path from 'path'; | ||
import path from "path"; | ||
// db | ||
import db from './db.js'; | ||
import db from "./db.js"; | ||
@@ -11,8 +11,10 @@ /** | ||
export default (filePath) => { | ||
// path | ||
const defaultPath = path.resolve(__dirname, './config.json'); | ||
const finalPath = !filePath ? defaultPath : path.resolve(process.cwd(), filePath); | ||
// path | ||
const defaultPath = path.resolve(__dirname, "./config.json"); | ||
const finalPath = !filePath | ||
? defaultPath | ||
: path.resolve(process.cwd(), filePath); | ||
// db | ||
return db(finalPath); | ||
}; | ||
// db | ||
return db(finalPath); | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
419
75
0
10435
8