Skip to content

Commit 58454db

Browse files
authored
* chore: rewrite package to esmodules (SoftwareBrothers#16) BREAKING CHANGE: The package won't support CJS anymore. * chore: use componentLoader instead of AdminJS.bundle BREAKING CHANGE: Configuration changed due to AdminJS.bundle being removed, use componentLoader instead
1 parent 9d01941 commit 58454db

25 files changed

+5061
-6079
lines changed

.eslintrc.js renamed to .eslintrc.cjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
'plugin:react/recommended',
99
],
1010
parserOptions: {
11-
ecmaVersion: 2018,
11+
ecmaVersion: 20,
1212
sourceType: 'module',
1313
ecmaFeatures: {
1414
jsx: true,
@@ -17,8 +17,8 @@ module.exports = {
1717
rules: {
1818
'prettier/prettier': 'error',
1919
'react/prop-types': 'off',
20-
'semi': ['error', 'always'],
21-
'quotes': ['error', 'single'],
20+
semi: ['error', 'always'],
21+
quotes: ['error', 'single'],
2222
},
2323
ignorePatterns: ['node_modules', 'lib'],
2424
};

.github/workflows/push.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
- name: Setup
1111
uses: actions/setup-node@v2
1212
with:
13-
node-version: '16'
13+
node-version: '18'
1414
- uses: actions/cache@v2
1515
id: yarn-cache
1616
with:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,4 @@ dist
122122
/lib
123123
/types
124124
example/.adminjs
125+
.nova
File renamed without changes.

jest.config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

jest.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"moduleFileExtensions": ["js", "json", "ts", "tsx"],
3+
"rootDir": "./test",
4+
"testEnvironment": "node",
5+
"testRegex": ".test.ts$",
6+
"extensionsToTreatAsEsm": [".ts"],
7+
"transformIgnorePatterns": ["node_modules"],
8+
"transform": {
9+
"^.+\\.(t|j)sx?$": [
10+
"ts-jest",
11+
{
12+
"useESM": true,
13+
"tsconfig": "./tsconfig.test.json",
14+
"isolatedModules": true
15+
}
16+
]
17+
},
18+
"moduleNameMapper": {
19+
"^(\\.{1,2}/.*)\\.js$": "$1"
20+
},
21+
"testTimeout": 10000,
22+
"preset": "ts-jest/presets/default-esm",
23+
"verbose": true,
24+
"silent": true,
25+
"forceExit": true
26+
}

package.json

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
{
22
"name": "@adminjs/logger",
33
"version": "4.0.1",
4-
"main": "lib/index.js",
5-
"types": "types/index.d.ts",
4+
"type": "module",
5+
"exports": {
6+
".": {
7+
"import": "./lib/index.js",
8+
"types": "./types/index.d.ts"
9+
}
10+
},
611
"private": false,
712
"repository": "[email protected]:SoftwareBrothers/adminjs-logger.git",
813
"license": "MIT",
@@ -11,8 +16,8 @@
1116
"clean": "rimraf lib types",
1217
"build": "tsc",
1318
"dev": "tsc --watch",
14-
"test": "jest",
15-
"lint": "eslint -c '.eslintrc.js' './src/**/*'",
19+
"test": "NODE_OPTIONS=--experimental-vm-modules dotenv -e .env -- jest --config ./jest.json --runInBand --detectOpenHandles",
20+
"lint": "eslint -c '.eslintrc.cjs' './src/**/*'",
1621
"check:all": "yarn lint && yarn build && yarn test"
1722
},
1823
"husky": {
@@ -21,35 +26,34 @@
2126
}
2227
},
2328
"peerDependencies": {
24-
"@adminjs/design-system": "^3.0.4",
25-
"adminjs": "^6.4.1"
29+
"adminjs": "^7.0.0"
2630
},
2731
"devDependencies": {
28-
"@adminjs/design-system": "^3.0.4",
29-
"@commitlint/cli": "^8.3.5",
30-
"@commitlint/config-conventional": "^8.3.4",
31-
"@semantic-release/git": "^9.0.0",
32-
"@types/jest": "^26.0.14",
33-
"@types/lodash": "^4.14.186",
34-
"@typescript-eslint/eslint-plugin": "^5.16.0",
35-
"@typescript-eslint/parser": "^5.16.0",
36-
"adminjs": "^6.4.1",
37-
"eslint": "^8.11.0",
38-
"eslint-config-prettier": "^8.5.0",
39-
"eslint-plugin-import": "^2.25.4",
40-
"eslint-plugin-prettier": "^3.1.4",
41-
"eslint-plugin-react": "^7.29.4",
32+
"@commitlint/cli": "^17.4.4",
33+
"@commitlint/config-conventional": "^17.4.4",
34+
"@semantic-release/git": "^10.0.1",
35+
"@types/jest": "^29.5.0",
36+
"@types/lodash": "^4.14.191",
37+
"@typescript-eslint/eslint-plugin": "^5.56.0",
38+
"@typescript-eslint/parser": "^5.56.0",
39+
"adminjs": "^7.0.0",
40+
"dotenv-cli": "^7.1.0",
41+
"eslint": "^8.36.0",
42+
"eslint-config-prettier": "^8.8.0",
43+
"eslint-plugin-import": "^2.27.5",
44+
"eslint-plugin-prettier": "^4.2.1",
45+
"eslint-plugin-react": "^7.32.2",
4246
"husky": "^4.3.0",
43-
"jest": "^26.5.3",
44-
"prettier": "^2.0.5",
45-
"rimraf": "^3.0.2",
46-
"semantic-release": "^17.0.7",
47-
"semantic-release-slack-bot": "^1.6.2",
48-
"ts-jest": "^26.4.1",
47+
"jest": "^29.5.0",
48+
"prettier": "^2.8.6",
49+
"rimraf": "^4.4.0",
50+
"semantic-release": "^20.1.3",
51+
"semantic-release-slack-bot": "^4.0.0",
52+
"ts-jest": "^29.0.5",
4953
"ts-node": "^10.9.1",
50-
"typescript": "^4.8.4"
54+
"typescript": "^4.9.5"
5155
},
5256
"dependencies": {
53-
"lodash": "^4.17.20"
57+
"lodash": "^4.17.21"
5458
}
5559
}

