refactor: add @packages/resolve-dist to isolate client/server code (#17109)

This commit is contained in:
Tim Griesser
2021-06-29 12:54:29 -04:00
committed by GitHub
parent 7509042e47
commit e3a5bdf111
30 changed files with 116 additions and 140 deletions

View File

@@ -1,5 +0,0 @@
{
"env": {
"node": true
}
}

View File

@@ -1,11 +0,0 @@
const path = require('path')
function file (name) {
return `file://${path.join(__dirname, '..', 'dist', name)}`
}
module.exports = {
getPathToIndex () {
return file('index.html')
},
}

View File

@@ -57,7 +57,6 @@
},
"files": [
"dist",
"lib",
"!lib/**/*_spec.jsx"
]
}

View File

@@ -1,13 +0,0 @@
const path = require('path')
function dist (...args) {
const paths = [__dirname, '..', 'dist'].concat(args)
return path.join(...paths)
}
module.exports = {
getPathToDist (...args) {
return dist(...args)
},
}

View File

@@ -18,6 +18,7 @@
"@cypress/webpack-preprocessor": "0.0.0-development",
"@cypress/what-is-circular": "1.0.1",
"@packages/network": "0.0.0-development",
"@packages/resolve-dist": "0.0.0-development",
"@packages/runner": "0.0.0-development",
"@packages/server": "0.0.0-development",
"@packages/ts": "0.0.0-development",
@@ -78,7 +79,6 @@
"zone.js": "0.9.0"
},
"files": [
"lib",
"patches"
],
"workspaces": {

View File

@@ -1,5 +1,5 @@
import { oneLine } from 'common-tags'
import runner from '@packages/runner/lib/resolve-dist'
import { getRunnerInjectionContents } from '@packages/resolve-dist'
export function partial (domain) {
return oneLine`
@@ -10,7 +10,7 @@ export function partial (domain) {
}
export function full (domain) {
return runner.getInjectionContents().then((contents) => {
return getRunnerInjectionContents().then((contents) => {
return oneLine`
<script type='text/javascript'>
document.domain = '${domain}';

View File

@@ -27,6 +27,7 @@
"@cypress/request": "2.88.5",
"@cypress/request-promise": "4.2.6",
"@cypress/sinon-chai": "2.9.1",
"@packages/resolve-dist": "0.0.0-development",
"@types/express": "4.17.2",
"@types/supertest": "2.0.10",
"express": "4.17.1",

View File

@@ -1,5 +0,0 @@
{
"env": {
"node": true
}
}

View File

@@ -1,7 +0,0 @@
const path = require('path')
module.exports = {
getPathToDist (...args) {
return path.join(...[__dirname, '..', 'dist', ...args])
},
}

View File

@@ -16,6 +16,7 @@
"@fontsource/open-sans": "4.3.0",
"@fortawesome/fontawesome-free": "5.11.2",
"@packages/driver": "0.0.0-development",
"@packages/resolve-dist": "0.0.0-development",
"@packages/socket": "0.0.0-development",
"@packages/web-config": "0.0.0-development",
"@reach/dialog": "0.10.5",
@@ -36,7 +37,5 @@
"webpack": "4.35.3",
"webpack-cli": "3.3.2"
},
"files": [
"lib"
]
"files": []
}

View File

@@ -0,0 +1,5 @@
if (process.env.CYPRESS_INTERNAL_ENV !== 'production') {
require('@packages/ts/register')
}
module.exports = require('./lib')

View File

@@ -0,0 +1,23 @@
import path from 'path'
let fs: typeof import('fs-extra')
type FoldersWithDist = 'static' | 'runner' | 'runner-ct' | 'driver'
export const getPathToDist = (folder: FoldersWithDist, ...args: string[]) => {
return path.join(...[__dirname, '..', '..', folder, 'dist', ...args])
}
export const getRunnerInjectionContents = () => {
fs ??= require('fs-extra') as typeof import('fs-extra')
return fs.readFile(getPathToDist('runner', 'injection.js'))
}
export const getPathToIndex = (pkg: 'runner' | 'runner-ct') => {
return getPathToDist(pkg, 'index.html')
}
export const getPathToDesktopIndex = () => {
return `file://${path.join(__dirname, '..', '..', 'desktop-gui', 'dist', 'index.html')}`
}

View File

