-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate
executable file
·66 lines (53 loc) · 1.58 KB
/
migrate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env node
var path = require('path');
var fs = require('fs');
var program = require('commander');
var MIGRATIONS_DIR = './imports/migrations';
var MIGRATIONS_EXT = 'ts';
function main() {
parseArgs();
program.parse(process.argv);
}
function parseArgs() {
program
.command('generate <migrationName> [moreMigrations...]')
.alias('g')
.action(onCreateMigration)
}
function onCreateMigration(migrationName, moreMigrations) {
var timestamp = Date.now();
var filename = timestamp + "_" + migrationName + "." + MIGRATIONS_EXT;
var migrationPath = path.join(MIGRATIONS_DIR, filename);
console.log('Create migration %s', path.join(process.cwd(), migrationPath));
try {
fs.mkdirSync(MIGRATIONS_DIR);
} catch (e) {
// ignore existing directory error
if (e.code !== 'EEXIST') {
throw e;
}
}
fs.writeFileSync(migrationPath, getMigrationTemplate(migrationName, timestamp));
if (moreMigrations && moreMigrations.length) {
moreMigrations.forEach(onCreateMigration);
}
}
function getMigrationTemplate(name, version) {
return [
"import { Migration } from '../packages/meteor-seed-migrate/index.js';",
"",
"export default class " + captalize(name) + " extends Migration {",
" version = " + version + ";",
" up() {",
" ",
" }",
" down() {",
" ",
" }",
"}"
].join('\n');
}
function captalize(name) {
return name.charAt(0).toUpperCase() + name.slice(1);
}
main();