Skip to content

Remove type dependencies from cli. #7194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,10 @@ jobs:
command: ls -la types
working_directory: cli
- run:
command: yarn lerna exec --scope cypress "yarn dtslint"
command: ls -la chai
working_directory: cli/types
- run:
command: yarn type-check --ignore-progress
command: yarn lerna exec --scope cypress "yarn dtslint"
- store-npm-logs

build-system-unit-tests:
Expand Down
20 changes: 10 additions & 10 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@
"@cypress/listr-verbose-renderer": "0.4.1",
"@cypress/request": "2.88.5",
"@cypress/xvfb": "1.2.4",
"@types/blob-util": "1.3.3",
"@types/bluebird": "3.5.29",
"@types/chai": "4.2.7",
"@types/chai-jquery": "1.1.40",
"@types/jquery": "3.3.31",
"@types/lodash": "4.14.149",
"@types/minimatch": "3.0.3",
"@types/mocha": "5.2.7",
"@types/sinon": "7.5.1",
"@types/sinon-chai": "3.2.3",
"@types/sizzle": "2.3.2",
"arch": "2.1.1",
"bluebird": "3.7.2",
Expand Down Expand Up @@ -72,6 +62,16 @@
"@babel/preset-env": "7.9.5",
"@cypress/sinon-chai": "1.1.0",
"@packages/root": "*",
"@types/blob-util": "1.3.3",
"@types/bluebird": "3.5.29",
"@types/chai": "4.2.7",
"@types/chai-jquery": "1.1.40",
"@types/jquery": "3.3.31",
"@types/lodash": "4.14.149",
"@types/minimatch": "3.0.3",
"@types/mocha": "5.2.7",
"@types/sinon": "7.5.1",
"@types/sinon-chai": "3.2.3",
"chai": "3.5.0",
"chai-as-promised": "7.1.1",
"chai-string": "1.5.0",
Expand Down
90 changes: 63 additions & 27 deletions cli/scripts/post-install.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,64 @@
const fs = require('../lib/fs')
const path = require('path')

/**
* https://github.com/cypress-io/cypress/pull/5780
* Folder names in "node_modules/@types" that were copied to cli/types to generate index.d.ts.
* They cause type errors in type checker. So, they should be removed.
*/
const includeTypes = [
'blob-util',
'bluebird',
'lodash',
'mocha',
'minimatch',
'sinon',
'sinon-chai',
'chai',
'chai-jquery',
'jquery',
]

