Skip to content

Commit

Permalink
feat: added new rc package, added missing docs
Browse files Browse the repository at this point in the history
  • Loading branch information
prisis committed May 22, 2024
1 parent 1aa691b commit e919c69
Show file tree
Hide file tree
Showing 37 changed files with 811 additions and 50 deletions.
15 changes: 15 additions & 0 deletions packages/rc/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
dist
node_modules
coverage

__fixtures__
__docs__

vitest.config.ts
.prettierrc.cjs
tsup.config.ts
.secretlintrc.cjs
tsconfig.eslint.json

README.md

90 changes: 90 additions & 0 deletions packages/rc/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/** @ts-check */
// eslint-disable-next-line import/no-commonjs,import/no-unused-modules
const { defineConfig } = require("@anolilab/eslint-config/define-config");
// eslint-disable-next-line import/no-commonjs
const globals = require("@anolilab/eslint-config/globals");

/// <reference types="@eslint-types/unicorn" />
/// <reference types="@eslint-types/typescript-eslint" />
/// <reference types="@eslint-types/jsdoc" />
/// <reference types="@eslint-types/import" />
/// <reference types="@eslint-types/deprecation" />

/** @type {import('eslint').Linter.Config} */
module.exports = defineConfig({
env: {
// Your environments (which contains several predefined global variables)
// Most environments are loaded automatically if our rules are added
},
extends: ["@anolilab/eslint-config", "@anolilab/eslint-config/typescript-type-checking"],
globals: {
...globals.es2021,
// Your global variables (setting to false means it's not allowed to be reassigned)
// myGlobal: false
},
ignorePatterns: ["!**/*"],
overrides: [
{
files: ["*.ts", "*.tsx", "*.mts", "*.cts", "*.js", "*.jsx"],
// Set parserOptions.project for the project to allow TypeScript to create the type-checker behind the scenes when we run linting
parserOptions: {},
rules: {},
},
{
files: ["*.ts", "*.tsx", "*.mts", "*.cts"],
// Set parserOptions.project for the project to allow TypeScript to create the type-checker behind the scenes when we run linting
parserOptions: {},
rules: {
"@typescript-eslint/no-unsafe-argument": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off",
"prefer-template": "off",
},
},
{
files: ["*.js", "*.jsx"],
rules: {},
},
{
files: ["*.mdx"],
rules: {
"jsx-a11y/anchor-has-content": "off",
// @see https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/issues/917
"jsx-a11y/heading-has-content": "off",
},
},
{
files: ["src/index.ts"],
rules: {
"import/no-unused-modules": "off",
},
},
{
files: ["__docs__/**"],
rules: {
"import/no-unresolved": "off",
"import/no-unused-modules": "off",
"no-console": "off",
"no-undef": "off",
"no-unused-vars": "off",
"unicorn/prefer-top-level-await": "off",
},
},
{
files: ["__tests__/**"],
rules: {
"import/no-unused-modules": "off",
},
},
],
parserOptions: {
ecmaVersion: 2021,
project: "./tsconfig.eslint.json",
sourceType: "module",
},
// Report unused `eslint-disable` comments.
reportUnusedDisableDirectives: true,
root: true,
});
9 changes: 9 additions & 0 deletions packages/rc/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package-lock.json

src
__tests__
__stories__
__fixtures__
.rpt2_cache
fixup.sh
.releaserc.json
9 changes: 9 additions & 0 deletions packages/rc/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.gitkeep
.env*
*.ico
*.lock
dist
CHANGELOG.md
coverage
node_modules
.eslintcache
5 changes: 5 additions & 0 deletions packages/rc/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const config = require("@anolilab/prettier-config");

module.exports = {
...config,
};
3 changes: 3 additions & 0 deletions packages/rc/.releaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@anolilab/semantic-release-preset/npm"
}
3 changes: 3 additions & 0 deletions packages/rc/.secretlintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.pnpm-store
packages/**/node_modules
node_modules
7 changes: 7 additions & 0 deletions packages/rc/.secretlintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
rules: [
{
id: "@secretlint/secretlint-rule-preset-recommend",
},
],
};
Empty file added packages/rc/CHANGELOG.md
Empty file.
21 changes: 21 additions & 0 deletions packages/rc/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 anolilab

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.
112 changes: 112 additions & 0 deletions packages/rc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<div align="center">
<h3>anolilab rc</h3>
<p>
This module provides a utility function to load rc configuration settings from various sources, including environment variables, default values, and configuration files located in multiple standard directories. It merges these settings into a single configuration object.
</p>
</div>

<br />

<div align="center">

