diff --git a/packages/cli/src/command.js b/packages/cli/src/command.js
index 0f1d4bb74d..0033811252 100644
--- a/packages/cli/src/command.js
+++ b/packages/cli/src/command.js
@@ -1,4 +1,4 @@
-
+import consola from 'consola'
import minimist from 'minimist'
import { name, version } from '../package.json'
import { loadNuxtConfig, forceExit } from './utils'
@@ -45,6 +45,10 @@ export default class NuxtCommand {
const runResolve = Promise.resolve(this.cmd.run(this))
+ if (this.argv.lock) {
+ runResolve.then(() => this.releaseLock())
+ }
+
if (this.argv['force-exit']) {
const forceExitByUser = this.isUserSuppliedArg('force-exit')
runResolve.then(() => forceExit(this.cmd.name, forceExitByUser ? false : forceExitTimeout))
@@ -71,7 +75,7 @@ export default class NuxtCommand {
async getNuxtConfig(extraOptions) {
const config = await loadNuxtConfig(this.argv)
- const options = Object.assign(config, extraOptions || {})
+ const options = Object.assign(config, extraOptions)
for (const name of Object.keys(this.cmd.options)) {
this.cmd.options[name].prepare && this.cmd.options[name].prepare(this, options, this.argv)
@@ -99,6 +103,26 @@ export default class NuxtCommand {
return new Generator(nuxt, builder)
}
+ async setLock(lockRelease) {
+ if (lockRelease) {
+ if (this._lockRelease) {
+ consola.warn(`A previous unreleased lock was found, this shouldn't happen and is probably an error in 'nuxt ${this.cmd.name}' command. The lock will be removed but be aware of potential strange results`)
+
+ await this.releaseLock()
+ this._lockRelease = lockRelease
+ } else {
+ this._lockRelease = lockRelease
+ }
+ }
+ }
+
+ async releaseLock() {
+ if (this._lockRelease) {
+ await this._lockRelease()
+ this._lockRelease = undefined
+ }
+ }
+
isUserSuppliedArg(option) {
return this._argv.includes(`--${option}`) || this._argv.includes(`--no-${option}`)
}
diff --git a/packages/cli/src/commands/build.js b/packages/cli/src/commands/build.js
index 48fa34f9b6..1787da6397 100644
--- a/packages/cli/src/commands/build.js
+++ b/packages/cli/src/commands/build.js
@@ -1,4 +1,5 @@
-import { common } from '../options'
+import { common, locking } from '../options'
+import { createLock } from '../utils'
export default {
name: 'build',
@@ -6,6 +7,7 @@ export default {
usage: 'build
',
options: {
...common,
+ ...locking,
analyze: {
alias: 'a',
type: 'boolean',
@@ -62,6 +64,14 @@ export default {
const config = await cmd.getNuxtConfig({ dev: false })
const nuxt = await cmd.getNuxt(config)
+ if (cmd.argv.lock) {
+ await cmd.setLock(await createLock({
+ id: 'build',
+ dir: nuxt.options.buildDir,
+ root: config.rootDir
+ }))
+ }
+
if (nuxt.options.mode !== 'spa' || cmd.argv.generate === false) {
// Build only
const builder = await cmd.getBuilder(nuxt)
diff --git a/packages/cli/src/commands/generate.js b/packages/cli/src/commands/generate.js
index 52c5cbf18c..5945a42986 100644
--- a/packages/cli/src/commands/generate.js
+++ b/packages/cli/src/commands/generate.js
@@ -1,5 +1,5 @@
-import { common } from '../options'
-import { normalizeArg } from '../utils'
+import { common, locking } from '../options'
+import { normalizeArg, createLock } from '../utils'
export default {
name: 'generate',
@@ -7,6 +7,7 @@ export default {
usage: 'generate ',
options: {
...common,
+ ...locking,
build: {
type: 'boolean',
default: true,
@@ -44,6 +45,25 @@ export default {
config.build.analyze = false
const nuxt = await cmd.getNuxt(config)
+
+ if (cmd.argv.lock) {
+ await cmd.setLock(await createLock({
+ id: 'build',
+ dir: nuxt.options.buildDir,
+ root: config.rootDir
+ }))
+
+ nuxt.hook('build:done', async () => {
+ await cmd.releaseLock()
+
+ await cmd.setLock(await createLock({
+ id: 'generate',
+ dir: nuxt.options.generate.dir,
+ root: config.rootDir
+ }))
+ })
+ }
+
const generator = await cmd.getGenerator(nuxt)
await generator.generate({
diff --git a/packages/cli/src/options/index.js b/packages/cli/src/options/index.js
index 347fa2c72a..21ddcaeca2 100644
--- a/packages/cli/src/options/index.js
+++ b/packages/cli/src/options/index.js
@@ -1,2 +1,3 @@
export { default as common } from './common'
export { default as server } from './server'
+export { default as locking } from './locking'
diff --git a/packages/cli/src/options/locking.js b/packages/cli/src/options/locking.js
new file mode 100644
index 0000000000..28035510ae
--- /dev/null
+++ b/packages/cli/src/options/locking.js
@@ -0,0 +1,7 @@
+export default {
+ lock: {
+ type: 'boolean',
+ default: true,
+ description: 'Do not set a lock on the project when building'
+ }
+}
diff --git a/packages/cli/src/utils/index.js b/packages/cli/src/utils/index.js
index 7050125164..b2f22d13b6 100644
--- a/packages/cli/src/utils/index.js
+++ b/packages/cli/src/utils/index.js
@@ -5,6 +5,7 @@ import esm from 'esm'
import exit from 'exit'
import defaultsDeep from 'lodash/defaultsDeep'
import { defaultNuxtConfigFile, getDefaultNuxtConfig } from '@nuxt/config'
+import { lock } from '@nuxt/utils'
import chalk from 'chalk'
import prettyBytes from 'pretty-bytes'
import env from 'std-env'
@@ -158,3 +159,9 @@ ${chalk.bold('DeprecationWarning: Starting with Nuxt version 3 this will be a fa
exit(0)
}
}
+
+// An immediate export throws an error when mocking with jest
+// TypeError: Cannot set property createLock of #