Skip to content

Commit

Permalink
Use util.promisify instead of Bluebird.promisify (#3470)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximelkin authored and kibertoad committed Oct 15, 2019
1 parent c9e3057 commit 5317148
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
17 changes: 11 additions & 6 deletions bin/cli.js
@@ -1,14 +1,14 @@
#!/usr/bin/env node
/* eslint no-console:0, no-var:0 */
const Liftoff = require('liftoff');
const Bluebird = require('bluebird');
const interpret = require('interpret');
const path = require('path');
const tildify = require('tildify');
const commander = require('commander');
const color = require('colorette');
const argv = require('getopts')(process.argv.slice(2));
const fs = Bluebird.promisifyAll(require('fs'));
const fs = require('fs');
const { promisify } = require('util');
const cliPkg = require('../package');
const {
mkConfigObj,
Expand All @@ -24,6 +24,11 @@ const {

const { listMigrations } = require('./utils/migrationsLister');

const fsPromised = {
readFile: promisify(fs.readFile),
writeFile: promisify(fs.writeFile),
};

function initKnex(env, opts) {
checkLocalModule(env);
if (process.cwd() !== env.cwd) {
Expand Down Expand Up @@ -124,15 +129,15 @@ function invoke(env) {
}
checkLocalModule(env);
const stubPath = `./knexfile.${type}`;
pending = fs
.readFileAsync(
pending = fsPromised
.readFile(
path.dirname(env.modulePath) +
'/lib/migrate/stub/knexfile-' +
type +
'.stub'
)
.then((code) => {
return fs.writeFileAsync(stubPath, code);
return fsPromised.writeFile(stubPath, code);
})
.then(() => {
success(color.green(`Created ${stubPath}`));
Expand Down Expand Up @@ -338,7 +343,7 @@ function invoke(env) {

commander.parse(process.argv);

Bluebird.resolve(pending).then(() => {
Promise.resolve(pending).then(() => {
commander.outputHelp();
exit('Unknown command-line options, exiting');
});
Expand Down
3 changes: 2 additions & 1 deletion lib/client.js
Expand Up @@ -19,6 +19,7 @@ const ColumnCompiler = require('./schema/columncompiler');
const { Pool, TimeoutError } = require('tarn');
const inherits = require('inherits');
const { EventEmitter } = require('events');
const { promisify } = require('util');

const { makeEscape } = require('./query/string');
const { uniqueId, cloneDeep, defaults } = require('lodash');
Expand Down Expand Up @@ -259,7 +260,7 @@ Object.assign(Client.prototype, {
connection.__knexUid = uniqueId('__knexUid');

if (poolConfig.afterCreate) {
await Bluebird.promisify(poolConfig.afterCreate)(connection);
await promisify(poolConfig.afterCreate)(connection);
}
return connection;
});
Expand Down
2 changes: 1 addition & 1 deletion lib/migrate/MigrationGenerator.js
@@ -1,7 +1,7 @@
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const mkdirp = require('mkdirp');
const {promisify} = require('util');
const { writeJsFileUsingTemplate } = require('../util/template');
const { getMergedConfig } = require('./configuration-merger');

Expand Down
5 changes: 2 additions & 3 deletions lib/migrate/sources/fs-migrations.js
@@ -1,10 +1,9 @@
const fs = require('fs');
const path = require('path');
const Bluebird = require('bluebird');
const { promisify } = require('util');
const { sortBy, filter } = require('lodash');

const readDirAsync = (path) =>
Bluebird.promisify(fs.readdir, { context: fs })(path);
const readDirAsync = promisify(fs.readdir);

const DEFAULT_LOAD_EXTENSIONS = Object.freeze([
'.co',
Expand Down
20 changes: 11 additions & 9 deletions lib/seed/Seeder.js
Expand Up @@ -3,6 +3,7 @@

const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const mkdirp = require('mkdirp');
const Bluebird = require('bluebird');
const {
Expand Down Expand Up @@ -50,12 +51,11 @@ class Seeder {
async _listAll(config) {
this.config = this.setConfig(config);
const loadExtensions = this.config.loadExtensions;
return Bluebird.promisify(fs.readdir, { context: fs })(
return promisify(fs.readdir)(
this._absoluteConfigDir()
)
.bind(this)
.then((seeds) =>
filter(seeds, function(value) {
filter(seeds, (value) => {
const extension = path.extname(value);
return includes(loadExtensions, extension);
}).sort()
Expand All @@ -69,11 +69,13 @@ class Seeder {

// Ensures a folder for the seeds exist, dependent on the
// seed config settings.
_ensureFolder() {
async _ensureFolder() {
const dir = this._absoluteConfigDir();
return Bluebird.promisify(fs.stat, { context: fs })(dir).catch(() =>
Bluebird.promisify(mkdirp)(dir)
);
try {
await promisify(fs.stat)(dir);
} catch (e) {
await promisify(mkdirp)(dir);
}
}

// Run seed files, in sequence.
Expand Down Expand Up @@ -106,7 +108,7 @@ class Seeder {
// Generates the stub template for the current seed file, returning a compiled template.
_generateStubTemplate() {
const stubPath = this._getStubPath();
return Bluebird.promisify(fs.readFile, { context: fs })(stubPath).then(
return promisify(fs.readFile)(stubPath).then(
(stub) => template(stub.toString(), { variable: 'd' })
);
}
Expand All @@ -118,7 +120,7 @@ class Seeder {

_getNewStubFilePath(name) {
return path.join(this._absoluteConfigDir(), this._getNewStubFileName(name));
}
}

// Write a new seed to disk, using the config and generated filename,
// passing any `variables` given in the config to the template.
Expand Down
13 changes: 6 additions & 7 deletions test/knexfile.js
Expand Up @@ -2,10 +2,10 @@
/* eslint no-var: 0 */

const assert = require('assert');
const { promisify } = require('util');
const testConfig =
(process.env.KNEX_TEST && require(process.env.KNEX_TEST)) || {};
const _ = require('lodash');
const Bluebird = require('bluebird');

// excluding redshift, oracle, and mssql dialects from default integrations test
const testIntegrationDialects = (
Expand All @@ -31,12 +31,11 @@ const poolSqlite = {

const mysqlPool = _.extend({}, pool, {
afterCreate: function(connection, callback) {
Bluebird.promisify(connection.query, { context: connection })(
"SET sql_mode='TRADITIONAL';",
[]
).then(function() {
callback(null, connection);
});
promisify(connection.query)
.call(connection, "SET sql_mode='TRADITIONAL';", [])
.then(function() {
callback(null, connection);
});
},
});

Expand Down

0 comments on commit 5317148

Please sign in to comment.