Init: RoggioApp Architecture, Prisma Schema, API MVP
This commit is contained in:
+39
@@ -0,0 +1,39 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Pooya Parsa <pooya@pi0.io>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
|
||||
-----
|
||||
|
||||
Bundled with https://github.com/hughsk/flat
|
||||
|
||||
Copyright (c) 2014, Hugh Kennedy
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
# RC9
|
||||
|
||||
<!-- automd:badges color=yellow codecov bundlejs -->
|
||||
|
||||
[](https://npmjs.com/package/rc9)
|
||||
[](https://npm.chart.dev/rc9)
|
||||
[](https://bundlejs.com/?q=rc9)
|
||||
[](https://codecov.io/gh/unjs/rc9)
|
||||
|
||||
<!-- /automd -->
|
||||
|
||||
Read/Write RC configs couldn't be easier!
|
||||
|
||||
## Install
|
||||
|
||||
Install dependencies:
|
||||
|
||||
<!-- automd:pm-i -->
|
||||
|
||||
```sh
|
||||
# ✨ Auto-detect
|
||||
npx nypm install rc9
|
||||
|
||||
# npm
|
||||
npm install rc9
|
||||
|
||||
# yarn
|
||||
yarn add rc9
|
||||
|
||||
# pnpm
|
||||
pnpm add rc9
|
||||
|
||||
# bun
|
||||
bun install rc9
|
||||
|
||||
# deno
|
||||
deno install npm:rc9
|
||||
```
|
||||
|
||||
<!-- /automd -->
|
||||
|
||||
Import utils:
|
||||
|
||||
<!-- automd:jsimport src="./src/index.ts"-->
|
||||
|
||||
**ESM** (Node.js, Bun, Deno)
|
||||
|
||||
```js
|
||||
import {
|
||||
defaults,
|
||||
parse,
|
||||
parseFile,
|
||||
read,
|
||||
readUser,
|
||||
serialize,
|
||||
write,
|
||||
writeUser,
|
||||
readUserConfig,
|
||||
writeUserConfig,
|
||||
updateUserConfig,
|
||||
update,
|
||||
updateUser,
|
||||
} from "rc9";
|
||||
```
|
||||
|
||||
<!-- /automd -->
|
||||
|
||||
## Usage
|
||||
|
||||
`.conf`:
|
||||
|
||||
```ini
|
||||
db.username=username
|
||||
db.password=multi word password
|
||||
db.enabled=true
|
||||
```
|
||||
|
||||
**Update config:**
|
||||
|
||||
```ts
|
||||
update({ "db.enabled": false }); // or update(..., { name: '.conf' })
|
||||
```
|
||||
|
||||
Push to an array:
|
||||
|
||||
```ts
|
||||
update({ "modules[]": "test" });
|
||||
```
|
||||
|
||||
**Read/Write config:**
|
||||
|
||||
```ts
|
||||
const config = read(); // or read('.conf')
|
||||
|
||||
// config = {
|
||||
// db: {
|
||||
// username: 'username',
|
||||
// password: 'multi word password',
|
||||
// enabled: true
|
||||
// }
|
||||
// }
|
||||
|
||||
config.enabled = false;
|
||||
write(config); // or write(config, '.conf')
|
||||
```
|
||||
|
||||
**User Config:**
|
||||
|
||||
You can use `readUserConfig`/`writeUserConfig`/`updateUserConfig` to store config in the user's config directory (`$XDG_CONFIG_HOME` or `~/.config`):
|
||||
|
||||
```js
|
||||
writeUserConfig({ token: 123 }, ".zoorc"); // Will be saved in ~/.config/.zoorc
|
||||
|
||||
const conf = readUserConfig(".zoorc"); // { token: 123 }
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> `readUser`/`writeUser`/`updateUser` are deprecated. Use `readUserConfig`/`writeUserConfig`/`updateUserConfig` instead, which follow XDG conventions (`~/.config`).
|
||||
|
||||
## Unflatten
|
||||
|
||||
RC uses [flat](https://www.npmjs.com/package/flat) to automatically flat/unflat when writing and reading rcfile.
|
||||
|
||||
It means that you can use `.` for keys to define objects. Some examples:
|
||||
|
||||
- `hello.world = true` <=> `{ hello: { world: true }`
|
||||
- `test.0 = A` <=> `tags: [ 'A' ]`
|
||||
|
||||
**Note:** If you use keys that can override like `x=` and `x.y=`, you can disable this feature by passing `flat: true` option.
|
||||
|
||||
**Tip:** You can use keys ending with `[]` to push to an array like `test[]=A`
|
||||
|
||||
## Native Values
|
||||
|
||||
RC uses [destr](https://www.npmjs.com/package/destr) to convert values into native javascript values.
|
||||
|
||||
So reading `count=123` results `{ count: 123 }` (instead of `{ count: "123" }`) if you want to preserve strings as is, can use `count="123"`.
|
||||
|
||||
## Exports
|
||||
|
||||
```ts
|
||||
const defaults: RCOptions;
|
||||
function parse(contents: string, options?: RCOptions): RC;
|
||||
function parseFile(path: string, options?: RCOptions): RC;
|
||||
function read(options?: RCOptions | string): RC;
|
||||
function readUserConfig(options?: RCOptions | string): RC;
|
||||
function serialize(config: RC): string;
|
||||
function write(config: RC, options?: RCOptions | string): void;
|
||||
function writeUserConfig(config: RC, options?: RCOptions | string): void;
|
||||
function update(config: RC, options?: RCOptions | string): RC;
|
||||
function updateUserConfig(config: RC, options?: RCOptions | string): RC;
|
||||
```
|
||||
|
||||
**Types:**
|
||||
|
||||
```ts
|
||||
type RC = Record<string, any>;
|
||||
interface RCOptions {
|
||||
name?: string;
|
||||
dir?: string;
|
||||
flat?: boolean;
|
||||
}
|
||||
```
|
||||
|
||||
**Defaults:**
|
||||
|
||||
```ini
|
||||
{
|
||||
name: '.conf',
|
||||
dir: process.cwd(),
|
||||
flat: false
|
||||
}
|
||||
```
|
||||
|
||||
### Why RC9?
|
||||
|
||||
Be the first one to guess 🐇 <!-- Hint: do research about rc files history -->
|
||||
|
||||
## License
|
||||
|
||||
<!-- automd:contributors license=MIT -->
|
||||
|
||||
Published under the [MIT](https://github.com/unjs/rc9/blob/main/LICENSE) license.
|
||||
Made by [community](https://github.com/unjs/rc9/graphs/contributors) 💛
|
||||
<br><br>
|
||||
<a href="https://github.com/unjs/rc9/graphs/contributors">
|
||||
<img src="https://contrib.rocks/image?repo=unjs/rc9" />
|
||||
</a>
|
||||
|
||||
<!-- /automd -->
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
# Licenses of Bundled Dependencies
|
||||
|
||||
The published artifact additionally contains code with the following licenses:
|
||||
BSD-3-Clause
|
||||
|
||||
# Bundled Dependencies
|
||||
|
||||
## flat
|
||||
|
||||
License: BSD-3-Clause
|
||||
By: Hugh Kennedy
|
||||
Repository: https://github.com/hughsk/flat
|
||||
|
||||
> Copyright (c) 2014, Hugh Kennedy
|
||||
> All rights reserved.
|
||||
>
|
||||
> Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
>
|
||||
> 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
>
|
||||
> 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
>
|
||||
> 3. Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
>
|
||||
> THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
//#region node_modules/.pnpm/flat@6.0.1/node_modules/flat/index.js
|
||||
function isBuffer(obj) {
|
||||
return obj && obj.constructor && typeof obj.constructor.isBuffer === "function" && obj.constructor.isBuffer(obj);
|
||||
}
|
||||
function keyIdentity(key) {
|
||||
return key;
|
||||
}
|
||||
function flatten(target, opts) {
|
||||
opts = opts || {};
|
||||
const delimiter = opts.delimiter || ".";
|
||||
const maxDepth = opts.maxDepth;
|
||||
const transformKey = opts.transformKey || keyIdentity;
|
||||
const output = {};
|
||||
function step(object, prev, currentDepth) {
|
||||
currentDepth = currentDepth || 1;
|
||||
Object.keys(object).forEach(function(key) {
|
||||
const value = object[key];
|
||||
const isarray = opts.safe && Array.isArray(value);
|
||||
const type = Object.prototype.toString.call(value);
|
||||
const isbuffer = isBuffer(value);
|
||||
const isobject = type === "[object Object]" || type === "[object Array]";
|
||||
const newKey = prev ? prev + delimiter + transformKey(key) : transformKey(key);
|
||||
if (!isarray && !isbuffer && isobject && Object.keys(value).length && (!opts.maxDepth || currentDepth < maxDepth)) return step(value, newKey, currentDepth + 1);
|
||||
output[newKey] = value;
|
||||
});
|
||||
}
|
||||
step(target);
|
||||
return output;
|
||||
}
|
||||
function unflatten(target, opts) {
|
||||
opts = opts || {};
|
||||
const delimiter = opts.delimiter || ".";
|
||||
const overwrite = opts.overwrite || false;
|
||||
const transformKey = opts.transformKey || keyIdentity;
|
||||
const result = {};
|
||||
if (isBuffer(target) || Object.prototype.toString.call(target) !== "[object Object]") return target;
|
||||
function getkey(key) {
|
||||
const parsedKey = Number(key);
|
||||
return isNaN(parsedKey) || key.indexOf(".") !== -1 || opts.object ? key : parsedKey;
|
||||
}
|
||||
function addKeys(keyPrefix, recipient, target) {
|
||||
return Object.keys(target).reduce(function(result, key) {
|
||||
result[keyPrefix + delimiter + key] = target[key];
|
||||
return result;
|
||||
}, recipient);
|
||||
}
|
||||
function isEmpty(val) {
|
||||
const type = Object.prototype.toString.call(val);
|
||||
const isArray = type === "[object Array]";
|
||||
const isObject = type === "[object Object]";
|
||||
if (!val) return true;
|
||||
else if (isArray) return !val.length;
|
||||
else if (isObject) return !Object.keys(val).length;
|
||||
}
|
||||
target = Object.keys(target).reduce(function(result, key) {
|
||||
const type = Object.prototype.toString.call(target[key]);
|
||||
if (!(type === "[object Object]" || type === "[object Array]") || isEmpty(target[key])) {
|
||||
result[key] = target[key];
|
||||
return result;
|
||||
} else return addKeys(key, result, flatten(target[key], opts));
|
||||
}, {});
|
||||
Object.keys(target).forEach(function(key) {
|
||||
const split = key.split(delimiter).map(transformKey);
|
||||
let key1 = getkey(split.shift());
|
||||
let key2 = getkey(split[0]);
|
||||
let recipient = result;
|
||||
while (key2 !== void 0) {
|
||||
if (key1 === "__proto__") return;
|
||||
const type = Object.prototype.toString.call(recipient[key1]);
|
||||
const isobject = type === "[object Object]" || type === "[object Array]";
|
||||
if (!overwrite && !isobject && typeof recipient[key1] !== "undefined") return;
|
||||
if (overwrite && !isobject || !overwrite && recipient[key1] == null) recipient[key1] = typeof key2 === "number" && !opts.object ? [] : {};
|
||||
recipient = recipient[key1];
|
||||
if (split.length > 0) {
|
||||
key1 = getkey(split.shift());
|
||||
key2 = getkey(split[0]);
|
||||
}
|
||||
}
|
||||
recipient[key1] = unflatten(target[key], opts);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
//#endregion
|
||||
export { unflatten as n, flatten as t };
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
//#region src/index.d.ts
|
||||
type RC = Record<string, any>;
|
||||
interface RCOptions {
|
||||
/**
|
||||
* The name of the configuration file.
|
||||
* @optional
|
||||
*/
|
||||
name?: string;
|
||||
/**
|
||||
* The directory where the configuration file is or should be written.
|
||||
* @optional
|
||||
*/
|
||||
dir?: string;
|
||||
/**
|
||||
* Specifies whether the configuration should be treated as a flat object.
|
||||
* @optional
|
||||
*/
|
||||
flat?: boolean;
|
||||
}
|
||||
/**
|
||||
* The default options for the configuration file.
|
||||
*/
|
||||
declare const defaults: RCOptions;
|
||||
declare function parse<T extends RC = RC>(contents: string, options?: RCOptions): T;
|
||||
/**
|
||||
* Parses a configuration string into an object.
|
||||
* @param {string} contents - The configuration data as a raw string.
|
||||
* @param {RCOptions} [options={}] - Options to control the parsing behaviour. See {@link RCOptions}.
|
||||
* @returns {RC} - The parsed configuration object. See {@link RC}.
|
||||
*/
|
||||
declare function parseFile<T extends RC = RC>(path: string, options?: RCOptions): T;
|
||||
/**
|
||||
* Reads a configuration file from a default or specified location and parses its contents.
|
||||
* @param {RCOptions|string} [options] - Options for reading the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @returns {RC} - The parsed configuration object. See {@link RC}.
|
||||
*/
|
||||
declare function read<T extends RC = RC>(options?: RCOptions | string): T;
|
||||
/**
|
||||
* Reads a custom configuration file from a default or specified location and parses its contents.
|
||||
* @param {RCOptions|string} [options] - Options for reading the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @returns {RC} - The parsed configuration object.
|
||||
* @deprecated Use {@link readUserConfig} instead, which uses `~/.config` following XDG conventions.
|
||||
*/
|
||||
declare function readUser<T extends RC = RC>(options?: RCOptions | string): T;
|
||||
/**
|
||||
* Serialises a configuration object to a string format.
|
||||
* @param {RC} config - The configuration object to serialise. See {@link RC}.
|
||||
* @returns {string} - The serialised configuration string.
|
||||
*/
|
||||
declare function serialize<T extends RC = RC>(config: T): string;
|
||||
/**
|
||||
* Writes a configuration object to a file in a default or specified location.
|
||||
* @param {RC} config - The configuration object to write. See {@link RC}.
|
||||
* @param {RCOptions|string} [options] - Options for writing the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
*/
|
||||
declare function write<T extends RC = RC>(config: T, options?: RCOptions | string): void;
|
||||
/**
|
||||
* Writes a custom configuration object to a file in a default or specified location.
|
||||
* @param {RC} config - The configuration object to write. See {@link RC}.
|
||||
* @param {RCOptions|string} [options] - Options for writing the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @deprecated Use {@link writeUserConfig} instead, which uses `~/.config` following XDG conventions.
|
||||
*/
|
||||
declare function writeUser<T extends RC = RC>(config: T, options?: RCOptions | string): void;
|
||||
/**
|
||||
* Reads a configuration file from `$XDG_CONFIG_HOME` or `$HOME/.config` and parses its contents.
|
||||
* @param {RCOptions|string} [options] - Options for reading the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @returns {RC} - The parsed configuration object.
|
||||
*/
|
||||
declare function readUserConfig<T extends RC = RC>(options?: RCOptions | string): T;
|
||||
/**
|
||||
* Writes a configuration object to a file in `$XDG_CONFIG_HOME` or `$HOME/.config`.
|
||||
* @param {RC} config - The configuration object to write. See {@link RC}.
|
||||
* @param {RCOptions|string} [options] - Options for writing the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
*/
|
||||
declare function writeUserConfig<T extends RC = RC>(config: T, options?: RCOptions | string): void;
|
||||
/**
|
||||
* Updates a configuration object in `$XDG_CONFIG_HOME` or `$HOME/.config` by merging and writing the result.
|
||||
* @param {RC} config - The configuration object to update. See {@link RC}.
|
||||
* @param {RCOptions|string} [options] - Options for updating the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @returns {RC} - The updated configuration object.
|
||||
*/
|
||||
declare function updateUserConfig<T extends RC = RC>(config: T, options?: RCOptions | string): T;
|
||||
/**
|
||||
* Updates an existing configuration object by merging it with the contents of a configuration file and writing the result.
|
||||
* @param {RC} config - The configuration object to update. See {@link RC}.
|
||||
* @param {RCOptions|string} [options] - Options for updating the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @returns {RC} - The updated configuration object. See {@link RC}.
|
||||
*/
|
||||
declare function update<T extends RC = RC>(config: T, options?: RCOptions | string): T;
|
||||
/**
|
||||
* Updates a custom configuration object by merging it with the contents of a configuration file in a default location and writing the result.
|
||||
* @param {RC} config - The configuration object to update. See {@link RC}.
|
||||
* @param {RCOptions|string} [options] - Options for updating the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @returns {RC} - The updated configuration object. See {@link RC}.
|
||||
* @deprecated Use {@link updateUserConfig} instead, which uses `~/.config` following XDG conventions.
|
||||
*/
|
||||
declare function updateUser<T extends RC = RC>(config: T, options?: RCOptions | string): T;
|
||||
//#endregion
|
||||
export { RC, RCOptions, defaults, parse, parseFile, read, readUser, readUserConfig, serialize, update, updateUser, updateUserConfig, write, writeUser, writeUserConfig };
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
import { n as unflatten, t as flatten } from "./_chunks/libs/flat.mjs";
|
||||
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import destr from "destr";
|
||||
import { defu } from "defu";
|
||||
//#region src/index.ts
|
||||
const RE_KEY_VAL = /^\s*([^\s=]+)\s*=\s*(.*)?\s*$/;
|
||||
const RE_LINES = /\n|\r|\r\n/;
|
||||
/**
|
||||
* The default options for the configuration file.
|
||||
*/
|
||||
const defaults = {
|
||||
name: ".conf",
|
||||
dir: process.cwd(),
|
||||
flat: false
|
||||
};
|
||||
function withDefaults(options) {
|
||||
if (typeof options === "string") options = { name: options };
|
||||
return {
|
||||
...defaults,
|
||||
...options
|
||||
};
|
||||
}
|
||||
function parse(contents, options = {}) {
|
||||
const config = {};
|
||||
const lines = contents.split(RE_LINES);
|
||||
for (const line of lines) {
|
||||
const match = line.match(RE_KEY_VAL);
|
||||
if (!match) continue;
|
||||
const key = match[1];
|
||||
if (!key || key === "__proto__" || key === "constructor") continue;
|
||||
const value = destr((match[2] || "").trim());
|
||||
if (key.endsWith("[]")) {
|
||||
const nkey = key.slice(0, Math.max(0, key.length - 2));
|
||||
config[nkey] = (config[nkey] || []).concat(value);
|
||||
continue;
|
||||
}
|
||||
config[key] = value;
|
||||
}
|
||||
return options.flat ? config : unflatten(config, { overwrite: true });
|
||||
}
|
||||
/**
|
||||
* Parses a configuration string into an object.
|
||||
* @param {string} contents - The configuration data as a raw string.
|
||||
* @param {RCOptions} [options={}] - Options to control the parsing behaviour. See {@link RCOptions}.
|
||||
* @returns {RC} - The parsed configuration object. See {@link RC}.
|
||||
*/
|
||||
function parseFile(path, options) {
|
||||
if (!existsSync(path)) return {};
|
||||
return parse(readFileSync(path, "utf8"), options);
|
||||
}
|
||||
/**
|
||||
* Reads a configuration file from a default or specified location and parses its contents.
|
||||
* @param {RCOptions|string} [options] - Options for reading the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @returns {RC} - The parsed configuration object. See {@link RC}.
|
||||
*/
|
||||
function read(options) {
|
||||
options = withDefaults(options);
|
||||
return parseFile(resolve(options.dir, options.name), options);
|
||||
}
|
||||
/**
|
||||
* Reads a custom configuration file from a default or specified location and parses its contents.
|
||||
* @param {RCOptions|string} [options] - Options for reading the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @returns {RC} - The parsed configuration object.
|
||||
* @deprecated Use {@link readUserConfig} instead, which uses `~/.config` following XDG conventions.
|
||||
*/
|
||||
function readUser(options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || homedir();
|
||||
return read(options);
|
||||
}
|
||||
/**
|
||||
* Serialises a configuration object to a string format.
|
||||
* @param {RC} config - The configuration object to serialise. See {@link RC}.
|
||||
* @returns {string} - The serialised configuration string.
|
||||
*/
|
||||
function serialize(config) {
|
||||
return Object.entries(flatten(config)).map(([key, value]) => `${key}=${JSON.stringify(value)}`).join("\n");
|
||||
}
|
||||
/**
|
||||
* Writes a configuration object to a file in a default or specified location.
|
||||
* @param {RC} config - The configuration object to write. See {@link RC}.
|
||||
* @param {RCOptions|string} [options] - Options for writing the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
*/
|
||||
function write(config, options) {
|
||||
options = withDefaults(options);
|
||||
writeFileSync(resolve(options.dir, options.name), serialize(config), { encoding: "utf8" });
|
||||
}
|
||||
/**
|
||||
* Writes a custom configuration object to a file in a default or specified location.
|
||||
* @param {RC} config - The configuration object to write. See {@link RC}.
|
||||
* @param {RCOptions|string} [options] - Options for writing the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @deprecated Use {@link writeUserConfig} instead, which uses `~/.config` following XDG conventions.
|
||||
*/
|
||||
function writeUser(config, options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || homedir();
|
||||
write(config, options);
|
||||
}
|
||||
/**
|
||||
* Reads a configuration file from `$XDG_CONFIG_HOME` or `$HOME/.config` and parses its contents.
|
||||
* @param {RCOptions|string} [options] - Options for reading the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @returns {RC} - The parsed configuration object.
|
||||
*/
|
||||
function readUserConfig(options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || resolve(homedir(), ".config");
|
||||
return read(options);
|
||||
}
|
||||
/**
|
||||
* Writes a configuration object to a file in `$XDG_CONFIG_HOME` or `$HOME/.config`.
|
||||
* @param {RC} config - The configuration object to write. See {@link RC}.
|
||||
* @param {RCOptions|string} [options] - Options for writing the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
*/
|
||||
function writeUserConfig(config, options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || resolve(homedir(), ".config");
|
||||
write(config, options);
|
||||
}
|
||||
/**
|
||||
* Updates a configuration object in `$XDG_CONFIG_HOME` or `$HOME/.config` by merging and writing the result.
|
||||
* @param {RC} config - The configuration object to update. See {@link RC}.
|
||||
* @param {RCOptions|string} [options] - Options for updating the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @returns {RC} - The updated configuration object.
|
||||
*/
|
||||
function updateUserConfig(config, options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || resolve(homedir(), ".config");
|
||||
return update(config, options);
|
||||
}
|
||||
/**
|
||||
* Updates an existing configuration object by merging it with the contents of a configuration file and writing the result.
|
||||
* @param {RC} config - The configuration object to update. See {@link RC}.
|
||||
* @param {RCOptions|string} [options] - Options for updating the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @returns {RC} - The updated configuration object. See {@link RC}.
|
||||
*/
|
||||
function update(config, options) {
|
||||
options = withDefaults(options);
|
||||
if (!options.flat) config = unflatten(config, { overwrite: true });
|
||||
const newConfig = defu(config, read(options));
|
||||
write(newConfig, options);
|
||||
return newConfig;
|
||||
}
|
||||
/**
|
||||
* Updates a custom configuration object by merging it with the contents of a configuration file in a default location and writing the result.
|
||||
* @param {RC} config - The configuration object to update. See {@link RC}.
|
||||
* @param {RCOptions|string} [options] - Options for updating the configuration file, or the name of the configuration file. See {@link RCOptions}.
|
||||
* @returns {RC} - The updated configuration object. See {@link RC}.
|
||||
* @deprecated Use {@link updateUserConfig} instead, which uses `~/.config` following XDG conventions.
|
||||
*/
|
||||
function updateUser(config, options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || homedir();
|
||||
return update(config, options);
|
||||
}
|
||||
//#endregion
|
||||
export { defaults, parse, parseFile, read, readUser, readUserConfig, serialize, update, updateUser, updateUserConfig, write, writeUser, writeUserConfig };
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "rc9",
|
||||
"version": "3.0.1",
|
||||
"description": "Read/Write config couldn't be easier!",
|
||||
"license": "MIT",
|
||||
"repository": "unjs/rc9",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"type": "module",
|
||||
"sideEffects": false,
|
||||
"main": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.mts",
|
||||
"exports": {
|
||||
".": "./dist/index.mjs"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "obuild",
|
||||
"dev": "vitest",
|
||||
"lint": "oxlint . && oxfmt --check src test",
|
||||
"format": "oxlint . --fix && oxfmt src test",
|
||||
"release": "pnpm test && pnpm build && changelogen --release --push && npm publish",
|
||||
"test": "pnpm lint && pnpm typecheck && vitest run --coverage",
|
||||
"typecheck": "tsgo --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"defu": "^6.1.6",
|
||||
"destr": "^2.0.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^25.5.0",
|
||||
"@typescript/native-preview": "^7.0.0-dev.20260401.1",
|
||||
"@vitest/coverage-v8": "^4.1.2",
|
||||
"automd": "^0.4.3",
|
||||
"changelogen": "^0.6.2",
|
||||
"flat": "^6.0.1",
|
||||
"obuild": "^0.4.32",
|
||||
"oxfmt": "^0.43.0",
|
||||
"oxlint": "^1.58.0",
|
||||
"typescript": "^6.0.2",
|
||||
"vitest": "^4.1.2"
|
||||
},
|
||||
"packageManager": "pnpm@10.33.0"
|
||||
}
|
||||
Reference in New Issue
Block a user