create-erxes 0.0.0-stage → 0.1.0

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.
Files changed (69) hide show
  1. package/README.md +23 -2
  2. package/dist/index.js +481 -0
  3. package/dist/templates/base/.agents/skills/endpoint/SKILL.md +21 -0
  4. package/dist/templates/base/.agents/skills/schema-change/SKILL.md +28 -0
  5. package/dist/templates/base/.agents/skills/verify/SKILL.md +24 -0
  6. package/dist/templates/base/.github/workflows/ci.yml +44 -0
  7. package/dist/templates/base/AGENTS.md +60 -0
  8. package/dist/templates/base/CLAUDE.md +1 -0
  9. package/dist/templates/base/Dockerfile +13 -0
  10. package/dist/templates/base/_dockerignore +4 -0
  11. package/dist/templates/base/_env.example +15 -0
  12. package/dist/templates/base/_gitignore +11 -0
  13. package/dist/templates/base/_node-version +1 -0
  14. package/dist/templates/base/api/AGENTS.md +22 -0
  15. package/dist/templates/base/api/drizzle/0000_items.sql +13 -0
  16. package/dist/templates/base/api/drizzle/meta/0000_snapshot.json +126 -0
  17. package/dist/templates/base/api/drizzle/meta/_journal.json +13 -0
  18. package/dist/templates/base/api/drizzle.config.ts +16 -0
  19. package/dist/templates/base/api/openapi.json +450 -0
  20. package/dist/templates/base/api/package.json +50 -0
  21. package/dist/templates/base/api/scripts/openapi.ts +27 -0
  22. package/dist/templates/base/api/src/__tests__/server.test.ts +51 -0
  23. package/dist/templates/base/api/src/config.ts +43 -0
  24. package/dist/templates/base/api/src/core.ts +37 -0
  25. package/dist/templates/base/api/src/db/client.ts +30 -0
  26. package/dist/templates/base/api/src/db/migrate.ts +86 -0
  27. package/dist/templates/base/api/src/db/schema.ts +34 -0
  28. package/dist/templates/base/api/src/db/table.ts +8 -0
  29. package/dist/templates/base/api/src/logger.ts +5 -0
  30. package/dist/templates/base/api/src/main.ts +23 -0
  31. package/dist/templates/base/api/src/modules/items/__tests__/items.test.ts +86 -0
  32. package/dist/templates/base/api/src/modules/items/items.ts +121 -0
  33. package/dist/templates/base/api/src/orpc.ts +25 -0
  34. package/dist/templates/base/api/src/router.ts +10 -0
  35. package/dist/templates/base/api/src/server.ts +58 -0
  36. package/dist/templates/base/api/src/telemetry.ts +27 -0
  37. package/dist/templates/base/api/src/test/core-functions.sql +18 -0
  38. package/dist/templates/base/api/src/test/setup.ts +68 -0
  39. package/dist/templates/base/api/tsconfig.json +9 -0
  40. package/dist/templates/base/knip.ts +22 -0
  41. package/dist/templates/base/package.json +31 -0
  42. package/dist/templates/base/pnpm-workspace.yaml +20 -0
  43. package/dist/templates/base/tools/check-generated.ts +78 -0
  44. package/dist/templates/base/tools/check-rls.sh +30 -0
  45. package/dist/templates/base/tools/check-rls.sql +109 -0
  46. package/dist/templates/base/tools/no-org-id.sql +5 -0
  47. package/dist/templates/base/tools/test/no-skips.ts +58 -0
  48. package/dist/templates/base/tsconfig.base.json +16 -0
  49. package/dist/templates/base/tsconfig.json +9 -0
  50. package/dist/templates/base/ui/AGENTS.md +18 -0
  51. package/dist/templates/base/ui/package.json +32 -0
  52. package/dist/templates/base/ui/rsbuild.config.ts +36 -0
  53. package/dist/templates/base/ui/src/__Pascal__.tsx +9 -0
  54. package/dist/templates/base/ui/src/config.tsx +11 -0
  55. package/dist/templates/base/ui/src/index.ts +3 -0
  56. package/dist/templates/base/ui/src/items/ItemSheet.tsx +107 -0
  57. package/dist/templates/base/ui/src/items/ItemsPage.tsx +116 -0
  58. package/dist/templates/base/ui/src/items/useItems.ts +79 -0
  59. package/dist/templates/base/ui/src/orpc.ts +32 -0
  60. package/dist/templates/base/ui/tsconfig.json +11 -0
  61. package/dist/templates/base/vite.config.ts +63 -0
  62. package/dist/templates/migrate/.agents/skills/plugin-migrate/SKILL.md +61 -0
  63. package/dist/templates/migrate/migration/checklist.ts +429 -0
  64. package/dist/templates/migrate/migration/core-map.ts +149 -0
  65. package/dist/templates/migrate/migration/import-map.ts +4 -0
  66. package/dist/templates/migrate/migration/inventory.ts +881 -0
  67. package/dist/templates/migrate/package.json +15 -0
  68. package/dist/templates/migrate/scripts/import-mongo.ts +143 -0
  69. 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. */
