Skip to content

Commit

Permalink
chore: code review
Browse files Browse the repository at this point in the history
  • Loading branch information
heliomarpm committed May 31, 2024
1 parent 05782db commit ca73e79
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 101 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ on:
- "**/*.md"
- ".github/**"
- "!.github/workflows/codeql.yml"

pull_request:
paths-ignore:
- "**/*.sh"
Expand Down Expand Up @@ -63,11 +63,11 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -81,7 +81,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
Expand All @@ -94,6 +94,6 @@ jobs:
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
4 changes: 2 additions & 2 deletions .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: 'Checkout Repository'
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: 'Dependency Review'
uses: actions/dependency-review-action@v3
uses: actions/dependency-review-action@v4
63 changes: 47 additions & 16 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,60 @@ on:
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

# PUBLICANDO EM NPM
- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: '>=16.8.0'
node-version: '>=20'
registry-url: https://registry.npmjs.org/
scope: "@heliomarpm"

- name: Install Dependencies
run: npm ci

- name: Test & Build
run: |
npm run test
npm run build
- name: Publish NPM Package
run: npm publish --access=public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- run: npm ci
- run: npm run build

- name: Publish Lib
uses: JS-DevTools/npm-publish@v2
# PUBLICANDO EM GITHUB PACKAGE
- name: Setup Node GPR
uses: actions/setup-node@v4
with:
token: ${{ secrets.NPM_TOKEN }}
strategy: all
node-version: '>=20'
registry-url: https://npm.pkg.github.com/
scope: "@heliomarpm"

- name: Install Dependencies
run: npm ci

- name: Build
run: npm run build

- name: Publish GPR
run: npm publish --access=public
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}


# - uses: actions/setup-node@v3
# with:
# node-version: '>=18'
# registry-url: https://registry.npmjs.org/
# # ISSO PUBLICOU NO NPM
# - name: Publish Lib
# uses: JS-DevTools/npm-publish@v3
# with:
# token: ${{ secrets.NPM_TOKEN }}
# strategy: all

# - run: npm publish
# env:
# NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
26 changes: 16 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"description": "A simple and robust KeyValues Storage's library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"author": "Heliomar P. Marques",
"author": {
"name": "Heliomar P. Marques",
"email": "[email protected]",
"url": "https://navto.me/heliomarpm"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/heliomarpm/keyvalues-storage/issues"
Expand Down Expand Up @@ -33,8 +37,8 @@
"storage"
],
"scripts": {
"build": "tsc",
"test": "jest --detectOpenHandles",
"build": "tsc -p tsconfig.prod.json",
"test": "jest",
"test:c": "jest --coverage",
"pub:release": "node ./cmd/modules/release/index.js"
},
Expand All @@ -43,13 +47,15 @@
"write-file-atomic": "^5.0.1"
},
"devDependencies": {
"@types/jest": "^29.5.3",
"@types/write-file-atomic": "^4.0.0",
"jest": "^29.6.3",
"ts-jest": "^29.1.1"
"@types/jest": "^29.5.12",
"@types/write-file-atomic": "^4.0.3",
"jest": "^29.7.0",
"open": "^10.1.0",
"semver": "^7.6.0",
"ts-jest": "^29.1.2"
},
"peerDependencies": {
"@types/lodash": "^4.14.194",
"@types/write-file-atomic": "^4.0.0"
"@types/lodash": "^4.17.1",
"@types/write-file-atomic": "^4.0.3"
}
}
}
61 changes: 31 additions & 30 deletions src/internal/JsonFileHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const DEFAULT_DIR_NAME = 'localdb';
export const DEFAULT_FILE_NAME = 'keyvalues.json';

