Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

Commit 0b94fc5

Browse files
Merge pull request #52 from cypress-io/issue-46-47-typescript-validation
2 parents d64122c + 28565c4 commit 0b94fc5

File tree

7 files changed

+192
-37
lines changed

7 files changed

+192
-37
lines changed

.vscode/settings.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"eslint.enable": true,
3-
"editor.formatOnSave": true,
43
// enable for eslint-plugin json-format
5-
"eslint.validate": ["json"],
4+
"eslint.validate": [
5+
"json"
6+
],
67
}

index.js

+55-10
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22

33
const path = require('path')
44
const Promise = require('bluebird')
5-
const fs = require('./lib/fs')
5+
const fs = require('fs-extra')
66

77
const cloneDeep = require('lodash.clonedeep')
88
const browserify = require('browserify')
99
const watchify = require('watchify')
1010

1111
const debug = require('debug')('cypress:browserify')
1212

13+
const typescriptExtensionRegex = /\.tsx?$/
14+
const errorTypes = {
15+
TYPESCRIPT_AND_TSIFY: 'TYPESCRIPT_AND_TSIFY',
16+
TYPESCRIPT_NONEXISTENT: 'TYPESCRIPT_NONEXISTENT',
17+
TYPESCRIPT_NOT_CONFIGURED: 'TYPESCRIPT_NOT_CONFIGURED',
18+
TYPESCRIPT_NOT_STRING: 'TYPESCRIPT_NOT_STRING',
19+
}
20+
1321
const bundles = {}
1422

1523
// by default, we transform JavaScript (including some proposal features),
@@ -60,7 +68,17 @@ const defaultOptions = {
6068
},
6169
}
6270

63-
const getBrowserifyOptions = (entry, userBrowserifyOptions = {}, typescriptPath = null) => {
71+
const throwError = ({ message, type }) => {
72+
const prefix = 'Error running @cypress/browserify-preprocessor:\n\n'
73+
74+
const err = new Error(`${prefix}${message}`)
75+
76+
if (type) err.type = type
77+
78+
throw err
79+
}
80+
81+
const getBrowserifyOptions = async (entry, userBrowserifyOptions = {}, typescriptPath = null) => {
6482
let browserifyOptions = cloneDeep(defaultOptions.browserifyOptions)
6583

6684
// allow user to override default options
@@ -82,22 +100,38 @@ const getBrowserifyOptions = (entry, userBrowserifyOptions = {}, typescriptPath
82100
})
83101

84102
if (typescriptPath) {
103+
if (typeof typescriptPath !== 'string') {
104+
throwError({
105+
type: errorTypes.TYPESCRIPT_NOT_STRING,
106+
message: `The 'typescript' option must be a string. You passed: ${typescriptPath}`,
107+
})
108+
}
109+
110+
const pathExists = await fs.pathExists(typescriptPath)
111+
112+
if (!pathExists) {
113+
throwError({
114+
type: errorTypes.TYPESCRIPT_NONEXISTENT,
115+
message: `The 'typescript' option must be a valid path to your TypeScript installation. We could not find anything at the following path: ${typescriptPath}`,
116+
})
117+
}
118+
85119
const transform = browserifyOptions.transform
86120
const hasTsifyTransform = transform.some(([name]) => name.includes('tsify'))
87121
const hastsifyPlugin = browserifyOptions.plugin.includes('tsify')
88122

89123
if (hasTsifyTransform || hastsifyPlugin) {
90124
const type = hasTsifyTransform ? 'transform' : 'plugin'
91125

92-
throw new Error(`Error running @cypress/browserify-preprocessor:
93-
94-
It looks like you passed the 'typescript' option and also specified a browserify ${type} for TypeScript. This may cause conflicts.
126+
throwError({
127+
type: errorTypes.TYPESCRIPT_AND_TSIFY,
128+
message: `It looks like you passed the 'typescript' option and also specified a browserify ${type} for TypeScript. This may cause conflicts.
95129
96130
Please do one of the following:
97131
98132
1) Pass in the 'typescript' option and omit the browserify ${type} (Recommmended)
99-
2) Omit the 'typescript' option and continue to use your own browserify ${type}
100-
`)
133+
2) Omit the 'typescript' option and continue to use your own browserify ${type}`,
134+
})
101135
}
102136

