Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Sep 27, 2022
1 parent d825081 commit ff52c24
Showing 1 changed file with 17 additions and 31 deletions.
48 changes: 17 additions & 31 deletions test/lib/next-modes/base.ts
Expand Up @@ -154,6 +154,18 @@ export class NextInstance {
}

const tempNodeModulesFolder = 'node_modules_bak'
// Merge customized node_modules and installed node_modules.
// Move files[node_modules] to a temporary folder node_modules_back,
// then copy the modules back to node_modules after installation.
const tempNodeModulesPath = path.join(this.testDir, tempNodeModulesFolder)
const nodeModulesDir = path.join(this.testDir, 'node_modules')
const nodeModulesStats = await fs.stat(nodeModulesDir)
const hasNodeModulesDir = nodeModulesStats.isDirectory()
if (hasNodeModulesDir) {
// Move node_modules to temp folder
await fs.move(nodeModulesDir, tempNodeModulesPath)
}

if (this.files instanceof FileRef) {
// if a FileRef is passed directly to `files` we copy the
// entire folder to the test directory
Expand All @@ -165,35 +177,11 @@ export class NextInstance {
)
}

// Merge customized node_modules and installed node_modules.
// Move files[node_modules] to a temporary folder node_modules_back,
// then copy the modules back to node_modules after installation.
const tempNodeModulesPath = path.join(this.testDir, tempNodeModulesFolder)
const nodeModulesDir = path.join(this.testDir, 'node_modules')
const nodeModulesStats = await fs.stat(this.files.fsPath)
const hasNodeModulesDir = nodeModulesStats.isDirectory()
if (hasNodeModulesDir) {
// Move node_modules to temp folder
await fs.move(nodeModulesDir, tempNodeModulesPath)
}
await fs.copy(this.files.fsPath, this.testDir)
if (hasNodeModulesDir) {
// Move node_modules from temp back to origin
await fs.copy(tempNodeModulesPath, nodeModulesDir)
await fs.remove(tempNodeModulesPath)
}
} else {
let nodeModulesFolder: string | undefined
for (const filename of Object.keys(this.files)) {
const item = this.files[filename]
let outputFilename = path.join(this.testDir, filename)
if (filename === 'node_modules') {
nodeModulesFolder = path.join(
this.testDir,
typeof item === 'string' ? item : item.fsPath
)
outputFilename = path.join(this.testDir, tempNodeModulesFolder)
}

if (typeof item === 'string') {
await fs.ensureDir(path.dirname(outputFilename))
Expand All @@ -202,14 +190,12 @@ export class NextInstance {
await fs.copy(item.fsPath, outputFilename)
}
}
}

if (nodeModulesFolder) {
await fs.copy(
nodeModulesFolder,
path.join(this.testDir, 'node_modules')
)
await fs.remove(nodeModulesFolder)
}
if (hasNodeModulesDir) {
// Move node_modules from temp back to origin
await fs.copy(tempNodeModulesPath, nodeModulesDir)
await fs.remove(tempNodeModulesPath)
}

let nextConfigFile = Object.keys(this.files).find((file) =>
Expand Down

0 comments on commit ff52c24

Please sign in to comment.