19
+ export const useItems = () =>
20
+ useInfiniteQuery(
21
+ orpc.items.list.infiniteOptions({
22
+ input: (cursor: string | undefined) => ({ cursor }),
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,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"]
9
+ },
10
+ "include": ["src", "rsbuild.config.ts"]
11
+ }
@@ -0,0 +1,63 @@
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
+ 'api/openapi.json',
34
+ 'pnpm-lock.yaml',
35
+ ],
36
+ },
37
+ // The rules core and every plugin share, with the overrides for a plugin's api/, ui/ and
38
+ // migration kit. Change them in core's packages/lint; a new @erxes/lint brings them here.
39
+ lint: {
40
+ ...preset,
41
+ ignorePatterns: ['**/node_modules/**', '**/dist/**', 'coverage/**'],
42
+ },
43
+ test: {
44
+ reporters: [...configDefaults.reporters, noSkips()],
45
+ projects: [
46
+ {
47
+ root: 'api',
48
+ test: {
49
+ name: 'api',
50
+ include: ['src/**/*.test.ts'],
51
+ environment: 'node',
52
+ setupFiles: [resolve(import.meta.dirname, 'api/src/test/setup.ts')],
53
+ // config.ts needs these; tests never reach core.
54
+ env: {
55
+ CORE_URL: 'http://127.0.0.1:9',
56
+ PLUGIN_SECRET: 'test-secret-'.padEnd(32, 'x'),
57
+ LOG_LEVEL: 'silent',
58
+ },
59
+ },
60
+ },
61
+ ],
62
+ },
63
+ });
@@ -0,0 +1,61 @@
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; `plugin-owned` lives in this plugin's tables or code.
41
+ 5. Pages. One per UI route, under `ui/src/<module>/`, built from `erxes-ui`
42
+ the way `ui/src/items/` is. The old `ui-modules` components don't exist
43
+ here: erxes-ui's old-to-new component table, `packages/ui/MIGRATION.md` in
44
+ the erxes repo (branch `postgre`), says what replaces each one.
45
+ 6. Check parity in the browser, following the `verify` skill: open the old
46
+ page and the new one side by side, and walk the same actions on both.
47
+ 7. Tick an item once it works on the new core and its old bugs and
48
+ workarounds are gone. When an item needs something core doesn't offer
49
+ (marked **gap**, or one you find), file an issue in the plugin's repo that
50
+ says what the old plugin did and what core would need, tick the item and
51
+ end its line with `(filed <issue url>)`. The feature waits for core.
52
+ 8. Commit the module: schema, migration, procedures, page and the ticks
53
+ together.
54
+ 9. Copy data once all modules are in: `pnpm migrate:import` with `MONGO_URL`,
55
+ `DATA_URL` and `ORG_ID` set. Its `Skipped` list must be empty or explained.
56
+
57
+ Rerun `pnpm migrate:inventory` and `pnpm migrate:checklist` only to pick up
58
+ new old-side code; ticks survive the rerun.
59
+
60
+ Done when every checklist item is ticked, each gap with its issue
61
+ 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\`. Its 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`);