export class JsonFileHelper {

options: Options;

constructor(options: Options) {
Expand All @@ -24,7 +23,7 @@ export class JsonFileHelper {
* @internal
*/
private getJsonDirPath(): string {
let dir = (this.options.dir ?? path.resolve(DEFAULT_DIR_NAME)).trim();
const dir = (this.options.dir ?? path.resolve(DEFAULT_DIR_NAME)).trim();
return dir === '' ? './' : dir;
}

Expand Down Expand Up @@ -54,7 +53,7 @@ export class JsonFileHelper {
const filePath = this.getJsonFilePath();

return new Promise((resolve, reject) => {
fs.stat(filePath, (err: any) => {
fs.stat(filePath, (err) => {
if (err) {
if (err.code === 'ENOENT') {
this.saveKeyValues({}).then(resolve, reject);
Expand All @@ -79,7 +78,8 @@ export class JsonFileHelper {

try {
fs.statSync(filePath);
} catch (err: any) {
} catch (ex) {
const err = ex as NodeJS.ErrnoException;
if (err.code === 'ENOENT') {
this.saveKeyValuesSync({});
} else {
Expand All @@ -89,7 +89,7 @@ export class JsonFileHelper {
}

/**
* Ensures that the keyvalues directory exists. If it does
* Ensures that the KeyValues directory exists. If it does
* not exist, then it is created.
*
* @returns A promise which resolves when the keyvalues dir exists.
Expand All @@ -99,7 +99,7 @@ export class JsonFileHelper {
const dirPath = this.getJsonDirPath();

return new Promise((resolve, reject) => {
fs.stat(dirPath, (err: any) => {
fs.stat(dirPath, (err) => {
if (err) {
if (err.code === 'ENOENT') {
fs.mkdir(dirPath, { recursive: true }, (error: any) => {
Expand All @@ -117,17 +117,20 @@ export class JsonFileHelper {
}

/**
* Ensures that the keyvalues directory exists. If it does
* not exist, then it is created.
* Ensures that the KeyValues directory exists synchronously. If it does not exist,
* then it is created.
*
* @returns {void}
* @internal
*/
private ensureJsonDirSync(): void {
const dirPath = this.getJsonDirPath();

try {
fs.statSync(dirPath);
} catch (err: any) {
} catch (ex) {
const err = ex as NodeJS.ErrnoException;

if (err.code === 'ENOENT') {
fs.mkdirSync(dirPath, { recursive: true });
// mkdirp.sync(dirPath);
Expand All @@ -138,52 +141,53 @@ export class JsonFileHelper {
}

/**
* First ensures that the keyvalues file exists then loads
* the keyvalues from the disk.
* Asynchronously loads key-value pairs from a JSON file. First ensures that the file exists,
* then reads the file and parses its contents into a JavaScript object.
*
* @returns A promise which resolves with the keyvalues object.
* @template T - The type of the key-value pairs.
* @return {Promise<T>} A promise that resolves with the key-value pairs.
* @internal
*/
public async loadKeyValues<T extends valueTypes>(): Promise<T> {
await this.ensureJsonFile();
const filePath = this.getJsonFilePath();

return await new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf-8', (err: any, data: string | any[]) => {
fs.readFile(filePath, 'utf-8', (err, data: string | any[]) => {
if (err) {
reject(err);
} else {
try {
// resolve(JSON.parse(data.length ? data : "{}"));
resolve(JSON.parse(data ? (Array.isArray(data) ? data.join('') : data) : "{}"));
} catch (err_1) {
reject(err_1);
resolve(JSON.parse(data ? (Array.isArray(data) ? data.join('') : data) : '{}'));
} catch (error) {
reject(error);
}
}
});
});
}

/**
* First ensures that the keyvalues file exists then loads
* the keyvalues from the disk.
* Loads the key-value pairs synchronously from the JSON file.
*
* @returns The keyvalues object.
* @template T - The type of the key-value pairs.
* @returns {T} - The loaded key-value pairs.
* @internal
*/
public loadKeyValuesSync<T extends valueTypes>(): T {
this.ensureJsonFileSync();
const filePath = this.getJsonFilePath();
const data = fs.readFileSync(filePath, 'utf-8');

return JSON.parse(data.length ? data : "{}");
return JSON.parse(data.length ? data : '{}');
}

/**
* Saves the keyvalues to the disk.
*
* @param obj The keyvalues object to save.
* @returns A promise which resolves when the keyvalues have been saved.
* @param {T} obj - The keyvalues object to save.
* @return {Promise<void>} A promise that resolves when the keyvalues have been saved.
* @internal
*/
public async saveKeyValues<T>(obj: T): Promise<void> {
Expand All @@ -195,23 +199,21 @@ export class JsonFileHelper {
return await new Promise((resolve, reject) => {
if (this.options.atomicSave) {
writeFileAtomic(filePath, data, (err: any) => {
return err
? reject(err)
: resolve();
return err ? reject(err) : resolve();
});
} else {
fs.writeFile(filePath, data, (err_1: any) => {
return err_1 ? reject(err_1)
: resolve();
return err_1 ? reject(err_1) : resolve();
});
}
});
}

/**
* Saves the keyvalues to the disk.
* Saves the keyvalues to the disk synchronously.
*
* @param obj The keyvalues object to save.
* @param {T} obj - The keyvalues object to save.
* @return {void} This function does not return anything.
* @internal
*/
public saveKeyValuesSync<T>(obj: T): void {
Expand All @@ -227,5 +229,4 @@ export class JsonFileHelper {
fs.writeFileSync(filePath, data);
}
}

}
Loading

0 comments on commit ca73e79

Please sign in to comment.