add.js
1.5 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
const moment = require('moment');
const shelljs = require('shelljs');
const chalk = require('chalk');
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const argv = require('optimist').argv
const time = moment();
const author = shelljs.exec('git config --get user.email', {
silent: true
}).stdout.trim();
const authorName = shelljs.exec('git config --get user.name', {
silent: true
}).stdout.trim();
const comment = process.env.npm_config_comment || argv.comment;
if (!comment) {
console.log(chalk.red('缺少-comment(备注)参数'));
return;
}
const migrationPath = path.join(__dirname, '../../migrations');
const fileName = time.format('YYYYMMDDkkmmss');
let migrationTemplate = fs.readFileSync(path.join(__dirname, './migration.sql'), 'utf-8');
migrationTemplate = migrationTemplate
.replace('${author}', author)
.replace('${time}', time.format('YYYY-MM-DD kk:mm:ss'))
.replace('${comment}', comment)
.replace('${authorName}', authorName);
fs.writeFileSync(path.join(migrationPath, `${fileName}.sql`), migrationTemplate, {
encoding: 'utf8'
});
let migrations = require('../../migrations');
migrations.push(fileName);
let migrationStr = _.join(_.map(migrations, m => {
return `'${m}'`;
}), ', ');
fs.writeFileSync(path.join(migrationPath, 'index.js'), `module.exports = [${migrationStr}];
`, {
encoding: 'utf8'
});
console.log(chalk.yellow('注意:GO;分割执行块'));
console.log(chalk.green(`新增迁移记录:${fileName}.sql`));