@neohm/nh-cli 1.1.2 → 1.1.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/package.json +1 -1
- package/src/commands/gen.js +22 -0
- package/src/commands/migration.js +1 -1
- package/src/lib/naming.js +15 -0
- package/src/lib/templates.js +9 -4
package/package.json
CHANGED
package/src/commands/gen.js
CHANGED
|
@@ -222,6 +222,28 @@ async function gen() {
|
|
|
222
222
|
);
|
|
223
223
|
}
|
|
224
224
|
|
|
225
|
+
// 7. Optional: migration -------------------------------------------------
|
|
226
|
+
const { migration } = await inquirer.prompt([
|
|
227
|
+
{
|
|
228
|
+
type: "confirm",
|
|
229
|
+
name: "migration",
|
|
230
|
+
message: `Create migration (M-${n.migrationName})?`,
|
|
231
|
+
default: true,
|
|
232
|
+
},
|
|
233
|
+
]);
|
|
234
|
+
|
|
235
|
+
if (migration) {
|
|
236
|
+
const ts = Date.now();
|
|
237
|
+
// Same filename/class rule as `nh migration`.
|
|
238
|
+
const file = naming.migrationFileName(n.migrationName, ts);
|
|
239
|
+
const migrationPath = path.join(paths.migrationsDir, file);
|
|
240
|
+
report(
|
|
241
|
+
writeFile(migrationPath, tpl.genMigration(entityName, tableName, ts)),
|
|
242
|
+
migrationPath,
|
|
243
|
+
paths.root,
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
225
247
|
// Auto-wire: rebuild this module's barrel and register it in app.module.ts.
|
|
226
248
|
log.info("\nWiring imports...");
|
|
227
249
|
const mod = sync.scanModule(
|
|
@@ -27,7 +27,7 @@ async function migration() {
|
|
|
27
27
|
]);
|
|
28
28
|
|
|
29
29
|
const ts = Date.now();
|
|
30
|
-
const file =
|
|
30
|
+
const file = naming.migrationFileName(migrationName, ts);
|
|
31
31
|
const p = path.join(paths.migrationsDir, file);
|
|
32
32
|
report(
|
|
33
33
|
writeFile(p, tpl.migration(migrationName, tableName, ts)),
|
package/src/lib/naming.js
CHANGED
|
@@ -42,10 +42,25 @@ function toKebabCase(input) {
|
|
|
42
42
|
return toWords(input).join('-');
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
/**
|
|
46
|
+
* `<timestamp>M-<PascalName>.ts` — the single migration filename rule, shared by
|
|
47
|
+
* `nh migration` and the migration `nh gen` scaffolds alongside an entity.
|
|
48
|
+
*/
|
|
49
|
+
function migrationFileName(migrationName, timestamp) {
|
|
50
|
+
return `${timestamp}M-${toPascalCase(migrationName)}.ts`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** `<PascalName><timestamp>` — the class name matching migrationFileName(). */
|
|
54
|
+
function migrationClassName(migrationName, timestamp) {
|
|
55
|
+
return `${toPascalCase(migrationName)}${timestamp}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
45
58
|
module.exports = {
|
|
46
59
|
toWords,
|
|
47
60
|
toDotCase,
|
|
48
61
|
toPascalCase,
|
|
49
62
|
toCamelCase,
|
|
50
63
|
toKebabCase,
|
|
64
|
+
migrationFileName,
|
|
65
|
+
migrationClassName,
|
|
51
66
|
};
|
package/src/lib/templates.js
CHANGED
|
@@ -5,6 +5,7 @@ const {
|
|
|
5
5
|
toPascalCase,
|
|
6
6
|
toKebabCase,
|
|
7
7
|
toCamelCase,
|
|
8
|
+
migrationClassName,
|
|
8
9
|
} = require("./naming");
|
|
9
10
|
|
|
10
11
|
/**
|
|
@@ -41,6 +42,9 @@ function names(base) {
|
|
|
41
42
|
subscriberFile: `${dot}.subscriber.ts`,
|
|
42
43
|
jobClass: `${pascal}Job`,
|
|
43
44
|
jobFile: `${dot}.job.ts`,
|
|
45
|
+
// Fed through naming.migrationFileName/migrationClassName so the migration
|
|
46
|
+
// `nh gen` writes is named exactly like one from `nh migration`.
|
|
47
|
+
migrationName: `Add${pascal}Table`,
|
|
44
48
|
};
|
|
45
49
|
}
|
|
46
50
|
|
|
@@ -201,9 +205,10 @@ export class ${n.subscriberClass} extends CommonSubscriber<${n.entityClass}> {
|
|
|
201
205
|
|
|
202
206
|
function job(base) {
|
|
203
207
|
const n = names(base);
|
|
204
|
-
return `import { CommonJob, QueueService } from '@neohm/nestend';
|
|
208
|
+
return `import { CommonJob, QueueService, DatabaseEventDto } from '@neohm/nestend';
|
|
205
209
|
import { Injectable } from '@nestjs/common';
|
|
206
210
|
import { JobConstants } from '../../constants/job.constants';
|
|
211
|
+
import { ${n.entityClass} } from '../entities/${n.dot}.entity';
|
|
207
212
|
|
|
208
213
|
@Injectable()
|
|
209
214
|
export class ${n.jobClass} extends CommonJob {
|
|
@@ -211,7 +216,7 @@ export class ${n.jobClass} extends CommonJob {
|
|
|
211
216
|
super(JobConstants.${n.camel}Job);
|
|
212
217
|
}
|
|
213
218
|
|
|
214
|
-
async handle(event:
|
|
219
|
+
async handle(event: DatabaseEventDto<${n.entityClass}>): Promise<void> {
|
|
215
220
|
return;
|
|
216
221
|
}
|
|
217
222
|
}
|
|
@@ -225,7 +230,7 @@ export class ${n.jobClass} extends CommonJob {
|
|
|
225
230
|
*/
|
|
226
231
|
function genMigration(base, tableName, timestamp) {
|
|
227
232
|
const n = names(base);
|
|
228
|
-
const className =
|
|
233
|
+
const className = migrationClassName(n.migrationName, timestamp);
|
|
229
234
|
return `import { MigrationUtility } from '@neohm/nestend';
|
|
230
235
|
|
|
231
236
|
export class ${className} extends MigrationUtility {
|
|
@@ -247,7 +252,7 @@ export class ${className} extends MigrationUtility {
|
|
|
247
252
|
|
|
248
253
|
/** Standalone migration (`nh migration`). */
|
|
249
254
|
function migration(migrationName, tableName, timestamp) {
|
|
250
|
-
const className =
|
|
255
|
+
const className = migrationClassName(migrationName, timestamp);
|
|
251
256
|
return `import { MigrationUtility } from '@neohm/nestend';
|
|
252
257
|
|
|
253
258
|
export class ${className} extends MigrationUtility {
|