diff --git a/package.json b/package.json index f28b4dae..a71964c3 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "babel-plugin-transform-es2015-modules-commonjs": "6.18.0", "babel-preset-lux": "1.3.0", "chai": "3.5.0", - "eslint-config-airbnb-base": "10.0.1", + "eslint-config-airbnb-base": "11.0.0", "eslint-plugin-flowtype": "2.29.1", "eslint-plugin-import": "2.2.0", "faker": "3.1.0", diff --git a/src/packages/cli/commands/dbmigrate.js b/src/packages/cli/commands/dbmigrate.js index 0e49377f..b9456249 100644 --- a/src/packages/cli/commands/dbmigrate.js +++ b/src/packages/cli/commands/dbmigrate.js @@ -4,6 +4,7 @@ import { CWD } from '../../../constants'; import Database, { pendingMigrations } from '../../database'; import Logger, { sql } from '../../logger'; import { createLoader } from '../../loader'; +import { composeAsync } from '../../../utils/compose'; /** * @private @@ -29,23 +30,32 @@ export async function dbmigrate() { const pending = await pendingMigrations(CWD, () => connection('migrations')); if (pending.length) { - for (const migration of pending) { - const version = migration.replace(/^(\d{16})-.+$/g, '$1'); - const key = migration.replace(new RegExp(`${version}-(.+)\\.js`), '$1'); - const value = migrations.get(`${key}-up`); - - if (value) { - const query = value.run(schema()); - - await query.on('query', () => { - process.stdout.write(sql`${query.toString()}`); - process.stdout.write(EOL); - }); - - await connection('migrations').insert({ - version - }); - } - } + const runners = pending + .map(name => { + const version = name.replace(/^(\d{16})-.+$/g, '$1'); + const key = name.replace(new RegExp(`${version}-(.+)\\.js`), '$1'); + + return [version, migrations.get(`${key}-up`)]; + }) + .filter(([, migration]) => Boolean(migration)) + .reverse() + .map(([version, migration]) => () => { + const query = migration.run(schema()); + + return query + .on('query', () => { + process.stdout.write(sql`${query.toString()}`); + process.stdout.write(EOL); + }) + .then(() => ( + connection('migrations').insert({ + version + }) + )); + }); + + await composeAsync(...runners)(); } + + return true; } diff --git a/src/packages/compiler/index.js b/src/packages/compiler/index.js index 278c8fbf..e951b7bf 100644 --- a/src/packages/compiler/index.js +++ b/src/packages/compiler/index.js @@ -129,7 +129,7 @@ export async function compile(dir: string, env: string, { banner = `'use strict';\n\n${banner}`; } - return await bundle.write({ + return bundle.write({ banner, dest: path.join(dir, 'dist', 'bundle.js'), format: 'cjs', diff --git a/src/packages/compiler/utils/create-manifest.js b/src/packages/compiler/utils/create-manifest.js index 9e80ac0a..b61939bb 100644 --- a/src/packages/compiler/utils/create-manifest.js +++ b/src/packages/compiler/utils/create-manifest.js @@ -7,6 +7,7 @@ import { mkdir, writeFile, appendFile } from '../../fs'; import chain from '../../../utils/chain'; import tryCatch from '../../../utils/try-catch'; import underscore from '../../../utils/underscore'; +import { compose } from '../../../utils/compose'; import stripExt from './strip-ext'; import formatName from './format-name'; @@ -36,31 +37,32 @@ function createWriter(file: string) { const writerFor = ( type: string, handleWrite: void | (value: string) => Promise - ) => async (value: string | Array) => { - for (const item of value) { - if (handleWrite) { - await handleWrite(item); - } else { + ) => (value: Array) => { + const formatSymbol = compose(str => str + capitalize(type), formatName); + + return Promise.all( + value.map(item => { + if (handleWrite) { + return handleWrite(item); + } + const path = joinPath('app', pluralize(type), item); - const name = chain(item) - .pipe(formatName) - .pipe(str => str + capitalize(type)) - .value(); - - await appendFile(file, createExportStatement(name, path)); - } - } + const symbol = formatSymbol(item); + + return appendFile(file, createExportStatement(symbol, path)); + }) + ); }; return { controllers: writerFor('controller'), serializers: writerFor('serializer'), - models: writerFor('model', async (item) => { + models: writerFor('model', async item => { const path = joinPath('app', 'models', item); const name = formatName(item); - return await appendFile(file, createExportStatement(name, path)); + return appendFile(file, createExportStatement(name, path)); }), migrations: writerFor('migration', async (item) => { @@ -102,13 +104,19 @@ export default async function createManifest( await tryCatch(() => mkdir(dist)); await writeFile(file, useStrict ? '\'use strict\';\n\n' : ''); - for (const [key, value] of assets) { - const write = Reflect.get(writer, key); - - if (write) { - await write(value); - } else if (!write && typeof value === 'string') { - await appendFile(file, createExportStatement(key, value)); - } - } + await Promise.all( + Array + .from(assets) + .map(([key, value]) => { + const write = Reflect.get(writer, key); + + if (write) { + return write(value); + } else if (!write && typeof value === 'string') { + return appendFile(file, createExportStatement(key, value)); + } + + return Promise.resolve(); + }) + ); } diff --git a/src/packages/fs/index.js b/src/packages/fs/index.js index 2446be2e..34d4825d 100644 --- a/src/packages/fs/index.js +++ b/src/packages/fs/index.js @@ -18,8 +18,8 @@ export type { fs$ParsedPath } from './interfaces'; /** * @private */ -export async function watch(path: string): Promise { - return await new Watcher(path); +export function watch(path: string): Promise { + return new Watcher(path); } /** diff --git a/src/packages/pm/cluster/index.js b/src/packages/pm/cluster/index.js index bf4561e8..4a12e741 100644 --- a/src/packages/pm/cluster/index.js +++ b/src/packages/pm/cluster/index.js @@ -11,6 +11,7 @@ import { NODE_ENV } from '../../../constants'; import { line } from '../../logger'; import omit from '../../../utils/omit'; import range from '../../../utils/range'; +import { composeAsync } from '../../../utils/compose'; import type Logger from '../../logger'; // eslint-disable-line max-len, no-duplicate-imports import type { Cluster$opts } from './interfaces'; @@ -201,24 +202,27 @@ class Cluster extends EventEmitter { }); } - async reload() { + reload() { if (this.workers.size) { - const workers = Array + const groups = Array .from(this.workers) .reduce((arr, item, idx, src) => { if ((idx + 1) % 2) { - return [...arr, src.slice(idx, idx + 2)]; + const group = src.slice(idx, idx + 2); + + return [ + ...arr, + () => Promise.all(group.map(worker => this.shutdown(worker))) + ]; } return arr; }, []); - for (const group of workers) { - await Promise.all(group.map(worker => this.shutdown(worker))); - } - } else { - await this.fork(); + return composeAsync(...groups)(); } + + return this.fork(); } forkAll() { diff --git a/src/packages/router/route/action/enhancers/resource.js b/src/packages/router/route/action/enhancers/resource.js index 435fa31c..3554887f 100644 --- a/src/packages/router/route/action/enhancers/resource.js +++ b/src/packages/router/route/action/enhancers/resource.js @@ -63,7 +63,7 @@ export default function resource(action: Action): Action { }; } - return await serializer.format({ + return serializer.format({ data, links, domain, diff --git a/src/packages/router/route/index.js b/src/packages/router/route/index.js index a72b0ec7..8745894f 100644 --- a/src/packages/router/route/index.js +++ b/src/packages/router/route/index.js @@ -131,6 +131,7 @@ class Route extends FreezeableSet> { let data; for (const handler of this) { + // eslint-disable-next-line no-await-in-loop data = await handler(req, res, data); if (handler.name === FINAL_HANDLER) { @@ -160,7 +161,7 @@ class Route extends FreezeableSet> { validateResourceId(req); } - return await this.execHandlers(req, res); + return this.execHandlers(req, res); } } diff --git a/src/packages/serializer/index.js b/src/packages/serializer/index.js index d284ebe4..245f44eb 100644 --- a/src/packages/serializer/index.js +++ b/src/packages/serializer/index.js @@ -617,7 +617,7 @@ class Serializer { ) }; } else if (related && related.id) { - return await this.formatRelationship({ + return this.formatRelationship({ domain, included, item: related,