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

Commit

Permalink
Merge pull request #52 from cypress-io/issue-46-47-typescript-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding authored May 21, 2020
2 parents d64122c + 28565c4 commit 0b94fc5
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 37 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"eslint.enable": true,
"editor.formatOnSave": true,
// enable for eslint-plugin json-format
"eslint.validate": ["json"],
"eslint.validate": [
"json"
],
}
65 changes: 55 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

const path = require('path')
const Promise = require('bluebird')
const fs = require('./lib/fs')
const fs = require('fs-extra')

const cloneDeep = require('lodash.clonedeep')
const browserify = require('browserify')
const watchify = require('watchify')

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

const typescriptExtensionRegex = /\.tsx?$/
const errorTypes = {
TYPESCRIPT_AND_TSIFY: 'TYPESCRIPT_AND_TSIFY',
TYPESCRIPT_NONEXISTENT: 'TYPESCRIPT_NONEXISTENT',
TYPESCRIPT_NOT_CONFIGURED: 'TYPESCRIPT_NOT_CONFIGURED',
TYPESCRIPT_NOT_STRING: 'TYPESCRIPT_NOT_STRING',
}

const bundles = {}

// by default, we transform JavaScript (including some proposal features),
Expand Down Expand Up @@ -60,7 +68,17 @@ const defaultOptions = {
},
}

const getBrowserifyOptions = (entry, userBrowserifyOptions = {}, typescriptPath = null) => {
const throwError = ({ message, type }) => {
const prefix = 'Error running @cypress/browserify-preprocessor:\n\n'

const err = new Error(`${prefix}${message}`)

if (type) err.type = type

throw err
}

const getBrowserifyOptions = async (entry, userBrowserifyOptions = {}, typescriptPath = null) => {
let browserifyOptions = cloneDeep(defaultOptions.browserifyOptions)

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

if (typescriptPath) {
if (typeof typescriptPath !== 'string') {
throwError({
type: errorTypes.TYPESCRIPT_NOT_STRING,
message: `The 'typescript' option must be a string. You passed: ${typescriptPath}`,
})
}

const pathExists = await fs.pathExists(typescriptPath)

if (!pathExists) {
throwError({
type: errorTypes.TYPESCRIPT_NONEXISTENT,
message: `The 'typescript' option must be a valid path to your TypeScript installation. We could not find anything at the following path: ${typescriptPath}`,
})
}

const transform = browserifyOptions.transform
const hasTsifyTransform = transform.some(([name]) => name.includes('tsify'))
const hastsifyPlugin = browserifyOptions.plugin.includes('tsify')

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

throw new Error(`Error running @cypress/browserify-preprocessor:
It looks like you passed the 'typescript' option and also specified a browserify ${type} for TypeScript. This may cause conflicts.
throwError({
type: errorTypes.TYPESCRIPT_AND_TSIFY,
message: `It looks like you passed the 'typescript' option and also specified a browserify ${type} for TypeScript. This may cause conflicts.
Please do one of the following:
1) Pass in the 'typescript' option and omit the browserify ${type} (Recommmended)
2) Omit the 'typescript' option and continue to use your own browserify ${type}
`)
2) Omit the 'typescript' option and continue to use your own browserify ${type}`,
})
}

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

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

const browserifyOptions = getBrowserifyOptions(filePath, options.browserifyOptions, options.typescript)
const browserifyOptions = await getBrowserifyOptions(filePath, options.browserifyOptions, options.typescript)
const watchifyOptions = Object.assign({}, defaultOptions.watchifyOptions, options.watchifyOptions)

if (!options.typescript && typescriptExtensionRegex.test(filePath)) {
throwError({
type: errorTypes.TYPESCRIPT_NOT_CONFIGURED,
message: `You are attempting to preprocess a TypeScript file, but do not have TypeScript configured. Pass the 'typescript' option to enable TypeScript support.
The file: ${filePath}`,
})
}

const bundler = browserify(browserifyOptions)

if (file.shouldWatch) {
Expand Down Expand Up @@ -227,7 +270,7 @@ const preprocessor = (options = {}) => {
})

const bundlePromise = fs
.ensureDirAsync(path.dirname(outputPath))
.ensureDir(path.dirname(outputPath))
.then(bundle)

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

preprocessor.errorTypes = errorTypes

if (process.env.__TESTING__) {
preprocessor.reset = () => {
for (let filePath in bundles) {
Expand Down
3 changes: 0 additions & 3 deletions lib/fs.js

This file was deleted.

54 changes: 47 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"coffeeify": "3.0.1",
"coffeescript": "1.12.7",
"debug": "4.1.1",
"fs-extra": "7.0.1",
"fs-extra": "9.0.0",
"lodash.clonedeep": "4.5.0",
"through2": "^2.0.0",
"watchify": "3.11.1"
Expand Down Expand Up @@ -66,7 +66,7 @@
"lib/*.js"
],
"engines": {
"node": ">=6.5"
"node": ">=8"
},
"license": "MIT",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/e2e_spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const _ = require('lodash')
const chai = require('chai')
const fs = require('fs-extra')
const path = require('path')
const snapshot = require('snap-shot-it')
const Bluebird = require('bluebird')

process.env.__TESTING__ = true

const fs = require('../../lib/fs')
const preprocessor = require('../../index')

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

return Bluebird.map(zippedArrays, ([sourcePath, sourceContent]) => {
return fs.readFileAsync(sourcePath, 'utf8')
return fs.readFile(sourcePath, 'utf8')
.then((str) => {
expect(str).to.eq(sourceContent)
})
Expand Down
Loading

0 comments on commit 0b94fc5

Please # to comment.