|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | + |
| 4 | +const Reset = '\x1b[0m' |
| 5 | +const Bright = '\x1b[1m' |
| 6 | +const FgRed = '\x1b[31m' |
| 7 | +const FgYellow = '\x1b[33m' |
| 8 | +const FgGreen = '\x1b[32m' |
| 9 | + |
| 10 | +// Matches only the leading date portion with underscore: |
| 11 | +// - YYYY = 4 digits |
| 12 | +// - MM = 01–12 |
| 13 | +// - DD = 01–31 |
| 14 | +// - ends with underscore |
| 15 | +const regDate = /^(\d{4})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])_/ |
| 16 | + |
| 17 | +// Matches only the required prefix before the name: |
| 18 | +// - 8 digits (YYYYMMDD) |
| 19 | +// - underscore |
| 20 | +// - 2 digits (NN) |
| 21 | +// - underscore |
| 22 | +const regPrefix = /^\d{8}_\d{2}_/ |
| 23 | + |
| 24 | +// Correct date + prefix and filename snake cased |
| 25 | +const regFull = /^(?:\d{4})(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])_\d{2}_[^\\/]+$/ |
| 26 | + |
| 27 | +const MIGRATIONS_PATH = './src/server/db/migrations' |
| 28 | +const dirPath = path.join(process.cwd(), MIGRATIONS_PATH) |
| 29 | +const files = fs.readdirSync(dirPath) |
| 30 | + |
| 31 | +let errors = [] |
| 32 | + |
| 33 | +for (const file of files) { |
| 34 | + if (!regPrefix.test(file)) { |
| 35 | + errors.push(`${FgRed}${Bright}✗${Reset} ${file} ${FgYellow}→ must start with YYYYMMDD_NN_${Reset}`) |
| 36 | + continue |
| 37 | + } |
| 38 | + if (!regDate.test(file)) { |
| 39 | + errors.push(`${FgRed}${Bright}✗${Reset} ${file} ${FgYellow}→ invalid date format${Reset}`) |
| 40 | + continue |
| 41 | + } |
| 42 | + if (!regFull.test(file)) { |
| 43 | + errors.push(`${FgRed}${Bright}✗${Reset} ${file} ${FgYellow}→ contains invalid characters${Reset}`) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +if (errors.length > 0) { |
| 48 | + console.error('Migration file name check failed:') |
| 49 | + console.error(errors.join('\n')) |
| 50 | + process.exit(1) |
| 51 | +} else { |
| 52 | + console.log(`${FgGreen}${Bright}Success:${Reset} All migration names ok\n`) |
| 53 | +} |
0 commit comments