Files
cypress/packages/server/lib/util/app_data.js
Ryan Manuel b0c0eaa508 feat: introduce v8 snapshots to improve startup performance (#24295)
Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com>
Co-authored-by: Zach Bloomquist <git@chary.us>
Co-authored-by: Tyler Biethman <tbiethman@users.noreply.github.com>
Co-authored-by: Matt Henkes <mjhenkes@gmail.com>
Co-authored-by: Chris Breiding <chrisbreiding@users.noreply.github.com>
Co-authored-by: Matt Schile <mschile@cypress.io>
Co-authored-by: Mark Noonan <mark@cypress.io>
Co-authored-by: Zachary Williams <ZachJW34@gmail.com>
Co-authored-by: Ben M <benm@cypress.io>
Co-authored-by: Zachary Williams <zachjw34@gmail.com>
Co-authored-by: astone123 <adams@cypress.io>
Co-authored-by: Bill Glesias <bglesias@gmail.com>
Co-authored-by: Emily Rohrbough <emilyrohrbough@yahoo.com>
Co-authored-by: Emily Rohrbough <emilyrohrbough@users.noreply.github.com>
Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>
Co-authored-by: Adam Stone <adams@cypress.io>
Co-authored-by: Blue F <blue@cypress.io>
Co-authored-by: GitStart <1501599+gitstart@users.noreply.github.com>
Co-authored-by: Mike Plummer <mike-plummer@users.noreply.github.com>
Co-authored-by: Jordan <jordan@jpdesigning.com>
Co-authored-by: Sam Goodger <turbo@tailz.dev>
Co-authored-by: Colum Ferry <cferry09@gmail.com>
Co-authored-by: Stokes Player <stokes@cypress.io>
Co-authored-by: Vilhelm Melkstam <vilhelm.melkstam@gmail.com>
Co-authored-by: amehta265 <65267668+amehta265@users.noreply.github.com>
2022-10-31 20:20:27 -05:00

124 lines
2.8 KiB
JavaScript

const os = require('os')
const path = require('path')
const ospath = require('ospath')
const Promise = require('bluebird')
const la = require('lazy-ass')
const check = require('check-more-types')
const log = require('debug')('cypress:server:appdata')
const pkg = require('@packages/root')
const { fs } = require('../util/fs')
const cwd = require('../cwd')
const md5 = require('md5')
const sanitize = require('sanitize-filename')
const PRODUCT_NAME = pkg.productName || pkg.name
const OS_DATA_PATH = ospath.data()
const ELECTRON_APP_DATA_PATH = path.join(OS_DATA_PATH, PRODUCT_NAME)
if (!PRODUCT_NAME) {
throw new Error('Root package is missing name')
}
const getSymlinkType = () => {
if (os.platform() === 'win32') {
return 'junction'
}
return 'dir'
}
const isProduction = () => {
return process.env.CYPRESS_INTERNAL_ENV === 'production'
}
const toHashName = (projectRoot) => {
if (!projectRoot) {
throw new Error('Missing project path')
}
if (!path.isAbsolute(projectRoot)) {
throw new Error(`Expected project absolute path, not just a name ${projectRoot}`)
}
const name = sanitize(path.basename(projectRoot))
const hash = md5(projectRoot)
return `${name}-${hash}`
}
module.exports = {
toHashName,
getBundledFilePath (projectRoot, filePath) {
return this.projectsPath(toHashName(projectRoot), 'bundles', filePath)
},
ensure () {
const ensure = () => {
return this.removeSymlink()
.then(() => {
return Promise.join(
fs.ensureDirAsync(this.path()),
!isProduction() ? this.symlink() : undefined,
)
})
}
// try twice to ensure the dir
return ensure()
.tapCatch(() => Promise.delay(100))
.catch(ensure)
},
symlink () {
const src = path.dirname(this.path())
const dest = cwd('.cy')
log('symlink folder from %s to %s', src, dest)
const symlinkType = getSymlinkType()
return fs.ensureSymlinkAsync(src, dest, symlinkType)
},
removeSymlink () {
return fs.removeAsync(cwd('.cy')).catch(() => {})
},
path (...paths) {
const { env } = process
la(check.unemptyString(env.CYPRESS_INTERNAL_ENV),
'expected CYPRESS_INTERNAL_ENV, found', env.CYPRESS_INTERNAL_ENV)
// allow overriding the app_data folder
let folder = env.CYPRESS_CONFIG_ENV || env.CYPRESS_INTERNAL_ENV
if (process.env.CYPRESS_INTERNAL_E2E_TESTING_SELF) {
folder = `${folder}-e2e-test`
}
const p = path.join(ELECTRON_APP_DATA_PATH, 'cy', folder, ...paths)
log('path: %s', p)
return p
},
electronPartitionsPath () {
return path.join(ELECTRON_APP_DATA_PATH, 'Partitions')
},
projectsPath (...paths) {
return this.path('projects', ...paths)
},
remove () {
return Promise.join(
fs.removeAsync(this.path()),
this.removeSymlink(),
)
},
}