@mikeyt23/node-cli-utils 1.3.0 → 1.3.1
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/index.js +101 -3
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -346,13 +346,111 @@ async function dotnetDbMigrate(dbContextName, relativeDbMigratorDirectoryPath, m
|
|
|
346
346
|
return waitForProcess(spawn('dotnet', args, spawnOptions))
|
|
347
347
|
}
|
|
348
348
|
|
|
349
|
-
async function dotnetDbAddMigration(dbContextName, relativeDbMigratorDirectoryPath, migrationName) {
|
|
349
|
+
async function dotnetDbAddMigration(dbContextName, relativeDbMigratorDirectoryPath, migrationName, withBoilerplate = false) {
|
|
350
350
|
throwIfRequiredIsFalsy(dbContextName, 'dbContextName')
|
|
351
351
|
throwIfRequiredIsFalsy(relativeDbMigratorDirectoryPath, 'relativeDbMigratorDirectoryPath')
|
|
352
352
|
throwIfRequiredIsFalsy(migrationName, 'migrationName')
|
|
353
|
-
|
|
353
|
+
|
|
354
|
+
const migrationsOutputDir = `Migrations/${dbContextName}Migrations`
|
|
355
|
+
|
|
356
|
+
let args = ['ef', 'migrations', 'add', migrationName, '--context', dbContextName, '-o', migrationsOutputDir]
|
|
354
357
|
let spawnOptions = {...defaultSpawnOptions, cwd: relativeDbMigratorDirectoryPath}
|
|
355
|
-
|
|
358
|
+
await waitForProcess(spawn('dotnet', args, spawnOptions))
|
|
359
|
+
|
|
360
|
+
if (withBoilerplate) {
|
|
361
|
+
await dotnetDbAddMigrationBoilerplate(dbContextName, relativeDbMigratorDirectoryPath, migrationName)
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
async function dotnetDbAddMigrationBoilerplate(dbContextName, relativeDbMigratorDirectoryPath, migrationName) {
|
|
366
|
+
console.log(`Attempting to write boilerplate to generated migration C# file`)
|
|
367
|
+
|
|
368
|
+
const migrationsOutputDir = `Migrations/${dbContextName}Migrations`
|
|
369
|
+
const dirPath = path.join(relativeDbMigratorDirectoryPath, migrationsOutputDir)
|
|
370
|
+
|
|
371
|
+
console.log(`Checking for generated C# file in directory: ${dirPath}`)
|
|
372
|
+
|
|
373
|
+
const filenames = fs.readdirSync(dirPath).filter(fn => fn.endsWith(`${migrationName}.cs`));
|
|
374
|
+
if (!filenames || filenames.length === 0) {
|
|
375
|
+
console.log(`Unable to add boilerplate - could not find auto generated file in directory: ${dirPath}`)
|
|
376
|
+
}
|
|
377
|
+
const filename = filenames[0]
|
|
378
|
+
const filePath = path.join(dirPath, filename)
|
|
379
|
+
|
|
380
|
+
if (!fs.existsSync(filePath)) {
|
|
381
|
+
console.log(`Could not find the file to add boilerplate to at: ${filePath}`)
|
|
382
|
+
return
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
console.log(`Auto generated C# file to modify: ${filePath}`)
|
|
386
|
+
|
|
387
|
+
const usingLine = 'using MikeyT.DbMigrations;'
|
|
388
|
+
const upLine = `MigrationScriptRunner.RunScript(migrationBuilder, "${migrationName}.sql");`
|
|
389
|
+
const downLine = `MigrationScriptRunner.RunScript(migrationBuilder, "${migrationName}_Down.sql");`
|
|
390
|
+
|
|
391
|
+
const fileContents = await fsp.readFile(filePath, {encoding: 'utf8'})
|
|
392
|
+
let lines = fileContents.replaceAll('\r', '').split('\n')
|
|
393
|
+
|
|
394
|
+
let newLines = []
|
|
395
|
+
|
|
396
|
+
newLines.push(lines[0].trim())
|
|
397
|
+
newLines.push(usingLine)
|
|
398
|
+
|
|
399
|
+
let addUpLine = false
|
|
400
|
+
let addDownLine = false
|
|
401
|
+
let skipNextLineIfBlank = false
|
|
402
|
+
for (let i = 1; i < lines.length; i++) {
|
|
403
|
+
if (skipNextLineIfBlank && lines[i].trim().length === 0) {
|
|
404
|
+
skipNextLineIfBlank = false
|
|
405
|
+
continue
|
|
406
|
+
}
|
|
407
|
+
if (addUpLine) {
|
|
408
|
+
let newLine = lines[i].replace('{', `{\n\t\t\t${upLine}`)
|
|
409
|
+
newLines.push(newLine)
|
|
410
|
+
addUpLine = false
|
|
411
|
+
skipNextLineIfBlank = true
|
|
412
|
+
continue
|
|
413
|
+
}
|
|
414
|
+
if (addDownLine) {
|
|
415
|
+
let newLine = lines[i].replace('{', `{\n\t\t\t${downLine}`)
|
|
416
|
+
newLines.push(newLine)
|
|
417
|
+
addDownLine = false
|
|
418
|
+
skipNextLineIfBlank = true
|
|
419
|
+
continue
|
|
420
|
+
}
|
|
421
|
+
newLines.push(lines[i])
|
|
422
|
+
if (lines[i].includes('void Up')) {
|
|
423
|
+
addUpLine = true
|
|
424
|
+
}
|
|
425
|
+
if (lines[i].includes('void Down')) {
|
|
426
|
+
addDownLine = true
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const newFileContents = newLines.join('\n')
|
|
431
|
+
|
|
432
|
+
await fsp.writeFile(filePath, newFileContents, {encoding: 'utf8'})
|
|
433
|
+
|
|
434
|
+
console.log(`Updated file with boilerplate - please ensure it is correct: ${filePath}`)
|
|
435
|
+
|
|
436
|
+
const upScriptPath = path.join(relativeDbMigratorDirectoryPath, `Scripts/${migrationName}.sql`)
|
|
437
|
+
const downScriptPath = path.join(relativeDbMigratorDirectoryPath, `Scripts/${migrationName}_Down.sql`)
|
|
438
|
+
|
|
439
|
+
console.log('Creating corresponding empty sql files (no action will be taken if they already exist):')
|
|
440
|
+
console.log(` - ${upScriptPath}`)
|
|
441
|
+
console.log(` - ${downScriptPath}`)
|
|
442
|
+
|
|
443
|
+
if (!fs.existsSync(upScriptPath)) {
|
|
444
|
+
await fsp.writeFile(upScriptPath, '', {encoding: 'utf8'})
|
|
445
|
+
} else {
|
|
446
|
+
console.log('Skipping Up sql script (already exists)')
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (!fs.existsSync(downScriptPath)) {
|
|
450
|
+
await fsp.writeFile(downScriptPath, '', {encoding: 'utf8'})
|
|
451
|
+
} else {
|
|
452
|
+
console.log('Skipping Down sql script (already exists)')
|
|
453
|
+
}
|
|
356
454
|
}
|
|
357
455
|
|
|
358
456
|
async function dotnetDbRemoveMigration(dbContextName, relativeDbMigratorDirectoryPath) {
|