Skip to content

Commit

Permalink
Allow passing FileRef directly to createNext (#37664)
Browse files Browse the repository at this point in the history
* Allow passing FileRef directly to createNext

* update type
  • Loading branch information
ijjk authored Jun 13, 2022
1 parent b62bb97 commit 3b9f180
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
8 changes: 5 additions & 3 deletions test/lib/e2e-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ if (typeof afterAll === 'function') {
* to prevent relying on modules that shouldn't be
*/
export async function createNext(opts: {
files: {
[filename: string]: string | FileRef
}
files:
| FileRef
| {
[filename: string]: string | FileRef
}
dependencies?: {
[name: string]: string
}
Expand Down
45 changes: 31 additions & 14 deletions test/lib/next-modes/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export type PackageJson = {
[key: string]: unknown
}
export class NextInstance {
protected files: {
[filename: string]: string | FileRef
}
protected files:
| FileRef
| {
[filename: string]: string | FileRef
}
protected nextConfig?: NextConfig
protected installCommand?: InstallCommand
protected buildCommand?: string
Expand Down Expand Up @@ -47,9 +49,11 @@ export class NextInstance {
packageLockPath,
env,
}: {
files: {
[filename: string]: string | FileRef
}
files:
| FileRef
| {
[filename: string]: string | FileRef
}
dependencies?: {
[name: string]: string
}
Expand Down Expand Up @@ -148,15 +152,28 @@ export class NextInstance {
require('console').log('created next.js install, writing test files')
}

for (const filename of Object.keys(this.files)) {
const item = this.files[filename]
const outputfilename = path.join(this.testDir, filename)
if (this.files instanceof FileRef) {
// if a FileRef is passed directly to `files` we copy the
// entire folder to the test directory
const stats = await fs.stat(this.files.fsPath)

if (!stats.isDirectory()) {
throw new Error(
`FileRef passed to "files" in "createNext" is not a directory ${this.files.fsPath}`
)
}
await fs.copy(this.files.fsPath, this.testDir)
} else {
for (const filename of Object.keys(this.files)) {
const item = this.files[filename]
const outputFilename = path.join(this.testDir, filename)

if (typeof item === 'string') {
await fs.ensureDir(path.dirname(outputfilename))
await fs.writeFile(outputfilename, item)
} else {
await fs.copy(item.fsPath, outputfilename)
if (typeof item === 'string') {
await fs.ensureDir(path.dirname(outputFilename))
await fs.writeFile(outputFilename, item)
} else {
await fs.copy(item.fsPath, outputFilename)
}
}
}

Expand Down

0 comments on commit 3b9f180

Please sign in to comment.