Skip to content

Commit

Permalink
Add eslint-plugin-unicorn
Browse files Browse the repository at this point in the history
  • Loading branch information
perrin4869 committed May 18, 2022
1 parent b1ec186 commit 092c81b
Show file tree
Hide file tree
Showing 15 changed files with 823 additions and 55 deletions.
11 changes: 9 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": "airbnb-base",
"extends": ["airbnb-base", "plugin:unicorn/recommended"],
"plugins": ["unicorn"],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
Expand All @@ -18,6 +19,12 @@
"import/no-dynamic-require": "off",
"max-len": ["error", { "code": 140 }],
"no-param-reassign": "off",
"no-use-before-define": "off"
"no-use-before-define": "off",
"unicorn/prevent-abbreviations": "off",
"unicorn/no-nested-ternary": "off",
"unicorn/no-array-push-push": "off",
"unicorn/no-null": "off",
"unicorn/no-useless-promise-resolve-reject": "off",
"unicorn/catch-error-name": ["error", { "name": "e" }]
}
}
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node

import { argv } from 'process';
import { argv } from 'node:process';
import { run } from './lib/postgrator-cli.js'; // eslint-disable-line import/extensions

run(argv).catch((err) => {
console.log(`Error: ${err.message}`);
run(argv).catch((e) => {
console.log(`Error: ${e.message}`);
process.exit(1);
});
2 changes: 1 addition & 1 deletion lib/clients/mssql.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default ({
connect: () => connection.connect(),
query: (query) => new Promise((resolve, reject) => {
const request = new Request(connection);
const batches = query.split(/^\s*GO\s*$/im);
const batches = query.split(/^\s*go\s*$/im);

function runBatch(batchIndex) {
request.batch(batches[batchIndex], (err, result) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/clients/mysql.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promisify } from 'util';
import { promisify } from 'node:util';
import { createConnection } from 'mysql';

export default ({ username, ...config }) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/clients/sqlite3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promisify } from 'util';
import { promisify } from 'node:util';
import sqlite3 from 'sqlite3';

export default ({ database }) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/command-line-options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint max-len: 0 */

import { readFileSync } from 'fs';
import { readFileSync } from 'node:fs';
import commandLineArgs from 'command-line-args';

import parse, { optionDefinitions as extraDefinitions } from './commands/index.js'; // eslint-disable-line import/extensions
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/drop-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export default (postgrator) => {
const schemaTable = postgrator.commonClient.quotedSchemaTable();
return postgrator
.runQuery(`DROP TABLE ${schemaTable}`)
.then(() => undefined);
.then(() => {});
};
7 changes: 3 additions & 4 deletions lib/commands/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import migrate, { COMMAND_MIGRATE, optionDefinitions as migrateDefinitions } from './migrate.js'; // eslint-disable-line import/extensions
import dropSchema, { COMMAND_DROP_SCHEMA } from './drop-schema.js'; // eslint-disable-line import/extensions

export const optionDefinitions = migrateDefinitions;

export default (command, defaults = {}) => {
if (command === 'max') {
return {
Expand All @@ -14,7 +12,7 @@ export default (command, defaults = {}) => {
if (isPositiveInteger(command)) {
return {
command: migrate,
constants: { to: parseInt(command, 10) },
constants: { to: Number.parseInt(command, 10) },
};
}

Expand All @@ -37,6 +35,7 @@ export default (command, defaults = {}) => {
};

function isPositiveInteger(val) {
const parsed = parseFloat(val);
const parsed = Number.parseFloat(val);
return Number.isInteger(parsed) && parsed >= 0;
}
export { optionDefinitions } from './migrate.js'; // eslint-disable-line import/extensions
2 changes: 1 addition & 1 deletion lib/commands/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function getMigrateToVersion(to, postgrator) {
}

function checkMigrations(migrations, migrationPattern) {
if (!migrations || !migrations.length) {
if (!migrations || migrations.length === 0) {
return Promise.reject(new Error(`No migration files found from "${migrationPattern}"`));
}
return Promise.resolve();
Expand Down
14 changes: 7 additions & 7 deletions lib/postgrator-cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs';
import path from 'path';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import getUsage from 'command-line-usage';
import Postgrator from 'postgrator';
import { lilconfig } from 'lilconfig';
Expand Down Expand Up @@ -46,7 +46,7 @@ const getDefaults = async (loadPath) => {
? explorer
.load(loadPath)
.catch(
tap.catch((err) => err.code === 'ENOENT' && Promise.reject(new Error(`Config file not found: ${loadPath}`))),
tap.catch((e) => e.code === 'ENOENT' && Promise.reject(new Error(`Config file not found: ${loadPath}`))),
)
: explorer.search()
).then((res) => res?.config ?? {});
Expand Down Expand Up @@ -82,7 +82,7 @@ const getClientOptions = async (options) => ({
*/
async function promptPassword() {
// Ask password if it is not set
const { default: readline } = await import('readline');
const { default: readline } = await import('node:readline');

const rl = readline.createInterface({
input: process.stdin,
Expand Down Expand Up @@ -150,12 +150,12 @@ export async function run(argv) {
...postgratorOptions,
execQuery: client.query,
});
} catch (err) {
} catch (e) {
printUsage();
return Promise.reject(err);
return Promise.reject(e);
}

return command(postgrator, postgratorOptions)
.then(tap(() => client.end()))
.catch((err) => Promise.reject(err && typeof err === 'string' ? new Error(err) : err));
.catch((e) => Promise.reject(e && typeof e === 'string' ? new Error(e) : e));
}
Loading

0 comments on commit 092c81b

Please sign in to comment.