Unverified Commit 3b9f180d authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Allow passing FileRef directly to createNext (#37664)

* Allow passing FileRef directly to createNext

* update type
parent b62bb97b
......@@ -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
}
......
......@@ -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
......@@ -47,9 +49,11 @@ export class NextInstance {
packageLockPath,
env,
}: {
files: {
[filename: string]: string | FileRef
}
files:
| FileRef
| {
[filename: string]: string | FileRef
}
dependencies?: {
[name: string]: string
}
......@@ -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)
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment