Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#9430: extracting first util lib #9491

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
node-version-file: applications/browser-extension/package.json
cache: npm
- run: npm ci
- run: npm run test --workspace=applications/browser-extension -- --coverage
- run: npm run test --workspaces -- --coverage
- uses: actions/upload-artifact@v4
with:
name: extension-test-coverage
Expand Down Expand Up @@ -151,7 +151,7 @@ jobs:
node-version-file: applications/browser-extension/package.json
cache: npm
- run: npm ci
- run: npm run build:typescript --workspace=applications/browser-extension
- run: npm run build:typecheck --workspaces

lint:
runs-on: ubuntu-latest
Expand Down
10 changes: 3 additions & 7 deletions applications/browser-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"watch:webpack": "ENV_FILE='.env.development' webpack ${HMR:-watch} --mode development",
"watch:webpack-hmr": "HMR=serve npm run watch:webpack",
"watch:typescript": "tsc --noEmit --watch",
"build": "concurrently npm:build:webpack npm:build:typescript -r",
"build": "concurrently npm:build:webpack npm:build:typecheck -r",
"build:webpack": "NODE_OPTIONS=--max_old_space_size=8192 webpack --mode production",
"build:typescript": "tsc --noEmit",
"build:typecheck": "tsc --noEmit",
"generate:headers": "npm run test -- src/development/headers.test.ts --no-silent",
"storybook": "storybook dev -p 6006 -s public",
"build-storybook": "storybook build",
Expand All @@ -27,11 +27,6 @@
"dead-code:base": "echo 'skipping knip until fixed in next changeset. See: https://knip.dev/guides/handling-issues#missing-binaries'",
"dead-code:prod": "npm run dead-code -- --production"
},
"engine-strict": true,
"engines": {
"node": "20.12.0",
"npm": "10.5.0"
},
Comment on lines -30 to -34
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

moved to the root package.json

"author": "Todd Schiller",
"license": "AGPL-3.0",
"repository": "https://github.com/pixiebrix/pixiebrix-extension",
Expand All @@ -50,6 +45,7 @@
"@fortawesome/react-fontawesome": "^0.2.2",
"@mozilla/readability": "^0.5.0",
"@pixiebrix/jq-web": "^0.5.1",
"@pixiebrix/utils": "*",
"@reduxjs/toolkit": "^1.9.7",
"@rjsf/bootstrap-4": "^5.22.3",
"@rjsf/core": "^5.22.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe("DataPanel state", () => {