includeTypes.forEach((t) => {
const dir = path.join(__dirname, '../types', t)

if (fs.existsSync(dir)) {
fs.removeSync(dir)
}
#!/usr/bin/env node

const { includeTypes } = require('./utils')
const shell = require('shelljs')
const { join } = require('path')
const resolvePkg = require('resolve-pkg')

shell.set('-v') // verbose
shell.set('-e') // any error is fatal

// We include the TypeScript definitions for the bundled 3rd party tools
// thus we need to copy them from "dev" dependencies into our types folder
// and we need to sometimes tweak these types files to use relative paths
// This ensures that globals like Cypress.$, Cypress._ etc are property typed
// yet we do not install "@types/.." packages with "npm install cypress"
// because they can conflict with user's own libraries

includeTypes.forEach((folder) => {
const source = resolvePkg(`@types/${folder}`, { cwd: join(__dirname, '..', '..') })

shell.cp('-R', source, 'types')
})

// jQuery v3.3.x includes "dist" folder that just references back to itself
// causing dtslint to think there are double definitions. Remove that folder.
const typesJqueryDistFolder = join('types', 'jquery', 'dist')

shell.rm('-rf', typesJqueryDistFolder)

// fix paths to Chai, jQuery and other types to be relative
shell.sed(
'-i',
'<reference types="chai" />',
'<reference path="../chai/index.d.ts" />',
join('types', 'chai-jquery', 'index.d.ts'),
)

shell.sed(
'-i',
'<reference types="jquery" />',
'<reference path="../jquery/index.d.ts" />',
join('types', 'chai-jquery', 'index.d.ts'),
)

const sinonChaiFilename = join('types', 'sinon-chai', 'index.d.ts')

shell.sed(
'-i',
'<reference types="chai" />',
'<reference path="../chai/index.d.ts" />',
sinonChaiFilename,
)

// also use relative import via path for sinon-chai
// there is reference comment line we need to fix to be relative
shell.sed(
'-i',
'<reference types="sinon" />',
'<reference path="../sinon/index.d.ts" />',
sinonChaiFilename,
)

// and an import sinon line to be changed to relative path
shell.sed('-i', 'from \'sinon\';', 'from \'../sinon\';', sinonChaiFilename)
8 changes: 8 additions & 0 deletions cli/scripts/start-build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

const { includeTypes } = require('./utils')
const { join } = require('path')
const shell = require('shelljs')

shell.set('-v') // verbose
Expand All @@ -13,6 +15,12 @@ shell.cp('NPM_README.md', 'build/README.md')
shell.cp('.release.json', 'build/.release.json')
// copies our typescript definitions
shell.cp('-R', 'types/*.ts', 'build/types/')
// copies 3rd party typescript definitions
includeTypes.forEach((folder) => {
const source = join('types', folder)

shell.cp('-R', source, 'build/types')
})

shell.exec('babel lib -d build/lib')
shell.exec('babel index.js -o build/index.js')
19 changes: 19 additions & 0 deletions cli/scripts/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Folder names in "node_modules/@types" that we should include
* when we bundle Cypress NPM package. These folder have ".d.ts"
* definition files that we will need to include with our NPM package.
*/
const includeTypes = [
'blob-util',
'bluebird',
'lodash',
'mocha',
'minimatch',
'sinon',
'sinon-chai',
'chai',
'chai-jquery',
'jquery',
]

module.exports = { includeTypes }
13 changes: 13 additions & 0 deletions cli/types/cy-blob-util.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Shim definition to export a namespace. Cypress is actually a global module
// so import/export isn't allowed there. We import here and define a global module
// so that Cypress can get and use the Blob type

// tslint:disable-next-line:no-implicit-dependencies
import * as blobUtil from './blob-util'

export = BlobUtil
export as namespace BlobUtil

declare namespace BlobUtil {
type BlobUtilStatic = typeof blobUtil
}
11 changes: 11 additions & 0 deletions cli/types/cy-bluebird.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Shim definition to export a namespace. Cypress is actually a global module
// so import/export isn't allowed there. We import here and define a global module
// so that Cypress can get and use the Blob type
import BluebirdStatic = require('./bluebird')

export = Bluebird
export as namespace Bluebird

declare namespace Bluebird {
type BluebirdStatic = typeof BluebirdStatic
}
10 changes: 10 additions & 0 deletions cli/types/cy-chai.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Shim definition to export a namespace. Cypress is actually a global module
// so import/export isn't allowed there. We import here and define a global module
/// <reference path="./chai/index.d.ts" />
declare namespace Chai {
interface Include {
html(html: string): Assertion
text(text: string): Assertion
value(text: string): Assertion
}
}
96 changes: 96 additions & 0 deletions cli/types/cy-minimatch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// I was trying to avoid relying on "import" of actual module from "minimatch"
// because it would not work in test project, and the only reliable way
// to get around type errors finally was to copy the minimal minimatch function
// definition from "minimatch/index.d.ts" here and just keep it in our code

export = Minimatch
export as namespace Minimatch

interface MinimatchOptions {
/**
* Dump a ton of stuff to stderr.
*
* @default false
*/
debug?: boolean

/**
* Do not expand {a,b} and {1..3} brace sets.
*
* @default false
*/
nobrace?: boolean

/**
* Disable ** matching against multiple folder names.
*
* @default false
*/
noglobstar?: boolean

/**
* Allow patterns to match filenames starting with a period,
* even if the pattern does not explicitly have a period in that spot.
*
* @default false
*/
dot?: boolean

/**
* Disable "extglob" style patterns like +(a|b).
*
* @default false
*/
noext?: boolean

/**
* Perform a case-insensitive match.
*
* @default false
*/
nocase?: boolean

/**
* When a match is not found by minimatch.match,
* return a list containing the pattern itself if this option is set.
* Otherwise, an empty list is returned if there are no matches.
*
* @default false
*/
nonull?: boolean

/**
* If set, then patterns without slashes will be matched against
* the basename of the path if it contains slashes.
*
* @default false
*/
matchBase?: boolean

/**
* Suppress the behavior of treating #
* at the start of a pattern as a comment.
*
* @default false
*/
nocomment?: boolean

/**
* Suppress the behavior of treating a leading ! character as negation.
*
* @default false
*/
nonegate?: boolean

/**
* Returns from negate expressions the same as if they were not negated.
* (Ie, true on a hit, false on a miss.)
*
* @default false
*/
flipNegate?: boolean
}

declare namespace Minimatch {
function minimatch(target: string, pattern: string, options?: MinimatchOptions): boolean
}
7 changes: 7 additions & 0 deletions cli/types/cy-moment.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import moment = require('moment')
export = Moment
export as namespace Moment

declare namespace Moment {
type MomentStatic = typeof moment
}
8 changes: 8 additions & 0 deletions cli/types/cypress-eventemitter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Cypress, cy, Log inherits EventEmitter.
type EventEmitter2 = import("eventemitter2").EventEmitter2

interface EventEmitter extends EventEmitter2 {
proxyTo: (cy: Cypress.cy) => null
emitMap: (eventName: string, args: any[]) => Array<(...args: any[]) => any>
emitThen: (eventName: string, args: any[]) => Bluebird.BluebirdStatic
}
3 changes: 3 additions & 0 deletions cli/types/cypress-expect.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Cypress adds chai expect and assert to global
declare const expect: Chai.ExpectStatic
declare const assert: Chai.AssertStatic
24 changes: 24 additions & 0 deletions cli/types/cypress-global-vars.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// <reference path="./cypress-eventemitter.d.ts" />

/**
* Global variables `cy` added by Cypress with all API commands.
* @see https://on.cypress.io/api
*
```
cy.get('button').click()
cy.get('.result').contains('Expected text')
```
*/
declare const cy: Cypress.cy & EventEmitter

/**
* Global variable `Cypress` holds common utilities and constants.
* @see https://on.cypress.io/api
*
```
Cypress.config("pageLoadTimeout") // => 60000
Cypress.version // => "1.4.0"
Cypress._ // => Lodash _
```
*/
declare const Cypress: Cypress.Cypress & EventEmitter
1 change: 1 addition & 0 deletions cli/types/cypress-type-helpers.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Nullable<T> = T | null
Loading