mirror of
https://github.com/cypress-io/cypress.git
synced 2026-02-27 03:19:22 -06:00
feat: component testing (#14479)
Co-authored-by: Jessica Sachs <jess@jessicasachs.io> Co-authored-by: Barthélémy Ledoux <bart@cypress.io> Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com> Co-authored-by: Zach Bloomquist <github@chary.us> Co-authored-by: Dmitriy Kovalenko <dmtr.kovalenko@outlook.com> Co-authored-by: ElevateBart <ledouxb@gmail.com> Co-authored-by: Ben Kucera <14625260+Bkucera@users.noreply.github.com>
This commit is contained in:
@@ -7,8 +7,34 @@ const Debug = require('debug')
|
||||
|
||||
const debug = Debug('cypress:scripts:util:transform-requires')
|
||||
|
||||
const requireRE = /(require\(["'`])@packages\/(.+?)([\/"'`])/g
|
||||
const globAsync = Promise.promisify(glob)
|
||||
|
||||
const rewritePackageNames = (fileStr, buildRoot, filePath, onFound) => {
|
||||
// const matches = requireRE.exec(fileStr)
|
||||
return fileStr.replace(requireRE, (...match) => {
|
||||
debug(match.slice(0, -1))
|
||||
const pkg = match[2]
|
||||
const afterPkg = match[3]
|
||||
|
||||
const pkgPath = path.join(buildRoot, `packages/${pkg}`)
|
||||
const replaceWith = path.relative(path.dirname(filePath), pkgPath).replace(/\\/g, '/')
|
||||
|
||||
const replaceString = `${match[1]}${replaceWith}${afterPkg}`
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log()
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('resolve:', chalk.grey(pkgPath), '\nfrom:', chalk.grey(filePath))
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(chalk.yellow(`@packages/${pkg}`), '->', chalk.green(replaceWith))
|
||||
|
||||
onFound && onFound(replaceString)
|
||||
|
||||
return replaceString
|
||||
})
|
||||
}
|
||||
|
||||
const transformRequires = async function (buildResourcePath) {
|
||||
const buildRoot = buildResourcePath
|
||||
|
||||
@@ -23,47 +49,33 @@ const transformRequires = async function (buildResourcePath) {
|
||||
// This prevents us having to ship symlinks in the final binary, because some OS's (Windows)
|
||||
// do not have relative symlinks/junctions or bad symlink support
|
||||
await globAsync(globPattern, { ignore: ['**/node_modules/**', '**/packages/**/dist/**'] })
|
||||
.map(async (item) => {
|
||||
debug('glob found:', item)
|
||||
const buff = await fs.readFile(item)
|
||||
.map(async (filePath) => {
|
||||
debug('glob found:', filePath)
|
||||
const buff = await fs.readFile(filePath)
|
||||
|
||||
const fileStr = buff.toString()
|
||||
const requireRE = /(require\(["'])@packages\/(\w+)/g
|
||||
|
||||
let shouldWriteFile = false
|
||||
|
||||
// const matches = requireRE.exec(fileStr)
|
||||
const newFile = fileStr.replace(requireRE, (...match) => {
|
||||
debug(match.slice(0, -1))
|
||||
const pkg = match[2]
|
||||
|
||||
const pkgPath = path.join(buildRoot, `packages/${pkg}`)
|
||||
const replaceWith = path.relative(path.dirname(item), pkgPath).replace(/\\/g, '/')
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log()
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('resolve:', chalk.grey(pkgPath), '\nfrom:', chalk.grey(item))
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(chalk.yellow(`@packages/${pkg}`), '->', chalk.green(replaceWith))
|
||||
|
||||
const replaceString = `${match[1]}${replaceWith}`
|
||||
|
||||
const newFile = rewritePackageNames(fileStr, buildRoot, filePath, (replaceString) => {
|
||||
debug(replaceString)
|
||||
|
||||
replaceCount++
|
||||
shouldWriteFile = true
|
||||
|
||||
return replaceString
|
||||
})
|
||||
|
||||
if (shouldWriteFile) {
|
||||
debug('writing to file:', chalk.red(item))
|
||||
await fs.writeFile(item, newFile)
|
||||
debug('writing to file:', chalk.red(filePath))
|
||||
|
||||
await fs.writeFile(filePath, newFile)
|
||||
}
|
||||
})
|
||||
|
||||
return replaceCount
|
||||
}
|
||||
|
||||
module.exports = { transformRequires }
|
||||
module.exports = {
|
||||
transformRequires,
|
||||
|
||||
rewritePackageNames,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user