create-nexwire 4.3.0 → 4.4.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.
- package/README.md +10 -5
- package/express/src/framework/facade.ts +1 -0
- package/express/src/framework/http/logger.ts +13 -4
- package/express/src/framework/maker-cli/runtime/core.mjs +1 -2
- package/express/src/framework/notification/channels/mail.ts +8 -6
- package/express/src/middlewares/auth-middleware.ts +1 -2
- package/express/src/middlewares/role-middleware.ts +3 -13
- package/express/src/modules/auth/routes/index.ts +1 -2
- package/express/src/providers/notification.ts +2 -2
- package/express/src/resources/src/components/Switch.vue +60 -65
- package/express/src/resources/src/pages/auth/forgetPassword.vue +23 -30
- package/express/src/resources/src/pages/auth/login.vue +25 -28
- package/express/src/resources/src/pages/auth/register.vue +35 -39
- package/express/src/resources/src/pages/auth/resetPassword.vue +29 -37
- package/express/src/resources/src/pages/auth/verifyEmail.vue +15 -19
- package/express/src/resources/src/plugins/gum.ts +519 -203
- package/express/src/resources/src/stores/auth.ts +9 -6
- package/express/src/storage/database/nexwire.sqlite +0 -0
- package/fastify/.dockerignore +14 -0
- package/fastify/.env +44 -0
- package/fastify/.env.example +42 -0
- package/fastify/.vscode/extensions.json +6 -0
- package/fastify/.vscode/settings.json +37 -0
- package/fastify/LICENSE +21 -0
- package/fastify/README.md +232 -0
- package/fastify/biome.json +99 -0
- package/fastify/drizzle.config.ts +25 -0
- package/fastify/gitignore-stub +31 -0
- package/fastify/package.json +130 -0
- package/fastify/public/.gitkeep +0 -0
- package/fastify/src/config/app.ts +41 -0
- package/fastify/src/config/auth.ts +12 -0
- package/fastify/src/config/cache.ts +16 -0
- package/fastify/src/config/cookie.ts +15 -0
- package/fastify/src/config/cors.ts +12 -0
- package/fastify/src/config/database.ts +48 -0
- package/fastify/src/config/index.ts +83 -0
- package/fastify/src/config/jwt.ts +19 -0
- package/fastify/src/config/logging.ts +13 -0
- package/fastify/src/config/mail.ts +24 -0
- package/fastify/src/config/openapi.ts +36 -0
- package/fastify/src/config/queue.ts +30 -0
- package/fastify/src/config/rateLimit.ts +17 -0
- package/fastify/src/config/realtime.ts +15 -0
- package/fastify/src/config/redis.ts +18 -0
- package/fastify/src/config/security.ts +44 -0
- package/fastify/src/config/session.ts +17 -0
- package/fastify/src/config/storage.ts +22 -0
- package/fastify/src/config/validate.ts +50 -0
- package/fastify/src/database/migrations/sqlite/0000_init.sql +48 -0
- package/fastify/src/database/migrations/sqlite/meta/0000_snapshot.json +324 -0
- package/fastify/src/database/migrations/sqlite/meta/_journal.json +13 -0
- package/fastify/src/database/schema.ts +2 -0
- package/fastify/src/env.ts +76 -0
- package/fastify/src/framework/cache/cache.ts +114 -0
- package/fastify/src/framework/circuit-breaker/cb.ts +88 -0
- package/fastify/src/framework/database/app-schema.ts +37 -0
- package/fastify/src/framework/database/connection.ts +223 -0
- package/fastify/src/framework/database/migrate-hooks.ts +87 -0
- package/fastify/src/framework/database/paginate.ts +226 -0
- package/fastify/src/framework/database/schema.ts +20 -0
- package/fastify/src/framework/database/seed.ts +152 -0
- package/fastify/src/framework/events/dispatcher.ts +61 -0
- package/fastify/src/framework/facade.ts +96 -0
- package/fastify/src/framework/http/app.ts +176 -0
- package/fastify/src/framework/http/cors.ts +13 -0
- package/fastify/src/framework/http/fastify.d.ts +13 -0
- package/fastify/src/framework/http/logger.ts +110 -0
- package/fastify/src/framework/http/metrics.ts +22 -0
- package/fastify/src/framework/http/openapi.ts +48 -0
- package/fastify/src/framework/http/ratelimiter.ts +47 -0
- package/fastify/src/framework/http/security.ts +25 -0
- package/fastify/src/framework/http/static.ts +83 -0
- package/fastify/src/framework/http/validation.ts +49 -0
- package/fastify/src/framework/kernel.ts +43 -0
- package/fastify/src/framework/maker-cli/db/core.mjs +291 -0
- package/fastify/src/framework/maker-cli/db/index.mjs +204 -0
- package/fastify/src/framework/maker-cli/deploy/core.mjs +1046 -0
- package/fastify/src/framework/maker-cli/deploy/index.mjs +91 -0
- package/fastify/src/framework/maker-cli/index.mjs +65 -0
- package/fastify/src/framework/maker-cli/middleware/core.mjs +41 -0
- package/fastify/src/framework/maker-cli/middleware/index.mjs +17 -0
- package/fastify/src/framework/maker-cli/module/core.mjs +1066 -0
- package/fastify/src/framework/maker-cli/module/index.mjs +211 -0
- package/fastify/src/framework/maker-cli/provider/core.mjs +49 -0
- package/fastify/src/framework/maker-cli/provider/index.mjs +12 -0
- package/fastify/src/framework/maker-cli/runtime/build-ui.mjs +16 -0
- package/fastify/src/framework/maker-cli/runtime/core.mjs +560 -0
- package/fastify/src/framework/maker-cli/runtime/index.mjs +120 -0
- package/fastify/src/framework/maker-cli/runtime/redis-commander-patch.cjs +20 -0
- package/fastify/src/framework/maker-cli/stubs/controller/openapi.ts.stub +74 -0
- package/fastify/src/framework/maker-cli/stubs/controller/plain.ts.stub +77 -0
- package/fastify/src/framework/maker-cli/stubs/controller/standalone.ts.stub +51 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/Dockerfile.bun.stub +52 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/Dockerfile.pnpm.stub +55 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/Dockerfile.stub +51 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/Dockerfile.yarn.stub +55 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/README.stub +59 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/compose/mysql.server.stub +45 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/compose/postgres.server.stub +45 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/compose/sqlite.stub +45 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/env/mysql.server.stub +68 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/env/postgres.server.stub +68 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/env/sqlite.stub +67 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/scripts/auto-migrate.sh.stub +41 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/README.stub +78 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/compose/noredis.stub +120 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/compose/redis.dev.stub +133 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/compose/redis.stub +131 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/env/local.example.stub +11 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/env/noredis.stub +25 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/env/redis.stub +25 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/nginx-vhost/README.stub +16 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/nginx-vhost/app.example.com.stub +13 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/pgadmin/servers.stub +14 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/server/redis/redis.conf.stub +7 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/supervisor/noredis.stub +54 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/supervisor/redis.stub +70 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/workflow/local.json.stub +25 -0
- package/fastify/src/framework/maker-cli/stubs/deploy/workflow/remote.json.stub +24 -0
- package/fastify/src/framework/maker-cli/stubs/facade/name.ts.stub +12 -0
- package/fastify/src/framework/maker-cli/stubs/helper/name.ts.stub +14 -0
- package/fastify/src/framework/maker-cli/stubs/job/name.ts.stub +25 -0
- package/fastify/src/framework/maker-cli/stubs/middleware/name.local.ts.stub +15 -0
- package/fastify/src/framework/maker-cli/stubs/middleware/name.ts.stub +15 -0
- package/fastify/src/framework/maker-cli/stubs/model/name.mysql.ts.stub +10 -0
- package/fastify/src/framework/maker-cli/stubs/model/name.postgresql.ts.stub +10 -0
- package/fastify/src/framework/maker-cli/stubs/model/name.sqlite.ts.stub +10 -0
- package/fastify/src/framework/maker-cli/stubs/notification/controller.ts.stub +79 -0
- package/fastify/src/framework/maker-cli/stubs/notification/job.ts.stub +11 -0
- package/fastify/src/framework/maker-cli/stubs/notification/model.ts.stub +29 -0
- package/fastify/src/framework/maker-cli/stubs/notification/provider.ts.stub +174 -0
- package/fastify/src/framework/maker-cli/stubs/notification/route.api.ts.stub +71 -0
- package/fastify/src/framework/maker-cli/stubs/notification/route.plain.ts.stub +15 -0
- package/fastify/src/framework/maker-cli/stubs/notification/schema.plain.ts.stub +26 -0
- package/fastify/src/framework/maker-cli/stubs/notification/schema.ts.stub +26 -0
- package/fastify/src/framework/maker-cli/stubs/provider/module-barrel.ts.stub +19 -0
- package/fastify/src/framework/maker-cli/stubs/provider/module.ts.stub +21 -0
- package/fastify/src/framework/maker-cli/stubs/provider/name.ts.stub +21 -0
- package/fastify/src/framework/maker-cli/stubs/route/api.ts.stub +101 -0
- package/fastify/src/framework/maker-cli/stubs/route/plain.ts.stub +23 -0
- package/fastify/src/framework/maker-cli/stubs/route/standalone.api.ts.stub +120 -0
- package/fastify/src/framework/maker-cli/stubs/route/standalone.plain.ts.stub +50 -0
- package/fastify/src/framework/maker-cli/stubs/schedule/name.ts.stub +36 -0
- package/fastify/src/framework/maker-cli/stubs/schema/name.plain.ts.stub +14 -0
- package/fastify/src/framework/maker-cli/stubs/schema/name.ts.stub +33 -0
- package/fastify/src/framework/maker-cli/stubs/seeder/name.ts.stub +18 -0
- package/fastify/src/framework/maker-cli/stubs/seeder/standalone.ts.stub +22 -0
- package/fastify/src/framework/maker-cli/stubs/service/name.plain.ts.stub +67 -0
- package/fastify/src/framework/maker-cli/stubs/service/name.ts.stub +65 -0
- package/fastify/src/framework/maker-cli/stubs/test/unit.ts.stub +22 -0
- package/fastify/src/framework/maker-cli/stubs/type/name.ts.stub +22 -0
- package/fastify/src/framework/maker-cli/utils/env-config.mjs +44 -0
- package/fastify/src/framework/maker-cli/utils/env-db.mjs +61 -0
- package/fastify/src/framework/maker-cli/utils/file-ops.mjs +30 -0
- package/fastify/src/framework/maker-cli/utils/flags.mjs +14 -0
- package/fastify/src/framework/maker-cli/utils/help.mjs +379 -0
- package/fastify/src/framework/maker-cli/utils/naming.mjs +119 -0
- package/fastify/src/framework/maker-cli/utils/process.mjs +52 -0
- package/fastify/src/framework/maker-cli/utils/project-commands.mjs +201 -0
- package/fastify/src/framework/modules/discover.ts +57 -0
- package/fastify/src/framework/modules/routes.ts +31 -0
- package/fastify/src/framework/notification/channels/broadcast.ts +15 -0
- package/fastify/src/framework/notification/channels/mail.ts +42 -0
- package/fastify/src/framework/notification/contracts.ts +29 -0
- package/fastify/src/framework/notification/index.ts +59 -0
- package/fastify/src/framework/notification/registry.ts +15 -0
- package/fastify/src/framework/providers/adapter.ts +149 -0
- package/fastify/src/framework/providers/contracts.ts +126 -0
- package/fastify/src/framework/providers/loader.ts +105 -0
- package/fastify/src/framework/providers/registry.ts +88 -0
- package/fastify/src/framework/queue/clear.ts +22 -0
- package/fastify/src/framework/queue/queue.ts +401 -0
- package/fastify/src/framework/queue/ui.ts +194 -0
- package/fastify/src/framework/queue/worker.ts +61 -0
- package/fastify/src/framework/realtime/broadcast.ts +70 -0
- package/fastify/src/framework/realtime/index.ts +2 -0
- package/fastify/src/framework/realtime/socket-cookie.ts +72 -0
- package/fastify/src/framework/realtime/socket.ts +149 -0
- package/fastify/src/framework/realtime/types.ts +6 -0
- package/fastify/src/framework/realtime/ui.ts +16 -0
- package/fastify/src/framework/redis/client.ts +148 -0
- package/fastify/src/framework/runtime/runtime.ts +23 -0
- package/fastify/src/framework/runtime/server-adapter.ts +33 -0
- package/fastify/src/framework/scheduler/lock.ts +128 -0
- package/fastify/src/framework/scheduler/run.ts +48 -0
- package/fastify/src/framework/scheduler/scheduler.ts +155 -0
- package/fastify/src/framework/server.ts +162 -0
- package/fastify/src/framework/session/session.ts +160 -0
- package/fastify/src/framework/storage/storage.ts +744 -0
- package/fastify/src/framework/support/cookie.ts +71 -0
- package/fastify/src/framework/support/jwt.ts +33 -0
- package/fastify/src/framework/support/lifecycle.ts +35 -0
- package/fastify/src/framework/support/logger.ts +148 -0
- package/fastify/src/framework/support/mail.ts +56 -0
- package/fastify/src/framework/support/password.ts +23 -0
- package/fastify/src/framework/support/status-codes.ts +75 -0
- package/fastify/src/framework/support/upload.ts +181 -0
- package/fastify/src/framework/support/url.ts +41 -0
- package/fastify/src/middlewares/auth-middleware.ts +134 -0
- package/fastify/src/middlewares/role-middleware.ts +19 -0
- package/fastify/src/modules/auth/__tests__/forgetpass.test.ts +200 -0
- package/fastify/src/modules/auth/__tests__/login.test.ts +171 -0
- package/fastify/src/modules/auth/__tests__/logout.test.ts +60 -0
- package/fastify/src/modules/auth/__tests__/logoutalldevices.test.ts +49 -0
- package/fastify/src/modules/auth/__tests__/me.test.ts +91 -0
- package/fastify/src/modules/auth/__tests__/refreshtoken.test.ts +305 -0
- package/fastify/src/modules/auth/__tests__/register.test.ts +291 -0
- package/fastify/src/modules/auth/__tests__/resetpass.test.ts +206 -0
- package/fastify/src/modules/auth/__tests__/verifyemail.test.ts +183 -0
- package/fastify/src/modules/auth/controllers/auth.ts +252 -0
- package/fastify/src/modules/auth/controllers/role.ts +35 -0
- package/fastify/src/modules/auth/database/models/role.ts +14 -0
- package/fastify/src/modules/auth/database/models/user.ts +46 -0
- package/fastify/src/modules/auth/database/seeders/role.ts +18 -0
- package/fastify/src/modules/auth/database/seeders/user.ts +35 -0
- package/fastify/src/modules/auth/facade.ts +12 -0
- package/fastify/src/modules/auth/helpers/auth.ts +115 -0
- package/fastify/src/modules/auth/jobs/forgetpass.ts +20 -0
- package/fastify/src/modules/auth/jobs/registeruser.ts +23 -0
- package/fastify/src/modules/auth/jobs/verifyemail.ts +18 -0
- package/fastify/src/modules/auth/routes/index.ts +185 -0
- package/fastify/src/modules/auth/routes/role.ts +56 -0
- package/fastify/src/modules/auth/schemas/auth.ts +116 -0
- package/fastify/src/modules/auth/services/forgetpassword.ts +66 -0
- package/fastify/src/modules/auth/services/login.ts +63 -0
- package/fastify/src/modules/auth/services/logout.ts +25 -0
- package/fastify/src/modules/auth/services/logoutalldevices.ts +19 -0
- package/fastify/src/modules/auth/services/me.ts +31 -0
- package/fastify/src/modules/auth/services/refreshtoken.ts +110 -0
- package/fastify/src/modules/auth/services/register.ts +141 -0
- package/fastify/src/modules/auth/services/resetpassword.ts +69 -0
- package/fastify/src/modules/auth/services/verifyemail.ts +77 -0
- package/fastify/src/modules/auth/types/auth.ts +31 -0
- package/fastify/src/providers/README.md +107 -0
- package/fastify/src/providers/notification.ts +4 -0
- package/fastify/src/resources/index.html +17 -0
- package/fastify/src/resources/src/App.vue +5 -0
- package/fastify/src/resources/src/assets/css/styles.css +14761 -0
- package/fastify/src/resources/src/assets/css/styles.css.map +1 -0
- package/fastify/src/resources/src/assets/fonts/public-sans/public-sans-latin-ext.woff2 +0 -0
- package/fastify/src/resources/src/assets/fonts/public-sans/public-sans-latin.woff2 +0 -0
- package/fastify/src/resources/src/assets/images/favicon/favicon.ico +0 -0
- package/fastify/src/resources/src/assets/images/logo.png +0 -0
- package/fastify/src/resources/src/assets/scss/custom.css +217 -0
- package/fastify/src/resources/src/assets/scss/custom.css.map +1 -0
- package/fastify/src/resources/src/assets/scss/custom.scss +1346 -0
- package/fastify/src/resources/src/components/Button.vue +32 -0
- package/fastify/src/resources/src/components/Checkbox.vue +32 -0
- package/fastify/src/resources/src/components/Datepicker.vue +106 -0
- package/fastify/src/resources/src/components/FeatureButton.vue +53 -0
- package/fastify/src/resources/src/components/FloatButton.vue +33 -0
- package/fastify/src/resources/src/components/Href.vue +32 -0
- package/fastify/src/resources/src/components/Input.vue +243 -0
- package/fastify/src/resources/src/components/InputGroup.vue +147 -0
- package/fastify/src/resources/src/components/InputPasswordToggle.vue +229 -0
- package/fastify/src/resources/src/components/Modal.vue +71 -0
- package/fastify/src/resources/src/components/Pagebar.vue +26 -0
- package/fastify/src/resources/src/components/Refresh.vue +19 -0
- package/fastify/src/resources/src/components/Select.vue +384 -0
- package/fastify/src/resources/src/components/Spinner.vue +42 -0
- package/fastify/src/resources/src/components/Switch.vue +60 -0
- package/fastify/src/resources/src/components/TextArea.vue +117 -0
- package/fastify/src/resources/src/components/Toast.vue +56 -0
- package/fastify/src/resources/src/components/datatable/DataTableSkeleton.vue +98 -0
- package/fastify/src/resources/src/components/datatable/Pagination.vue +147 -0
- package/fastify/src/resources/src/components/datatable/SelectOpption.vue +64 -0
- package/fastify/src/resources/src/components/datatable/index.vue +199 -0
- package/fastify/src/resources/src/composables/useAuth.ts +52 -0
- package/fastify/src/resources/src/helpers/assets.ts +51 -0
- package/fastify/src/resources/src/helpers/nformatter.ts +10 -0
- package/fastify/src/resources/src/helpers/utils.ts +75 -0
- package/fastify/src/resources/src/layouts/AuthLayout.vue +20 -0
- package/fastify/src/resources/src/layouts/Layout/Footer.vue +23 -0
- package/fastify/src/resources/src/layouts/Layout/Header.vue +124 -0
- package/fastify/src/resources/src/layouts/Layout/Sidebar.vue +137 -0
- package/fastify/src/resources/src/layouts/Layout/index.vue +87 -0
- package/fastify/src/resources/src/main.ts +28 -0
- package/fastify/src/resources/src/pages/auth/forgetPassword.vue +76 -0
- package/fastify/src/resources/src/pages/auth/login.vue +96 -0
- package/fastify/src/resources/src/pages/auth/register.vue +129 -0
- package/fastify/src/resources/src/pages/auth/resetPassword.vue +110 -0
- package/fastify/src/resources/src/pages/auth/verifyEmail.vue +64 -0
- package/fastify/src/resources/src/pages/dashboard/index.vue +66 -0
- package/fastify/src/resources/src/plugins/axios.ts +31 -0
- package/fastify/src/resources/src/plugins/browserDetect.ts +55 -0
- package/fastify/src/resources/src/plugins/dialog.ts +216 -0
- package/fastify/src/resources/src/plugins/gum.ts +683 -0
- package/fastify/src/resources/src/plugins/pulse.ts +157 -0
- package/fastify/src/resources/src/plugins/routeProgress.ts +87 -0
- package/fastify/src/resources/src/router/index.ts +85 -0
- package/fastify/src/resources/src/stores/admin-ui.ts +148 -0
- package/fastify/src/resources/src/stores/auth.ts +187 -0
- package/fastify/src/resources/tsconfig.json +19 -0
- package/fastify/src/resources/vite.config.ts +64 -0
- package/fastify/src/storage/app/private/.gitkeep +0 -0
- package/fastify/src/storage/app/public/.gitkeep +0 -0
- package/fastify/src/storage/app/tmp/generated/.gitkeep +0 -0
- package/fastify/src/storage/database/nexwire.sqlite +0 -0
- package/fastify/src/storage/logs/.gitkeep +0 -0
- package/fastify/src/storage/logs/app.log +0 -0
- package/fastify/src/storage/logs/fatal.log +0 -0
- package/fastify/src/storage/tmp/.gitkeep +0 -0
- package/fastify/src/storage/trash/modules/.gitkeep +0 -0
- package/fastify/src/types/global.d.ts +32 -0
- package/fastify/tsconfig.json +19 -0
- package/fastify/vitest.config.ts +38 -0
- package/hono/src/framework/facade.ts +1 -0
- package/hono/src/framework/http/logger.ts +86 -81
- package/hono/src/framework/maker-cli/runtime/core.mjs +1 -2
- package/hono/src/framework/notification/channels/mail.ts +8 -6
- package/hono/src/middlewares/auth-middleware.ts +1 -2
- package/hono/src/middlewares/role-middleware.ts +3 -13
- package/hono/src/modules/auth/routes/index.ts +1 -2
- package/hono/src/providers/notification.ts +2 -2
- package/hono/src/resources/src/components/Switch.vue +60 -65
- package/hono/src/resources/src/pages/auth/forgetPassword.vue +23 -29
- package/hono/src/resources/src/pages/auth/login.vue +26 -29
- package/hono/src/resources/src/pages/auth/register.vue +36 -40
- package/hono/src/resources/src/pages/auth/resetPassword.vue +29 -37
- package/hono/src/resources/src/pages/auth/verifyEmail.vue +15 -19
- package/hono/src/resources/src/plugins/gum.ts +519 -203
- package/hono/src/resources/src/stores/auth.ts +9 -6
- package/hono/src/storage/database/nexwire.sqlite +0 -0
- package/package.json +3 -2
- package/src/index.js +11 -8
|
@@ -0,0 +1,1066 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { glob } from "glob";
|
|
4
|
+
import { drizzleGenerateArgs, ensureDatabaseDirectory, ensureMigrationMeta, syncMigrationDialect } from "../db/core.mjs";
|
|
5
|
+
import { detectDialect, openApiEnabled } from "../utils/env-db.mjs";
|
|
6
|
+
import { writeFiles } from "../utils/file-ops.mjs";
|
|
7
|
+
import { hasFlag } from "../utils/flags.mjs";
|
|
8
|
+
import { assertName, camelCase, normalizeName, pascal, plural, snakeCase } from "../utils/naming.mjs";
|
|
9
|
+
import { packageScript, runNodeScript } from "../utils/process.mjs";
|
|
10
|
+
|
|
11
|
+
const stubsRoot = path.resolve(import.meta.dirname, "../stubs");
|
|
12
|
+
|
|
13
|
+
/** Template stubs used for module file generation. */
|
|
14
|
+
const STUBS = {
|
|
15
|
+
schema: {
|
|
16
|
+
openapi: "schema/name.ts.stub",
|
|
17
|
+
plain: "schema/name.plain.ts.stub"
|
|
18
|
+
},
|
|
19
|
+
controller: {
|
|
20
|
+
openapi: "controller/openapi.ts.stub",
|
|
21
|
+
plain: "controller/plain.ts.stub",
|
|
22
|
+
standalone: "controller/standalone.ts.stub"
|
|
23
|
+
},
|
|
24
|
+
route: {
|
|
25
|
+
api: "route/api.ts.stub",
|
|
26
|
+
plain: "route/plain.ts.stub",
|
|
27
|
+
standaloneApi: "route/standalone.api.ts.stub",
|
|
28
|
+
standalonePlain: "route/standalone.plain.ts.stub"
|
|
29
|
+
},
|
|
30
|
+
model: {
|
|
31
|
+
named: {
|
|
32
|
+
mysql: "model/name.mysql.ts.stub",
|
|
33
|
+
postgresql: "model/name.postgresql.ts.stub",
|
|
34
|
+
sqlite: "model/name.sqlite.ts.stub"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
seeder: {
|
|
38
|
+
named: "seeder/name.ts.stub",
|
|
39
|
+
standalone: "seeder/standalone.ts.stub"
|
|
40
|
+
},
|
|
41
|
+
service: {
|
|
42
|
+
named: "service/name.ts.stub",
|
|
43
|
+
plain: "service/name.plain.ts.stub"
|
|
44
|
+
},
|
|
45
|
+
facade: {
|
|
46
|
+
named: "facade/name.ts.stub"
|
|
47
|
+
},
|
|
48
|
+
provider: {
|
|
49
|
+
named: "provider/name.ts.stub",
|
|
50
|
+
module: "provider/module.ts.stub",
|
|
51
|
+
moduleBarrel: "provider/module-barrel.ts.stub"
|
|
52
|
+
},
|
|
53
|
+
helper: {
|
|
54
|
+
named: "helper/name.ts.stub"
|
|
55
|
+
},
|
|
56
|
+
middleware: {
|
|
57
|
+
local: "middleware/name.local.ts.stub"
|
|
58
|
+
},
|
|
59
|
+
type: {
|
|
60
|
+
named: "type/name.ts.stub"
|
|
61
|
+
},
|
|
62
|
+
job: {
|
|
63
|
+
named: "job/name.ts.stub"
|
|
64
|
+
},
|
|
65
|
+
schedule: {
|
|
66
|
+
named: "schedule/name.ts.stub"
|
|
67
|
+
},
|
|
68
|
+
notification: {
|
|
69
|
+
controller: "notification/controller.ts.stub",
|
|
70
|
+
schema: {
|
|
71
|
+
openapi: "notification/schema.ts.stub",
|
|
72
|
+
plain: "notification/schema.plain.ts.stub"
|
|
73
|
+
},
|
|
74
|
+
routeApi: "notification/route.api.ts.stub",
|
|
75
|
+
routePlain: "notification/route.plain.ts.stub",
|
|
76
|
+
model: "notification/model.ts.stub",
|
|
77
|
+
provider: "notification/provider.ts.stub",
|
|
78
|
+
job: "notification/job.ts.stub"
|
|
79
|
+
},
|
|
80
|
+
test: {
|
|
81
|
+
unit: "test/unit.ts.stub"
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/** Module scaffolding handlers shared by maker module commands. */
|
|
86
|
+
|
|
87
|
+
/** Read a stub file from the stubs directory. */
|
|
88
|
+
async function readStubRaw(name) {
|
|
89
|
+
const file = path.join(stubsRoot, name);
|
|
90
|
+
return fs.readFile(file, "utf8");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Read a stub file and replace {{key}} placeholders, removing unfilled ones. */
|
|
94
|
+
async function stub(name, values = {}) {
|
|
95
|
+
let content = await readStubRaw(name);
|
|
96
|
+
for (const [key, value] of Object.entries(values)) {
|
|
97
|
+
content = content.replaceAll(`{{${key}}}`, String(value));
|
|
98
|
+
}
|
|
99
|
+
return content.replace(/\{\{[A-Z0-9_]+\}\}/g, "");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Resolve the canonical module root path inside src/modules. */
|
|
103
|
+
function moduleRoot(moduleName) {
|
|
104
|
+
const base = path.resolve(process.cwd(), "src/modules");
|
|
105
|
+
const root = path.resolve(base, moduleName);
|
|
106
|
+
if (root !== base && !root.startsWith(base + path.sep)) {
|
|
107
|
+
throw new Error(`Module path escapes src/modules: ${moduleName}`);
|
|
108
|
+
}
|
|
109
|
+
return root;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Leaf folder name of a module. `admin/post` -> `post`, `post` -> `post`.
|
|
114
|
+
* File names and class names always come from the leaf; imports come from the
|
|
115
|
+
* full path.
|
|
116
|
+
*/
|
|
117
|
+
function leafName(moduleName) {
|
|
118
|
+
const segments = String(moduleName).split("/").filter(Boolean);
|
|
119
|
+
return segments[segments.length - 1] || String(moduleName);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Panel prefix of a module, everything before the leaf. `admin/post` -> `admin`,
|
|
124
|
+
* `admin/reporting/deep` -> `admin/reporting`, `post` -> "".
|
|
125
|
+
*/
|
|
126
|
+
function panelOf(moduleName) {
|
|
127
|
+
const segments = String(moduleName).split("/").filter(Boolean);
|
|
128
|
+
return segments.slice(0, -1).join("/");
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Scalar tag for a module. Panels become a nested tag group so Scalar renders
|
|
133
|
+
* `Admin / Post` instead of flattening everything under one panel.
|
|
134
|
+
*/
|
|
135
|
+
function moduleLabel(moduleName) {
|
|
136
|
+
return String(moduleName)
|
|
137
|
+
.split("/")
|
|
138
|
+
.filter(Boolean)
|
|
139
|
+
.map((segment) => pascal(segment))
|
|
140
|
+
.join(" / ");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Parse `--path=<panel>` into a normalized module prefix.
|
|
145
|
+
* A panel is only a folder separator, so nested values are allowed:
|
|
146
|
+
* `--path=admin` and `--path=admin/reporting` both validate.
|
|
147
|
+
*/
|
|
148
|
+
function parsePanelPath(flags) {
|
|
149
|
+
const matches = flags.filter((flag) => flag.startsWith("--path"));
|
|
150
|
+
if (matches.length === 0) return "";
|
|
151
|
+
if (matches.length > 1) throw new Error("--path can only be given once.");
|
|
152
|
+
const raw = matches[0].slice("--path".length).replace(/^=/, "").trim();
|
|
153
|
+
if (!raw) throw new Error("--path needs a value, e.g. --path=admin");
|
|
154
|
+
const segments = raw
|
|
155
|
+
.split(/[\\/]+/)
|
|
156
|
+
.filter(Boolean)
|
|
157
|
+
.map((segment) => segment.toLowerCase());
|
|
158
|
+
if (segments.length === 0) throw new Error(`Invalid --path value: ${raw}`);
|
|
159
|
+
for (const segment of segments) {
|
|
160
|
+
if (!/^[a-z0-9][a-z0-9-_]*$/.test(segment)) {
|
|
161
|
+
throw new Error(`Invalid --path segment "${segment}". Use lowercase letters, numbers and dashes.`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return segments.join("/");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Parse `--notifiable=<type>` into the owner class written into the generated
|
|
169
|
+
* controller. Accepts both `--notifiable=user` and `--notifiable user`, and
|
|
170
|
+
* defaults to `user` so the common case needs no flag.
|
|
171
|
+
*/
|
|
172
|
+
function parseNotifiable(flags) {
|
|
173
|
+
const matches = flags
|
|
174
|
+
.map((flag, index) => ({ flag, index }))
|
|
175
|
+
.filter(({ flag }) => typeof flag === "string" && flag.startsWith("--notifiable"));
|
|
176
|
+
if (matches.length === 0) return "user";
|
|
177
|
+
if (matches.length > 1) throw new Error("--notifiable can only be given once.");
|
|
178
|
+
|
|
179
|
+
const [{ flag, index }] = matches;
|
|
180
|
+
let raw = flag.slice("--notifiable".length).replace(/^=/, "").trim();
|
|
181
|
+
if (!raw && typeof flags[index + 1] === "string" && !flags[index + 1].startsWith("--")) raw = flags[index + 1].trim();
|
|
182
|
+
if (!raw) throw new Error("--notifiable needs a value, e.g. --notifiable=user");
|
|
183
|
+
if (!/^[a-z0-9][a-z0-9_-]*$/.test(raw)) {
|
|
184
|
+
throw new Error(`Invalid --notifiable value "${raw}". Use lowercase letters, numbers, dashes and underscores.`);
|
|
185
|
+
}
|
|
186
|
+
return raw;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** True when the path is a directory holding at least one entry. */
|
|
190
|
+
async function dirHasFiles(target) {
|
|
191
|
+
try {
|
|
192
|
+
const entries = await fs.readdir(target);
|
|
193
|
+
return entries.length > 0;
|
|
194
|
+
} catch {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Path of the notification store already shipped by any module, or "".
|
|
201
|
+
*
|
|
202
|
+
* The table/model/provider is a singleton: two modules shipping it would
|
|
203
|
+
* generate the same `notifications` table twice and register two providers for
|
|
204
|
+
* one contract. `module:make-notification` therefore writes the store only when
|
|
205
|
+
* no module owns it yet, and otherwise adds just another HTTP surface over it.
|
|
206
|
+
*/
|
|
207
|
+
async function findNotificationStore() {
|
|
208
|
+
const backendSrc = path.resolve(process.cwd(), "src");
|
|
209
|
+
const files = await glob("modules/**/database/models/notification.{ts,js}", {
|
|
210
|
+
cwd: backendSrc,
|
|
211
|
+
nodir: true,
|
|
212
|
+
windowsPathsNoEscape: true
|
|
213
|
+
});
|
|
214
|
+
return files.sort()[0] ? path.join(backendSrc, files.sort()[0]) : "";
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Resolve the module a subcommand targets.
|
|
219
|
+
*
|
|
220
|
+
* `rawModule` is always the leaf name the developer typed. Subcommands accept the
|
|
221
|
+
* same `--path=<panel>` flag as `module:make`, so `panelme --path=admin` addresses
|
|
222
|
+
* src/modules/admin/panelme instead of failing with "module does not exist".
|
|
223
|
+
* Flags may arrive either in the positional slot or in extraFlags depending on
|
|
224
|
+
* whether a name was given, so every candidate string is scanned.
|
|
225
|
+
*/
|
|
226
|
+
function resolveModuleRef(rawModule, ...flagGroups) {
|
|
227
|
+
const raw = assertName(rawModule, "Module name");
|
|
228
|
+
const panel = parsePanelPath(flagGroups.flat().filter((value) => typeof value === "string"));
|
|
229
|
+
const moduleName = panel ? `${panel}/${raw}` : raw;
|
|
230
|
+
// Derive the leaf from the resolved path so `admin/post` passed directly as the
|
|
231
|
+
// module argument still yields `post` for filenames and default names.
|
|
232
|
+
return { moduleName, leaf: leafName(moduleName) };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Find an existing module directory whose folder name spells the requested
|
|
237
|
+
* module differently, only in the separators used (`my_module` vs `my-module`).
|
|
238
|
+
*
|
|
239
|
+
* Names are canonicalised to kebab-case, so a module scaffolded before this
|
|
240
|
+
* happened can live in a folder the canonical name no longer resolves to.
|
|
241
|
+
* Without this lookup, normalising the name would lock a developer out of their
|
|
242
|
+
* own module. Matching is segment by segment so a panel path such as
|
|
243
|
+
* `admin/reporting` keeps working, and each candidate is a real directory
|
|
244
|
+
* inside src/modules, so the result cannot escape it.
|
|
245
|
+
*/
|
|
246
|
+
async function resolveExistingModuleDir(moduleName) {
|
|
247
|
+
let current = path.resolve(process.cwd(), "src/modules");
|
|
248
|
+
for (const segment of moduleName.split("/").filter(Boolean)) {
|
|
249
|
+
let entries;
|
|
250
|
+
try {
|
|
251
|
+
entries = await fs.readdir(current, { withFileTypes: true });
|
|
252
|
+
} catch {
|
|
253
|
+
return "";
|
|
254
|
+
}
|
|
255
|
+
const directories = entries.filter((entry) => entry.isDirectory());
|
|
256
|
+
const exact = directories.find((entry) => entry.name === segment);
|
|
257
|
+
const loose = exact ?? directories.find((entry) => normalizeName(entry.name) === normalizeName(segment));
|
|
258
|
+
if (!loose) return "";
|
|
259
|
+
current = path.join(current, loose.name);
|
|
260
|
+
}
|
|
261
|
+
return current;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Ensure a module directory exists, throwing a helpful error if not. */
|
|
265
|
+
async function assertModuleExists(moduleName) {
|
|
266
|
+
const root = moduleRoot(moduleName);
|
|
267
|
+
let stats;
|
|
268
|
+
try {
|
|
269
|
+
stats = await fs.stat(root);
|
|
270
|
+
} catch {
|
|
271
|
+
const legacy = await resolveExistingModuleDir(moduleName);
|
|
272
|
+
if (legacy) return legacy;
|
|
273
|
+
throw new Error(`Module does not exist: ${moduleName}. Create it first with: bun maker module:make ${moduleName}`);
|
|
274
|
+
}
|
|
275
|
+
if (!stats.isDirectory()) {
|
|
276
|
+
throw new Error(`Module path is not a directory: src/modules/${moduleName}`);
|
|
277
|
+
}
|
|
278
|
+
return root;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Build the placeholder substitution map shared by module file generators.
|
|
283
|
+
*
|
|
284
|
+
* The same developer-supplied name reaches four different places in a stub, and
|
|
285
|
+
* each needs a different shape: import paths use the kebab file name, the
|
|
286
|
+
* service identifier is camelCase, the table identifier is camelCase plural and
|
|
287
|
+
* the SQL table name is snake_case plural. Deriving all of them from one
|
|
288
|
+
* canonical name is what keeps a multi-word name from landing in an identifier
|
|
289
|
+
* position as `game-configService`, which parses as subtraction.
|
|
290
|
+
*/
|
|
291
|
+
function moduleFileVars(moduleName, controller) {
|
|
292
|
+
const base = normalizeName(controller);
|
|
293
|
+
return {
|
|
294
|
+
module: moduleName,
|
|
295
|
+
// File name on disk and therefore every import path that points at it.
|
|
296
|
+
controller: base,
|
|
297
|
+
// Identifier the stubs export and call.
|
|
298
|
+
ServiceName: `${camelCase(base)}Service`,
|
|
299
|
+
ClassName: pascal(base),
|
|
300
|
+
name: leafName(moduleName),
|
|
301
|
+
tableName: plural(snakeCase(base)),
|
|
302
|
+
tableVariable: plural(camelCase(base))
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Generate a controller file for a module.
|
|
308
|
+
* When a matching service exists the controller delegates to it; otherwise it
|
|
309
|
+
* falls back to the standalone shape so the file never imports a missing module.
|
|
310
|
+
*/
|
|
311
|
+
async function controllerFile(moduleName, controllerName, openApi, withService) {
|
|
312
|
+
const controller = controllerName.trim().toLowerCase();
|
|
313
|
+
const controllerStub = withService
|
|
314
|
+
? openApi
|
|
315
|
+
? STUBS.controller.openapi
|
|
316
|
+
: STUBS.controller.plain
|
|
317
|
+
: STUBS.controller.standalone;
|
|
318
|
+
return await stub(controllerStub, moduleFileVars(moduleName, controller));
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** Generate a schema file for a module. */
|
|
322
|
+
async function schemaFile(moduleName, controllerName, openApi) {
|
|
323
|
+
const controller = controllerName.trim().toLowerCase();
|
|
324
|
+
const schemaStub = openApi ? STUBS.schema.openapi : STUBS.schema.plain;
|
|
325
|
+
return await stub(schemaStub, moduleFileVars(moduleName, controller));
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** Generate a service file for a module. withModel picks the Drizzle-backed variant. */
|
|
329
|
+
async function serviceFile(moduleName, controllerName, withModel) {
|
|
330
|
+
const controller = controllerName.trim().toLowerCase();
|
|
331
|
+
const serviceStub = withModel ? STUBS.service.named : STUBS.service.plain;
|
|
332
|
+
return await stub(serviceStub, moduleFileVars(moduleName, controller));
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** Build the full default scaffold for module:make. */
|
|
336
|
+
async function moduleFiles(moduleName, openApi) {
|
|
337
|
+
const name = leafName(moduleName);
|
|
338
|
+
const vars = moduleFileVars(moduleName, name);
|
|
339
|
+
const controllerStub = openApi ? STUBS.controller.openapi : STUBS.controller.plain;
|
|
340
|
+
return {
|
|
341
|
+
"facade.ts": await stub(STUBS.facade.named, vars),
|
|
342
|
+
[`controllers/${name}.ts`]: await stub(controllerStub, vars),
|
|
343
|
+
[`schemas/${name}.ts`]: await schemaFile(moduleName, name, openApi),
|
|
344
|
+
// module:make also writes database/models/<name>.ts, so the service can query it.
|
|
345
|
+
[`services/${name}.ts`]: await serviceFile(moduleName, name, true)
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Generate a wired route file. Only module:make uses this: it owns the whole
|
|
351
|
+
* module, so the controller and schema it just created are known to exist.
|
|
352
|
+
*/
|
|
353
|
+
async function routeTemplate(moduleName, controllerName = moduleName) {
|
|
354
|
+
const controller = leafName(controllerName);
|
|
355
|
+
const routeStub = openApiEnabled() ? STUBS.route.api : STUBS.route.plain;
|
|
356
|
+
return await stub(routeStub, {
|
|
357
|
+
module: moduleName,
|
|
358
|
+
controller,
|
|
359
|
+
ClassName: pascal(controller),
|
|
360
|
+
ModuleClass: moduleLabel(moduleName)
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Generate a barebone route file that wires to nothing. module:make-route uses
|
|
366
|
+
* this so the developer decides which controller and schema to attach.
|
|
367
|
+
*/
|
|
368
|
+
async function standaloneRouteTemplate(moduleName, routeName) {
|
|
369
|
+
const routeStub = openApiEnabled() ? STUBS.route.standaloneApi : STUBS.route.standalonePlain;
|
|
370
|
+
return await stub(routeStub, {
|
|
371
|
+
module: moduleName,
|
|
372
|
+
controller: routeName,
|
|
373
|
+
ClassName: pascal(routeName),
|
|
374
|
+
ModuleClass: moduleLabel(moduleName)
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** Generate a named model file for a module. */
|
|
379
|
+
async function namedModelTemplate(moduleName, name, dialect) {
|
|
380
|
+
const base = normalizeName(name);
|
|
381
|
+
return await stub(STUBS.model.named[dialect], {
|
|
382
|
+
module: moduleName,
|
|
383
|
+
name: base,
|
|
384
|
+
controller: base,
|
|
385
|
+
ClassName: pascal(base),
|
|
386
|
+
tableName: plural(snakeCase(base)),
|
|
387
|
+
tableVariable: plural(camelCase(base))
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** Generate a seeder file for a module model. */
|
|
392
|
+
async function namedSeederTemplate(moduleName, modelName, className) {
|
|
393
|
+
const base = normalizeName(modelName);
|
|
394
|
+
return await stub(STUBS.seeder.named, {
|
|
395
|
+
module: moduleName,
|
|
396
|
+
// Import path: the model file is named after the model, in kebab-case.
|
|
397
|
+
name: base,
|
|
398
|
+
controller: className,
|
|
399
|
+
ClassName: pascal(className),
|
|
400
|
+
tableName: plural(snakeCase(base)),
|
|
401
|
+
tableVariable: plural(camelCase(base))
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** Check if a file path exists on disk. */
|
|
406
|
+
async function pathExists(filePath) {
|
|
407
|
+
try {
|
|
408
|
+
await fs.access(filePath);
|
|
409
|
+
return true;
|
|
410
|
+
} catch {
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Modules scaffolded before 4.1.0 kept schemas and services inside controllers/
|
|
417
|
+
* and used suffixed filenames. Returns the first legacy path that still exists
|
|
418
|
+
* so lookups resolve for existing projects.
|
|
419
|
+
*/
|
|
420
|
+
function legacyPath(moduleRootPath, folder, baseName) {
|
|
421
|
+
if (folder === "schemas") {
|
|
422
|
+
return path.join(moduleRootPath, "controllers", `${baseName}.schema.ts`);
|
|
423
|
+
}
|
|
424
|
+
if (folder === "helpers") {
|
|
425
|
+
return path.join(moduleRootPath, "controllers", `${baseName}.helpers.ts`);
|
|
426
|
+
}
|
|
427
|
+
if (folder === "services") {
|
|
428
|
+
return path.join(moduleRootPath, "controllers", `${baseName}.service.ts`);
|
|
429
|
+
}
|
|
430
|
+
return path.join(moduleRootPath, "controllers", `${baseName}.controller.ts`);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/** Write a file with safety checks (dry-run support, force overwrite protection). */
|
|
434
|
+
async function writeFileSafe(filePath, content, flags = [], label = "File") {
|
|
435
|
+
const dryRun = hasFlag(flags, "--dry-run");
|
|
436
|
+
const force = hasFlag(flags, "--force") || hasFlag(flags, "--yes");
|
|
437
|
+
if (dryRun) return;
|
|
438
|
+
const exists = await pathExists(filePath);
|
|
439
|
+
if (exists && !force) {
|
|
440
|
+
throw new Error(`${label} already exists: ${path.relative(process.cwd(), filePath)}. Re-run with --force to overwrite.`);
|
|
441
|
+
}
|
|
442
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
443
|
+
await fs.writeFile(filePath, content);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Generate a complete module scaffold (facade, schema, service, controller, route, model, seeder).
|
|
448
|
+
* Pass `--path=<panel>` to nest the module under a panel folder, e.g.
|
|
449
|
+
* `module:make post --path=admin` writes to src/modules/admin/post/.
|
|
450
|
+
*/
|
|
451
|
+
export async function makeModule(rawName, flags = []) {
|
|
452
|
+
const result = await createModule(rawName, flags);
|
|
453
|
+
reportModule(result);
|
|
454
|
+
return result;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Create several modules in one run: `module:make post tag testme`.
|
|
459
|
+
*
|
|
460
|
+
* A name that cannot be created is reported and the batch continues, so one bad
|
|
461
|
+
* argument never costs you the modules that were fine. The caller decides the
|
|
462
|
+
* exit code from `failed`.
|
|
463
|
+
*/
|
|
464
|
+
export async function makeModules(rawNames, flags = []) {
|
|
465
|
+
const created = [];
|
|
466
|
+
const failed = [];
|
|
467
|
+
for (const rawName of rawNames) {
|
|
468
|
+
try {
|
|
469
|
+
created.push(await createModule(rawName, flags));
|
|
470
|
+
} catch (error) {
|
|
471
|
+
failed.push({ name: rawName, reason: error.message });
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
if (created.length) {
|
|
476
|
+
console.log(`Module${created.length > 1 ? "s" : ""} ready: ${created.map((item) => item.moduleName).join(", ")}`);
|
|
477
|
+
const panel = created[0].panel;
|
|
478
|
+
if (panel) {
|
|
479
|
+
console.log(` panel ${panel} -> ${created.map((item) => `/api/${item.moduleName}`).join(", ")}`);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
for (const item of failed) {
|
|
483
|
+
console.log(` skipped ${item.name}: ${item.reason}`);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
return { created, failed };
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** Print the per-module summary for a single-module run. */
|
|
490
|
+
function reportModule({ moduleName, panel, leaf }) {
|
|
491
|
+
console.log(`Module ready: ${moduleName}`);
|
|
492
|
+
if (panel) console.log(`Panel: ${panel} -> /api/${moduleName}`);
|
|
493
|
+
console.log(`Database files: models/${leaf}.ts, seeders/${leaf}.ts`);
|
|
494
|
+
console.log(`Route style: ${openApiEnabled() ? "openapi" : "plain"}`);
|
|
495
|
+
if (panel) {
|
|
496
|
+
console.log(`Fix unresolved imports with: bun maker alias`);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Scaffold one module.
|
|
502
|
+
*
|
|
503
|
+
* Refuses to touch a module folder that already has content. Without this guard
|
|
504
|
+
* writeFiles overwrites unconditionally, so re-running module:make on an
|
|
505
|
+
* existing module silently destroys the developer's edits to the controller,
|
|
506
|
+
* service, schema and route.
|
|
507
|
+
*/
|
|
508
|
+
async function createModule(rawName, flags = []) {
|
|
509
|
+
const leaf = assertName(rawName, "Module name");
|
|
510
|
+
const panel = parsePanelPath(flags);
|
|
511
|
+
const moduleName = panel ? `${panel}/${leaf}` : leaf;
|
|
512
|
+
const root = moduleRoot(moduleName);
|
|
513
|
+
const force = hasFlag(flags, "--force") || hasFlag(flags, "--yes");
|
|
514
|
+
if (!force && (await dirHasFiles(root))) {
|
|
515
|
+
throw new Error(`Module already exists: src/modules/${moduleName}. Re-run with --force to overwrite its files.`);
|
|
516
|
+
}
|
|
517
|
+
const openApi = openApiEnabled();
|
|
518
|
+
const files = await moduleFiles(moduleName, openApi);
|
|
519
|
+
const route = await routeTemplate(moduleName);
|
|
520
|
+
const dialect = detectDialect();
|
|
521
|
+
const model = await namedModelTemplate(moduleName, leaf, dialect);
|
|
522
|
+
const seeder = await namedSeederTemplate(moduleName, leaf, leaf);
|
|
523
|
+
await writeFiles(root, { ...files, "routes/index.ts": route });
|
|
524
|
+
await fs.mkdir(path.join(root, "database", "models"), { recursive: true });
|
|
525
|
+
await fs.mkdir(path.join(root, "database", "seeders"), { recursive: true });
|
|
526
|
+
await fs.writeFile(path.join(root, "database", "models", `${leaf}.ts`), model);
|
|
527
|
+
await fs.writeFile(path.join(root, "database", "seeders", `${leaf}.ts`), seeder);
|
|
528
|
+
return { moduleName, panel, leaf };
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** Generate a route file for an existing module. */
|
|
532
|
+
export async function makeRoute(rawModule, rawControllerOrFlag, extraFlags = []) {
|
|
533
|
+
const { moduleName, leaf } = resolveModuleRef(rawModule, rawControllerOrFlag, extraFlags);
|
|
534
|
+
const root = await assertModuleExists(moduleName);
|
|
535
|
+
const flags = [];
|
|
536
|
+
let routeName = leaf;
|
|
537
|
+
let controllerExplicit = false;
|
|
538
|
+
if (rawControllerOrFlag) {
|
|
539
|
+
if (rawControllerOrFlag.startsWith("--")) flags.push(rawControllerOrFlag);
|
|
540
|
+
else {
|
|
541
|
+
routeName = assertName(rawControllerOrFlag, "Route name");
|
|
542
|
+
controllerExplicit = true;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
flags.push(...extraFlags);
|
|
546
|
+
const route = await standaloneRouteTemplate(moduleName, routeName);
|
|
547
|
+
const routeFile = controllerExplicit ? `${routeName}.ts` : "index.ts";
|
|
548
|
+
const routePath = path.join(root, `routes/${routeFile}`);
|
|
549
|
+
const dryRun = hasFlag(flags, "--dry-run");
|
|
550
|
+
const force = hasFlag(flags, "--force") || hasFlag(flags, "--yes");
|
|
551
|
+
if (dryRun) return;
|
|
552
|
+
let exists = false;
|
|
553
|
+
try {
|
|
554
|
+
await fs.access(routePath);
|
|
555
|
+
exists = true;
|
|
556
|
+
} catch { }
|
|
557
|
+
if (exists && !force)
|
|
558
|
+
throw new Error(`Route file already exists: ${path.relative(process.cwd(), routePath)}. Re-run with --force to overwrite.`);
|
|
559
|
+
await fs.mkdir(path.dirname(routePath), { recursive: true });
|
|
560
|
+
await fs.writeFile(routePath, route);
|
|
561
|
+
console.log(`Route ready: ${path.relative(process.cwd(), routePath)}`);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Generate the notification HTTP surface, plus the shared store the first time.
|
|
566
|
+
*
|
|
567
|
+
* The controller/schema/routes are always written: they are what lets a project
|
|
568
|
+
* expose `/api/notification/*` for a panel. The model, provider and mail job are
|
|
569
|
+
* written only when no module already ships the store, because the store is a
|
|
570
|
+
* singleton — a second copy would create a duplicate `notifications` table and
|
|
571
|
+
* register a second provider for the same contract.
|
|
572
|
+
*/
|
|
573
|
+
export async function makeNotificationModule(rawName = "notification", flags = []) {
|
|
574
|
+
const { moduleName, leaf } = resolveModuleRef(rawName, flags);
|
|
575
|
+
const root = moduleRoot(moduleName);
|
|
576
|
+
const force = hasFlag(flags, "--force") || hasFlag(flags, "--yes");
|
|
577
|
+
if (!force && (await dirHasFiles(root))) {
|
|
578
|
+
throw new Error(`Module already exists: src/modules/${moduleName}. Re-run with --force to overwrite its files.`);
|
|
579
|
+
}
|
|
580
|
+
const openApi = openApiEnabled();
|
|
581
|
+
const notifiable = parseNotifiable(flags);
|
|
582
|
+
const vars = { module: moduleName, leaf, notifiable };
|
|
583
|
+
|
|
584
|
+
const files = {
|
|
585
|
+
[`controllers/${leaf}.ts`]: await stub(STUBS.notification.controller, vars),
|
|
586
|
+
[`schemas/${leaf}.ts`]: await stub(openApi ? STUBS.notification.schema.openapi : STUBS.notification.schema.plain, vars),
|
|
587
|
+
"routes/index.ts": await stub(openApi ? STUBS.notification.routeApi : STUBS.notification.routePlain, vars)
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
const existingStore = await findNotificationStore();
|
|
591
|
+
const ownsStore = existingStore ? path.resolve(existingStore).startsWith(root + path.sep) : false;
|
|
592
|
+
const writesStore = !existingStore || (force && ownsStore);
|
|
593
|
+
|
|
594
|
+
if (writesStore) {
|
|
595
|
+
files["database/models/notification.ts"] = await stub(STUBS.notification.model, vars);
|
|
596
|
+
files["providers/notification.ts"] = await stub(STUBS.notification.provider, vars);
|
|
597
|
+
files["jobs/mail.ts"] = await stub(STUBS.notification.job, vars);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
await writeFiles(root, files);
|
|
601
|
+
|
|
602
|
+
console.log(`Notification module ready: ${moduleName}`);
|
|
603
|
+
if (writesStore) {
|
|
604
|
+
console.log("Notification store included: model, provider, mail job");
|
|
605
|
+
const panelFlag = panelOf(moduleName) ? ` --path=${panelOf(moduleName)}` : "";
|
|
606
|
+
console.log(`Next: bun maker module:migrate ${leaf}${panelFlag}`);
|
|
607
|
+
} else {
|
|
608
|
+
console.log(`Notification store reused: ${path.relative(process.cwd(), existingStore)}`);
|
|
609
|
+
}
|
|
610
|
+
if (panelOf(moduleName)) console.log(`Panel: ${panelOf(moduleName)} -> /api/${moduleName}`);
|
|
611
|
+
console.log(`Notifiable type: ${notifiable}`);
|
|
612
|
+
console.log(`Route style: ${openApi ? "openapi" : "plain"}`);
|
|
613
|
+
console.log("See docs (packages/docs/guide/notification.md) for usage and UI integration.");
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/** Soft-delete a module by moving it to storage trash. */
|
|
617
|
+
export async function deleteModule(rawName, flags = []) {
|
|
618
|
+
const { moduleName: name, leaf } = resolveModuleRef(rawName, flags);
|
|
619
|
+
if (leaf === "notification") {
|
|
620
|
+
console.log("Use `bun maker module:delete-notification` to remove the notification module (removes UI files too).");
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
const modulesRoot = path.resolve(process.cwd(), "src/modules");
|
|
624
|
+
const modulePath = path.resolve(modulesRoot, name);
|
|
625
|
+
const relative = path.relative(modulesRoot, modulePath);
|
|
626
|
+
if (relative.startsWith("..") || path.isAbsolute(relative)) throw new Error("Unsafe module path.");
|
|
627
|
+
let stats;
|
|
628
|
+
try {
|
|
629
|
+
stats = await fs.stat(modulePath);
|
|
630
|
+
} catch {
|
|
631
|
+
throw new Error(`Module not found: ${name}`);
|
|
632
|
+
}
|
|
633
|
+
if (!stats.isDirectory()) throw new Error(`Module path is not a directory: src/modules/${name}`);
|
|
634
|
+
const trashRoot = path.resolve(process.cwd(), "src/storage/trash/modules");
|
|
635
|
+
const stamp = new Date().toISOString().replace(/[.:]/g, "-");
|
|
636
|
+
// Trash keeps the panel path so admin/post stays distinguishable from admin/user.
|
|
637
|
+
const trashPath = path.join(trashRoot, `${name.replaceAll("/", "-")}-${stamp}`);
|
|
638
|
+
const dryRun = hasFlag(flags, "--dry-run");
|
|
639
|
+
const confirmed = hasFlag(flags, "--yes") || hasFlag(flags, "--force");
|
|
640
|
+
if (dryRun) return;
|
|
641
|
+
if (!confirmed) throw new Error(`Refusing to delete module without confirmation. Re-run with: bun maker module:delete ${name} --yes`);
|
|
642
|
+
await fs.mkdir(trashRoot, { recursive: true });
|
|
643
|
+
try {
|
|
644
|
+
await fs.rename(modulePath, trashPath);
|
|
645
|
+
} catch (error) {
|
|
646
|
+
if (error?.code === "EPERM" || error?.code === "EXDEV") {
|
|
647
|
+
await fs.cp(modulePath, trashPath, { recursive: true });
|
|
648
|
+
await fs.rm(modulePath, { recursive: true, force: true });
|
|
649
|
+
} else {
|
|
650
|
+
throw error;
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
console.log(`Moved module to trash: ${path.relative(process.cwd(), trashPath)}`);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/** Remove a notification module (backend only — moves to trash). */
|
|
657
|
+
export async function deleteNotificationModule(rawName = "notification", flags = []) {
|
|
658
|
+
const { moduleName, leaf } = resolveModuleRef(rawName, flags);
|
|
659
|
+
const dryRun = hasFlag(flags, "--dry-run");
|
|
660
|
+
const confirmed = hasFlag(flags, "--yes") || hasFlag(flags, "--force");
|
|
661
|
+
|
|
662
|
+
const modulePath = moduleRoot(moduleName);
|
|
663
|
+
|
|
664
|
+
if (dryRun) {
|
|
665
|
+
const exists = await fs
|
|
666
|
+
.stat(modulePath)
|
|
667
|
+
.then(() => true)
|
|
668
|
+
.catch(() => false);
|
|
669
|
+
if (exists) console.log(`Would delete: ${path.relative(process.cwd(), modulePath)}`);
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
if (!confirmed) {
|
|
674
|
+
throw new Error(
|
|
675
|
+
`Refusing to delete notification module without confirmation. Re-run with: bun maker module:delete-notification ${leaf} --yes${panelOf(moduleName) ? ` --path=${panelOf(moduleName)}` : ""}`
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
const exists = await fs
|
|
680
|
+
.stat(modulePath)
|
|
681
|
+
.then(() => true)
|
|
682
|
+
.catch(() => false);
|
|
683
|
+
if (!exists) {
|
|
684
|
+
console.log("Notification module not found — nothing to delete.");
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
const trashRoot = path.resolve(process.cwd(), "src/storage/trash/modules");
|
|
689
|
+
const stamp = new Date().toISOString().replace(/[.:]/g, "-");
|
|
690
|
+
await fs.mkdir(trashRoot, { recursive: true });
|
|
691
|
+
|
|
692
|
+
const relPath = path.relative(process.cwd(), modulePath);
|
|
693
|
+
const trashPath = path.join(trashRoot, `${relPath.replace(/[/\\]/g, "-")}-${stamp}`);
|
|
694
|
+
|
|
695
|
+
try {
|
|
696
|
+
await fs.rename(modulePath, trashPath);
|
|
697
|
+
} catch (error) {
|
|
698
|
+
if (error?.code === "EPERM" || error?.code === "EXDEV") {
|
|
699
|
+
await fs.cp(modulePath, trashPath, { recursive: true });
|
|
700
|
+
await fs.rm(modulePath, { recursive: true, force: true });
|
|
701
|
+
} else {
|
|
702
|
+
throw error;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
console.log(`Moved to trash: ${relPath}`);
|
|
706
|
+
console.log("Notification module deleted.");
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
/** Permanently remove entries from module trash storage. */
|
|
710
|
+
export async function cleanModuleTrash(rawName, flags = []) {
|
|
711
|
+
const trashRoot = path.resolve(process.cwd(), "src/storage/trash/modules");
|
|
712
|
+
const dryRun = hasFlag(flags, "--dry-run");
|
|
713
|
+
const confirmed = hasFlag(flags, "--yes") || hasFlag(flags, "--force");
|
|
714
|
+
let entries = [];
|
|
715
|
+
try {
|
|
716
|
+
entries = await fs.readdir(trashRoot, { withFileTypes: true });
|
|
717
|
+
} catch {
|
|
718
|
+
if (dryRun) return;
|
|
719
|
+
console.log("No trash entries found");
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
const targetName = rawName && !rawName.startsWith("--") ? assertName(rawName, "Module name") : "";
|
|
723
|
+
const matches = entries.map((e) => e.name).filter((name) => !targetName || name === targetName || name.startsWith(`${targetName}-`));
|
|
724
|
+
if (!matches.length) {
|
|
725
|
+
console.log(targetName ? `No trash entries found for module '${targetName}'` : "No trash entries found");
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
if (dryRun) return;
|
|
729
|
+
if (!confirmed)
|
|
730
|
+
throw new Error(
|
|
731
|
+
targetName
|
|
732
|
+
? `Refusing to clean trash for '${targetName}' without confirmation. Re-run with: bun maker module:trash:clean ${targetName} --yes`
|
|
733
|
+
: "Refusing to clean all trash without confirmation. Re-run with: bun maker module:trash:clean --yes"
|
|
734
|
+
);
|
|
735
|
+
for (const name of matches) await fs.rm(path.join(trashRoot, name), { recursive: true, force: true });
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
* Split "<module> <nameOrFlag> [extraFlags]" into a validated name plus flags.
|
|
740
|
+
* Shared by the per-file commands so they all accept --force/--dry-run the same way.
|
|
741
|
+
*/
|
|
742
|
+
function parseNameArg(rawNameOrFlag, extraFlags, kindLabel, fallback) {
|
|
743
|
+
const flags = [];
|
|
744
|
+
let name = fallback;
|
|
745
|
+
if (rawNameOrFlag) {
|
|
746
|
+
if (rawNameOrFlag.startsWith("--")) flags.push(rawNameOrFlag);
|
|
747
|
+
else name = assertName(rawNameOrFlag, kindLabel);
|
|
748
|
+
}
|
|
749
|
+
flags.push(...extraFlags);
|
|
750
|
+
return { name, flags };
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/** Generate a controller for an existing module. */
|
|
754
|
+
export async function makeController(rawModule, rawControllerOrFlag, extraFlags = []) {
|
|
755
|
+
const { moduleName, leaf } = resolveModuleRef(rawModule, rawControllerOrFlag, extraFlags);
|
|
756
|
+
const { name, flags } = parseNameArg(rawControllerOrFlag, extraFlags, "Controller name", leaf);
|
|
757
|
+
const root = await assertModuleExists(moduleName);
|
|
758
|
+
const lower = name.toLowerCase();
|
|
759
|
+
const hasService = (await pathExists(path.join(root, "services", `${lower}.ts`))) || (await pathExists(legacyPath(root, "services", lower)));
|
|
760
|
+
const content = await controllerFile(moduleName, name, openApiEnabled(), hasService);
|
|
761
|
+
await writeFileSafe(path.join(root, `controllers/${lower}.ts`), content, flags, "Controller file");
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/** Generate a schema file for an existing module. */
|
|
765
|
+
export async function makeSchema(rawModule, rawNameOrFlag, extraFlags = []) {
|
|
766
|
+
const { moduleName, leaf } = resolveModuleRef(rawModule, rawNameOrFlag, extraFlags);
|
|
767
|
+
const { name, flags } = parseNameArg(rawNameOrFlag, extraFlags, "Schema name", leaf);
|
|
768
|
+
const root = await assertModuleExists(moduleName);
|
|
769
|
+
const content = await schemaFile(moduleName, name, openApiEnabled());
|
|
770
|
+
await writeFileSafe(path.join(root, `schemas/${name.toLowerCase()}.ts`), content, flags, "Schema file");
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/**
|
|
774
|
+
* Generate a service file for an existing module.
|
|
775
|
+
* Pass --with-model when the module has database/models/<name>.ts so the
|
|
776
|
+
* service queries the table instead of returning the empty defaults.
|
|
777
|
+
*/
|
|
778
|
+
export async function makeService(rawModule, rawNameOrFlag, extraFlags = []) {
|
|
779
|
+
const { moduleName, leaf } = resolveModuleRef(rawModule, rawNameOrFlag, extraFlags);
|
|
780
|
+
const { name, flags } = parseNameArg(rawNameOrFlag, extraFlags, "Service name", leaf);
|
|
781
|
+
const root = await assertModuleExists(moduleName);
|
|
782
|
+
const lower = name.toLowerCase();
|
|
783
|
+
const withModel = hasFlag(flags, "--with-model") || (await pathExists(path.join(root, "database", "models", `${lower}.ts`)));
|
|
784
|
+
const content = await serviceFile(moduleName, name, withModel);
|
|
785
|
+
await writeFileSafe(path.join(root, `services/${lower}.ts`), content, flags, "Service file");
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/** Generate a helper file for an existing module. */
|
|
789
|
+
export async function makeHelper(rawModule, rawNameOrFlag, extraFlags = []) {
|
|
790
|
+
const { moduleName, leaf } = resolveModuleRef(rawModule, rawNameOrFlag, extraFlags);
|
|
791
|
+
const { name, flags } = parseNameArg(rawNameOrFlag, extraFlags, "Helper name", leaf);
|
|
792
|
+
const root = await assertModuleExists(moduleName);
|
|
793
|
+
const content = await stub(STUBS.helper.named, { module: moduleName, name, Name: pascal(name) });
|
|
794
|
+
await writeFileSafe(path.join(root, `helpers/${name}.ts`), content, flags, "Helper file");
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/** Generate a module-local middleware file for an existing module. */
|
|
798
|
+
export async function makeLocalMiddleware(rawModule, rawNameOrFlag, extraFlags = []) {
|
|
799
|
+
const { moduleName, leaf } = resolveModuleRef(rawModule, rawNameOrFlag, extraFlags);
|
|
800
|
+
const { name, flags } = parseNameArg(rawNameOrFlag, extraFlags, "Middleware name", leaf);
|
|
801
|
+
const root = await assertModuleExists(moduleName);
|
|
802
|
+
const content = await stub(STUBS.middleware.local, {
|
|
803
|
+
module: moduleName,
|
|
804
|
+
name,
|
|
805
|
+
camel: camelCase(name),
|
|
806
|
+
Name: pascal(name)
|
|
807
|
+
});
|
|
808
|
+
await writeFileSafe(path.join(root, `middlewares/${name}.ts`), content, flags, "Middleware file");
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* Generate a provider file for an existing module.
|
|
813
|
+
*
|
|
814
|
+
* The file lands in the module's providers/ folder, which the framework
|
|
815
|
+
* imports at boot just like src/providers/. That is what lets a module offer
|
|
816
|
+
* an implementation to the whole application without the application knowing
|
|
817
|
+
* anything about the module's internals.
|
|
818
|
+
*/
|
|
819
|
+
export async function makeModuleProvider(rawModule, rawNameOrFlag, extraFlags = []) {
|
|
820
|
+
const { moduleName, leaf } = resolveModuleRef(rawModule, rawNameOrFlag, extraFlags);
|
|
821
|
+
const { name, flags } = parseNameArg(rawNameOrFlag, extraFlags, "Provider name", leaf);
|
|
822
|
+
const root = await assertModuleExists(moduleName);
|
|
823
|
+
const filePath = path.join(root, `providers/${name}.ts`);
|
|
824
|
+
if (hasFlag(flags, "--dry-run")) {
|
|
825
|
+
console.log(`Would create provider file: ${path.relative(process.cwd(), filePath)}`);
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
const content = await stub(STUBS.provider.module, {
|
|
829
|
+
module: moduleName,
|
|
830
|
+
name,
|
|
831
|
+
camel: camelCase(name)
|
|
832
|
+
});
|
|
833
|
+
await writeFileSafe(filePath, content, flags, "Provider file");
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
/** Generate a types file for an existing module. */
|
|
837
|
+
export async function makeType(rawModule, rawNameOrFlag, extraFlags = []) {
|
|
838
|
+
const { moduleName, leaf } = resolveModuleRef(rawModule, rawNameOrFlag, extraFlags);
|
|
839
|
+
const { name, flags } = parseNameArg(rawNameOrFlag, extraFlags, "Type name", leaf);
|
|
840
|
+
const root = await assertModuleExists(moduleName);
|
|
841
|
+
const content = await stub(STUBS.type.named, { module: moduleName, name, Name: pascal(name) });
|
|
842
|
+
await writeFileSafe(path.join(root, `types/${name}.ts`), content, flags, "Type file");
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/** Generate a model file for an existing module. */
|
|
846
|
+
export async function makeModel(rawModule, rawNameOrFlag, extraFlags = []) {
|
|
847
|
+
const { moduleName, leaf } = resolveModuleRef(rawModule, rawNameOrFlag, extraFlags);
|
|
848
|
+
const flags = [];
|
|
849
|
+
let name = leaf;
|
|
850
|
+
if (rawNameOrFlag) {
|
|
851
|
+
if (rawNameOrFlag.startsWith("--")) flags.push(rawNameOrFlag);
|
|
852
|
+
else name = assertName(rawNameOrFlag, "Model name");
|
|
853
|
+
}
|
|
854
|
+
flags.push(...extraFlags);
|
|
855
|
+
const dialect = detectDialect();
|
|
856
|
+
const root = await assertModuleExists(moduleName);
|
|
857
|
+
const modelPath = path.join(root, `database/models/${name}.ts`);
|
|
858
|
+
await writeFileSafe(modelPath, await namedModelTemplate(moduleName, name, dialect), flags, "Model file");
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
/**
|
|
862
|
+
* Generate a seeder file.
|
|
863
|
+
*
|
|
864
|
+
* A seeder does not have to own the table it writes to, and does not have to
|
|
865
|
+
* write to only one table, so a missing model is not fatal. When the model is
|
|
866
|
+
* there we import it and pre-wire the insert; when it is not we emit the
|
|
867
|
+
* standalone shape and let the developer choose what to write. That is the
|
|
868
|
+
* same trade the controller makes when there is no service, and the service
|
|
869
|
+
* makes when there is no model. Seeder was the only generator that refused.
|
|
870
|
+
*/
|
|
871
|
+
export async function makeSeeder(rawModule, rawNameOrFlag, extraFlags = []) {
|
|
872
|
+
const { moduleName, leaf } = resolveModuleRef(rawModule, rawNameOrFlag, extraFlags);
|
|
873
|
+
const flags = [];
|
|
874
|
+
let name = leaf;
|
|
875
|
+
if (rawNameOrFlag) {
|
|
876
|
+
if (rawNameOrFlag.startsWith("--")) flags.push(rawNameOrFlag);
|
|
877
|
+
else name = assertName(rawNameOrFlag, "Seeder name");
|
|
878
|
+
}
|
|
879
|
+
flags.push(...extraFlags);
|
|
880
|
+
const root = await assertModuleExists(moduleName);
|
|
881
|
+
|
|
882
|
+
// The fallback probes the leaf, not moduleName: for a panel module moduleName
|
|
883
|
+
// is "admin/testme", so using it would look for database/models/admin/testme.ts
|
|
884
|
+
// and never match.
|
|
885
|
+
const modelName = await existingModelName(root, name, leaf);
|
|
886
|
+
let seeder;
|
|
887
|
+
if (modelName) {
|
|
888
|
+
seeder = await namedSeederTemplate(moduleName, modelName, name);
|
|
889
|
+
if (modelName !== name) {
|
|
890
|
+
console.log(`Model '${name}' not found, using module model '${modelName}' for seeder '${name}'.`);
|
|
891
|
+
}
|
|
892
|
+
} else {
|
|
893
|
+
seeder = await stub(STUBS.seeder.standalone, {
|
|
894
|
+
module: moduleName,
|
|
895
|
+
name,
|
|
896
|
+
ClassName: pascal(name),
|
|
897
|
+
// The stub shows this as the conventional table name, so derive it the
|
|
898
|
+
// same way the bound stub does - it is what a model of this name exports.
|
|
899
|
+
tableVariable: plural(camelCase(name))
|
|
900
|
+
});
|
|
901
|
+
const available = await listModels(root);
|
|
902
|
+
console.log(`No model '${name}' in module ${moduleName}, so this is a standalone seeder: it imports no model and writes nothing until you fill it in.`);
|
|
903
|
+
console.log(
|
|
904
|
+
available.length
|
|
905
|
+
? ` Models in this module: ${available.join(", ")}. Run module:make-seeder ${moduleName} <one of these> for a seeder with the model already imported.`
|
|
906
|
+
: ` This module has no models yet. Create one with: bun maker module:make-model ${moduleName} ${name}`
|
|
907
|
+
);
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
await writeFileSafe(path.join(root, `database/seeders/${name}.ts`), seeder, flags, "Seeder file");
|
|
911
|
+
|
|
912
|
+
console.log(`Seeder ready: ${moduleName}/${name}`);
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
/** The model a seeder binds to: the requested one, else the module's own. Null when neither exists. */
|
|
916
|
+
async function existingModelName(root, name, leaf) {
|
|
917
|
+
for (const candidate of [name, leaf]) {
|
|
918
|
+
if (await pathExists(path.join(root, "database", "models", `${candidate}.ts`))) return candidate;
|
|
919
|
+
}
|
|
920
|
+
return null;
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
/** Model file names in a module, without the extension. Empty when there are none. */
|
|
924
|
+
async function listModels(root) {
|
|
925
|
+
try {
|
|
926
|
+
const entries = await fs.readdir(path.join(root, "database", "models"));
|
|
927
|
+
return entries.filter((f) => f.endsWith(".ts")).map((f) => f.slice(0, -3));
|
|
928
|
+
} catch {
|
|
929
|
+
return [];
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
/** Generate a job file for an existing module. */
|
|
934
|
+
export async function makeJob(rawModule, rawNameOrFlag, extraFlags = []) {
|
|
935
|
+
const { moduleName, leaf } = resolveModuleRef(rawModule, rawNameOrFlag, extraFlags);
|
|
936
|
+
const flags = [];
|
|
937
|
+
let name = leaf;
|
|
938
|
+
if (rawNameOrFlag) {
|
|
939
|
+
if (rawNameOrFlag.startsWith("--")) flags.push(rawNameOrFlag);
|
|
940
|
+
else name = assertName(rawNameOrFlag, "Job name");
|
|
941
|
+
}
|
|
942
|
+
flags.push(...extraFlags);
|
|
943
|
+
const root = await assertModuleExists(moduleName);
|
|
944
|
+
await writeFileSafe(path.join(root, `jobs/${name}.ts`), await stub(STUBS.job.named, { module: moduleName, name }), flags, "Job file");
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
/** Generate a schedule/console file for an existing module. */
|
|
948
|
+
export async function makeSchedule(rawModule, rawNameOrFlag, extraFlags = []) {
|
|
949
|
+
const { moduleName, leaf } = resolveModuleRef(rawModule, rawNameOrFlag, extraFlags);
|
|
950
|
+
const flags = [];
|
|
951
|
+
let name = leaf;
|
|
952
|
+
if (rawNameOrFlag) {
|
|
953
|
+
if (rawNameOrFlag.startsWith("--")) flags.push(rawNameOrFlag);
|
|
954
|
+
else name = assertName(rawNameOrFlag, "Schedule name");
|
|
955
|
+
}
|
|
956
|
+
flags.push(...extraFlags);
|
|
957
|
+
const root = await assertModuleExists(moduleName);
|
|
958
|
+
await writeFileSafe(
|
|
959
|
+
path.join(root, `console/${name}.ts`),
|
|
960
|
+
await stub(STUBS.schedule.named, { module: moduleName, name }),
|
|
961
|
+
flags,
|
|
962
|
+
"Schedule file"
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/** List all discovered modules. */
|
|
967
|
+
export async function listModules() {
|
|
968
|
+
const modulesRoot = path.resolve(process.cwd(), "src/modules");
|
|
969
|
+
try {
|
|
970
|
+
const entries = await fs.readdir(modulesRoot, { withFileTypes: true });
|
|
971
|
+
const modules = entries
|
|
972
|
+
.filter((entry) => entry.isDirectory())
|
|
973
|
+
.map((entry) => entry.name)
|
|
974
|
+
.sort();
|
|
975
|
+
if (!modules.length) {
|
|
976
|
+
console.log("No modules found.");
|
|
977
|
+
return;
|
|
978
|
+
}
|
|
979
|
+
console.log("Modules:");
|
|
980
|
+
for (const module of modules) console.log(` - ${module}`);
|
|
981
|
+
} catch {
|
|
982
|
+
console.log("No modules found.");
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
/** Check that a module has at least one seeder file. */
|
|
987
|
+
export async function assertModuleHasSeeders(rawModule, flags = []) {
|
|
988
|
+
const { moduleName } = resolveModuleRef(rawModule, flags);
|
|
989
|
+
await assertModuleExists(moduleName);
|
|
990
|
+
const files = await glob(`${moduleName}/database/seeders/*.{ts,js}`, {
|
|
991
|
+
cwd: path.resolve(process.cwd(), "src/modules"),
|
|
992
|
+
nodir: true,
|
|
993
|
+
windowsPathsNoEscape: true
|
|
994
|
+
});
|
|
995
|
+
if (!files.length) throw new Error(`No seeders found for module '${moduleName}'.`);
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
/** Generate src/database/schema.ts by aggregating all module model exports. */
|
|
999
|
+
export async function generateSchema(options = {}) {
|
|
1000
|
+
const backendSrc = path.resolve(process.cwd(), "src");
|
|
1001
|
+
const files = await glob("modules/**/database/models/*.{ts,js}", {
|
|
1002
|
+
cwd: backendSrc,
|
|
1003
|
+
nodir: true,
|
|
1004
|
+
windowsPathsNoEscape: true
|
|
1005
|
+
});
|
|
1006
|
+
const exports = [];
|
|
1007
|
+
for (const file of files.sort()) {
|
|
1008
|
+
const absoluteFile = path.join(backendSrc, file);
|
|
1009
|
+
const outputDir = path.join(backendSrc, "database");
|
|
1010
|
+
const relativePath = path
|
|
1011
|
+
.relative(outputDir, absoluteFile)
|
|
1012
|
+
.replace(/\\/g, "/")
|
|
1013
|
+
.replace(/\.(ts|js)$/, ".js");
|
|
1014
|
+
const importPath = relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
|
|
1015
|
+
exports.push(`export * from "${importPath}";`);
|
|
1016
|
+
}
|
|
1017
|
+
const output = path.join(backendSrc, "database/schema.ts");
|
|
1018
|
+
await fs.mkdir(path.dirname(output), { recursive: true });
|
|
1019
|
+
await fs.writeFile(output, `${exports.join("\n")}\n`);
|
|
1020
|
+
if (!options.silent) console.log(`Generated database schema with ${files.length} model file(s)`);
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/**
|
|
1024
|
+
* Generate and run migrations that include a module.
|
|
1025
|
+
*
|
|
1026
|
+
* Drizzle diffs the schema against the last snapshot, so generating from a
|
|
1027
|
+
* module-only schema would emit `DROP TABLE` for every other module (auth
|
|
1028
|
+
* included). The full schema is used instead: it adds the module's tables and
|
|
1029
|
+
* keeps the rest of the database intact.
|
|
1030
|
+
*/
|
|
1031
|
+
export async function runModuleMigrate(rawModuleName, rawArgs = []) {
|
|
1032
|
+
const { moduleName } = resolveModuleRef(rawModuleName, rawArgs);
|
|
1033
|
+
await assertModuleExists(moduleName);
|
|
1034
|
+
const backendSrc = path.resolve(process.cwd(), "src");
|
|
1035
|
+
const files = await glob(`modules/${moduleName}/database/models/*.{ts,js}`, {
|
|
1036
|
+
cwd: backendSrc,
|
|
1037
|
+
nodir: true,
|
|
1038
|
+
windowsPathsNoEscape: true
|
|
1039
|
+
});
|
|
1040
|
+
if (!files.length) throw new Error(`No model files found for module '${moduleName}'.`);
|
|
1041
|
+
await syncMigrationDialect();
|
|
1042
|
+
await ensureMigrationMeta();
|
|
1043
|
+
await ensureDatabaseDirectory();
|
|
1044
|
+
await generateSchema();
|
|
1045
|
+
await runNodeScript(packageScript("drizzle-kit", "bin.cjs"), await drizzleGenerateArgs());
|
|
1046
|
+
await runNodeScript(packageScript("drizzle-kit", "bin.cjs"), ["migrate"]);
|
|
1047
|
+
console.log(`Module migration complete: ${moduleName} (${files.length} model file(s))`);
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
/** Generate a unit test file for a module. */
|
|
1051
|
+
export async function makeTest(rawModule, rawNameOrFlag, extraFlags = []) {
|
|
1052
|
+
const { moduleName, leaf } = resolveModuleRef(rawModule, rawNameOrFlag, extraFlags);
|
|
1053
|
+
const flags = [];
|
|
1054
|
+
let name = leaf;
|
|
1055
|
+
if (rawNameOrFlag) {
|
|
1056
|
+
if (rawNameOrFlag.startsWith("--")) flags.push(rawNameOrFlag);
|
|
1057
|
+
else name = assertName(rawNameOrFlag, "Test name");
|
|
1058
|
+
}
|
|
1059
|
+
flags.push(...extraFlags);
|
|
1060
|
+
const root = await assertModuleExists(moduleName);
|
|
1061
|
+
const testDir = path.join(root, "__tests__");
|
|
1062
|
+
await fs.mkdir(testDir, { recursive: true });
|
|
1063
|
+
const testPath = path.join(testDir, `${name}.test.ts`);
|
|
1064
|
+
await writeFileSafe(testPath, await stub(STUBS.test.unit, { module: moduleName, name }), flags, "Test file");
|
|
1065
|
+
console.log(`Test ready: ${path.relative(process.cwd(), testPath)}`);
|
|
1066
|
+
}
|