@@ -0,0 +1,25 @@
{
"name": "@packages/resolve-dist",
"version": "0.0.0-development",
"private": true,
"main": "index.js",
"scripts": {
"build-prod": "tsc --project .",
"clean": "rm lib/*.js",
"clean-deps": "rm -rf node_modules",
"test": "yarn test-unit",
"test-debug": "yarn test-unit --inspect-brk=5566",
"test-unit": "mocha --reporter mocha-multi-reporters --reporter-options configFile=../../mocha-reporter-config.json",
"test-watch": "yarn test-unit --watch"
},
"dependencies": {
"fs-extra": "8.1.0"
},
"devDependencies": {
"@packages/ts": "0.0.0-development"
},
"files": [
"lib"
],
"types": "./lib/index.ts"
}

View File

@@ -0,0 +1,12 @@
{
"extends": "./../ts/tsconfig.json",
"include": [
"**/*.ts"
],
"files": [
"./../ts/index.d.ts"
],
"exclude": [
"test"
]
}

View File

@@ -1 +0,0 @@
export * from './lib/runner-ct'

View File

@@ -2,7 +2,6 @@
"name": "@packages/runner-ct",
"version": "0.0.0-development",
"private": true,
"main": "index.js",
"browser": "src/index.js",
"scripts": {
"build": "webpack",
@@ -15,12 +14,7 @@
"test": "ts-node ../../scripts/cypress.js run-ct --project ${PWD}",
"watch": "webpack --watch --progress --config webpack.config.ts"
},
"dependencies": {
"debug": "^4.3.2",
"lodash": "^4.17.20",
"send": "^0.17.1",
"tslib": "^2.3.0"
},
"dependencies": {},
"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/preset-env": "^7.12.1",
@@ -71,7 +65,6 @@
},
"files": [
"dist",
"src",
"lib"
"src"
]
}

View File

@@ -1,22 +0,0 @@
const fs = require('fs-extra')
const path = require('path')
function dist (...args) {
const paths = [__dirname, '..', 'dist'].concat(args)
return path.join(...paths)
}
module.exports = {
getPathToDist (...args) {
return dist(...args)
},
getInjectionContents () {
return fs.readFile(dist('injection.js'))
},
getPathToIndex () {
return dist('index.html')
},
}

View File

@@ -61,7 +61,6 @@
"webpack-cli": "3.3.2"
},
"files": [
"dist",
"lib"
"dist"
]
}

View File

