Skip to content

Commit 6d5b81f

Browse files
committed
major: do not overwrite plugins file on every install, close #4
1 parent 3603c8b commit 6d5b81f

File tree

5 files changed

+55
-32
lines changed

5 files changed

+55
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
Which is too much hustle. With this module you just need to execute regular `npm install` command and you can write TypeScript spec files right away.
1616

17-
**note** overwrites existing `cypress/plugins/index.js` file.
17+
**note** overwrites existing `cypress/plugins/index.js` file once. In other installs looks for file `cypress/plugins/cy-ts-preprocessor.js` to avoid overwriting.
1818

1919
## Install
2020

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,18 @@
8585
"mocha": "4.0.1",
8686
"nsp": "3.1.0",
8787
"pre-git": "3.16.0",
88-
"prettier-standard": "7.0.3",
88+
"prettier-standard": "8.0.0",
8989
"semantic-release": "8.2.0",
9090
"simple-commit-message": "3.3.2",
9191
"standard": "10.0.3"
9292
},
9393
"dependencies": {
94-
"@cypress/webpack-preprocessor": "1.1.0",
94+
"@cypress/webpack-preprocessor": "1.1.3",
9595
"am-i-a-dependency": "1.1.2",
9696
"chalk": "2.3.0",
9797
"debug": "3.1.0",
98-
"shelljs": "0.7.8",
98+
"shelljs": "0.8.0",
99+
"terminal-banner": "1.1.0",
99100
"ts-loader": "3.2.0"
100101
},
101102
"peerDependencies": {

src/add-plugin.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const debug = require('debug')('add-typescript-to-cypress')
22
const chalk = require('chalk')
3+
const terminalBanner = require('terminal-banner').terminalBanner
34
const amDependency = require('am-i-a-dependency')()
5+
46
if (amDependency) {
5-
console.log('adding TypeScript plugin to Cypress')
6-
console.log('current folder', process.cwd())
7+
debug('current folder', process.cwd())
78

89
const fs = require('fs')
910
const path = require('path')
@@ -17,6 +18,18 @@ if (amDependency) {
1718
// node_modules/@bahmutov/add-typescript-to-cypress
1819
const root = path.join(process.cwd(), '..', '..', '..')
1920
const cypressFolder = path.join(root, 'cypress')
21+
const pluginsFolder = path.join(cypressFolder, 'plugins')
22+
const ourPreprocessorFilename = path.join(
23+
pluginsFolder,
24+
'cy-ts-preprocessor.js'
25+
)
26+
27+
if (fs.existsSync(ourPreprocessorFilename)) {
28+
debug('found existing file', ourPreprocessorFilename)
29+
debug('no need to overwrite again')
30+
process.exit(0)
31+
}
32+
2033
if (!fs.existsSync(cypressFolder)) {
2134
console.error('⚠️ Cannot find "cypress" folder in %s', chalk.yellow(root))
2235
console.error('Please scaffold Cypress folder by opening Cypress once')
@@ -32,10 +45,14 @@ if (amDependency) {
3245
}
3346

3447
const addPluginFile = () => {
35-
const plugins = path.join(cypressFolder, 'plugins')
36-
const pluginsIndex = path.join(plugins, 'index.js')
48+
console.log('copying plugin file')
49+
const pluginsIndex = path.join(pluginsFolder, 'index.js')
3750
const sourcePlugin = path.join(__dirname, 'plugin.js')
3851
shell.cp(sourcePlugin, pluginsIndex)
52+
53+
console.log('copying TS preprocessor file')
54+
const sourcePreprocessor = path.join(__dirname, 'cy-ts-preprocessor.js')
55+
shell.cp(sourcePreprocessor, ourPreprocessorFilename)
3956
}
4057

4158
const addTSConfigFile = () => {
@@ -52,6 +69,7 @@ if (amDependency) {
5269
}
5370
}
5471

72+
terminalBanner('adding TypeScript plugin for Cypress')
5573
addPluginFile()
5674
addTSConfigFile()
5775
} else {

src/cy-ts-preprocessor.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const wp = require('@cypress/webpack-preprocessor')
2+
3+
const webpackOptions = {
4+
resolve: {
5+
extensions: ['.ts', '.js']
6+
},
7+
module: {
8+
rules: [
9+
{
10+
test: /\.ts$/,
11+
exclude: [/node_modules/],
12+
use: [
13+
{
14+
loader: 'ts-loader'
15+
}
16+
]
17+
}
18+
]
19+
}
20+
}
21+
22+
const options = {
23+
webpackOptions
24+
}
25+
26+
module.exports = wp(options)

src/plugin.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
1-
const wp = require('@cypress/webpack-preprocessor')
2-
3-
const webpackOptions = {
4-
resolve: {
5-
extensions: ['.ts', '.js']
6-
},
7-
module: {
8-
rules: [
9-
{
10-
test: /\.ts$/,
11-
exclude: [/node_modules/],
12-
use: [
13-
{
14-
loader: 'ts-loader'
15-
}
16-
]
17-
}
18-
]
19-
}
20-
}
1+
const cypressTypeScriptPreprocessor = require('./cy-ts-preprocessor')
212

223
module.exports = on => {
23-
const options = {
24-
webpackOptions
25-
}
26-
on('file:preprocessor', wp(options))
4+
on('file:preprocessor', cypressTypeScriptPreprocessor)
275
}

0 commit comments

Comments
 (0)