-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcli
More file actions
executable file
·124 lines (110 loc) · 3.8 KB
/
cli
File metadata and controls
executable file
·124 lines (110 loc) · 3.8 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#! /usr/bin/env node
var path = require('path');
var fs = require('fs');
var mkdirp = require('mkdirp');
var debug = require('debug')('loopback-component-migrate');
/**
* Command line implementation for loopback-component-migrate.
*
* Common usage case is:
*
* ./node_modules/.bin/loopback-component-migrate create|up|down
*/
var program = require('commander');
program
.version(require(path.join(__dirname, '..', './package.json')).version)
.option('-d, --migrations-dir <path>', 'set migrations directory. defaults to ./migrations')
.option('-s, --server <path>', 'set server path. defaults to ./server/server.js');
program
.command('create [name]')
.description('create a new migration script')
.action(function(name, options){
function stringifyAndPadLeading(num) {
var str = num + '';
return (str.length === 1) ? '0' + str : str;
}
function generateFileName(name) {
var d = new Date(),
year = d.getFullYear() + '',
month = stringifyAndPadLeading(d.getMonth()+1),
day = stringifyAndPadLeading(d.getDate()),
hours = stringifyAndPadLeading(d.getHours()),
minutes = stringifyAndPadLeading(d.getMinutes()),
seconds = stringifyAndPadLeading(d.getSeconds()),
dateString = year + month + day + hours + minutes + seconds,
fileName = dateString + (name ? '-' + name : '') + '.js';
return fileName;
}
function getMigrationsDir () {
var dir = path.join(process.cwd(), 'migrations');
debug('Using migrations directory: %s', dir);
return dir;
}
function ensureDirectory (dir) {
debug('Preparing migrations directory: %s', dir);
mkdirp.sync(dir);
}
function writeFile (fileName, contents) {
debug('Creating migration script: %s', fileName);
var migrationsDir = getMigrationsDir();
ensureDirectory(migrationsDir);
var filePath = path.join(migrationsDir, fileName);
fs.writeFileSync(filePath, contents);
}
// Create the migration file.
var fileName = generateFileName(name);
var migrationsDir = path.join(process.cwd(), 'migrations');
console.log('Creating migration script %s in %s', fileName, migrationsDir);
var filePath = path.join(migrationsDir,fileName);
var fileContent = fs.readFileSync(path.join(__dirname, '..', 'migration-skeleton.js'));
writeFile(fileName, fileContent);
});
program
.command('migrate [to]')
.alias('up')
.description('Migrate to the given migration')
.action(function(to, options){
console.log('Migrating up to: "%s"', to || 'all');
var server = program.server || process.cwd() + '/server/server.js';
var app = require(path.resolve(process.cwd(), server));
app.models.Migration.migrate('up', to)
.then(function (res) {
console.log('Done.');
process.exit();
})
.catch(function (err) {
console.log(err);
process.exit();
})
}).on('--help', function() {
console.log(' Examples:');
console.log();
console.log(' $ migrate 005');
console.log(' $ up 005');
console.log();
});
program
.command('rollback [to]')
.alias('down')
.description('Rollback to the given migration')
.action(function(to, options){
console.log('Rolling back to: "%s"', to || 'all');
var server = program.server || process.cwd() + '/server/server.js';
var app = require(path.resolve(process.cwd(), server));
app.models.Migration.migrate('down', to)
.then(function (res) {
console.log('Done.');
process.exit();
})
.catch(function (err) {
console.log(err);
process.exit();
})
}).on('--help', function() {
console.log(' Examples:');
console.log();
console.log(' $ rollback 001');
console.log(' $ down 001');
console.log();
});
program.parse(process.argv);