qiao-config
Advanced tools
Comparing version 1.0.4 to 2.0.0
96
index.js
'use strict'; | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var qiaoFile = require('qiao-file'); | ||
// 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 | ||
@@ -51,3 +13,3 @@ | ||
*/ | ||
const clear = (filePath) => { | ||
const clear = async (filePath) => { | ||
// check | ||
@@ -61,3 +23,3 @@ if (!filePath) { | ||
try { | ||
writeFile(filePath, ''); | ||
await qiaoFile.writeFile(filePath, ''); | ||
} catch (e) { | ||
@@ -73,3 +35,3 @@ console.log(`qiao-config:clear, write file error ${e.message}`); | ||
*/ | ||
const all = (filePath) => { | ||
const all = async (filePath) => { | ||
// check | ||
@@ -83,3 +45,3 @@ if (!filePath) { | ||
try { | ||
const jsonStr = readFile(filePath); | ||
const jsonStr = await qiaoFile.readFile(filePath); | ||
@@ -100,3 +62,3 @@ json = JSON.parse(jsonStr); | ||
*/ | ||
const get = (filePath, key) => { | ||
const get = async (filePath, key) => { | ||
// check | ||
@@ -113,3 +75,3 @@ if (!filePath) { | ||
// get | ||
const json = all(filePath); | ||
const json = await all(filePath); | ||
return json[key]; | ||
@@ -125,3 +87,3 @@ }; | ||
*/ | ||
const set = (filePath, key, value) => { | ||
const set = async (filePath, key, value) => { | ||
// check | ||
@@ -138,3 +100,3 @@ if (!filePath) { | ||
// set | ||
const json = all(filePath); | ||
const json = await all(filePath); | ||
json[key] = value; | ||
@@ -144,3 +106,3 @@ | ||
try { | ||
writeFile(filePath, JSON.stringify(json)); | ||
await qiaoFile.writeFile(filePath, JSON.stringify(json)); | ||
} catch (e) { | ||
@@ -157,3 +119,3 @@ console.log(`qiao-config:set, write file error ${e.message}`); | ||
*/ | ||
const del = (filePath, key) => { | ||
const del = async (filePath, key) => { | ||
// check | ||
@@ -170,7 +132,7 @@ if (!filePath) { | ||
// get | ||
const v = get(filePath, key); | ||
const v = await get(filePath, key); | ||
if (!v) return; | ||
// del | ||
const json = all(filePath); | ||
const json = await all(filePath); | ||
delete json[key]; | ||
@@ -180,3 +142,3 @@ | ||
try { | ||
writeFile(filePath, JSON.stringify(json)); | ||
await qiaoFile.writeFile(filePath, JSON.stringify(json)); | ||
} catch (e) { | ||
@@ -199,14 +161,14 @@ console.log(`qiao-config:del, write file error ${e.message}`); | ||
// clear | ||
obj.clear = () => { | ||
clearDB(obj.path); | ||
obj.clear = async () => { | ||
await clear(obj.path); | ||
}; | ||
// all | ||
obj.all = () => { | ||
return listDB(obj.path); | ||
obj.all = async () => { | ||
return await all(obj.path); | ||
}; | ||
// config | ||
obj.config = (key, value) => { | ||
return configDB(obj.path, key, value); | ||
obj.config = async (key, value) => { | ||
return await configDB(obj.path, key, value); | ||
}; | ||
@@ -217,17 +179,7 @@ | ||
// clear db | ||
function clearDB(filePath) { | ||
clear(filePath); | ||
} | ||
// list db | ||
function listDB(filePath) { | ||
return all(filePath); | ||
} | ||
// config db | ||
function configDB(filePath, key, value) { | ||
async function configDB(filePath, key, value) { | ||
// remove | ||
if (value === null) { | ||
del(filePath, key); | ||
await del(filePath, key); | ||
return; | ||
@@ -238,7 +190,7 @@ } | ||
if (typeof value == 'undefined') { | ||
return get(filePath, key); | ||
return await get(filePath, key); | ||
} | ||
// set | ||
set(filePath, key, value); | ||
await set(filePath, key, value); | ||
} | ||
@@ -245,0 +197,0 @@ |
{ | ||
"name": "qiao-config", | ||
"version": "1.0.4", | ||
"version": "2.0.0", | ||
"description": "json config to local file", | ||
@@ -23,12 +23,32 @@ "keywords": [ | ||
"type": "git", | ||
"url": "git+https://github.com/uikoo9/qiao-monorepo.git" | ||
"url": "git+https://github.com/uikoo9/qiao-nodejs.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/uikoo9/qiao-monorepo/issues" | ||
"url": "https://github.com/uikoo9/qiao-nodejs/issues" | ||
}, | ||
"scripts": { | ||
"build": "rollup -c", | ||
"test": "jest" | ||
"test": "ava" | ||
}, | ||
"gitHead": "98dbad1526bc58003bbaa9a69843725a93a4be0f" | ||
"dependencies": { | ||
"qiao-file": "^2.0.9" | ||
}, | ||
"nx": { | ||
"namedInputs": { | ||
"default": [ | ||
"{projectRoot}/src/**/*" | ||
] | ||
}, | ||
"targets": { | ||
"build": { | ||
"inputs": [ | ||
"default" | ||
], | ||
"outputs": [ | ||
"{projectRoot}/index.js" | ||
] | ||
} | ||
} | ||
}, | ||
"gitHead": "20721e9fc0d12152e1923a56459923d9635ee98a" | ||
} |
@@ -10,2 +10,4 @@ ## qiao-config | ||
安装 | ||
```bash | ||
@@ -30,3 +32,3 @@ npm i qiao-config | ||
```javascript | ||
db.all(); | ||
await db.all(); | ||
``` | ||
@@ -37,3 +39,3 @@ | ||
```javascript | ||
db.clear(); | ||
await db.clear(); | ||
``` | ||
@@ -45,9 +47,9 @@ | ||
// get | ||
db.config(key); | ||
await db.config(key); | ||
// set | ||
db.config(key, value); | ||
await db.config(key, value); | ||
// del | ||
db.config(key, null); | ||
await db.config(key, null); | ||
``` | ||
@@ -57,2 +59,6 @@ | ||
### 0.0.5.20230404 | ||
1. add ava | ||
### 0.0.4.20221118 | ||
@@ -59,0 +65,0 @@ |
// io | ||
import { writeFile, readFile } from './_io.js'; | ||
import { writeFile, readFile } from 'qiao-file'; | ||
@@ -9,3 +9,3 @@ /** | ||
*/ | ||
export const clear = (filePath) => { | ||
export const clear = async (filePath) => { | ||
// check | ||
@@ -19,3 +19,3 @@ if (!filePath) { | ||
try { | ||
writeFile(filePath, ''); | ||
await writeFile(filePath, ''); | ||
} catch (e) { | ||
@@ -31,3 +31,3 @@ console.log(`qiao-config:clear, write file error ${e.message}`); | ||
*/ | ||
export const all = (filePath) => { | ||
export const all = async (filePath) => { | ||
// check | ||
@@ -41,3 +41,3 @@ if (!filePath) { | ||
try { | ||
const jsonStr = readFile(filePath); | ||
const jsonStr = await readFile(filePath); | ||
@@ -58,3 +58,3 @@ json = JSON.parse(jsonStr); | ||
*/ | ||
export const get = (filePath, key) => { | ||
export const get = async (filePath, key) => { | ||
// check | ||
@@ -71,3 +71,3 @@ if (!filePath) { | ||
// get | ||
const json = all(filePath); | ||
const json = await all(filePath); | ||
return json[key]; | ||
@@ -83,3 +83,3 @@ }; | ||
*/ | ||
export const set = (filePath, key, value) => { | ||
export const set = async (filePath, key, value) => { | ||
// check | ||
@@ -96,3 +96,3 @@ if (!filePath) { | ||
// set | ||
const json = all(filePath); | ||
const json = await all(filePath); | ||
json[key] = value; | ||
@@ -102,3 +102,3 @@ | ||
try { | ||
writeFile(filePath, JSON.stringify(json)); | ||
await writeFile(filePath, JSON.stringify(json)); | ||
} catch (e) { | ||
@@ -115,3 +115,3 @@ console.log(`qiao-config:set, write file error ${e.message}`); | ||
*/ | ||
export const del = (filePath, key) => { | ||
export const del = async (filePath, key) => { | ||
// check | ||
@@ -128,7 +128,7 @@ if (!filePath) { | ||
// get | ||
const v = get(filePath, key); | ||
const v = await get(filePath, key); | ||
if (!v) return; | ||
// del | ||
const json = all(filePath); | ||
const json = await all(filePath); | ||
delete json[key]; | ||
@@ -138,3 +138,3 @@ | ||
try { | ||
writeFile(filePath, JSON.stringify(json)); | ||
await writeFile(filePath, JSON.stringify(json)); | ||
} catch (e) { | ||
@@ -141,0 +141,0 @@ console.log(`qiao-config:del, write file error ${e.message}`); |
@@ -14,14 +14,14 @@ // data | ||
// clear | ||
obj.clear = () => { | ||
clearDB(obj.path); | ||
obj.clear = async () => { | ||
await clear(obj.path); | ||
}; | ||
// all | ||
obj.all = () => { | ||
return listDB(obj.path); | ||
obj.all = async () => { | ||
return await all(obj.path); | ||
}; | ||
// config | ||
obj.config = (key, value) => { | ||
return configDB(obj.path, key, value); | ||
obj.config = async (key, value) => { | ||
return await configDB(obj.path, key, value); | ||
}; | ||
@@ -32,17 +32,7 @@ | ||
// clear db | ||
function clearDB(filePath) { | ||
clear(filePath); | ||
} | ||
// list db | ||
function listDB(filePath) { | ||
return all(filePath); | ||
} | ||
// config db | ||
function configDB(filePath, key, value) { | ||
async function configDB(filePath, key, value) { | ||
// remove | ||
if (value === null) { | ||
del(filePath, key); | ||
await del(filePath, key); | ||
return; | ||
@@ -53,9 +43,9 @@ } | ||
if (typeof value == 'undefined') { | ||
return get(filePath, key); | ||
return await get(filePath, key); | ||
} | ||
// set | ||
set(filePath, key, value); | ||
await set(filePath, key, value); | ||
} | ||
export default db; |
Sorry, the diff of this file is not supported yet
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
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
81
0
9630
1
335
+ Addedqiao-file@^2.0.9
+ Addedfs-extra@11.2.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedjsonfile@6.1.0(transitive)
+ Addedqiao-file@2.1.2(transitive)
+ Addeduniversalify@2.0.1(transitive)