From 497d6737273c6fe878d4b6021619587e1108a365 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 11 May 2017 15:53:58 +0430 Subject: [PATCH] Install all modules in sequence before nuxt init For this functionality Nuxt constructor would return a Promise so all bin commands updated to resolve promise before using nuxt --- bin/nuxt-build | 17 +++++++++-------- bin/nuxt-dev | 17 +++++++++-------- bin/nuxt-generate | 17 +++++++++-------- bin/nuxt-start | 14 +++++++------- lib/nuxt.js | 9 ++++++++- lib/utils.js | 4 ++++ 6 files changed, 46 insertions(+), 32 deletions(-) diff --git a/bin/nuxt-build b/bin/nuxt-build index 50dbc246b8..c85d1b6fed 100755 --- a/bin/nuxt-build +++ b/bin/nuxt-build @@ -52,12 +52,13 @@ if (analyzeBuild) { } console.log('[nuxt] Building...') // eslint-disable-line no-console -var nuxt = new Nuxt(options) -nuxt.build() -.then(() => { - console.log('[nuxt] Building done') // eslint-disable-line no-console -}) -.catch((err) => { - console.error(err) // eslint-disable-line no-console - process.exit(1) +new Nuxt(options).then(nuxt => { + nuxt.build() + .then(() => { + console.log('[nuxt] Building done') // eslint-disable-line no-console + }) + .catch((err) => { + console.error(err) // eslint-disable-line no-console + process.exit(1) + }) }) diff --git a/bin/nuxt-dev b/bin/nuxt-dev index 0c2c1157a9..f6b3aace46 100755 --- a/bin/nuxt-dev +++ b/bin/nuxt-dev @@ -39,15 +39,16 @@ if (typeof options.rootDir !== 'string') { } options.dev = true // Add hot reloading and watching changes -var nuxt = new Nuxt(options) -var server = new nuxt.Server(nuxt) -.listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host) -listenOnConfigChanges(nuxt, server) +new Nuxt(options).then(nuxt => { + var server = new nuxt.Server(nuxt) + .listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host) + listenOnConfigChanges(nuxt, server) -nuxt.build() -.catch((err) => { - console.error(err) // eslint-disable-line no-console - process.exit(1) + nuxt.build() + .catch((err) => { + console.error(err) // eslint-disable-line no-console + process.exit(1) + }) }) function listenOnConfigChanges (nuxt, server) { diff --git a/bin/nuxt-generate b/bin/nuxt-generate index 10b1c4ff70..8d085b666c 100755 --- a/bin/nuxt-generate +++ b/bin/nuxt-generate @@ -20,12 +20,13 @@ if (typeof options.rootDir !== 'string') { options.dev = false // Force production mode (no webpack middleware called) console.log('[nuxt] Generating...') // eslint-disable-line no-console -var nuxt = new Nuxt(options) -nuxt.generate() -.then(() => { - console.log('[nuxt] Generate done') // eslint-disable-line no-console -}) -.catch((err) => { - console.error(err) // eslint-disable-line no-console - process.exit(1) +new Nuxt(options).then(nuxt => { + nuxt.generate() + .then(() => { + console.log('[nuxt] Generate done') // eslint-disable-line no-console + }) + .catch((err) => { + console.error(err) // eslint-disable-line no-console + process.exit(1) + }) }) diff --git a/bin/nuxt-start b/bin/nuxt-start index 9190178576..f39daf9b19 100755 --- a/bin/nuxt-start +++ b/bin/nuxt-start @@ -16,10 +16,10 @@ if (typeof options.rootDir !== 'string') { } options.dev = false // Force production mode (no webpack middleware called) -var nuxt = new Nuxt(options) - -new nuxt.Server(nuxt) -.listen( - process.env.PORT || process.env.npm_package_config_nuxt_port, - process.env.HOST || process.env.npm_package_config_nuxt_host -) +new Nuxt(options).then(nuxt => { + new nuxt.Server(nuxt) + .listen( + process.env.PORT || process.env.npm_package_config_nuxt_port, + process.env.HOST || process.env.npm_package_config_nuxt_host + ) +}) diff --git a/lib/nuxt.js b/lib/nuxt.js index c1637f3878..2d03d55e3d 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -114,7 +114,14 @@ class Nuxt { this.utils = utils // Add module integration this.module = new Module(this) - return this + // Install all modules in sequence and then return `this` instance + return utils.sequence(options.modules, this.module.installModule.bind(this.module)) + .then(() => this) + .catch((err) => { + console.error('[nuxt] error while initializing modules') // eslint-disable-line no-console + console.error(err) // eslint-disable-line no-console + process.exit(1) + }) } close (callback) { diff --git a/lib/utils.js b/lib/utils.js index edea410fdb..58ea779d46 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -57,3 +57,7 @@ export function promisifyRoute (fn) { } return promise } + +export function sequence (tasks, fn) { + return tasks.reduce((promise, task) => promise.then(() => fn(task)), Promise.resolve()) +}