@meghjs/cli 0.5.3 → 0.5.4
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/dist/generators/auth-files.d.ts +37 -0
- package/dist/generators/auth-files.js +198 -0
- package/dist/generators/auth.d.ts +26 -0
- package/dist/generators/auth.js +542 -0
- package/dist/generators/make.d.ts +8 -0
- package/dist/generators/make.js +12 -0
- package/dist/generators/migration-schema.d.ts +5 -0
- package/dist/generators/migration-schema.js +17 -0
- package/dist/generators/scaffold.d.ts +76 -0
- package/dist/generators/scaffold.js +353 -0
- package/dist/program.js +125 -3
- package/dist/templates/cascade.d.ts +37 -0
- package/dist/templates/cascade.js +87 -0
- package/dist/templates/catalog/common.d.ts +1 -0
- package/dist/templates/catalog/common.js +63 -0
- package/dist/templates/catalog/construction.d.ts +1 -0
- package/dist/templates/catalog/construction.js +138 -0
- package/dist/templates/catalog/crm.d.ts +1 -0
- package/dist/templates/catalog/crm.js +81 -0
- package/dist/templates/catalog/ecommerce.d.ts +1 -0
- package/dist/templates/catalog/ecommerce.js +143 -0
- package/dist/templates/catalog/education.d.ts +1 -0
- package/dist/templates/catalog/education.js +80 -0
- package/dist/templates/catalog/hr.d.ts +1 -0
- package/dist/templates/catalog/hr.js +83 -0
- package/dist/templates/catalog/logistics.d.ts +1 -0
- package/dist/templates/catalog/logistics.js +60 -0
- package/dist/templates/catalog/marketing.d.ts +1 -0
- package/dist/templates/catalog/marketing.js +104 -0
- package/dist/templates/catalog/medical.d.ts +1 -0
- package/dist/templates/catalog/medical.js +147 -0
- package/dist/templates/catalog/pos.d.ts +1 -0
- package/dist/templates/catalog/pos.js +110 -0
- package/dist/templates/catalog/real-estate.d.ts +1 -0
- package/dist/templates/catalog/real-estate.js +171 -0
- package/dist/templates/fields.d.ts +64 -0
- package/dist/templates/fields.js +331 -0
- package/dist/templates/index.d.ts +27 -0
- package/dist/templates/index.js +175 -0
- package/dist/templates/migration.d.ts +7 -0
- package/dist/templates/migration.js +80 -0
- package/dist/templates/template.d.ts +37 -0
- package/dist/templates/template.js +36 -0
- package/dist/utils/dialect.d.ts +8 -0
- package/dist/utils/dialect.js +57 -0
- package/package.json +17 -5
- package/readme.md +26 -1
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type FieldDef } from "./fields";
|
|
2
|
+
/** A table a model is scaffolded from: its columns, timestamps and keys. */
|
|
3
|
+
export interface ModelTemplate {
|
|
4
|
+
/** e.g. `ecommerce`. */
|
|
5
|
+
domain: string;
|
|
6
|
+
/** snake_case singular, e.g. `order_item`. */
|
|
7
|
+
name: string;
|
|
8
|
+
/** e.g. `order_items`. */
|
|
9
|
+
table: string;
|
|
10
|
+
description: string;
|
|
11
|
+
fields: FieldDef[];
|
|
12
|
+
timestamps: boolean;
|
|
13
|
+
softDeletes: boolean;
|
|
14
|
+
/** A join table: no id, a composite primary key, migration only (no model). */
|
|
15
|
+
pivot: boolean;
|
|
16
|
+
/** Composite unique keys. */
|
|
17
|
+
unique: string[][];
|
|
18
|
+
}
|
|
19
|
+
export interface Domain {
|
|
20
|
+
name: string;
|
|
21
|
+
title: string;
|
|
22
|
+
description: string;
|
|
23
|
+
templates: ModelTemplate[];
|
|
24
|
+
}
|
|
25
|
+
export type TemplateOptions = {
|
|
26
|
+
table?: string;
|
|
27
|
+
timestamps?: boolean;
|
|
28
|
+
softDeletes?: boolean;
|
|
29
|
+
pivot?: boolean;
|
|
30
|
+
unique?: string[][];
|
|
31
|
+
};
|
|
32
|
+
/** A template from field specs (see fields.ts). */
|
|
33
|
+
export declare function template(domain: string, name: string, description: string, specs: string[], opts?: TemplateOptions): ModelTemplate;
|
|
34
|
+
/** A domain whose templates are written as `[name, description, specs, options?]`. */
|
|
35
|
+
export declare function domain(name: string, title: string, description: string, templates: [string, string, string[], TemplateOptions?][]): Domain;
|
|
36
|
+
/** Tables a template's foreign keys point to (itself excluded). */
|
|
37
|
+
export declare function dependencies(t: ModelTemplate): string[];
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.template = template;
|
|
4
|
+
exports.domain = domain;
|
|
5
|
+
exports.dependencies = dependencies;
|
|
6
|
+
const orm_model_1 = require("../generators/orm-model");
|
|
7
|
+
const fields_1 = require("./fields");
|
|
8
|
+
/** A template from field specs (see fields.ts). */
|
|
9
|
+
function template(domain, name, description, specs, opts = {}) {
|
|
10
|
+
const pivot = opts.pivot === true;
|
|
11
|
+
return {
|
|
12
|
+
domain,
|
|
13
|
+
name,
|
|
14
|
+
table: opts.table ?? (0, orm_model_1.pluralize)(name),
|
|
15
|
+
description,
|
|
16
|
+
fields: specs.flatMap(fields_1.parseFieldSpec),
|
|
17
|
+
timestamps: opts.timestamps ?? !pivot,
|
|
18
|
+
softDeletes: opts.softDeletes ?? false,
|
|
19
|
+
pivot,
|
|
20
|
+
unique: opts.unique ?? [],
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** A domain whose templates are written as `[name, description, specs, options?]`. */
|
|
24
|
+
function domain(name, title, description, templates) {
|
|
25
|
+
return {
|
|
26
|
+
name,
|
|
27
|
+
title,
|
|
28
|
+
description,
|
|
29
|
+
templates: templates.map(([n, d, specs, opts]) => template(name, n, d, specs, opts)),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** Tables a template's foreign keys point to (itself excluded). */
|
|
33
|
+
function dependencies(t) {
|
|
34
|
+
const tables = t.fields.flatMap((f) => (f.references ? [f.references.table] : []));
|
|
35
|
+
return [...new Set(tables)].filter((table) => table !== t.table);
|
|
36
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type Dialect = "postgres" | "mysql" | "sqlite" | "mssql";
|
|
2
|
+
/**
|
|
3
|
+
* The database the project's migrations run on, found the way
|
|
4
|
+
* app.useMeghORM() finds it: `megh.database` in package.json, then
|
|
5
|
+
* DB_DIALECT, then the scheme of DATABASE_URL (the environment over .env).
|
|
6
|
+
* Undefined when nothing says.
|
|
7
|
+
*/
|
|
8
|
+
export declare function projectDialect(projectRoot: string): Dialect | undefined;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.projectDialect = projectDialect;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const SCHEMES = {
|
|
10
|
+
postgres: "postgres",
|
|
11
|
+
postgresql: "postgres",
|
|
12
|
+
mysql: "mysql",
|
|
13
|
+
mariadb: "mysql",
|
|
14
|
+
mssql: "mssql",
|
|
15
|
+
sqlserver: "mssql",
|
|
16
|
+
sqlite: "sqlite",
|
|
17
|
+
file: "sqlite",
|
|
18
|
+
};
|
|
19
|
+
const DIALECTS = new Set(["postgres", "mysql", "sqlite", "mssql"]);
|
|
20
|
+
/** `KEY=value` pairs of a .env file (no expansion). */
|
|
21
|
+
function readDotEnv(file) {
|
|
22
|
+
if (!fs_1.default.existsSync(file))
|
|
23
|
+
return {};
|
|
24
|
+
const out = {};
|
|
25
|
+
for (const line of fs_1.default.readFileSync(file, "utf8").split(/\r?\n/)) {
|
|
26
|
+
const m = line.match(/^\s*(?:export\s+)?([A-Za-z_][\w]*)\s*=\s*(.*?)\s*$/);
|
|
27
|
+
if (m)
|
|
28
|
+
out[m[1]] = m[2].replace(/^(["'])(.*)\1$/, "$2");
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
const fromUrl = (url) => {
|
|
33
|
+
const scheme = url?.match(/^([a-z][a-z0-9+.-]*):/i)?.[1].toLowerCase();
|
|
34
|
+
return scheme ? SCHEMES[scheme] : undefined;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* The database the project's migrations run on, found the way
|
|
38
|
+
* app.useMeghORM() finds it: `megh.database` in package.json, then
|
|
39
|
+
* DB_DIALECT, then the scheme of DATABASE_URL (the environment over .env).
|
|
40
|
+
* Undefined when nothing says.
|
|
41
|
+
*/
|
|
42
|
+
function projectDialect(projectRoot) {
|
|
43
|
+
let database = {};
|
|
44
|
+
try {
|
|
45
|
+
const pkg = JSON.parse(fs_1.default.readFileSync(path_1.default.join(projectRoot, "package.json"), "utf8"));
|
|
46
|
+
database = pkg?.megh?.database ?? {};
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// No or invalid package.json: use the environment.
|
|
50
|
+
}
|
|
51
|
+
const dotenv = readDotEnv(path_1.default.join(projectRoot, ".env"));
|
|
52
|
+
const env = (key) => process.env[key] ?? dotenv[key];
|
|
53
|
+
const named = database.dialect ?? env("DB_DIALECT");
|
|
54
|
+
if (named && DIALECTS.has(named))
|
|
55
|
+
return named;
|
|
56
|
+
return fromUrl(database.url) ?? fromUrl(env("DATABASE_URL"));
|
|
57
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meghjs/cli",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.4",
|
|
4
4
|
"description": "CLI for scaffolding and running Megh applications",
|
|
5
5
|
"author": "Tareq Hossain",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,10 +27,18 @@
|
|
|
27
27
|
"prepublishOnly": "npm run build"
|
|
28
28
|
},
|
|
29
29
|
"keywords": [
|
|
30
|
-
"cli",
|
|
31
30
|
"megh",
|
|
31
|
+
"cli",
|
|
32
|
+
"typescript",
|
|
32
33
|
"backend",
|
|
33
|
-
"
|
|
34
|
+
"generator",
|
|
35
|
+
"scaffold",
|
|
36
|
+
"scaffolding",
|
|
37
|
+
"migrations",
|
|
38
|
+
"orm",
|
|
39
|
+
"auth",
|
|
40
|
+
"rbac",
|
|
41
|
+
"fastify"
|
|
34
42
|
],
|
|
35
43
|
"dependencies": {
|
|
36
44
|
"@swc/core": "^1.13.5",
|
|
@@ -44,7 +52,7 @@
|
|
|
44
52
|
"vitest": "^4.1.12"
|
|
45
53
|
},
|
|
46
54
|
"peerDependencies": {
|
|
47
|
-
"@meghjs/core": "^0.5.
|
|
55
|
+
"@meghjs/core": "^0.5.4"
|
|
48
56
|
},
|
|
49
57
|
"peerDependenciesMeta": {
|
|
50
58
|
"@meghjs/core": {
|
|
@@ -56,8 +64,12 @@
|
|
|
56
64
|
"url": "git+https://github.com/meghjs/megh.git",
|
|
57
65
|
"directory": "packages/cli"
|
|
58
66
|
},
|
|
67
|
+
"homepage": "https://meghjs.github.io/cli/basic",
|
|
68
|
+
"bugs": {
|
|
69
|
+
"url": "https://github.com/meghjs/megh/issues"
|
|
70
|
+
},
|
|
59
71
|
"publishConfig": {
|
|
60
72
|
"access": "public"
|
|
61
73
|
},
|
|
62
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "25ab107490492cc54d1eb3419a33b735613c57ab"
|
|
63
75
|
}
|
package/readme.md
CHANGED
|
@@ -43,7 +43,8 @@ npm run start:dev
|
|
|
43
43
|
| `start` | | `--entry <file>` | Run the compiled app with `NODE_ENV=production` |
|
|
44
44
|
| `doctor` | | | Check Node.js, `@meghjs/core`, `reflect-metadata` and decorator settings |
|
|
45
45
|
| `route:list` | `routes` | `--json`, `--static` | List the app's routes |
|
|
46
|
-
| `make:model` | `m:m` | `-f`, `--orm`, `--root <dir
|
|
46
|
+
| `make:model` | `m:m` | `-f`, `--orm`, `--root <dir>`, `--template [ref]`, `--fields <list>`, `--factory` | Create a model / TypeORM entity; with `--template` / `--fields`, also its migration |
|
|
47
|
+
| `make:resource` | `m:r` | `-f`, `-t <table>`, `--template [ref]`, `--fields <list>`, `--factory` | Create a model's DTOs, CRUD service and controller |
|
|
47
48
|
| `update:model` | `u:m` | `-t <table>`, `--dto`, `--dry-run`, `--root <dir>` | Sync a model's columns with its migrations, keeping relations and methods |
|
|
48
49
|
| `make:controller` | `m:c` | `-f`, `-r`, `-m <model>`, `--orm`, `--root <dir>`, `--base-path` | Create a controller |
|
|
49
50
|
| `make:service` | `m:s` | `-f`, `-r`, `-m <model>`, `--orm`, `--root <dir>` | Create a service |
|
|
@@ -53,6 +54,10 @@ npm run start:dev
|
|
|
53
54
|
| `make:seeder` | `m:sd` | `-f` | Create a seeder in `src/database/seeders` (`database` for the entry seeder) |
|
|
54
55
|
| `db:seed` | | `-s <name>`, `--force` | Run the seeders in one transaction (also `migrate --seed`) |
|
|
55
56
|
| `make:factory` | `m:f` | `-f` | Create a model factory in `src/database/factories`, with Faker values per column |
|
|
57
|
+
| `scaffold:domain [domain]` | `sc:domain` | `--only <list>`, `--resource`, `--factory`, `--migrations-only`, `--no-deps`, `-y` | Create a business domain's tables and models (or resources) |
|
|
58
|
+
| `scaffold:auth` | `sc:auth` | `--strategy <jwt\|api-key\|both>`, `--roles <none\|single\|multiple>`, `--permissions`, `-f`, `-y` | Add sign-up and sign-in, optionally with roles and permissions |
|
|
59
|
+
| `scaffold:rbac` | `sc:rbac` | `--roles <single\|multiple>`, `--permissions`, `-f`, `-y` | Add roles and permissions to an app's users |
|
|
60
|
+
| `template:list [domain]` | `templates` | `--json` | List the model templates |
|
|
56
61
|
|
|
57
62
|
`-f` / `--force` overwrites an existing file. `-r` / `--resource` generates a
|
|
58
63
|
full REST skeleton. `--root <dir>` (also `-rt`) writes under `src/<dir>`
|
|
@@ -118,6 +123,26 @@ npx megh make:config jwt
|
|
|
118
123
|
|
|
119
124
|
Generates a class extending `MeghConfig`, registered via `@AppConfig`.
|
|
120
125
|
|
|
126
|
+
## Scaffold
|
|
127
|
+
|
|
128
|
+
Build tables, models and whole features from templates. A template becomes a
|
|
129
|
+
create-table migration; the model, DTOs, service and controller are generated
|
|
130
|
+
from it.
|
|
131
|
+
|
|
132
|
+
```sh
|
|
133
|
+
npx megh make:model Product --template ecommerce/product # migration + model (+ categories, brands)
|
|
134
|
+
npx megh make:resource Ticket --fields "title,body,status:in(open|closed),user_id:cascade"
|
|
135
|
+
npx megh sc:domain ecommerce --resource # a whole domain as REST resources
|
|
136
|
+
npx megh sc:auth --strategy both --roles multiple --permissions
|
|
137
|
+
npx megh sc:rbac --roles single --permissions
|
|
138
|
+
npx megh templates ecommerce # list a domain's templates
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
There are 11 domains (common, ecommerce, pos, medical, construction,
|
|
142
|
+
marketing, real-estate, hr, crm, education, logistics), and your own go in
|
|
143
|
+
`.megh/templates/<domain>.json`. See [Scaffold](https://meghjs.github.io/cli/scaffold/)
|
|
144
|
+
for the field spec, every template, and what `sc:auth` generates.
|
|
145
|
+
|
|
121
146
|
## Development Server
|
|
122
147
|
|
|
123
148
|
```sh
|