[![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url]

</div>

---

<div align="center">
<p>
<sup>
Daniel Bannert's open source work is supported by the community on <a href="https://github.com/sponsors/prisis">GitHub Sponsors</a>
</sup>
</p>
</div>

---

## Install

```sh
npm install @anolilab/rc
```

```sh
yarn add @anolilab/rc
```

```sh
pnpm add @anolilab/rc
```

## Usage

The main function provided by this module is rc. It allows you to load configuration settings for your application from different sources.

```ts
import { rc } from "@anolilab/rc";

const { config, files } = rc("npm");

// returns a merged config object with all found npmrc files and a files list what files where found.
```

### Api

- name (string): The application name. This is used to locate configuration files and environment variables.
- options (object, optional): An object with the following properties:
- config (string, optional): Path to a specific configuration file.
- cwd (string, optional): The current working directory to start searching for configuration files. Defaults to process.cwd().
- defaults (object, optional): Default configuration values.
- home (string, optional): The home directory to use. Defaults to os.homedir().
- stopAt (string, optional): Directory to stop searching for configuration files.

## Standards

Given your application name (appname), rc will look in all the obvious places for configuration.

- The defaults object you passed in
- `/etc/${appname}/config`
- `/etc/${appname}rc`
- `$HOME/.config/${appname}/config`
- `$HOME/.config/${appname}`
- `$HOME/.${appname}/config`
- `$HOME/.${appname}rc`
- a local `.${appname}/config` and `.${appname}rc` and all found looking in `../../../ ../../ ../ ./` etc.
- if you passed environment variable `${appname}_config` then from that file
- if you passed options.config variable, then from that file
- environment variables prefixed with `${appname}_`
or use "\_\_" to indicate nested properties <br/> _(e.g. `appname_foo__bar__baz` => `foo.bar.baz`)_

All configuration sources that were found will be flattened into one object, in this exact order.

## Related

- [rc](https://github.com/dominictarr/rc) - The non-configurable configuration loader for lazy people.

## Supported Node.js Versions

Libraries in this ecosystem make the best effort to track [Node.js’ release schedule](https://github.com/nodejs/release#release-schedule).
Here’s [a post on why we think this is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a).

## Contributing

If you would like to help take a look at the [list of issues](https://github.com/anolilab/semantic-release/issues) and check our [Contributing](.github/CONTRIBUTING.md) guidelines.

> **Note:** please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
## Credits

- [Daniel Bannert](https://github.com/prisis)
- [All Contributors](https://github.com/anolilab/semantic-release/graphs/contributors)

## License

The anolilab rc is open-sourced software licensed under the [MIT][license-url]

[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[typescript-url]: "typescript"
[license-image]: https://img.shields.io/npm/l/@anolilab/rc?color=blueviolet&style=for-the-badge
[license-url]: LICENSE.md "license"
[npm-image]: https://img.shields.io/npm/v/@anolilab/rc/latest.svg?style=for-the-badge&logo=npm
[npm-url]: https://www.npmjs.com/package/@anolilab/rc/v/latest "npm"
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { join } from "@visulima/path";
import { temporaryDirectory } from "tempy";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import rc from "../../../src/utils/rc";
import { rc } from "../src";

const mocks = vi.hoisted(() => {
return { mockedCwd: vi.fn(), mockedFindUpSync: vi.fn(), mockedHomeDir: vi.fn(), mockedIsAccessibleSync: vi.fn(), mockedReadFileSync: vi.fn() };
Expand Down Expand Up @@ -43,8 +43,9 @@ describe("rc-unmocked", () => {
// eslint-disable-next-line no-loops/no-loops,no-restricted-syntax
for (const key in env) {
if (key.startsWith("npm_")) {
// eslint-disable-next-line security/detect-object-injection
npmEnvironment[key as keyof typeof env] = env[key];
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete,security/detect-object-injection
delete env[key];
}
}
Expand All @@ -53,6 +54,7 @@ describe("rc-unmocked", () => {
afterEach(async () => {
// eslint-disable-next-line no-loops/no-loops,no-restricted-syntax,guard-for-in
for (const key in npmEnvironment) {
// eslint-disable-next-line security/detect-object-injection
env[key] = npmEnvironment[key];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { join } from "@visulima/path";
import { temporaryDirectory } from "tempy";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import rc from "../../../src/utils/rc";
import { rc } from "../src";

// Adds extention to the last item of an array representing fs path
const addExtensions = (sources: string[][]) =>
Expand Down Expand Up @@ -64,8 +64,9 @@ describe("rc", () => {
// eslint-disable-next-line no-loops/no-loops,no-restricted-syntax
for (const key in env) {
if (key.startsWith("npm_")) {
// eslint-disable-next-line security/detect-object-injection
npmEnvironment[key as keyof typeof env] = env[key];
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete,security/detect-object-injection
delete env[key];
}
}
Expand All @@ -74,6 +75,7 @@ describe("rc", () => {
afterEach(async () => {
// eslint-disable-next-line no-loops/no-loops,no-restricted-syntax,guard-for-in
for (const key in npmEnvironment) {
// eslint-disable-next-line security/detect-object-injection
env[key] = npmEnvironment[key];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";

import isJson from "../../../src/utils/is-json";
import isJson from "../../src/utils/is-json";

describe("isJson", () => {
it.each([5, "5", "true", "null", "{}", '{"foo": "bar"}', "[1, 2, 3]", '{"foo": "bar", "baz": "qux"}'])("should return true for valid JSON", (value) => {
Expand Down
Loading

0 comments on commit e919c69

Please sign in to comment.