This commit is contained in:
darenhsu
2022-07-17 13:16:16 +08:00
parent 84759556ff
commit befd344ab0
28070 changed files with 4008428 additions and 1 deletions
+40
View File
@@ -0,0 +1,40 @@
# Changelog
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
## [1.0.0](https://github.com/nuxt-contrib/rc9/compare/v0.0.7...v1.0.0) (2020-06-16)
### [0.0.7](https://github.com/nuxt-contrib/rc9/compare/v0.0.6...v0.0.7) (2020-05-28)
### Bug Fixes
* set `sideEffects` field in package.json to allow tree-shaking ([c8fc9cc](https://github.com/nuxt-contrib/rc9/commit/c8fc9ccc8eeffe70f5cf6d8ae832989c9ce3bdb4))
### [0.0.6](https://github.com/nuxt-contrib/rc9/compare/v0.0.5...v0.0.6) (2020-05-28)
### [0.0.5](https://github.com/nuxt-contrib/rc9/compare/v0.0.4...v0.0.5) (2020-05-28)
### [0.0.4](https://github.com/nuxt-contrib/rc9/compare/v0.0.3...v0.0.4) (2020-05-28)
### [0.0.3](https://github.com/nuxt-contrib/rc9/compare/v0.0.2...v0.0.3) (2020-05-28)
### Features
* update() and improvements ([69539be](https://github.com/nuxt-contrib/rc9/commit/69539bed862cf5659971329d2007e78d97bcd2a4))
### [0.0.2](https://github.com/nuxt-contrib/rc9/compare/v0.0.1...v0.0.2) (2020-05-27)
### Features
* support directly passing name as options ([e493cdf](https://github.com/nuxt-contrib/rc9/commit/e493cdf8fda7bda4eb2b95148485d8a008feff4c))
### Bug Fixes
* allow more characters for key ([9557009](https://github.com/nuxt-contrib/rc9/commit/955700996ff0b9f3c34135adb42146d718df83a7))
* pass encoding to writeFileSync ([59c937a](https://github.com/nuxt-contrib/rc9/commit/59c937a9a434e28d9e083db66b552383b61a975f))
### 0.0.1 (2020-05-27)
Generated Vendored
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 - Pooya Parsa <pyapar@gmail.com>
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.
Generated Vendored
+147
View File
@@ -0,0 +1,147 @@
# RC9
> Read/Write config couldn't be easier!
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![Github Actions][github-actions-src]][github-actions-href]
[![Codecov][codecov-src]][codecov-href]
## Install
Install using npm or yarn:
```bash
npm i rc9
# or
yarn add rc9
```
Import into your Node.js project:
```js
// CommonJS
const { read, write, update } = require('rc9')
// ESM
import { read, write, update } from 'rc9'
```
## Usage
`.conf`:
```ts
db.username=db username
db.password=db pass
db.enabled=true
```
**Update config:**
```ts
update({ 'db.enabled': true }) // or update(..., { name: '.conf' })
```
**Read/Write config:**
```ts
const config = read() // or read('.conf')
// config = {
// db: {
// username: 'db username',
// password: 'db pass',
// enabled: true
// }
// }
config.enabled = false
write(config) // or write(config, '.conf')
```
**User Config:**
It is common to keep config in user home directory (MacOS: `/Users/{name}`, Linux: `/home/{name}`, Windows: `C:\users\{name}`)
you can use `readUser`/`writeuser`/`updateUser` shortcuts to quickly do this:
```js
writeUser({ token: 123 }, '.zoorc') // Will be saved in {home}/.zoorc
const conf = readUser('.zoorc') // { token: 123 }
```
## 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.
## 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 readUser(options?: RCOptions | string): RC;
function serialize(config: RC): string;
function write(config: RC, options?: RCOptions | string): void;
function writeUser(config: RC, options?: RCOptions | string): void;
function update(config: RC, options?: RCOptions | string): RC;
function updateUser(config: RC, options?: RCOptions | string): RC;
```
**Types:**
```ts
type RC = Record<string, any>;
interface RCOptions {
name?: string;
dir?: string;
flat?: boolean;
}
```
**Defaults:**
```ts
{
name: '.conf',
dir: process.cwd(),
flat: false
}
```
### Why RC9?
Be the first one to guess 🐇 <!-- Hint: do research about rc files history -->
## License
MIT. Made with 💖
<!-- Badges -->
[npm-version-src]: https://img.shields.io/npm/v/rc9?style=flat-square
[npm-version-href]: https://npmjs.com/package/rc9
[npm-downloads-src]: https://img.shields.io/npm/dm/rc9?style=flat-square
[npm-downloads-href]: https://npmjs.com/package/rc9
[github-actions-src]: https://img.shields.io/github/workflow/status/nuxt-contrib/rc9/ci/master?style=flat-square
[github-actions-href]: https://github.com/nuxt-contrib/rc9/actions?query=workflow%3Aci
[codecov-src]: https://img.shields.io/codecov/c/gh/nuxt-contrib/rc9/master?style=flat-square
[codecov-href]: https://codecov.io/gh/nuxt-contrib/rc9
+17
View File
@@ -0,0 +1,17 @@
declare type RC = Record<string, any>;
interface RCOptions {
name?: string;
dir?: string;
flat?: boolean;
}
export declare const defaults: RCOptions;
export declare function parse(contents: string, options?: RCOptions): RC;
export declare function parseFile(path: string, options?: RCOptions): RC;
export declare function read(options?: RCOptions | string): RC;
export declare function readUser(options?: RCOptions | string): RC;
export declare function serialize(config: RC): string;
export declare function write(config: RC, options?: RCOptions | string): void;
export declare function writeUser(config: RC, options?: RCOptions | string): void;
export declare function update(config: RC, options?: RCOptions | string): RC;
export declare function updateUser(config: RC, options?: RCOptions | string): RC;
export {};
+124
View File
@@ -0,0 +1,124 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var fs = require('fs');
var path = require('path');
var os = require('os');
var destr = _interopDefault(require('destr'));
var flat = require('flat');
var defu = _interopDefault(require('defu'));
var RE_KEY_VAL = /^\s*([^=\s]+)\s*=\s*(.*)?\s*$/;
var RE_LINES = /\n|\r|\r\n/;
var defaults = {
name: '.conf',
dir: process.cwd(),
flat: false
};
function withDefaults(options) {
if (typeof options === 'string') {
options = {
name: options
};
}
return Object.assign(Object.assign({}, defaults), options);
}
function parse(contents, options) {
var config = {};
var lines = contents.split(RE_LINES);
for (var i = 0, list = lines; i < list.length; i += 1) {
var line = list[i];
var match = line.match(RE_KEY_VAL);
if (!match) {
continue;
} // Key
var key = match[1];
if (!key || key === '__proto__' || key === 'constructor') {
continue;
}
config[key] = destr(match[2].trim()
/* val */
);
}
return (options === null || options === void 0 ? void 0 : options.flat) ? config : flat.unflatten(config, {
overwrite: true
});
}
function parseFile(path, options) {
if (!fs.existsSync(path)) {
return {};
}
return parse(fs.readFileSync(path, 'utf-8'), options);
}
function read(options) {
options = withDefaults(options);
return parseFile(path.resolve(options.dir, options.name), options);
}
function readUser(options) {
options = withDefaults(options);
options.dir = os.homedir();
return read(options);
}
function serialize(config) {
return Object.entries(flat.flatten(config)).map(function (ref) {
var key = ref[0];
var val = ref[1];
return (key + "=" + (typeof val === 'string' ? val : JSON.stringify(val)));
}).join('\n');
}
function write(config, options) {
options = withDefaults(options);
fs.writeFileSync(path.resolve(options.dir, options.name), serialize(config), {
encoding: 'utf-8'
});
}
function writeUser(config, options) {
options = withDefaults(options);
options.dir = os.homedir();
write(config, options);
}
function update(config, options) {
options = withDefaults(options);
if (!options.flat) {
config = flat.unflatten(config, {
overwrite: true
});
}
var newConfig = defu(config, read(options));
write(newConfig, options);
return newConfig;
}
function updateUser(config, options) {
options = withDefaults(options);
options.dir = os.homedir();
return update(config, options);
}
exports.defaults = defaults;
exports.parse = parse;
exports.parseFile = parseFile;
exports.read = read;
exports.readUser = readUser;
exports.serialize = serialize;
exports.update = update;
exports.updateUser = updateUser;
exports.write = write;
exports.writeUser = writeUser;
+73
View File
@@ -0,0 +1,73 @@
{
"_args": [
[
"rc9@1.0.0",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
]
],
"_from": "rc9@1.0.0",
"_id": "rc9@1.0.0",
"_inBundle": false,
"_integrity": "sha512-MVgjFCXkndOD08FBqnCgX92ND36NaGVEzBuImhZ5XVFZz2rF9RGfTiQ0yczxdNEoNbwIRC1guhQJVUMljsqvTg==",
"_location": "/rc9",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "rc9@1.0.0",
"name": "rc9",
"escapedName": "rc9",
"rawSpec": "1.0.0",
"saveSpec": null,
"fetchSpec": "1.0.0"
},
"_requiredBy": [
"/@nuxt/config",
"/@nuxt/telemetry"
],
"_resolved": "https://registry.npmjs.org/rc9/-/rc9-1.0.0.tgz",
"_spec": "1.0.0",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"bugs": {
"url": "https://github.com/nuxt-contrib/rc9/issues"
},
"dependencies": {
"defu": "^2.0.4",
"destr": "^1.0.0",
"flat": "^5.0.0"
},
"description": "Read/Write config couldn't be easier!",
"devDependencies": {
"@nuxtjs/eslint-config-typescript": "latest",
"@types/flat": "latest",
"@types/jest": "latest",
"@types/node": "latest",
"bili": "latest",
"eslint": "latest",
"jest": "latest",
"rollup-plugin-typescript2": "latest",
"standard-version": "latest",
"ts-jest": "latest",
"typescript": "latest"
},
"files": [
"dist"
],
"homepage": "https://github.com/nuxt-contrib/rc9#readme",
"license": "MIT",
"main": "dist/index.js",
"name": "rc9",
"repository": {
"type": "git",
"url": "git+https://github.com/nuxt-contrib/rc9.git"
},
"scripts": {
"build": "bili src/index.ts --minimal",
"lint": "eslint --ext .ts .",
"release": "yarn test && yarn build && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint && jest"
},
"sideEffects": false,
"types": "dist/index.d.ts",
"version": "1.0.0"
}