Skip to content

Commit

Permalink
fix: handle npm v7 overwriting node_modules symlink (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Dec 2, 2022
1 parent 7012f85 commit ff20a34
Show file tree
Hide file tree
Showing 10 changed files with 26,739 additions and 8,797 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"refresh:example-basic": "cd examples/basic && rm yarn.lock && yarn && rm -rf node_modules",
"refresh:example-side-by-side-admin": "cd examples/side-by-side/admin && rm yarn.lock && yarn && rm -rf node_modules",
"refresh:example-side-by-side-app": "cd examples/side-by-side/app && rm yarn.lock && yarn && rm -rf node_modules",
"refresh:fixture": "cd test/fixture/www && rm yarn.lock && yarn && rm -rf node_modules",
"refresh:fixture": "cd test/fixture/www && rm package-lock.json && npm install && rm -rf node_modules",
"refresh:fixture-generated": "cd test/fixture-generated/www && rm yarn.lock && yarn && rm -rf node_modules",
"refresh:fixture-ts": "cd test/fixture-ts/www && rm yarn.lock && yarn && rm -rf node_modules",
"release": "release-it",
Expand Down
6 changes: 5 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { gte, gt } from 'semver'
import { update as updaterc } from 'rc9'
import { hasProtocol } from 'ufo'

import { endStep, exec, getNuxtConfig, getNuxtConfigName, globAndPrefix, MutablePackageJson, prepareNodeModules, preparePkgForProd, readJSON, startStep, validateEntrypoint } from './utils'
import { endStep, exec, getNuxtConfig, getNuxtConfigName, globAndPrefix, MutablePackageJson, prepareNodeModules, backupNodeModules, preparePkgForProd, readJSON, startStep, validateEntrypoint } from './utils'
import { prepareTypescriptEnvironment, compileTypescriptBuildFiles, JsonOptions } from './typescript'

interface BuilderOutput {
Expand Down Expand Up @@ -178,6 +178,8 @@ export async function build (opts: BuildOptions & { config: NuxtBuilderConfig })
], spawnOpts)
}

await backupNodeModules(entrypointPath, 'node_modules_dev')

// ----------------- Install dependencies -----------------
startStep('Install dependencies')

Expand Down Expand Up @@ -217,6 +219,8 @@ export async function build (opts: BuildOptions & { config: NuxtBuilderConfig })
await fs.unlink('.npmrc')
}

await backupNodeModules(entrypointPath, 'node_modules_prod')

// ----------------- Collect artifacts -----------------
startStep('Collect artifacts')

Expand Down
46 changes: 27 additions & 19 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ export function globAndPrefix (pattern: string, opts: IOptions | string, prefix:
}

interface NuxtVersion {
name: string;
version: string;
semver: string;
suffix: string;
section: string;
name: string
version: string
semver: string
suffix: string
section: string
}

export function findNuxtDep (pkg: MutablePackageJson): void | NuxtVersion {
Expand Down Expand Up @@ -192,28 +192,36 @@ export function getNuxtConfigName (rootDir: string): string {
throw new Error(`Can not read nuxt.config from ${rootDir}`)
}

export async function prepareNodeModules (entrypointPath: string, modulesDir: string): Promise<void> {
export async function prepareNodeModules (entrypointPath: string, namespaceDir: string): Promise<void> {
const modulesPath = path.join(entrypointPath, 'node_modules')

try {
const prodPath = path.join(entrypointPath, modulesDir)
if (fs.existsSync(prodPath)) {
consola.log(`Using cached ${modulesDir}`)
}
const namespacedPath = path.join(entrypointPath, namespaceDir)
try {
if (fs.existsSync(modulesPath)) {
await fs.unlink(modulesPath)
}
await fs.mkdirp(modulesDir)
} catch {
if (fs.existsSync(prodPath)) {
fs.rmdirSync(modulesPath, { recursive: true })
} else {
fs.moveSync(modulesPath, prodPath)
}
} catch {}
if (fs.existsSync(namespacedPath)) {
consola.log(`Using cached ${namespaceDir}`)
fs.moveSync(namespaceDir, modulesPath)
}
} catch (e) {
consola.log(`Error creating ${namespaceDir}.`, e)
}
}

export async function backupNodeModules (entrypointPath: string, namespaceDir: string): Promise<void> {
const modulesPath = path.join(entrypointPath, 'node_modules')

try {
const namespacedPath = path.join(entrypointPath, namespaceDir)
const stats = await fs.stat(modulesPath)
if (stats.isDirectory()) {
await fs.rm(namespacedPath, { force: true, recursive: true })
await fs.move(modulesPath, namespacedPath)
}
await fs.symlink(modulesDir, modulesPath)
} catch (e) {
consola.log(`Error linking/unlinking ${modulesDir}.`, e)
consola.log(`Error backing up node_modules to ${namespaceDir}.`, e)
}
}
2 changes: 1 addition & 1 deletion test/fixture-generated/www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"now-build": "echo 1 > static/now-build"
},
"devDependencies": {
"nuxt": "latest"
"nuxt": "^2.0.0"
},
"engines": {
"node": ">=10.x"
Expand Down

0 comments on commit ff20a34

Please sign in to comment.