Repository navigation
Expand file tree
/
Copy pathfixtures.ts
More file actions
177 lines (141 loc) · 5.39 KB
/
Copy pathfixtures.ts
File metadata and controls
177 lines (141 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import fs from 'fs-extra'
import _path from 'path'
import chokidar from 'chokidar'
import tempDir from 'temp-dir'
import type { ProjectFixtureDir } from './fixtureDirs'
export const root = _path.join(__dirname, '..')
const serverRoot = _path.join(__dirname, '../../packages/server/')
export { fixtureDirs } from './fixtureDirs'
export type { ProjectFixtureDir } from './fixtureDirs'
export const projects = _path.join(root, 'projects')
export const projectFixtures = _path.join(root, 'project-fixtures')
export const cyTmpDir = _path.join(tempDir, 'cy-projects')
const projectFixtureDirs = fs.readdirSync(projectFixtures, { withFileTypes: true }).filter((f) => f.isDirectory()).map((f) => f.name)
const safeRemove = (path: string) => {
try {
// fs-extra's lstat-based removeSync unlinks junctions/symlinks instead of
// recursing into them. Native fs.rmSync must not be used here: under
// Electron's libc++-built Node 24+, std::filesystem::remove_all classifies
// Windows junctions (created when scaffolding project node_modules) as
// plain directories and deletes their target contents — including the
// monorepo dirs they point to. removeSync also retries transient Windows
// races (ENOTEMPTY/EBUSY/EPERM) internally.
fs.removeSync(path)
} catch (_err) {
const err = _err as NodeJS.ErrnoException
// Windows does not like the en masse deleting of files, since the AV will hold
// a lock on files when they are written. This skips deleting if the lock is
// encountered.
if (err.code === 'EBUSY' && process.platform === 'win32') {
return console.error(`Remove failed for ${ path } due to EBUSY. Skipping on Windows.`)
}
throw err
}
}
// copy contents instead of deleting+creating new file, which can cause
// filewatchers to lose track of toFile.
const copyContents = (fromFile: string, toFile: string) => {
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> {
const to = _path.join(cyTmpDir, project)
const from = projectFixturePath(project)
fs.copySync(from, to)
try {
const packageJson = require(`${to}/package.json`)
const fixtureDir = packageJson.projectFixtureDirectory
if (fixtureDir) {
if (!projectFixtureDirs.includes(fixtureDir)) {
throw new Error(`Invalid project fixture directory: ${fixtureDir}, expected one of ${projectFixtureDirs}`)
}
await fs.copy(_path.join(projectFixtures, fixtureDir), to, { overwrite: false })
}
} catch (err) {
// Jest's resolver throws this from another realm, where `instanceof Error` is false
if (!(typeof err === 'object' && err !== null && 'code' in err && err.code === 'MODULE_NOT_FOUND')) {
throw err
}
}
return to
}
export function scaffoldWatch () {
const watchdir = _path.resolve(__dirname, '../projects')
console.log('watching files due to --no-exit', watchdir)
chokidar.watch(watchdir, {
})
.on('change', (srcFilepath, stats) => {
const tmpFilepath = _path.join(cyTmpDir, _path.relative(watchdir, srcFilepath))
return copyContents(srcFilepath, tmpFilepath)
})
.on('error', console.error)
}
// removes all of the project fixtures
// from the cyTmpDir .projects in the root
export function remove () {
safeRemove(cyTmpDir)
}
export function removeProject (name: ProjectFixtureDir) {
safeRemove(projectPath(name))
}
// Removes node_modules that might have been leftover from an initial "yarn"
// in the fixture dir
export function clearFixtureNodeModules (name: ProjectFixtureDir) {
try {
safeRemove(_path.join(projects, name, 'node_modules'))
} catch {
//
}
}
// returns the path to project fixture
// in the cyTmpDir
export function project (name: ProjectFixtureDir) {
return projectPath(name)
}
export function projectPath (name: ProjectFixtureDir) {
return _path.join(cyTmpDir, name)
}
export function get (fixture: string, encoding: BufferEncoding = 'utf8') {
return fs.readFileSync(_path.join(serverRoot, 'test', 'support', 'fixtures', fixture), { encoding })
}
export function path (fixture: ProjectFixtureDir) {
return _path.join(serverRoot, 'test', 'support', 'fixtures', fixture)
}
export function projectFixturePath (name: ProjectFixtureDir) {
return _path.join(projects, name)
}
export default module.exports