@@ -24,6 +24,7 @@
"tslib": "^2.3.0"
},
"devDependencies": {
"@packages/resolve-dist": "0.0.0-development",
"chai": "^4.2.0",
"expect": "^26.6.2",
"mocha": "^8.1.3"

View File

@@ -3,11 +3,11 @@ import { ErrorRequestHandler, Express } from 'express'
import httpProxy from 'http-proxy'
import send from 'send'
import { NetworkProxy } from '@packages/proxy'
import { handle, serve, serveChunk } from '@packages/runner-ct'
import { handle, serve, serveChunk } from './runner-ct'
import xhrs from '@packages/server/lib/controllers/xhrs'
import { SpecsStore } from '@packages/server/lib/specs-store'
import staticPkg from '@packages/static'
import { ProjectBase } from '../../server/lib/project-base'
import { getPathToDist } from '@packages/resolve-dist'
const debug = Debug('cypress:server:routes')
@@ -33,7 +33,7 @@ export const createRoutes = ({
app.get('/__cypress/runner/*', handle)
app.get('/__cypress/static/*', (req, res) => {
const pathToFile = staticPkg.getPathToDist(req.params[0])
const pathToFile = getPathToDist('static', req.params[0])
return send(req, pathToFile)
.pipe(res)

View File

@@ -1,8 +1,8 @@
import Debug from 'debug'
import _ from 'lodash'
import path from 'path'
import send from 'send'
import { SpecsStore } from '@packages/server/lib/specs-store'
import { getPathToIndex, getPathToDist } from '@packages/resolve-dist'
import { Cfg, ProjectBase } from '@packages/server/lib/project-base'
import { ServerCt } from '../../server-ct'
@@ -14,18 +14,8 @@ interface ServeOptions {
const debug = Debug('cypress:server:runner-ct')
function dist (...args) {
const paths = [__dirname, '..', 'dist'].concat(args)
return path.join(...paths)
}
export const getPathToDist = (...args) => {
return dist(...args)
}
export const handle = (req, res) => {
const pathToFile = getPathToDist(req.params[0])
const pathToFile = getPathToDist('runner-ct', req.params[0])
return send(req, pathToFile)
.pipe(res)
@@ -49,7 +39,7 @@ export const serve = (req, res, options: ServeOptions) => {
// https://github.com/cypress-io/cypress/issues/4952
const base64Config = Buffer.from(JSON.stringify(config)).toString('base64')
const runnerPath = process.env.CYPRESS_INTERNAL_RUNNER_PATH || getPathToDist('index.html')
const runnerPath = process.env.CYPRESS_INTERNAL_RUNNER_PATH || getPathToIndex('runner-ct')
return res.render(runnerPath, {
base64Config,
@@ -59,7 +49,7 @@ export const serve = (req, res, options: ServeOptions) => {
export const serveChunk = (req, res, options) => {
let { config } = options
let pathToFile = getPathToDist(req.originalUrl.replace(config.clientRoute, ''))
let pathToFile = getPathToDist('runner-ct', req.originalUrl.replace(config.clientRoute, ''))
return send(req, pathToFile).pipe(res)
}

View File

@@ -1,9 +1,12 @@
const send = require('send')
const reporter = require('@packages/reporter/lib/resolve-dist')
/**
* @type {import('@packages/resolve-dist')}
*/
const { resolveDistPath } = require('@packages/resolve-dist')
module.exports = {
handle (req, res) {
const pathToFile = reporter.getPathToDist(req.params[0])
const pathToFile = resolveDistPath('reporter', req.params[0])
return send(req, pathToFile)
.pipe(res)

View File

@@ -5,7 +5,10 @@ const { fs } = require('../util/fs')
const path = require('path')
const debug = require('debug')('cypress:server:runner')
const pkg = require('@packages/root')
const runner = require('@packages/runner/lib/resolve-dist')
/**
* @type {import('@packages/resolve-dist')}
*/
const { getPathToDist, getPathToIndex } = require('@packages/resolve-dist')
const PATH_TO_NON_PROXIED_ERROR = path.join(__dirname, '..', 'html', 'non_proxied_error.html')
@@ -47,7 +50,7 @@ module.exports = {
// https://github.com/cypress-io/cypress/issues/4952
const base64Config = Buffer.from(JSON.stringify(config)).toString('base64')
const runnerPath = process.env.CYPRESS_INTERNAL_RUNNER_PATH || runner.getPathToIndex()
const runnerPath = process.env.CYPRESS_INTERNAL_RUNNER_PATH || getPathToIndex('runner')
return res.render(runnerPath, {
base64Config,
@@ -56,14 +59,7 @@ module.exports = {
},
handle (req, res) {
const pathToFile = runner.getPathToDist(req.params[0])
return send(req, pathToFile)
.pipe(res)
},
handleSourceMappings (req, res) {
const pathToFile = runner.getPathToSourceMappings()
const pathToFile = getPathToDist('runner', req.params[0])
return send(req, pathToFile)
.pipe(res)

View File

@@ -1,9 +1,12 @@
const send = require('send')
const staticPkg = require('@packages/static')
/**
* @type {import('@packages/resolve-dist')}
*/
const { getPathToDist } = require('@packages/resolve-dist')
module.exports = {
handle (req, res) {
const pathToFile = staticPkg.getPathToDist(req.params[0])
const pathToFile = getPathToDist('static', req.params[0])
return send(req, pathToFile)
.pipe(res)

View File

@@ -5,7 +5,7 @@ import { BrowserWindow } from 'electron'
import Debug from 'debug'
import cwd from '../cwd'
import savedState from '../saved_state'
const cyDesktop = require('@packages/desktop-gui')
import { getPathToDesktopIndex } from '@packages/resolve-dist'
const debug = Debug('cypress:server:windows')
@@ -21,7 +21,7 @@ let recentlyCreatedWindow = false
const getUrl = function (type) {
switch (type) {
case 'INDEX':
return cyDesktop.getPathToIndex()
return getPathToDesktopIndex()
default:
throw new Error(`No acceptable window type found for: '${type}'`)
}

View File

@@ -139,6 +139,7 @@
"@packages/net-stubbing": "0.0.0-development",
"@packages/network": "0.0.0-development",
"@packages/reporter": "0.0.0-development",
"@packages/resolve-dist": "0.0.0-development",
"@packages/root": "0.0.0-development",
"@packages/socket": "0.0.0-development",
"@packages/static": "0.0.0-development",

View File

@@ -31,7 +31,10 @@ const { fs } = require(`${root}lib/util/fs`)
const glob = require(`${root}lib/util/glob`)
const CacheBuster = require(`${root}lib/util/cache_buster`)
const Fixtures = require(`${root}test/support/helpers/fixtures`)
const runner = require(`${root}../runner/lib/resolve-dist`)
/**
* @type {import('@packages/resolve-dist')}
*/
const { getRunnerInjectionContents } = require(`@packages/resolve-dist`)
const { createRoutes } = require(`${root}lib/routes`)
zlib = Promise.promisifyAll(zlib)
@@ -2485,7 +2488,7 @@ describe('Routes', () => {
'Content-Type': 'text/html',
})
const injection = await runner.getInjectionContents()
const injection = await getRunnerInjectionContents()
const contents = removeWhitespace(Fixtures.get('server/expected_head_inject.html').replace('{{injection}}', injection))
const res = await this.rp({
url: 'http://www.google.com/bar',
@@ -2506,7 +2509,7 @@ describe('Routes', () => {
'Content-Type': 'text/html',
})
const injection = await runner.getInjectionContents()
const injection = await getRunnerInjectionContents()
const contents = removeWhitespace(Fixtures.get('server/expected_no_head_tag_inject.html').replace('{{injection}}', injection))
const res = await this.rp({
@@ -2770,7 +2773,7 @@ describe('Routes', () => {
it('injects into https server', async function () {
await this.setup('https://localhost:8443')
const injection = await runner.getInjectionContents()
const injection = await getRunnerInjectionContents()
const contents = removeWhitespace(Fixtures.get('server/expected_https_inject.html').replace('{{injection}}', injection))
const res = await this.rp({
url: 'https://localhost:8443/',
@@ -2838,7 +2841,7 @@ describe('Routes', () => {
await this.setup('https://www.foobar.com:8443')
evilDns.add('*.foobar.com', '127.0.0.1')
const injection = await runner.getInjectionContents()
const injection = await getRunnerInjectionContents()
const contents = removeWhitespace(Fixtures.get('server/expected_https_inject.html').replace('{{injection}}', injection))
const res = await this.rp({
url: 'https://www.foobar.com:8443/index.html',
@@ -2856,7 +2859,7 @@ describe('Routes', () => {
await this.setup('https://www.foobar.com:8443')
evilDns.add('*.foobar.com', '127.0.0.1')
const injection = await runner.getInjectionContents()
const injection = await getRunnerInjectionContents()
const contents = removeWhitespace(Fixtures.get('server/expected_https_inject.html').replace('{{injection}}', injection))
const res = await this.rp({
url: 'https://docs.foobar.com:8443/index.html',

View File

@@ -10,8 +10,7 @@ import { EventEmitter } from 'events'
import { BrowserWindow } from 'electron'
import * as Windows from '../../../lib/gui/windows'
import savedState from '../../../lib/saved_state'
const cyDesktop = require('@packages/desktop-gui')
import { getPathToDesktopIndex } from '@packages/resolve-dist'
const DEFAULT_USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/0.0.0 Chrome/59.0.3071.115 Electron/1.8.2 Safari/537.36'
@@ -55,14 +54,14 @@ describe('lib/gui/windows', () => {
width: 600,
type: 'INDEX',
show: true,
url: cyDesktop.getPathToIndex(),
url: getPathToDesktopIndex(),
})
expect(options.webPreferences).to.include({
preload: path.resolve('lib', 'ipc', 'ipc.js'),
})
expect(win.loadURL).to.be.calledWith(cyDesktop.getPathToIndex())
expect(win.loadURL).to.be.calledWith(getPathToDesktopIndex())
})
})
})

View File

@@ -1,21 +1,9 @@
const path = require('path')
function dist (...args) {
const paths = [__dirname, '..', 'dist'].concat(args)
return path.join(...paths)
}
const getPathToDist = (...args) => {
return dist(...args)
}
const { getPathToDist } = require('@packages/resolve-dist')
module.exports = {
getPathToDist,
handle (send) {
return (req, res) => {
const pathToFile = getPathToDist(req.params[0])
const pathToFile = getPathToDist('static', req.params[0])
return send(req, pathToFile)
.pipe(res)

View File

@@ -34289,7 +34289,7 @@ semver@~5.0.1:
resolved "https://registry.yarnpkg.com/semver/-/semver-5.0.3.tgz#77466de589cd5d3c95f138aa78bc569a3cb5d27a"
integrity sha1-d0Zt5YnNXTyV8TiqeLxWmjy10no=
send@0.17.1, send@^0.17.1:
send@0.17.1:
version "0.17.1"
resolved "https://registry.yarnpkg.com/send/-/send-0.17.1.tgz#c1d8b059f7900f7466dd4938bdc44e11ddb376c8"
integrity sha512-BsVKsiGcQMFwT8UxypobUKyv7irCNRHk1T0G680vk88yf6LBByGcZJOTJCrTP2xVN6yI+XjPJcNuE3V4fT9sAg==