create-erxes 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -2
- package/dist/index.js +481 -0
- package/dist/templates/base/.agents/skills/endpoint/SKILL.md +20 -0
- package/dist/templates/base/.agents/skills/schema-change/SKILL.md +28 -0
- package/dist/templates/base/.agents/skills/verify/SKILL.md +24 -0
- package/dist/templates/base/.github/workflows/ci.yml +44 -0
- package/dist/templates/base/AGENTS.md +59 -0
- package/dist/templates/base/CLAUDE.md +1 -0
- package/dist/templates/base/Dockerfile +13 -0
- package/dist/templates/base/_dockerignore +4 -0
- package/dist/templates/base/_env.example +15 -0
- package/dist/templates/base/_gitignore +11 -0
- package/dist/templates/base/_node-version +1 -0
- package/dist/templates/base/api/AGENTS.md +22 -0
- package/dist/templates/base/api/drizzle/0000_items.sql +13 -0
- package/dist/templates/base/api/drizzle/meta/0000_snapshot.json +126 -0
- package/dist/templates/base/api/drizzle/meta/_journal.json +13 -0
- package/dist/templates/base/api/drizzle.config.ts +16 -0
- package/dist/templates/base/api/package.json +47 -0
- package/dist/templates/base/api/src/__tests__/server.test.ts +51 -0
- package/dist/templates/base/api/src/config.ts +43 -0
- package/dist/templates/base/api/src/core.ts +37 -0
- package/dist/templates/base/api/src/db/client.ts +30 -0
- package/dist/templates/base/api/src/db/migrate.ts +80 -0
- package/dist/templates/base/api/src/db/schema.ts +34 -0
- package/dist/templates/base/api/src/db/table.ts +8 -0
- package/dist/templates/base/api/src/logger.ts +5 -0
- package/dist/templates/base/api/src/main.ts +23 -0
- package/dist/templates/base/api/src/migrate.ts +9 -0
- package/dist/templates/base/api/src/modules/items/__tests__/items.test.ts +92 -0
- package/dist/templates/base/api/src/modules/items/items.ts +127 -0
- package/dist/templates/base/api/src/orpc.ts +25 -0
- package/dist/templates/base/api/src/router.ts +10 -0
- package/dist/templates/base/api/src/server.ts +58 -0
- package/dist/templates/base/api/src/telemetry.ts +27 -0
- package/dist/templates/base/api/src/test/core-functions.sql +18 -0
- package/dist/templates/base/api/src/test/setup.ts +68 -0
- package/dist/templates/base/api/tsconfig.json +9 -0
- package/dist/templates/base/knip.ts +21 -0
- package/dist/templates/base/package.json +30 -0
- package/dist/templates/base/pnpm-workspace.yaml +20 -0
- package/dist/templates/base/tools/check-generated.ts +70 -0
- package/dist/templates/base/tools/check-rls.sh +30 -0
- package/dist/templates/base/tools/check-rls.sql +109 -0
- package/dist/templates/base/tools/no-org-id.sql +5 -0
- package/dist/templates/base/tools/test/no-skips.ts +58 -0
- package/dist/templates/base/tsconfig.base.json +16 -0
- package/dist/templates/base/tsconfig.json +9 -0
- package/dist/templates/base/ui/AGENTS.md +34 -0
- package/dist/templates/base/ui/package.json +35 -0
- package/dist/templates/base/ui/rsbuild.config.ts +39 -0
- package/dist/templates/base/ui/src/__Pascal__.tsx +14 -0
- package/dist/templates/base/ui/src/config.tsx +11 -0
- package/dist/templates/base/ui/src/index.ts +3 -0
- package/dist/templates/base/ui/src/items/ItemSheet.tsx +107 -0
- package/dist/templates/base/ui/src/items/ItemsPage.tsx +143 -0
- package/dist/templates/base/ui/src/items/useItems.ts +79 -0
- package/dist/templates/base/ui/src/orpc.ts +32 -0
- package/dist/templates/base/ui/src/styles.css +8 -0
- package/dist/templates/base/ui/tsconfig.json +11 -0
- package/dist/templates/base/vite.config.ts +62 -0
- package/dist/templates/migrate/.agents/skills/plugin-migrate/SKILL.md +66 -0
- package/dist/templates/migrate/migration/checklist.ts +429 -0
- package/dist/templates/migrate/migration/core-map.ts +150 -0
- package/dist/templates/migrate/migration/import-map.ts +4 -0
- package/dist/templates/migrate/migration/inventory.ts +881 -0
- package/dist/templates/migrate/package.json +15 -0
- package/dist/templates/migrate/scripts/import-mongo.ts +143 -0
- package/package.json +32 -3
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { InferClientOutputs } from '@orpc/client';
|
|
2
|
+
import {
|
|
3
|
+
type InfiniteData,
|
|
4
|
+
useInfiniteQuery,
|
|
5
|
+
useMutation,
|
|
6
|
+
useQueryClient,
|
|
7
|
+
} from '@tanstack/react-query';
|
|
8
|
+
import type { Client } from '@__id__/api';
|
|
9
|
+
import { toast } from 'erxes-ui';
|
|
10
|
+
import { useTranslation } from 'react-i18next';
|
|
11
|
+
import { useSearchParams } from 'react-router-dom';
|
|
12
|
+
import { orpc } from '../orpc.ts';
|
|
13
|
+
|
|
14
|
+
type Outputs = InferClientOutputs<Client>['items'];
|
|
15
|
+
|
|
16
|
+
export type Item = Outputs['create'];
|
|
17
|
+
|
|
18
|
+
/** Every page loaded so far, newest first; only `createdBy`'s when given. */
|
|
19
|
+
export const useItems = (createdBy: string | null) =>
|
|
20
|
+
useInfiniteQuery(
|
|
21
|
+
orpc.items.list.infiniteOptions({
|
|
22
|
+
input: (cursor: string | undefined) => ({ cursor, createdBy: createdBy ?? undefined }),
|
|
23
|
+
initialPageParam: undefined,
|
|
24
|
+
getNextPageParam: (page) => page.nextCursor ?? undefined,
|
|
25
|
+
}),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Deletes an item. It leaves every loaded list at once and comes back if the
|
|
30
|
+
* API refuses; the lists refetch either way.
|
|
31
|
+
*/
|
|
32
|
+
export const useRemoveItem = () => {
|
|
33
|
+
const { t } = useTranslation('__id__');
|
|
34
|
+
const queryClient = useQueryClient();
|
|
35
|
+
const lists = { queryKey: orpc.items.list.key() };
|
|
36
|
+
|
|
37
|
+
return useMutation(
|
|
38
|
+
orpc.items.remove.mutationOptions({
|
|
39
|
+
onMutate: async ({ id }) => {
|
|
40
|
+
await queryClient.cancelQueries(lists);
|
|
41
|
+
const before = queryClient.getQueriesData<InfiniteData<Outputs['list']>>(lists);
|
|
42
|
+
|
|
43
|
+
queryClient.setQueriesData<InfiniteData<Outputs['list']>>(lists, (data) =>
|
|
44
|
+
data
|
|
45
|
+
? {
|
|
46
|
+
...data,
|
|
47
|
+
pages: data.pages.map((page) => ({
|
|
48
|
+
...page,
|
|
49
|
+
items: page.items.filter((item) => item.id !== id),
|
|
50
|
+
})),
|
|
51
|
+
}
|
|
52
|
+
: data,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
return { before };
|
|
56
|
+
},
|
|
57
|
+
onError: (error, _input, context) => {
|
|
58
|
+
for (const [key, data] of context?.before ?? []) queryClient.setQueryData(key, data);
|
|
59
|
+
toast({
|
|
60
|
+
variant: 'destructive',
|
|
61
|
+
title: t('delete-failed', 'Could not delete'),
|
|
62
|
+
description: error.message,
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
onSettled: () => queryClient.invalidateQueries(lists),
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** `?item=<id>` opens that item in the side panel, `?item=new` a blank one. */
|
|
71
|
+
export const useOpenItem = () => {
|
|
72
|
+
const [params, setParams] = useSearchParams();
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
openId: params.get('item'),
|
|
76
|
+
open: (id: string) => setParams({ item: id }),
|
|
77
|
+
close: () => setParams({}),
|
|
78
|
+
};
|
|
79
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createORPCClient } from '@orpc/client';
|
|
2
|
+
import { RPCLink } from '@orpc/client/fetch';
|
|
3
|
+
import { createTanstackQueryUtils } from '@orpc/tanstack-query';
|
|
4
|
+
import type { Client } from '@__id__/api';
|
|
5
|
+
import { pluginFetch } from 'erxes-ui';
|
|
6
|
+
|
|
7
|
+
/** The plugin's name in core. */
|
|
8
|
+
export const PLUGIN = '__id__';
|
|
9
|
+
|
|
10
|
+
const PREFIX = '/rpc';
|
|
11
|
+
|
|
12
|
+
const link = new RPCLink({
|
|
13
|
+
// A placeholder origin: pluginFetch swaps it for the API address in the
|
|
14
|
+
// manifest and adds this member's 5-minute session token.
|
|
15
|
+
url: new URL(PREFIX, window.location.origin),
|
|
16
|
+
fetch: async (request) => {
|
|
17
|
+
const { pathname, search } = new URL(request.url);
|
|
18
|
+
|
|
19
|
+
return pluginFetch(PLUGIN, `rpc${pathname.slice(PREFIX.length)}${search}`, {
|
|
20
|
+
method: request.method,
|
|
21
|
+
headers: request.headers,
|
|
22
|
+
body: request.method === 'GET' ? undefined : await request.arrayBuffer(),
|
|
23
|
+
signal: request.signal,
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
/** The API, typed from api/src/router.ts: `await client.items.list({})`. */
|
|
29
|
+
const client: Client = createORPCClient(link);
|
|
30
|
+
|
|
31
|
+
/** TanStack Query options per procedure: `useQuery(orpc.items.list.queryOptions({ input }))`. */
|
|
32
|
+
export const orpc = createTanstackQueryUtils(client);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/* This plugin's own Tailwind utilities, built on erxes-ui's tokens. web's stylesheet already has
|
|
2
|
+
the page defaults and every class web and erxes-ui use; addPluginStyles (in __Pascal__.tsx)
|
|
3
|
+
adds only the classes it lacks. The themes are inline, so the utilities carry their values and
|
|
4
|
+
set no variable of web's. */
|
|
5
|
+
@import 'tailwindcss/theme.css' theme(reference inline);
|
|
6
|
+
@import 'erxes-ui/theme.css' theme(inline);
|
|
7
|
+
@import 'tailwindcss/utilities.css' layer(utilities) source(none);
|
|
8
|
+
@source '.';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"lib": ["es2024", "dom", "dom.iterable"],
|
|
5
|
+
"module": "esnext",
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"jsx": "react-jsx",
|
|
8
|
+
"types": ["node", "@rsbuild/core/types"]
|
|
9
|
+
},
|
|
10
|
+
"include": ["src", "rsbuild.config.ts"]
|
|
11
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import { preset } from '@erxes/lint';
|
|
3
|
+
import { configDefaults, defineConfig } from 'vite-plus';
|
|
4
|
+
import { noSkips } from './tools/test/no-skips.ts';
|
|
5
|
+
|
|
6
|
+
// The type check is tsrs (TypeScript 7) running as oxlint's tsgolint.
|
|
7
|
+
const tsrs = 'OXLINT_TSGOLINT_PATH=node_modules/.bin/tsrs-lint';
|
|
8
|
+
|
|
9
|
+
// Vite Task tracks the files each command reads; the lockfile is listed too, so an
|
|
10
|
+
// install that changes a dependency always reruns the task.
|
|
11
|
+
const tracked = { input: [{ auto: true }, 'pnpm-lock.yaml'] };
|
|
12
|
+
|
|
13
|
+
export default defineConfig({
|
|
14
|
+
run: {
|
|
15
|
+
tasks: {
|
|
16
|
+
// A rerun with no edits replays the last result.
|
|
17
|
+
check: {
|
|
18
|
+
command: `erxes-check-escapes && ${tsrs} vp check && knip && erxes-check-docs && pnpm check:generated`,
|
|
19
|
+
cache: tracked,
|
|
20
|
+
},
|
|
21
|
+
fix: { command: `${tsrs} vp check --fix`, cache: false },
|
|
22
|
+
test: { command: 'vp test run', cache: { ...tracked, env: ['DATA_TEST_URL'] } },
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
fmt: {
|
|
26
|
+
singleQuote: true,
|
|
27
|
+
sortPackageJson: true,
|
|
28
|
+
ignorePatterns: [
|
|
29
|
+
'**/node_modules/**',
|
|
30
|
+
'**/dist/**',
|
|
31
|
+
'coverage/**',
|
|
32
|
+
'api/drizzle/**',
|
|
33
|
+
'pnpm-lock.yaml',
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
// The rules core and every plugin share, with the overrides for a plugin's api/, ui/ and
|
|
37
|
+
// migration kit. Change them in core's packages/lint; a new @erxes/lint brings them here.
|
|
38
|
+
lint: {
|
|
39
|
+
...preset,
|
|
40
|
+
ignorePatterns: ['**/node_modules/**', '**/dist/**', 'coverage/**'],
|
|
41
|
+
},
|
|
42
|
+
test: {
|
|
43
|
+
reporters: [...configDefaults.reporters, noSkips()],
|
|
44
|
+
projects: [
|
|
45
|
+
{
|
|
46
|
+
root: 'api',
|
|
47
|
+
test: {
|
|
48
|
+
name: 'api',
|
|
49
|
+
include: ['src/**/*.test.ts'],
|
|
50
|
+
environment: 'node',
|
|
51
|
+
setupFiles: [resolve(import.meta.dirname, 'api/src/test/setup.ts')],
|
|
52
|
+
// config.ts needs these; tests never reach core.
|
|
53
|
+
env: {
|
|
54
|
+
CORE_URL: 'http://127.0.0.1:9',
|
|
55
|
+
PLUGIN_SECRET: 'test-secret-'.padEnd(32, 'x'),
|
|
56
|
+
LOG_LEVEL: 'silent',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: plugin-migrate
|
|
3
|
+
description: Port the old erxes plugin this repo was made from onto the new core. Use when working through MIGRATION.md, porting a module, or when someone asks how far the migration is.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
The port keeps every feature the old plugin had and fixes its architecture.
|
|
7
|
+
Port what each feature does, never its code: where the old plugin got
|
|
8
|
+
something wrong (a missing check, a race, a workaround for the old core), the
|
|
9
|
+
new one does it right.
|
|
10
|
+
|
|
11
|
+
If scaffolding skipped installation, first run `pnpm migrate:inventory -- --from <old-checkout> --plugin <old-plugin> --ref <ref>`
|
|
12
|
+
and `pnpm migrate:checklist`. These commands generate the port's working files:
|
|
13
|
+
|
|
14
|
+
```text
|
|
15
|
+
MIGRATION.md Checklist, one section per old module
|
|
16
|
+
migration/inventory.json Code inventory behind the checklist
|
|
17
|
+
migration/schema-draft.ts Draft tables to port
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`migration/core-map.ts` says what each old core call becomes. Use synthetic
|
|
21
|
+
data only. The inventory reads code, never a database.
|
|
22
|
+
|
|
23
|
+
1. Read the checklist. Port the module with the fewest unticked items first;
|
|
24
|
+
`Plugin-wide` goes last.
|
|
25
|
+
2. Tables. Move the module's draft tables into
|
|
26
|
+
`api/src/db/schema.ts`, then follow the `schema-change` skill. Fix the draft
|
|
27
|
+
types while moving: whole numbers become `integer()`, fixed string sets
|
|
28
|
+
become `text({ enum })`, required ids stay `text().notNull()`, and a value
|
|
29
|
+
the old code kept unique by checking first gets a unique index. Drop
|
|
30
|
+
columns nothing reads. Update the table's entry in
|
|
31
|
+
`migration/import-map.ts` to match.
|
|
32
|
+
3. Procedures. One per GraphQL operation listed, following the `endpoint`
|
|
33
|
+
skill and shaped like `api/src/modules/items`. Name it after what it does
|
|
34
|
+
(`tasks.list`, not `getTasks`), and fold operations that only differ by
|
|
35
|
+
filter into one input. A failure the UI acts on gets a typed error. An
|
|
36
|
+
operation the new UI never calls is ticked with no procedure.
|
|
37
|
+
4. Core calls. Do what the arrow says: `sdk` is
|
|
38
|
+
`core.forOrg(orgId).<procedure>` in the API with that scope in
|
|
39
|
+
`manifest.scopes`; `contract` is a call from the UI with the member's own
|
|
40
|
+
session, through `orpc` from `@erxes/core-contract/client` (members through
|
|
41
|
+
erxes-ui's `SelectMember` and `MemberInline`, `ui/AGENTS.md`);
|
|
42
|
+
`plugin-owned` lives in this plugin's tables or code.
|
|
43
|
+
5. Pages. One per UI route, under `ui/src/<module>/`, built from `erxes-ui`
|
|
44
|
+
the way `ui/src/items/` is. The old `ui-modules` components don't exist
|
|
45
|
+
here. Member pickers and names become erxes-ui's `SelectMember` and
|
|
46
|
+
`MemberInline`; a picker of other records is erxes-ui's `EntityPicker` over
|
|
47
|
+
the plugin's own query. For erxes-ui components whose props changed, see
|
|
48
|
+
its old-to-new table, `packages/ui/MIGRATION.md` in the erxes repo (branch
|
|
49
|
+
`postgre`).
|
|
50
|
+
6. Check parity in the browser, following the `verify` skill: open the old
|
|
51
|
+
page and the new one side by side, and walk the same actions on both.
|
|
52
|
+
7. Tick an item once it works on the new core and its old bugs and
|
|
53
|
+
workarounds are gone. When an item needs something core doesn't offer
|
|
54
|
+
(marked **gap**, or one you find), file an issue in the plugin's repo that
|
|
55
|
+
says what the old plugin did and what core would need, tick the item and
|
|
56
|
+
end its line with `(filed <issue url>)`. The feature waits for core.
|
|
57
|
+
8. Commit the module: schema, migration, procedures, page and the ticks
|
|
58
|
+
together.
|
|
59
|
+
9. Copy data once all modules are in: `pnpm migrate:import` with `MONGO_URL`,
|
|
60
|
+
`DATA_URL` and `ORG_ID` set. Its `Skipped` list must be empty or explained.
|
|
61
|
+
|
|
62
|
+
Rerun `pnpm migrate:inventory` and `pnpm migrate:checklist` only to pick up
|
|
63
|
+
new old-side code; ticks survive the rerun.
|
|
64
|
+
|
|
65
|
+
Done when every checklist item is ticked, each gap with its issue
|
|
66
|
+
link, and `vp run check`, `vp run test`, `pnpm check:rls` and `pnpm build` pass.
|
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
// Turns migration/inventory.json into the port's checklist: MIGRATION.md (one
|
|
2
|
+
// section per module), migration/schema-draft.ts (draft Drizzle tables from the
|
|
3
|
+
// Mongoose schemas) and migration/import-map.ts (collection → table, for
|
|
4
|
+
// scripts/import-mongo.ts). Rerun after `pnpm migrate:inventory`; ticks in an
|
|
5
|
+
// existing MIGRATION.md are kept.
|
|
6
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
7
|
+
import { resolve } from 'node:path';
|
|
8
|
+
import type * as pg from 'drizzle-orm/pg-core';
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
import {
|
|
11
|
+
CORE_CALLS,
|
|
12
|
+
type Equivalent,
|
|
13
|
+
HELPERS,
|
|
14
|
+
pluginCall,
|
|
15
|
+
REGISTRATIONS,
|
|
16
|
+
UNMAPPED,
|
|
17
|
+
} from './core-map.ts';
|
|
18
|
+
|
|
19
|
+
const here = import.meta.dirname;
|
|
20
|
+
const root = resolve(here, '..');
|
|
21
|
+
const alphabetical = (a: string, b: string) => a.localeCompare(b, 'en');
|
|
22
|
+
|
|
23
|
+
const Value = z.union([
|
|
24
|
+
z.string(),
|
|
25
|
+
z.object({ dynamic: z.string() }),
|
|
26
|
+
z.object({ missing: z.literal(true) }),
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
const Located = z.object({ file: z.string(), line: z.number() });
|
|
30
|
+
const InModule = Located.extend({ module: z.string() });
|
|
31
|
+
|
|
32
|
+
const Inventory = z.object({
|
|
33
|
+
source: z.object({ plugin: z.string(), ref: z.string(), sha: z.string() }),
|
|
34
|
+
modules: z.array(z.string()),
|
|
35
|
+
graphql: z.object({
|
|
36
|
+
operations: z.array(
|
|
37
|
+
InModule.extend({ kind: z.string(), name: z.string(), returns: z.string() }),
|
|
38
|
+
),
|
|
39
|
+
types: z.array(InModule.extend({ kind: z.string(), name: z.string() })),
|
|
40
|
+
}),
|
|
41
|
+
schemas: z.array(
|
|
42
|
+
InModule.extend({
|
|
43
|
+
name: z.string(),
|
|
44
|
+
timestamps: z.boolean(),
|
|
45
|
+
fields: z.array(
|
|
46
|
+
z.object({
|
|
47
|
+
name: z.string(),
|
|
48
|
+
type: z.string(),
|
|
49
|
+
required: z.boolean().optional(),
|
|
50
|
+
}),
|
|
51
|
+
),
|
|
52
|
+
}),
|
|
53
|
+
),
|
|
54
|
+
collections: z.array(
|
|
55
|
+
Located.extend({ collection: z.string(), module: z.string(), schema: z.string().nullable() }),
|
|
56
|
+
),
|
|
57
|
+
routes: z.array(InModule.extend({ method: z.string(), path: z.string(), webhook: z.boolean() })),
|
|
58
|
+
uiRoutes: z.array(Located.extend({ path: z.string(), element: z.string() })),
|
|
59
|
+
trpcCalls: z.array(
|
|
60
|
+
InModule.extend({
|
|
61
|
+
pluginName: Value.optional(),
|
|
62
|
+
target: Value.optional(),
|
|
63
|
+
action: Value.optional(),
|
|
64
|
+
dynamic: z.string().optional(),
|
|
65
|
+
}),
|
|
66
|
+
),
|
|
67
|
+
eventCalls: z.array(InModule.extend({ helper: z.string(), type: Value.optional() })),
|
|
68
|
+
workerFiles: z.array(z.object({ file: z.string(), evidence: z.array(z.string()) })),
|
|
69
|
+
registrations: z.array(
|
|
70
|
+
Located.extend({ kind: z.string(), id: Value, group: z.string().optional() }),
|
|
71
|
+
),
|
|
72
|
+
env: z.array(Located.extend({ name: z.string() })),
|
|
73
|
+
uiModules: z.object({ files: z.number(), symbols: z.record(z.string(), z.number()) }),
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const inventory = Inventory.parse(
|
|
77
|
+
JSON.parse(readFileSync(resolve(here, 'inventory.json'), 'utf8')),
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
/** The new plugin's name; the old one's is `inventory.source.plugin`. */
|
|
81
|
+
const PLUGIN = '__id__';
|
|
82
|
+
const { plugin } = inventory.source;
|
|
83
|
+
const short = (file: string) => file.replace(/^(backend|frontend)\/plugins\//, '');
|
|
84
|
+
const at = (item: { file: string; line: number }) => `\`${short(item.file)}:${item.line}\``;
|
|
85
|
+
|
|
86
|
+
const show = (value: z.infer<typeof Value> | undefined) =>
|
|
87
|
+
value === undefined
|
|
88
|
+
? '?'
|
|
89
|
+
: typeof value === 'string'
|
|
90
|
+
? value
|
|
91
|
+
: 'dynamic' in value
|
|
92
|
+
? `<${value.dynamic}>`
|
|
93
|
+
: '?';
|
|
94
|
+
|
|
95
|
+
// Variables old erxes itself needed; core and the template replace them.
|
|
96
|
+
const RUNTIME_ENV =
|
|
97
|
+
/^(NODE_ENV|PORT|MONGO_URL|REDIS_\w+|RABBITMQ\w*|ELASTICSEARCH\w*|ENABLED_PLUGINS\w*|LOAD_BALANCER_ADDRESS|VERSION|DOMAIN|MAIN_APP_DOMAIN|UI_ENTRY_URL)$/;
|
|
98
|
+
|
|
99
|
+
// Checklist items -------------------------------------------------------------
|
|
100
|
+
|
|
101
|
+
type Item = { section: string; text: string; gap: boolean };
|
|
102
|
+
|
|
103
|
+
const items = new Map<string, Item[]>();
|
|
104
|
+
|
|
105
|
+
const add = (module: string, section: string, text: string, gap = false) => {
|
|
106
|
+
const list = items.get(module) ?? [];
|
|
107
|
+
|
|
108
|
+
list.push({ section, text, gap });
|
|
109
|
+
items.set(module, list);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const describe = (equivalent: Equivalent) =>
|
|
113
|
+
equivalent.kind === 'gap'
|
|
114
|
+
? `**gap**: ${equivalent.note ?? ''}`
|
|
115
|
+
: `${equivalent.kind} \`${equivalent.to.replaceAll('<plugin>', PLUGIN)}\`${equivalent.note ? ` (${equivalent.note})` : ''}`;
|
|
116
|
+
|
|
117
|
+
const camel = (text: string) =>
|
|
118
|
+
text.replaceAll(/[_-](\w)/g, (_match, char: string) => char.toUpperCase());
|
|
119
|
+
|
|
120
|
+
const tableName = (collection: string) =>
|
|
121
|
+
`${PLUGIN}_${collection.startsWith(`${plugin}_`) ? collection.slice(plugin.length + 1) : collection}`;
|
|
122
|
+
|
|
123
|
+
const schemaByRef = new Map(
|
|
124
|
+
inventory.schemas.map((schema) => [`${schema.file}#${schema.name}`, schema]),
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
for (const collection of inventory.collections) {
|
|
128
|
+
const schema = collection.schema ? schemaByRef.get(collection.schema) : undefined;
|
|
129
|
+
const table = tableName(collection.collection);
|
|
130
|
+
|
|
131
|
+
add(
|
|
132
|
+
collection.module,
|
|
133
|
+
'Tables',
|
|
134
|
+
schema
|
|
135
|
+
? `\`${table}\` from collection \`${collection.collection}\` (${schema.fields.length} fields, draft \`${camel(table)}\` in migration/schema-draft.ts) ${at(schema)}`
|
|
136
|
+
: `\`${table}\` from collection \`${collection.collection}\` (schema not found: write it by hand) ${at(collection)}`,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
for (const op of inventory.graphql.operations) {
|
|
141
|
+
add(
|
|
142
|
+
op.module,
|
|
143
|
+
'Procedures',
|
|
144
|
+
`${op.kind} \`${op.name}\` → procedure \`${op.module}.${op.name}\` (returns \`${op.returns}\`) ${at(op)}`,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// A page belongs to the module its component or last path segment names.
|
|
149
|
+
const singular = (word: string) => word.replace(/ies$/, 'y').replace(/s$/, '');
|
|
150
|
+
|
|
151
|
+
const moduleOfPage = (route: { path: string; element: string }) => {
|
|
152
|
+
const byName = inventory.modules
|
|
153
|
+
.filter((module) => route.element.toLowerCase().includes(module.toLowerCase()))
|
|
154
|
+
.toSorted((a, b) => b.length - a.length)[0];
|
|
155
|
+
|
|
156
|
+
const segment = route.path
|
|
157
|
+
.split('/')
|
|
158
|
+
.filter((part) => part && !part.startsWith(':') && part !== '*')
|
|
159
|
+
.at(-1);
|
|
160
|
+
|
|
161
|
+
const bySegment = segment && inventory.modules.find((module) => module === singular(segment));
|
|
162
|
+
|
|
163
|
+
return byName ?? bySegment ?? '(plugin)';
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
for (const route of inventory.uiRoutes) {
|
|
167
|
+
add(
|
|
168
|
+
moduleOfPage(route),
|
|
169
|
+
'Pages',
|
|
170
|
+
`\`/${PLUGIN}/${route.path.replace(/^\//, '')}\` (${route.element}) ${at(route)}`,
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
for (const call of inventory.trpcCalls) {
|
|
175
|
+
const target = typeof call.pluginName === 'string' ? call.pluginName : undefined;
|
|
176
|
+
const key = `${show(call.target)}.${show(call.action)}`;
|
|
177
|
+
|
|
178
|
+
const equivalent =
|
|
179
|
+
call.dynamic || !target
|
|
180
|
+
? UNMAPPED
|
|
181
|
+
: target === 'core'
|
|
182
|
+
? (CORE_CALLS[key] ?? UNMAPPED)
|
|
183
|
+
: pluginCall(target, plugin);
|
|
184
|
+
|
|
185
|
+
add(
|
|
186
|
+
call.module,
|
|
187
|
+
'Core calls',
|
|
188
|
+
`${target ?? '<dynamic>'} \`${key}\` → ${describe(equivalent)} ${at(call)}`,
|
|
189
|
+
equivalent.kind === 'gap',
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
for (const call of inventory.eventCalls) {
|
|
194
|
+
const equivalent = HELPERS[call.helper] ?? UNMAPPED;
|
|
195
|
+
|
|
196
|
+
add(
|
|
197
|
+
call.module,
|
|
198
|
+
'Events',
|
|
199
|
+
`\`${call.helper}\`${call.type ? ` ${show(call.type)}` : ''} → ${describe(equivalent)} ${at(call)}`,
|
|
200
|
+
equivalent.kind === 'gap',
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
for (const route of inventory.routes) {
|
|
205
|
+
add(
|
|
206
|
+
route.module,
|
|
207
|
+
'Routes',
|
|
208
|
+
`${route.method} \`${route.path}\`${route.webhook ? ' (webhook)' : ''} → **gap**: a public route, and core can't yet tell a public request's organisation ${at(route)}`,
|
|
209
|
+
true,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
for (const worker of inventory.workerFiles) {
|
|
214
|
+
add(
|
|
215
|
+
'(plugin)',
|
|
216
|
+
'Workers and crons',
|
|
217
|
+
`\`${short(worker.file)}\` (${worker.evidence.join(', ')}) → plugin-owned: a job in the API process`,
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
for (const entry of inventory.registrations) {
|
|
222
|
+
const equivalent = REGISTRATIONS[entry.kind] ?? UNMAPPED;
|
|
223
|
+
|
|
224
|
+
add(
|
|
225
|
+
'(plugin)',
|
|
226
|
+
'Registrations',
|
|
227
|
+
`${entry.kind} \`${show(entry.id)}\`${entry.group ? ` (${entry.group})` : ''} → ${describe(equivalent)} ${at(entry)}`,
|
|
228
|
+
equivalent.kind === 'gap',
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const env = inventory.env.filter((entry) => !RUNTIME_ENV.test(entry.name));
|
|
233
|
+
|
|
234
|
+
for (const entry of env) {
|
|
235
|
+
add('(plugin)', 'Environment', `\`${entry.name}\` → api/src/config.ts ${at(entry)}`);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// MIGRATION.md -------------------------------------------------------------------
|
|
239
|
+
|
|
240
|
+
const markdown = resolve(root, 'MIGRATION.md');
|
|
241
|
+
|
|
242
|
+
// Ticks survive a rerun: an item's text without its checkbox is its key, and a
|
|
243
|
+
// filed gap keeps its ` (filed <url>)` suffix.
|
|
244
|
+
const ticked = new Map(
|
|
245
|
+
existsSync(markdown)
|
|
246
|
+
? [...readFileSync(markdown, 'utf8').matchAll(/^- \[x\] (.+?)( \(filed \S+\))?$/gm)].map(
|
|
247
|
+
(match) => [match[1], match[2] ?? ''] as const,
|
|
248
|
+
)
|
|
249
|
+
: [],
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
const all = [...items.values()].flat();
|
|
253
|
+
const gaps = all.filter((item) => item.gap).length;
|
|
254
|
+
|
|
255
|
+
const order = [
|
|
256
|
+
'(plugin)',
|
|
257
|
+
...[...items.keys()].filter((module) => module !== '(plugin)').toSorted(alphabetical),
|
|
258
|
+
];
|
|
259
|
+
|
|
260
|
+
const sections = [
|
|
261
|
+
'Tables',
|
|
262
|
+
'Procedures',
|
|
263
|
+
'Pages',
|
|
264
|
+
'Core calls',
|
|
265
|
+
'Events',
|
|
266
|
+
'Routes',
|
|
267
|
+
'Workers and crons',
|
|
268
|
+
'Registrations',
|
|
269
|
+
'Environment',
|
|
270
|
+
];
|
|
271
|
+
|
|
272
|
+
const symbols = Object.keys(inventory.uiModules.symbols).length;
|
|
273
|
+
|
|
274
|
+
const lines = [
|
|
275
|
+
`# Migrating ${plugin} to the new core`,
|
|
276
|
+
'',
|
|
277
|
+
`From erxes/erxes \`${inventory.source.ref}\` (${inventory.source.sha.slice(0, 10)}), \`${plugin}_api\` and \`${plugin}_ui\`.`,
|
|
278
|
+
`${all.length} items, ${gaps} of them gaps. Follow \`.agents/skills/plugin-migrate\`: one module at a time,`,
|
|
279
|
+
'tick an item once it works on the new core and its old bugs and workarounds are gone, and tick a gap once filed, ending its line with `(filed <issue url>)`.',
|
|
280
|
+
'',
|
|
281
|
+
`The old UI imports ${symbols} symbols from \`ui-modules\` in ${inventory.uiModules.files} files; the host has no \`ui-modules\`, so each page rebuilds them from \`erxes-ui\` (members through its \`SelectMember\` and \`MemberInline\`, other records through \`EntityPicker\`). erxes-ui's own old-to-new component table is \`packages/ui/MIGRATION.md\` in erxes (branch \`postgre\`).`,
|
|
282
|
+
`GraphQL types (${inventory.graphql.types.length}) are listed in \`migration/inventory.json\` for reference; procedures replace them.`,
|
|
283
|
+
'',
|
|
284
|
+
];
|
|
285
|
+
|
|
286
|
+
for (const module of order) {
|
|
287
|
+
const moduleItems = items.get(module) ?? [];
|
|
288
|
+
|
|
289
|
+
if (!moduleItems.length) continue;
|
|
290
|
+
|
|
291
|
+
lines.push(`## ${module === '(plugin)' ? 'Plugin-wide' : module}`, '');
|
|
292
|
+
|
|
293
|
+
for (const section of sections) {
|
|
294
|
+
const sectionItems = moduleItems.filter((item) => item.section === section);
|
|
295
|
+
|
|
296
|
+
if (!sectionItems.length) continue;
|
|
297
|
+
|
|
298
|
+
lines.push(`### ${section}`, '');
|
|
299
|
+
|
|
300
|
+
for (const item of sectionItems) {
|
|
301
|
+
const filed = ticked.get(item.text);
|
|
302
|
+
lines.push(filed === undefined ? `- [ ] ${item.text}` : `- [x] ${item.text}${filed}`);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
lines.push('');
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
writeFileSync(markdown, lines.join('\n'));
|
|
310
|
+
|
|
311
|
+
// schema-draft.ts and import-map.ts ------------------------------------------------
|
|
312
|
+
|
|
313
|
+
const COLUMN: Record<string, `${keyof typeof pg}(${string})`> = {
|
|
314
|
+
String: 'text()',
|
|
315
|
+
Number: 'doublePrecision()',
|
|
316
|
+
Boolean: 'boolean()',
|
|
317
|
+
Date: 'timestamp({ withTimezone: true })',
|
|
318
|
+
ObjectId: 'text()',
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
const columnOf = (type: string) => {
|
|
322
|
+
const array = /^\[(.*)\]$/.exec(type);
|
|
323
|
+
const scalar = COLUMN[array?.[1] ?? type];
|
|
324
|
+
|
|
325
|
+
if (array) return scalar && array[1] !== 'Date' ? `${scalar}.array()` : 'jsonb()';
|
|
326
|
+
|
|
327
|
+
return scalar ?? 'jsonb()';
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
const identifier = (name: string) =>
|
|
331
|
+
/^[A-Za-z_$][\w$]*$/.test(name) ? name : JSON.stringify(name);
|
|
332
|
+
|
|
333
|
+
const snake = (name: string) => name.replaceAll(/([a-z0-9])([A-Z])/g, '$1_$2').toLowerCase();
|
|
334
|
+
const drafted = new Set<string>();
|
|
335
|
+
const tables: string[] = [];
|
|
336
|
+
const imports: string[] = [];
|
|
337
|
+
|
|
338
|
+
for (const collection of inventory.collections) {
|
|
339
|
+
const schema = collection.schema ? schemaByRef.get(collection.schema) : undefined;
|
|
340
|
+
const table = tableName(collection.collection);
|
|
341
|
+
|
|
342
|
+
if (!schema || drafted.has(table)) continue;
|
|
343
|
+
|
|
344
|
+
drafted.add(table);
|
|
345
|
+
|
|
346
|
+
const fields = schema.fields.filter((entry) => entry.type !== 'spread' && entry.name !== '_id');
|
|
347
|
+
|
|
348
|
+
const columns = fields.map((entry) => {
|
|
349
|
+
const column = `${columnOf(entry.type)}${entry.required ? '.notNull()' : ''}`;
|
|
350
|
+
|
|
351
|
+
return ` ${identifier(entry.name)}: ${column}, // ${entry.type}`;
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
const spreads = schema.fields.filter((entry) => entry.type === 'spread');
|
|
355
|
+
|
|
356
|
+
const stamps = schema.timestamps
|
|
357
|
+
? [
|
|
358
|
+
' createdAt: timestamp({ withTimezone: true }).notNull().defaultNow(),',
|
|
359
|
+
' updatedAt: timestamp({ withTimezone: true }).notNull().defaultNow(),',
|
|
360
|
+
]
|
|
361
|
+
: [];
|
|
362
|
+
|
|
363
|
+
tables.push(
|
|
364
|
+
[
|
|
365
|
+
`/** From \`${collection.collection}\` (${short(schema.file)}:${schema.line}). */`,
|
|
366
|
+
`export const ${camel(table)} = table(`,
|
|
367
|
+
` '${table.slice(PLUGIN.length + 1)}',`,
|
|
368
|
+
' {',
|
|
369
|
+
' // The Mongo `_id`, kept as text so references survive the import.',
|
|
370
|
+
' id: text().primaryKey(),',
|
|
371
|
+
' orgId: text().notNull(),',
|
|
372
|
+
...spreads.map((entry) => ` // TODO spread in the old schema: ${entry.name}`),
|
|
373
|
+
...columns,
|
|
374
|
+
...stamps,
|
|
375
|
+
' },',
|
|
376
|
+
` (t) => [index('${table}_org_idx').on(t.orgId)],`,
|
|
377
|
+
');',
|
|
378
|
+
].join('\n'),
|
|
379
|
+
);
|
|
380
|
+
|
|
381
|
+
const columnMap = [
|
|
382
|
+
" _id: 'id',",
|
|
383
|
+
...fields.map((entry) => ` ${identifier(entry.name)}: '${snake(entry.name)}',`),
|
|
384
|
+
...(schema.timestamps
|
|
385
|
+
? [" createdAt: 'created_at',", " updatedAt: 'updated_at',"]
|
|
386
|
+
: []),
|
|
387
|
+
];
|
|
388
|
+
|
|
389
|
+
imports.push(
|
|
390
|
+
[
|
|
391
|
+
` {`,
|
|
392
|
+
` collection: '${collection.collection}',`,
|
|
393
|
+
` table: '${table}',`,
|
|
394
|
+
' columns: {',
|
|
395
|
+
...columnMap,
|
|
396
|
+
' },',
|
|
397
|
+
' },',
|
|
398
|
+
].join('\n'),
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
writeFileSync(
|
|
403
|
+
resolve(here, 'schema-draft.ts'),
|
|
404
|
+
[
|
|
405
|
+
`// Draft tables for ${plugin}, written by migration/checklist.ts from the Mongoose`,
|
|
406
|
+
'// schemas. Nothing imports this file: move a table into api/src/db/schema.ts when',
|
|
407
|
+
'// you port its module, pick real types (integer, enums, foreign keys), then run',
|
|
408
|
+
'// `pnpm db:generate` and add its core_enable_tenant_rls line (schema-change skill).',
|
|
409
|
+
"import { boolean, doublePrecision, index, jsonb, text, timestamp } from 'drizzle-orm/pg-core';",
|
|
410
|
+
"import { table } from '../api/src/db/table.ts';",
|
|
411
|
+
'',
|
|
412
|
+
tables.join('\n\n'),
|
|
413
|
+
'',
|
|
414
|
+
].join('\n'),
|
|
415
|
+
);
|
|
416
|
+
|
|
417
|
+
writeFileSync(
|
|
418
|
+
resolve(here, 'import-map.ts'),
|
|
419
|
+
[
|
|
420
|
+
'// Which Mongo field goes to which column, per collection, for scripts/import-mongo.ts.',
|
|
421
|
+
'// Written by migration/checklist.ts; edit it as the tables in api/src/db/schema.ts change.',
|
|
422
|
+
'export const IMPORT_MAP: { collection: string; table: string; columns: Record<string, string> }[] = [',
|
|
423
|
+
...imports,
|
|
424
|
+
'];',
|
|
425
|
+
'',
|
|
426
|
+
].join('\n'),
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
console.info(`MIGRATION.md: ${all.length} items, ${gaps} gaps; ${tables.length} draft tables`);
|