Repository: cypress-io/cypress
File: system-tests/lib/fixtures.ts (TypeScript)

Code before:
    throw err
  }
}

// copy contents instead of deleting+creating new file, which can cause
// filewatchers to lose track of toFile.
const copyContents = (fromFile, toFile) => {
  return Promise.all([
    fs.open(toFile, 'w'),
    fs.readFile(fromFile),
  ])
  .then(([toFd, fromFileBuf]) => {
    return fs.write(toFd, fromFileBuf)
    .finally(() => {
      return fs.close(toFd)
    })
  })
}

// copies all of the project fixtures
// to the cyTmpDir .projects in the root
export function scaffold () {
  // Prevent copying directory into itself
  if (projects === cyTmpDir || projects.startsWith(cyTmpDir) || cyTmpDir.startsWith(projects)) {
    throw new Error(`Cannot copy directory into itself: projects=${projects}, cyTmpDir=${cyTmpDir}`)
  }

  // Ensure temp directory is clean before copying
  try {
    fs.removeSync(cyTmpDir)
  } catch (err) {
    // Ignore errors if directory doesn't exist
  }

  // Create temp directory if it doesn't exist
  fs.ensureDirSync(cyTmpDir)

  // Copy projects to temp directory
  fs.copySync(projects, cyTmpDir)
}

/**
 * Given a project name, copy the project's test files to the temp dir.
 * Returns the scaffolded directory
 */
export async function scaffoldProject (project: ProjectFixtureDir): Promise<string> {
  const to = _path.join(cyTmpDir, project)
  const from = projectFixturePath(project)

  fs.copySync(from, to)


Review comment (suggestion):
There were a bunch of "copying directory into itself" errors when I ran these locally. Like:

```
  9) lib/util/spec_writer "before each" hook for "can generate a command with no message":
     Error: Cannot copy '../acorn/bin/acorn' to a subdirectory of itself, '../acorn/bin/acorn'.
```


Code after:
    throw err
  }
}

// copy contents instead of deleting+creating new file, which can cause
// filewatchers to lose track of toFile.
const copyContents = (fromFile, toFile) => {
  return Promise.all([
    fs.open(toFile, 'w'),
    fs.readFile(fromFile),
  ])
  .then(([toFd, fromFileBuf]) => {
    return fs.write(toFd, fromFileBuf)
    .finally(() => {
      return fs.close(toFd)
    })
  })
}

// copies all of the project fixtures
// to the cyTmpDir .projects in the root
export function scaffold () {
  // Prevent copying directory into itself
  const resolvedProjects = _path.resolve(projects)
  const resolvedCyTmpDir = _path.resolve(cyTmpDir)

  if (resolvedProjects === resolvedCyTmpDir ||
      resolvedCyTmpDir.startsWith(resolvedProjects + _path.sep) ||
      resolvedProjects.startsWith(resolvedCyTmpDir + _path.sep)) {
    throw new Error(`Cannot copy directory into itself: projects=${projects}, cyTmpDir=${cyTmpDir}`)
  }

  // Ensure temp directory is clean before copying
  try {
    fs.removeSync(cyTmpDir)
  } catch (err) {
    // Ignore errors if directory doesn't exist
  }

  // Create temp directory if it doesn't exist
  fs.ensureDirSync(cyTmpDir)

  // Copy projects to temp directory
  fs.copySync(projects, cyTmpDir)
}

/**
 * Given a project name, copy the project's test files to the temp dir.
 * Returns the scaffolded directory
 */
export async function scaffoldProject (project: ProjectFixtureDir): Promise<string> {