src/components/RecordDifference.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import React, { FC } from 'react';
2-
import { BasePropertyProps, flat } from 'adminjs';
31
import {
42
FormGroup,
53
Label,
64
Table as AdminTable,
7-
TableCell,
85
TableBody,
6+
TableCell,
97
TableHead,
108
TableRow,
119
} from '@adminjs/design-system';
12-
import styled from 'styled-components';
10+
import { BasePropertyProps, flat } from 'adminjs';
11+
import React, { FC } from 'react';
12+
import { styled } from 'styled-components';
1313

1414
const Cell = styled(TableCell)`
1515
width: 100%;
@@ -44,9 +44,11 @@ const RecordDifference: FC<BasePropertyProps> = ({ record, property }) => {
4444
<Label>{property.label}</Label>
4545
<Table>
4646
<Head>
47-
<Cell>Property name</Cell>
48-
<Cell>Before</Cell>
49-
<Cell>After</Cell>
47+
<Row>
48+
<Cell>Property name</Cell>
49+
<Cell>Before</Cell>
50+
<Cell>After</Cell>
51+
</Row>
5052
</Head>
5153
<TableBody>
5254
{Object.entries(

src/components/RecordLink.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React, { FC } from 'react';
2-
import { BasePropertyProps, ViewHelpers } from 'adminjs';
31
import { FormGroup, Link } from '@adminjs/design-system';
2+
import { BasePropertyProps, ViewHelpers } from 'adminjs';
3+
import React, { FC } from 'react';
44

5-
import { getLogPropertyName } from '../utils/get-log-property-name';
5+
import { getLogPropertyName } from '../utils/get-log-property-name.js';
66

77
const viewHelpers = new ViewHelpers();
88
const RecordLink: FC<BasePropertyProps> = ({ record, property }) => {

src/components/bundle.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
* @section modules
55
*/
66

7-
import loggerFeature from './logger.feature';
7+
import loggerFeature from './logger.feature.js';
88

9-
export { createLoggerResource } from './logger.resource';
10-
export { withLogger } from './utils/with-logger';
11-
export { bundleComponents } from './components/bundle';
12-
export * from './types';
13-
export * from './constants';
9+
export * from './constants.js';
10+
export { createLoggerResource } from './logger.resource.js';
11+
export * from './types.js';
12+
export { withLogger } from './utils/with-logger.js';
1413

1514
export default loggerFeature;

src/log.action.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import {
99
flat,
1010
} from 'adminjs';
1111

12-
import { ADMINJS_LOGGER_DEFAULT_RESOURCE_ID } from './constants';
13-
import { MISSING_USER_ID_ERROR } from './errors';
14-
import { CreateLogActionParams, LoggerFeatureOptions } from './types';
15-
import { difference } from './utils/difference';
16-
import { getLogPropertyName } from './utils/get-log-property-name';
12+
import { ADMINJS_LOGGER_DEFAULT_RESOURCE_ID } from './constants.js';
13+
import { MISSING_USER_ID_ERROR } from './errors.js';
14+
import { CreateLogActionParams, LoggerActionOptions } from './types.js';
15+
import { difference } from './utils/difference.js';
16+
import { getLogPropertyName } from './utils/get-log-property-name.js';
1717

1818
export const rememberInitialRecord: Before = async (
1919
request: ActionRequest,
@@ -145,7 +145,7 @@ const createPersistLogAction =
145145
(
146146
request: ActionRequest,
147147
context: ActionContext,
148-
options: LoggerFeatureOptions
148+
options: LoggerActionOptions
149149
) =>
150150
async ({ recordId, record, initialRecord }: CreatePersistLogParams) => {
151151
const { currentAdmin, _admin, action } = context;
@@ -176,10 +176,10 @@ const createPersistLogAction =
176176
record ?? (await ModifiedResource.findOne(String(recordId))) ?? null;
177177

178178
const newParamsToCompare = ['delete', 'bulkDelete'].includes(action.name)
179-
? {}
180-
: flat.flatten<object, object>(
179+
? ({} as Record<string, any>)
180+
: (flat.flatten(
181181
JSON.parse(JSON.stringify(modifiedRecord?.params ?? {}))
182-
);
182+
) as Record<string, any>);
183183
const logParams = {
184184
[getLogPropertyName('recordTitle', propertiesMapping)]: getRecordTitle(
185185
modifiedRecord,
@@ -204,7 +204,7 @@ const createPersistLogAction =
204204
await Log.create(logParams);
205205
} catch (e) {
206206
/* The action should not fail nor display a message to the end-user
207-
but we must log the error in server's console for developers */
207+
but we must log the error in server's console for developers */
208208
console.error(e);
209209
}
210210
};

src/logger.feature.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { buildFeature, FeatureType } from 'adminjs';
22

3-
import { createLogAction, rememberInitialRecord } from './log.action';
4-
import { LoggerFeatureOptions } from './types';
3+
import { createLogAction, rememberInitialRecord } from './log.action.js';
4+
import { LoggerFeatureOptions } from './types.js';
55

66
const loggerFeature = (options: LoggerFeatureOptions): FeatureType => {
77
return buildFeature({

src/logger.resource.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import { ResourceWithOptions } from 'adminjs';
1+
import { ComponentLoader, ResourceWithOptions } from 'adminjs';
22

3-
import { bundleComponents } from './components/bundle';
4-
import { getLogPropertyName } from './utils/get-log-property-name';
5-
import { LoggerFeatureOptions } from './types';
6-
import { ADMINJS_LOGGER_DEFAULT_RESOURCE_ID } from './constants';
7-
8-
const { RECORD_DIFFERENCE, RECORD_LINK } = bundleComponents();
3+
import { bundleComponent } from './utils/bundle-component.js';
4+
import { ADMINJS_LOGGER_DEFAULT_RESOURCE_ID } from './constants.js';
5+
import { LoggerFeatureOptions } from './types.js';
6+
import { getLogPropertyName } from './utils/get-log-property-name.js';
97

108
export const createLoggerResource = <T = unknown>({
9+
componentLoader,
1110
resource,
1211
featureOptions,
1312
}: {
13+
componentLoader: ComponentLoader;
1414
resource: T;
1515
featureOptions?: LoggerFeatureOptions;
1616
}): ResourceWithOptions => {
1717
const { resourceOptions = {}, propertiesMapping = {} } = featureOptions ?? {};
1818
const { resourceId, navigation, actions = {} } = resourceOptions;
19-
19+
const recordDifferenceComponent = bundleComponent(
20+
componentLoader,
21+
'RecordDifference'
22+
);
23+
const recordLinkComponent = bundleComponent(componentLoader, 'RecordLink');
2024
return {
2125
resource,
2226
options: {
@@ -55,7 +59,7 @@ export const createLoggerResource = <T = unknown>({
5559
},
5660
[getLogPropertyName('difference', propertiesMapping)]: {
5761
components: {
58-
show: RECORD_DIFFERENCE,
62+
show: recordDifferenceComponent,
5963
},
6064
custom: {
6165
propertiesMapping,
@@ -64,8 +68,8 @@ export const createLoggerResource = <T = unknown>({
6468
},
6569
[getLogPropertyName('recordId', propertiesMapping)]: {
6670
components: {
67-
list: RECORD_LINK,
68-
show: RECORD_LINK,
71+
list: recordLinkComponent,
72+
show: recordLinkComponent,
6973
},
7074
custom: {
7175
propertiesMapping,

src/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
Action,
3+
ComponentLoader,
34
ListActionResponse,
45
RecordActionResponse,
56
ResourceOptions,
@@ -53,7 +54,7 @@ export type LoggerPropertiesMapping = {
5354

5455
export type CreateLogActionParams = {
5556
onlyForPostMethod?: boolean;
56-
options?: LoggerFeatureOptions;
57+
options?: LoggerActionOptions;
5758
};
5859

5960
/**
@@ -78,6 +79,10 @@ export type LoggerResourceOptions = {
7879
* @alias LoggerFeatureOptions
7980
*/
8081
export type LoggerFeatureOptions = {
82+
/**
83+
* Your ComponentLoader instance. It is required for the feature to add it's components.
84+
*/
85+
componentLoader: ComponentLoader;
8186
/**
8287
* For the feature to work you must define a model using an ORM of your choice.
8388
* In case you want to use different attribute names, you can use this
@@ -93,3 +98,5 @@ export type LoggerFeatureOptions = {
9398
*/
9499
resourceOptions?: LoggerResourceOptions;
95100
};
101+
102+
export type LoggerActionOptions = Omit<LoggerFeatureOptions, 'componentLoader'>;

src/utils/bundle-component.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import path from 'path';
2+
import * as url from 'url';
3+
4+
import type { ComponentLoader } from 'adminjs';
5+
6+
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
7+
8+
export const bundleComponent = (
9+
loader: ComponentLoader,
10+
componentName: string
11+
) => {
12+
const componentPath = path.join(__dirname, `../components/${componentName}`);
13+
return loader.add(componentName, componentPath);
14+
};

src/utils/difference.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { isEqual, transform } from 'lodash';
1+
import isEqual from 'lodash/isEqual.js';
2+
import transform from 'lodash/transform.js';
23

34
export const difference = (
45
object: Record<string, string>,

0 commit comments

Comments
 (0)