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

Commit

Permalink
feat!: enable on browser env (#7)
Browse files Browse the repository at this point in the history
* v1.0.4

* v1.0.5

* feat!: embedded default configuration at build

* feat!: embedded default configuration at build

* v1.1.0

* chore: removed unused directives

* chore: changed node version on Action
  • Loading branch information
Fernand authored Mar 4, 2024
1 parent 9e79ad1 commit 709dd26
Show file tree
Hide file tree
Showing 13 changed files with 446 additions and 1,636 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"plugins": ["@typescript-eslint", "sonarjs"],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "plugin:sonarjs/recommended"],
"ignorePatterns": ["**/*.js"],
"ignorePatterns": ["**/*.js", "**/tests/*", "dist/**"],
"rules": {
"prefer-arrow-callback": ["warn", { "allowNamedFunctions": true }],
"func-style": ["warn", "declaration", { "allowArrowFunctions": false }],
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/jest-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
steps:
- uses: actions/setup-node@v4
with:
node-version: latest
node-version: '20.x'
- uses: actions/checkout@v4
with:
fetch-depth: 0
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ static/dist
*.tgz
coverage
coverage.txt

dist
26 changes: 15 additions & 11 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
roots: ["./tests"],
coveragePathIgnorePatterns: ["node_modules", "mocks"],
collectCoverage: true,
coverageReporters: ["json", "lcov", "text", "clover", "json-summary"],
reporters: ["default", "jest-junit"],
coverageDirectory: "coverage",
};
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
roots: ["./tests"],
coveragePathIgnorePatterns: ["node_modules", "mocks"],
collectCoverage: true,
coverageReporters: ["json", "lcov", "text", "clover", "json-summary"],
reporters: ["default", "jest-junit"],
coverageDirectory: "coverage",
transform: {
"\\.yml$": "jest-transform-yaml",
},
moduleFileExtensions: ["yml", "ts", "js"],
};
30 changes: 22 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
{
"name": "@ubiquibot/configuration",
"version": "1.0.0",
"version": "1.1.0",
"private": false,
"description": "Configuration helpers for Ubiquibot.",
"main": "src/index.ts",
"author": "Ubiquity DAO",
"license": "MIT",
"engines": {
"node": ">=20.10.0"
},
"scripts": {
"start": "tsx build/esbuild-server.ts",
"build": "tsx build/esbuild-build.ts",
"build": "rollup -c",
"format": "run-s format:lint format:prettier format:cspell",
"format:lint": "eslint --fix .",
"format:prettier": "prettier --write .",
Expand All @@ -24,6 +22,7 @@
"template",
"dao",
"ubiquity",
"ubiquibot",
"open-source"
],
"dependencies": {
Expand All @@ -32,15 +31,21 @@
"ajv-formats": "2.1.1",
"dotenv": "16.4.4",
"lodash": "4.17.21",
"ms": "2.1.3",
"ubiquibot-logger": "0.3.5"
"ms": "2.1.3"
},
"peerDependencies": {},
"devDependencies": {
"@commitlint/cli": "18.6.1",
"@commitlint/config-conventional": "18.6.2",
"@cspell/dict-node": "4.0.3",
"@cspell/dict-software-terms": "3.3.18",
"@cspell/dict-typescript": "3.1.2",
"@rollup/plugin-commonjs": "25.0.7",
"@rollup/plugin-json": "6.1.0",
"@rollup/plugin-node-resolve": "15.2.3",
"@rollup/plugin-terser": "0.4.4",
"@rollup/plugin-typescript": "11.1.6",
"@rollup/plugin-yaml": "4.1.2",
"@types/jest": "29.5.12",
"@types/lodash": "4.14.202",
"@types/ms": "0.7.34",
Expand All @@ -49,18 +54,24 @@
"@typescript-eslint/parser": "7.0.1",
"cspell": "8.3.2",
"esbuild": "0.20.0",
"esbuild-plugin-yaml": "0.0.1",
"eslint": "8.56.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.1.3",
"eslint-plugin-sonarjs": "0.24.0",
"husky": "9.0.11",
"jest": "29.7.0",
"jest-junit": "16.0.0",
"jest-transform-yaml": "1.1.2",
"knip": "5.0.1",
"lint-staged": "15.2.2",
"npm-run-all": "4.1.5",
"prettier": "3.2.5",
"rollup": "4.12.0",
"rollup-plugin-dts-bundle-generator": "1.4.0",
"rollup-plugin-typescript2": "0.36.0",
"ts-jest": "29.1.2",
"tslib": "2.6.2",
"tsx": "4.7.1",
"typescript": "5.3.3"
},
Expand All @@ -79,8 +90,11 @@
]
},
"files": [
"src/*",
"dist/*",
"README.md",
"package.json"
]
],
"module": "dist/index.esm.js",
"main": "dist/index.js",
"typings": "dist/index.d.ts"
}
16 changes: 16 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import typescript from "rollup-plugin-typescript2";
import yaml from "@rollup/plugin-yaml";
import { generateDtsBundle } from "rollup-plugin-dts-bundle-generator";
import nodeResolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import terser from "@rollup/plugin-terser";

