@neohm/nh-cli 1.1.2 → 1.1.3
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 +7 -3
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
|
|
|
@@ -211,7 +215,7 @@ export class ${n.jobClass} extends CommonJob {
|
|
|
211
215
|
super(JobConstants.${n.camel}Job);
|
|
212
216
|
}
|
|
213
217
|
|
|
214
|
-
async handle(event:
|
|
218
|
+
async handle(event: DataBaseEventDto<${n.entityClass}>): Promise<void> {
|
|
215
219
|
return;
|
|
216
220
|
}
|
|
217
221
|
}
|
|
@@ -225,7 +229,7 @@ export class ${n.jobClass} extends CommonJob {
|
|
|
225
229
|
*/
|
|
226
230
|
function genMigration(base, tableName, timestamp) {
|
|
227
231
|
const n = names(base);
|
|
228
|
-
const className =
|
|
232
|
+
const className = migrationClassName(n.migrationName, timestamp);
|
|
229
233
|
return `import { MigrationUtility } from '@neohm/nestend';
|
|
230
234
|
|
|
231
235
|
export class ${className} extends MigrationUtility {
|
|
@@ -247,7 +251,7 @@ export class ${className} extends MigrationUtility {
|
|
|
247
251
|
|
|
248
252
|
/** Standalone migration (`nh migration`). */
|
|
249
253
|
function migration(migrationName, tableName, timestamp) {
|
|
250
|
-
const className =
|
|
254
|
+
const className = migrationClassName(migrationName, timestamp);
|
|
251
255
|
return `import { MigrationUtility } from '@neohm/nestend';
|
|
252
256
|
|
|
253
257
|
export class ${className} extends MigrationUtility {
|