Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
chore: implementation of a first functional draft (#2)
Browse files Browse the repository at this point in the history
* chore: implementation of a first functional draft

chore: update @slimio/config (0.18.0 to 1.0.0)

chore: continue implementation

chore: draft continue

* refactor: add .js extension & .gitkeep in fixtures

* refactor: remove .json import and use fs:readFileSync instead

* refactor: destruct Result from import

* chore: add missing tests & add new i18n property

* chore: fix/update dependencies

* chore: add new properties

* fix: warnings type

* feat: add generation mode & complete tests

* test(tsd): add assertions for types

* refactor: enhance writeOptions type

* docs: complete with latest features

* chore: update dependencies

* test: enhance error checking with [email protected]
  • Loading branch information
fraxken authored Feb 3, 2022
1 parent fe3c623 commit c64a2cc
Show file tree
Hide file tree
Showing 24 changed files with 3,532 additions and 620 deletions.
6 changes: 6 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extension": ["ts"],
"node-option": ["experimental-specifier-resolution=node", "loader=ts-node/esm", "no-warnings"],
"spec": ["test/*.spec.ts"],
"timeout": 10000
}
129 changes: 126 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
)
[![mit](https://img.shields.io/github/license/Naereen/StrapDown.js.svg)](https://github.com/NodeSecure/rc/blob/master/LICENSE)

**[WIP]** NodeSecure runtime configuration
NodeSecure runtime configuration.

## Requirements
- [Node.js](https://nodejs.org/en/) v16 or higher
Expand All @@ -21,10 +21,133 @@ $ yarn add @nodesecure/rc
```

## Usage example
TBC

Read:

```ts
import * as RC from "@nodesecure/rc";

const configurationPayload = (
await RC.read(void 0, { createIfDoesNotExist: true })
).unwrap();
console.log(configurationPayload);
```

Write:
```ts
import assert from "node:assert/strict";
import * as RC from "@nodesecure/rc";

const writeOpts: RC.writeOptions = {
payload: { version: "2.0.0" },
partialUpdate: true
};

const result = (
await RC.write(void 0, writeOpts)
).unwrap();
assert.strictEqual(result, void 0);
```

> 👀 .read and .write return Rust like [Result](https://doc.rust-lang.org/std/result/) object. Under the hood we use [ts-results](https://github.com/vultix/ts-results) to achieve this.
## API
TBC

> If `undefined` the location will be assigned to `process.cwd()`.
### read(location?: string, options?: readOptions): Promise< Result< RC, NodeJS.ErrnoException > >

```ts
interface createReadOptions {
/**
* If enabled the file will be created if it does not exist on the disk.
*
* @default false
*/
createIfDoesNotExist?: boolean;
/**
* RC Generation mode. This option allows to generate a more or less complete configuration for some NodeSecure tools.
*
* @default `minimal`
*/
createMode?: RCGenerationMode | RCGenerationMode[];
}

export type readOptions = RequireAtLeastOne<createReadOptions, "createIfDoesNotExist" | "createMode">;
```

The `createIfDoesNotExist` argument can be ignored if `createMode` is provided.

```ts
import * as RC from "@nodesecure/rc";

const configurationPayload = (
await RC.read(void 0, { createMode: "ci" })
).unwrap();
console.log(configurationPayload);
```

### write(location?: string, options: writeOptions): Promise< Result< void, NodeJS.ErrnoException > >

By default the write API will overwrite the current payload with the provided one. When the `partialUpdate` option is enabled it will merge the new properties with the existing one.

```ts
/**
* Overwrite the complete payload. partialUpdate property is mandatory.
*/
export interface writeCompletePayload {
payload: RC;
partialUpdate?: false;
}

/**
* Partially update the payload. This implies not to rewrite the content of the file when enabled.
**/
export interface writePartialPayload {
payload: Partial<RC>;
partialUpdate: true;
}

export type writeOptions = writeCompletePayload | writePartialPayload;
```

### CONSTANTS

```ts
import assert from "node:assert/strict";
import * as RC from "@nodesecure/rc";

assert.strictEqual(RC.CONSTANTS.CONFIGURATION_NAME, ".nodesecurerc");
```

### Generation Mode

We provide by default a configuration generation that we consider `minimal`. On the contrary, a `complete` value will indicate the generation with all possible default keys.

```ts
export type RCGenerationMode = "minimal" | "ci" | "complete";
```

However, depending on the NodeSecure tool you are working on, it can be interesting to generate a configuration with some property sets specific to your needs.

Note that you can combine several modes:

```ts
import * as RC from "@nodesecure/rc";

await RC.read(void 0, { createMode: ["ci", "scanner"] })
```

## JSON Schema

The runtime configuration is validated with a JSON Schema: `./src/schema/nodesecurerc.json`.

It can be retrieved by API if required:
```ts
import * as RC from "@nodesecure/rc";

console.log(RC.JSONSchema);
```

## Contributors ✨

Expand Down
1 change: 0 additions & 1 deletion index.js

This file was deleted.

Loading

0 comments on commit c64a2cc

Please sign in to comment.