export default {
input: "src/index.ts",
output: {
dir: "dist",
format: "cjs",
},
plugins: [nodeResolve(), commonjs(), typescript(), yaml(), json(), generateDtsBundle(), terser()],
};
11 changes: 9 additions & 2 deletions src/types/configuration-types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ObjectOptions, Static, StaticDecode, StringOptions, TProperties, Type as T } from "@sinclair/typebox";
import ms from "ms";
import { LogLevel } from "ubiquibot-logger/pretty-logs";

import { ajv } from "../utils/ajv";
import { ajv } from "../utils";
import { validHTMLElements } from "./valid-html-elements";

const promotionComment =
Expand All @@ -20,6 +19,14 @@ const allHtmlElementsSetToZero = validHTMLElements.reduce(
{} as Record<keyof HTMLElementTagNameMap, number>
);

export enum LogLevel {
FATAL = "fatal",
ERROR = "error",
INFO = "info",
VERBOSE = "verbose",
DEBUG = "debug",
}

const allCommands = ["start", "stop", "help", "query", "ask", "multiplier", "labels", "authorize", "wallet"] as const;

const defaultTimeLabels = ["Time: <1 Hour", "Time: <2 Hours", "Time: <4 Hours", "Time: <1 Day", "Time: <1 Week"];
Expand Down
12 changes: 4 additions & 8 deletions src/utils/generate-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Value } from "@sinclair/typebox/value";
import { DefinedError } from "ajv";
import * as fs from "fs/promises";
import mergeWith from "lodash/merge";
import path from "node:path";
import YAML from "yaml";
import { BotConfig, stringDuration, validateBotConfig } from "../types/configuration-types";

const UBIQUIBOT_CONFIG_FULL_PATH = path.join(__dirname, "../../.github/ubiquibot-config.yml");

export async function generateConfiguration(repoConfig?: BotConfig): Promise<BotConfig> {
const orgConfig = parseYaml(await fs.readFile(UBIQUIBOT_CONFIG_FULL_PATH, { encoding: "utf-8" }));
import { BotConfig, stringDuration, validateBotConfig } from "../types";
// @ts-expect-error gets transformed by rollup
import orgConfig from "../../.github/ubiquibot-config.yml";

export function generateConfiguration(repoConfig?: BotConfig): BotConfig {
const merged = mergeWith({}, orgConfig, repoConfig, (objValue: unknown, srcValue: unknown) => {
if (Array.isArray(objValue) && Array.isArray(srcValue)) {
// if it's string array, concat and remove duplicates
Expand Down
File renamed without changes.
File renamed without changes.
17 changes: 10 additions & 7 deletions tests/configuration.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import * as fs from "fs";
import path from "node:path";
import { BotConfig, generateConfiguration, parseYaml, transformConfig } from "../src";
import validConfig from "./mocks/test-valid-conf-obj";
import validConfig from "./__mocks__/test-valid-conf-obj";

const filePath = path.join(__dirname, "./mocks/test-valid-config.yml");
const filePath = path.join(__dirname, "./__mocks__/test-valid-config.yml");

describe("Configuration generation", () => {
test("Parse Yaml file", () => {
const fileContent = fs.readFileSync(filePath, { encoding: "utf8" });
const parsed = parseYaml(fileContent);
// Silences the error output since it is expected to have errors logged
const spy = jest.spyOn(console, "error").mockImplementation(jest.fn());
expect(() => parseYaml("$@-test-123\\\ntest -: test")).toThrow();
spy.mockClear();
const parseEmpty = parseYaml(null);
expect(parseEmpty).toBeNull();
expect(parsed).toStrictEqual({
Expand All @@ -19,15 +22,15 @@ describe("Configuration generation", () => {
});

test("Generate configuration", async () => {
const cfg = await generateConfiguration();
const cfg = generateConfiguration();
expect(cfg).toStrictEqual(validConfig);
await expect(async () => {
await generateConfiguration({ keys: ["123"] } as unknown as BotConfig);
}).rejects.toThrow();
expect(() => {
generateConfiguration({ keys: ["123"] } as unknown as BotConfig);
}).toThrow();
});

test("Transform configuration", async () => {
const cfg = await generateConfiguration();
const cfg = generateConfiguration();
expect(() => {
transformConfig(cfg);
}).toThrow();
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "commonjs" /* Specify what module code is generated. */,
"module": "esnext" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
"moduleResolution": "Node10", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
Expand Down
Loading

1 comment on commit 709dd26

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines Statements Branches Functions
Coverage: 91%
91.08% (92/101) 71.42% (15/21) 80% (12/15)

JUnit

Tests Skipped Failures Errors Time
3 0 💤 0 ❌ 0 🔥 3.842s ⏱️
Coverage Report (91%)
File% Stmts% Branch% Funcs% LinesUncovered Line #s
All files91.0871.428091.66 
   .github100100100100 
   ubiquibot-config.yml100100100100 
src100100100100 
   index.ts100100100100 
src/types94.288085.7194.11 
   configuration-types.ts93.758085.7193.5445, 49
   index.ts100100100100 
   valid-html-elements.ts100100100100 
src/utils88.8868.757589.83 
   ajv.ts100100100100 
   generate-configuration.ts87.9368.757588.8811–17, 33–34
   index.ts100100100100 

Please sign in to comment.