describe("Add/Remove Bricks", () => {
let editor: EditorState;
let consoleErrorSpy: jest.SpyInstance;

const source = formStateFactory({
formStateConfig: {
Expand All @@ -155,6 +156,12 @@ describe("Add/Remove Bricks", () => {
initialState,
actions.addModComponentFormState(source),
);

consoleErrorSpy = jest.spyOn(console, "error").mockImplementation();
});

afterEach(() => {
consoleErrorSpy.mockRestore();
});

test("Add Brick", async () => {
Expand All @@ -178,6 +185,42 @@ describe("Add/Remove Bricks", () => {
).toBeArrayOfSize(initialBricks.length + 1);
});

test("Add Brick - bad pipeline path error", async () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added this test just to ensure there was no regression with this existing debug util code.

// Add a Brick
expect(() =>
editorSlice.reducer(
editor,
actions.addNode({
block: standardBrick,
pipelinePath: "badPath.to.modComponent",
pipelineIndex: 0,
}),
),
).toThrowError(
"Invalid pipeline path for mod component form state: badPath.to.modComponent",
);

expect(consoleErrorSpy).toHaveBeenCalledWith(
"Invalid pipeline path for mod component form state: %s",
"badPath.to.modComponent",
{
block: {
config: {},
id: "test/teapot",
instanceId: "00000001-0000-4000-A000-000000000000",
outputKey: "teapotOutput",
},
element: expect.any(Object),
invalidPath: {
invalidPath: "badPath",
values: expect.any(Object),
},
pipelineIndex: 0,
pipelinePath: "badPath.to.modComponent",
},
);
});

test("Remove Brick with Integration Dependency", async () => {
// Get initial bricks and integration dependencies
const initialBricks =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { uuidv4 } from "@/types/helpers";
import { cloneDeep, compact, get, pull, uniq } from "lodash";
import { DataPanelTabKey } from "@/pageEditor/tabs/editTab/dataPanel/dataPanelTypes";
import { type TreeExpandedState } from "@/components/jsonTree/JsonTree";
import { getInvalidPath } from "@/utils/debugUtils";
import { getInvalidPath } from "@pixiebrix/utils/src/debugUtils";
import {
selectActiveBrickConfigurationUIState,
selectActiveBrickPipelineUIState,
Expand Down
30 changes: 30 additions & 0 deletions libraries/utils/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2024 PixieBrix, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

const config = {
silent: true,
modulePaths: ["/src"],
moduleFileExtensions: ["ts", "tsx", "js", "jsx"],
transform: {
"\\.[jt]sx?$": "@swc/jest",
},
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
},
};

module.exports = config;
22 changes: 22 additions & 0 deletions libraries/utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@pixiebrix/utils",
"version": "1.0.0",
"description": "PixieBrix Utility Library",
"scripts": {
"test": "TZ=UTC jest",
"lint": "echo 'missing eslint'",
"build": "echo 'buildless package'",
"build:typecheck": "tsc --noEmit"
},
"license": "AGPL-3.0",
"repository": "https://github.com/pixiebrix/pixiebrix-extension",
"dependencies": {
"formik": "^2.4.6"
},
"devDependencies": {
"@swc/core": "^1.7.42",
"@swc/jest": "^0.2.37",
"eslint": "^8.57.0",
"typescript": "^5.6.3"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { getInvalidPath } from "@/utils/debugUtils";
import { getInvalidPath } from "@/debugUtils";

describe("getInvalidPath", () => {
it("returns invalid path", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type InvalidPathInformation = {
* @param path period separated path
*/
export function getInvalidPath(
Copy link
Contributor

@twschiller twschiller Nov 12, 2024

Choose a reason for hiding this comment

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

NIT: as an aside, given this method uses Formik's getIn vs. lodash's (I'm not sure if there are any differences?) we might consider moving it to a formikDebugUtils module or similar in the future

Or, if there are no differences in getIn implementation, we might consider dropping the Formik dependency from this library in favor of lodash

value: UnknownObject,
value: Record<string, unknown>,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

UnknownObject comes from a global type def, which we might eventually want to move to the root as well.

Copy link
Contributor

@twschiller twschiller Nov 11, 2024

Choose a reason for hiding this comment

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

We might also consider using typefest's UnknownRecord instead: https://github.com/sindresorhus/type-fest/blob/main/source/unknown-record.d.ts. There's is maybe technically more accurate — ours only supports string keys vs. strings/symbols/numbers: https://www.totaltypescript.com/concepts/propertykey-type

Although, ours is helpful for distinguishing against mistaken Array uses

path: string,
): InvalidPathInformation {
const parts = path.split(".");
Expand Down
25 changes: 25 additions & 0 deletions libraries/utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
// You can see the full details at https://github.com/sindresorhus/tsconfig/blob/main/tsconfig.json
// Note: `strict: true` enables many flags that aren’t explicitly listed in that file
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"sourceMap": true,
"module": "esnext",
"target": "es2023",
Copy link
Contributor

@twschiller twschiller Nov 11, 2024

Choose a reason for hiding this comment

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

Should we consider defining a config these can extend from? E.g., in some libraries we might be able to enforce stricter rules (e.g., noUnusedParameters)

Update: I see you already mentioned the duplication in the PR description

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I think we should define a base tsconfig. That's the follow-up I mentioned in the PR description

"moduleResolution": "bundler",
"resolveJsonModule": true,
"baseUrl": ".",
"outDir": null,
"declaration": false,

// TODO: Drop these lines to make TS stricter https://github.com/pixiebrix/pixiebrix-extension/issues/775
"strictFunctionTypes": false,
"noPropertyAccessFromIndexSignature": false,
"noImplicitReturns": false,
"noUnusedParameters": false,
"paths": {
"@/*": ["src/*"]
}
},
"exclude": ["node_modules"]
}
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
"scripts": {
"test": "npm run test --workspaces",
"lint": "npm run lint --workspaces",
"build": "npm run build --workspaces"
"build": "npm run build --workspaces",
"build:typecheck": "npm run build:typecheck --workspaces"
},
"author": "Todd Schiller",
"license": "AGPL-3.0",
"repository": "https://github.com/pixiebrix/pixiebrix-extension",
"engine-strict": true,
"engines": {
"node": "20.12.0",
"npm": "10.5.0"
},
"devDependencies": {
"prettier": "3.1.0"
}
Expand Down
Loading