Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
feat: improve CLI output
Browse files Browse the repository at this point in the history
  • Loading branch information
justinlettau committed Oct 7, 2018
1 parent 2573dc2 commit 5d36353
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 51 deletions.
56 changes: 56 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"inquirer": "^6.2.0",
"mssql": "^4.2.1",
"multimatch": "^2.1.0",
"ora": "^3.0.0",
"tedious": "^3.0.0",
"ts-util-is": "^1.1.3",
"update-notifier": "^2.5.0",
Expand All @@ -63,6 +64,7 @@
"@types/jasmine": "^2.8.8",
"@types/mssql": "^4.0.10",
"@types/multimatch": "^2.1.2",
"@types/ora": "^1.3.4",
"@types/xml2js": "^0.4.3",
"checksum": "^0.1.1",
"codecov": "^3.1.0",
Expand Down
45 changes: 28 additions & 17 deletions src/commands/pull.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from 'chalk';
import * as sql from 'mssql';
import * as multimatch from 'multimatch';
import * as ora from 'ora';

import Config from '../common/config';
import Connection from '../common/connection';
Expand All @@ -15,37 +16,50 @@ import {
SqlPrimaryKey,
SqlTable,
SqlType
} from '../sql/interfaces';
import { columnRead, foreignKeyRead, indexRead, objectRead, primaryKeyRead, tableRead, typeRead } from '../sql/sys';
} from '../queries/interfaces';
import {
columnsRead,
foreignKeysRead,
indexesRead,
objectsRead,
primaryKeysRead,
tablesRead,
typesRead
} from '../queries/mssql';
import { PullOptions } from './interfaces';

export default class Pull {

/**
* Spinner instance.
*/
// tslint:disable-next-line:typedef
private spinner = ora();

/**
* Invoke action.
*
* @param name Optional connection name to use.
* @param options CLI options.
*/
public invoke(name: string, options: PullOptions): void {
const start: [number, number] = process.hrtime();
const config: Config = new Config(options.config);
const conn: Connection = config.getConnection(name);

console.log(`Pulling ${chalk.magenta(conn.database)} from ${chalk.magenta(conn.server)} ...`);
this.spinner.start(`Pulling from ${chalk.blue(conn.server)} ...`);

// connect to db
new sql.ConnectionPool(conn)
.connect()
.then(pool => {
return Promise.all<sql.IResult<any>>([
pool.request().query(objectRead),
pool.request().query(tableRead),
pool.request().query(columnRead),
pool.request().query(primaryKeyRead),
pool.request().query(foreignKeyRead),
pool.request().query(indexRead),
pool.request().query(typeRead)
pool.request().query(objectsRead),
pool.request().query(tablesRead),
pool.request().query(columnsRead),
pool.request().query(primaryKeysRead),
pool.request().query(foreignKeysRead),
pool.request().query(indexesRead),
pool.request().query(typesRead)
])
.then(results => {
const tables: string[] = results[1].recordset
Expand All @@ -72,11 +86,7 @@ export default class Pull {
});
})
.then(results => this.writeFiles(config, results))
.then(() => {
const time: [number, number] = process.hrtime(start);
console.log(chalk.green(`Finished after ${time[0]}s!`));
})
.catch(err => console.error(err));
.catch(error => this.spinner.fail(error));
}

/**
Expand Down Expand Up @@ -176,6 +186,7 @@ export default class Pull {
file.write(config.output.data, name, content);
});

file.removeRemaining();
const msg: string = file.finalize();
this.spinner.succeed(msg);
}
}
21 changes: 11 additions & 10 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs-extra';
import * as glob from 'glob';
import * as inquirer from 'inquirer';
import * as sql from 'mssql';
import * as ora from 'ora';
import { EOL } from 'os';

import Config from '../common/config';
Expand All @@ -11,14 +12,19 @@ import { PushOptions } from './interfaces';

export default class Push {

/**
* Spinner instance.
*/
// tslint:disable-next-line:typedef
private spinner = ora();

/**
* Invoke actions.
*
* @param name Optional connection name to use.
* @param options CLI options.
*/
public invoke(name: string, options: PushOptions): void {
const start: [number, number] = process.hrtime();
const config: Config = new Config();
const conn: Connection = config.getConnection(name);

Expand All @@ -38,15 +44,12 @@ export default class Push {
])
.then(answers => {
if (answers.continue === false) {
throw 'Command aborted!';
throw new Error('Command aborted!');
}
})
.then(() => this.batch(config, conn))
.then(() => {
const time: [number, number] = process.hrtime(start);
console.log(chalk.green(`Finished after ${time[0]}s!`));
})
.catch(err => console.error(err));
.then(() => this.spinner.succeed('Sucessfully pushed!'))
.catch(error => this.spinner.fail(error));
}

/**
Expand All @@ -59,11 +62,9 @@ export default class Push {
const files: string[] = this.getFilesOrdered(config);
let promise: Promise<sql.ConnectionPool> = new sql.ConnectionPool(conn).connect();

console.log(`Pushing to ${chalk.magenta(conn.database)} on ${chalk.magenta(conn.server)} ...`);
this.spinner.start(`Pushing to ${chalk.blue(conn.server)} ...`);

files.forEach(file => {
console.log(`Executing ${chalk.cyan(file)} ...`);

const content: string = fs.readFileSync(file, 'utf8');
const statements: string[] = content.split('go' + EOL);

Expand Down
6 changes: 5 additions & 1 deletion src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ export default class Config implements IConfig {
process.exit();
}

return conn;
return Object.assign(conn, {
options: {
encrypt: true
}
});
}

/**
Expand Down
Loading

0 comments on commit 5d36353

Please sign in to comment.