Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Commit

Permalink
Init cli app
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev committed Mar 21, 2019
1 parent 9a0e2b0 commit 87dccd7
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 23 deletions.
78 changes: 78 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env node

import { migrator } from './dist/src/index';
import yargs from 'yargs';
import path from 'path';
import fs from 'fs';

const argv = yargs
.command('migrate <migration>', 'Execute migrations')
.command('list', 'Show all migrations versions')
.command('status', 'Show migrations status')
.argv;

let metro = {};

try {
metro = require(argv.config);
}
catch(err) {}

const config = {
// false disables logging
log: true,
// null or a function
logger: (level, ...arg) => console.log(`[${level}]`, ...arg),
// enable/disable info log "already at latest."
logIfLatest: true,
// migrations collection name. Defaults to 'migrations'
collectionName: metro.collectionName || 'migrations',
// mongdb url
db: argv.db || metro.db || "mongodb://localhost:27017/api",
// enable automatic backups
backups: metro.backups || false,
// directory to save backups
backupsDir: path.resolve(argv.backupsDir || metro.backupsDir || './migrations/backups'),
migrationsDir: path.resolve(argv.migrationsDir || metro.migrationsDir || './migrations'),
};

(async () => {
await migrator.config(config); // Returns a promise

let versions = fs.readdirSync(config.migrationsDir)
.filter((v) => v.match(new RegExp(/^[\d].[\d]$/)));

switch (argv._[0]) {
case 'migrate':
versions = versions.map((v) => parseFloat(v));
// console.log(versions, argv.migration);

if (argv.migration != 0 && versions.indexOf(parseFloat(argv.migration)) < 0) {
console.log('This version does not exists.');
process.exit();
}

versions = versions.map((v) => v.toFixed(1));

versions.forEach(async (v) => {
// console.log(`${config.migrationsDir}/${v.toString()}`);
const migrationObj = require(`${config.migrationsDir}/${v}`).default;
await migrator.add(migrationObj);
});

await migrator.migrateTo(argv.migration);
break;
case 'list':
versions.forEach((v) => console.log(v));
break;
case 'status':
break;
default:
console.log('help');
break;
}

if (config.backup) { }

process.exit();
})();
75 changes: 53 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@types/lodash": "4.14.120",
"@types/mongodb": "3.1.19",
"@types/node": "10.12.21",
"esm": "^3.2.18",
"jest": "24.1.0",
"tslint": "5.12.1",
"tslint-eslint-rules": "5.4.0",
Expand Down
2 changes: 1 addition & 1 deletion src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class Migration {
if (version === 'latest') {
await this.execute(_.last<any>(this._list).version);
} else {
await this.execute(parseInt(version as string, null), (subcommand === 'rerun'));
await this.execute(parseFloat(version as string), (subcommand === 'rerun'));
}
} catch (e) {
this.options.
Expand Down

0 comments on commit 87dccd7

Please sign in to comment.