103137
browserifyOptions.extensions.push('.ts', '.tsx')
@@ -135,7 +169,7 @@ const preprocessor = (options = {}) => {
135169
// when running in the GUI, it will likely get called multiple times
136170
// with the same filePath, as the user could re-run the tests, causing
137171
// the supported file and spec file to be requested again
138-
return (file) => {
172+
return async (file) => {
139173
const filePath = file.filePath
140174

141175
debug('get:', filePath)
@@ -157,9 +191,18 @@ const preprocessor = (options = {}) => {
157191
debug('input:', filePath)
158192
debug('output:', outputPath)
159193

160-
const browserifyOptions = getBrowserifyOptions(filePath, options.browserifyOptions, options.typescript)
194+
const browserifyOptions = await getBrowserifyOptions(filePath, options.browserifyOptions, options.typescript)
161195
const watchifyOptions = Object.assign({}, defaultOptions.watchifyOptions, options.watchifyOptions)
162196

197+
if (!options.typescript && typescriptExtensionRegex.test(filePath)) {
198+
throwError({
199+
type: errorTypes.TYPESCRIPT_NOT_CONFIGURED,
200+
message: `You are attempting to preprocess a TypeScript file, but do not have TypeScript configured. Pass the 'typescript' option to enable TypeScript support.
201+
202+
The file: ${filePath}`,
203+
})
204+
}
205+
163206
const bundler = browserify(browserifyOptions)
164207

165208
if (file.shouldWatch) {
@@ -227,7 +270,7 @@ const preprocessor = (options = {}) => {
227270
})
228271

229272
const bundlePromise = fs
230-
.ensureDirAsync(path.dirname(outputPath))
273+
.ensureDir(path.dirname(outputPath))
231274
.then(bundle)
232275

233276
// cache the bundle promise, so it can be returned if this function
@@ -253,6 +296,8 @@ const preprocessor = (options = {}) => {
253296
// provide a clone of the default options
254297
preprocessor.defaultOptions = JSON.parse(JSON.stringify(defaultOptions))
255298

299+
preprocessor.errorTypes = errorTypes
300+
256301
if (process.env.__TESTING__) {
257302
preprocessor.reset = () => {
258303
for (let filePath in bundles) {

lib/fs.js

-3
This file was deleted.

package-lock.json

+47-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"coffeeify": "3.0.1",
3434
"coffeescript": "1.12.7",
3535
"debug": "4.1.1",
36-
"fs-extra": "7.0.1",
36+
"fs-extra": "9.0.0",
3737
"lodash.clonedeep": "4.5.0",
3838
"through2": "^2.0.0",
3939
"watchify": "3.11.1"
@@ -66,7 +66,7 @@
6666
"lib/*.js"
6767
],
6868
"engines": {
69-
"node": ">=6.5"
69+
"node": ">=8"
7070
},
7171
"license": "MIT",
7272
"repository": {

test/e2e/e2e_spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const _ = require('lodash')
22
const chai = require('chai')
3+
const fs = require('fs-extra')
34
const path = require('path')
45
const snapshot = require('snap-shot-it')
56
const Bluebird = require('bluebird')
67

78
process.env.__TESTING__ = true
89

9-
const fs = require('../../lib/fs')
1010
const preprocessor = require('../../index')
1111

1212
/* eslint-disable-next-line no-unused-vars */
@@ -49,7 +49,7 @@ const verifySourceContents = ({ sources, sourcesContent }) => {
4949
const zippedArrays = _.zip(sources, sourcesContent)
5050

5151
return Bluebird.map(zippedArrays, ([sourcePath, sourceContent]) => {
52-
return fs.readFileAsync(sourcePath, 'utf8')
52+
return fs.readFile(sourcePath, 'utf8')
5353
.then((str) => {
5454
expect(str).to.eq(sourceContent)
5555
})

0 commit comments

Comments
 (0)