server: simplify scaffolding support/index.js

the previous strategy was meant to aid the upgrade to 0.18.0 so that users with files in the support folder would automatically get a support/index.js with the right contents. most users should be beyond 0.18.0 at this point, so we can simplify this code
This commit is contained in:
Chris Breiding
2017-07-07 16:06:55 -04:00
parent fde040421e
commit c13a14fe2d
3 changed files with 15 additions and 61 deletions
+2 -33
View File
@@ -4,7 +4,6 @@ Promise = require("bluebird")
path = require("path")
cypressEx = require("@packages/example")
glob = require("glob")
hbs = require("hbs")
cwd = require("./cwd")
log = require("debug")("cypress:server:scaffold")
{ propEq, complement, equals, compose, head, isEmpty, always } = require("ramda")
@@ -71,21 +70,19 @@ isNewProject = (integrationFolder) ->
module.exports = {
isNewProject
# when is this called?
integration: (folder, config) ->
log "integration in folder #{folder}"
@verifyScaffolding folder, =>
log "copying examples into #{folder}"
@_copy(INTEGRATION_EXAMPLE_SPEC, folder, config)
# when is this called?
fixture: (folder, config) ->
@verifyScaffolding folder, =>
log "fixture needs to copy example.json"
@_copy("example.json", folder, config)
support: (folder, config) ->
log "support folder #{folder} support file #{config.resolved.supportFile.from}"
log "support folder #{folder} support file #{config.supportFile}"
## skip if user has explicitly set supportFile
return Promise.resolve() if not isDefault(config, "supportFile")
@@ -95,37 +92,9 @@ module.exports = {
Promise.join(
@_copy("defaults.js", folder, config)
@_copy("commands.js", folder, config)
@_copy("index.js", folder, config)
)
)
.then =>
log "checking if support file #{config.supportFile} exists"
fs.statAsync(config.supportFile)
.catch {code: "ENOENT"}, =>
## only if support/index.js doesn't exist already
## rethrow error if it's something unexpected
log "support file does not exist #{config.supportFile}"
Promise.join(
fs.readFileAsync(cwd("lib", "scaffold", "index.js.hbs"), "utf8"),
glob(path.join(folder, "**", "*"), {nodir: true})
.map (filePath) ->
## strip off the extension from our filePath
ext = path.extname(filePath)
filePath = filePath.replace(ext, "")
## get the relative path from the supportFolder
## to this specific file's path
path.relative(config.supportFolder, filePath)
)
.spread (indexTemplate, supportFiles) =>
indexTemplate = hbs.handlebars.compile(indexTemplate)
contents = indexTemplate({ files: supportFiles })
filePath = path.join(folder, "index.js")
@_assertInFileTree(filePath, config)
log "writing file #{filePath}"
fs.outputFileAsync(filePath, contents)
_copy: (file, folder, config) ->
## allow file to be relative or absolute
@@ -15,11 +15,9 @@
// Import commands.js and defaults.js
// using ES2015 syntax:
{{#each files}}
import "./{{this}}"
{{/each}}
import "./commands"
import "./defaults"
// Alternatively you can use CommonJS syntax:
{{#each files}}
// require("./{{this}}")
{{/each}}
// require("./commands")
// require("./defaults")
+9 -22
View File
@@ -138,33 +138,20 @@ describe "lib/scaffold", ->
config.get(pristinePath).then (@cfg) =>
{@supportFolder} = @cfg
it "does not create any files but index.js if supportFolder directory already exists", ->
## create the supportFolder ourselves manually
fs.ensureDirAsync(@supportFolder)
it "does not create any files if supportFolder directory already exists", ->
## first remove it
fs.removeAsync(@supportFolder)
.then =>
## create the supportFolder ourselves manually
fs.ensureDirAsync(@supportFolder)
.then =>
## now scaffold
scaffold.support(@supportFolder, @cfg)
scaffold.integration(@supportFolder, @cfg)
.then =>
glob("**/*", {cwd: @supportFolder})
.then (files) ->
expect(files.length).to.eq(1)
expect(files[0]).to.include('index.js')
it "does not create any files if supportFolder and index.js already exist", ->
indexPath = path.join(@supportFolder, "index.js")
## create the supportFolder ourselves manually
fs.ensureDirAsync(@supportFolder)
.then =>
## now scaffold
scaffold.support(@supportFolder, @cfg).then =>
fs.outputFileAsync(indexPath, ";")
.then =>
glob("**/*", {cwd: @supportFolder})
.then (files) ->
fs.readFileAsync(indexPath).then (buffer) ->
expect(files.length).to.eq(1)
## it doesn't change the contents of the existing index.js
expect(buffer.toString()).to.equal(";")
## ensure no files exist
expect(files.length).to.eq(0)
it "does not create any files if supportFile is not default", ->
@cfg.resolved.supportFile.from = "config"