All files / gulpfile.ts/tasks clean.ts

0% Statements 0/23
0% Branches 0/10
0% Functions 0/3
0% Lines 0/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53                                                                                                         
import chalk from 'chalk'
import del from 'del'
import * as log from 'fancy-log'
import {
  isChildOf,
  ITimplaTask,
  projectDestPath,
  projectPath,
  projectSrcPath,
  TIMPLA_PROCESS as TP,
} from '../internal'
 
export const clean: ITimplaTask = ({ src, dest, clean: cleanOptions }) => cb => {
  if (!cleanOptions) {
    return cb
  }
 
  let cleanPatterns = cleanOptions.patterns || projectDestPath()
  cleanPatterns = Array.isArray(cleanPatterns) ? cleanPatterns : [cleanPatterns]
  const delOptions = cleanOptions.delOptions
  const invalidCleanPaths = [TP.INIT_CWD, projectSrcPath(), src]
 
  /** Blacklist naughty ways to delete files. */
  const isInvalidCleanPathConfig = cleanPatterns.every(e => {
    const resolvedRule = projectPath(e)
    const relativeRule = e
    const isNotInBlacklist =
      !invalidCleanPaths.includes(relativeRule) && !invalidCleanPaths.includes(resolvedRule)
 
    // If a user adds `../ etc.`
    const isNotDirectParent = !isChildOf(TP.INIT_CWD, e) && !isChildOf(TP.INIT_CWD, resolvedRule)
 
    return !isNotInBlacklist && !isNotDirectParent
  })
 
  if (isInvalidCleanPathConfig) {
    log.error(
      chalk.red(
        `=== To protect your files, Timpla skipped the misconfigured clean task.
        Please ensure that the dest option in timplaconfig is set to a path different from your project root! ===
        You attempted to run it with:
        ${cleanOptions.patterns}
        Blacklisted paths:
        ${invalidCleanPaths}
        `
      )
    )
    cb()
  } else {
    return del(cleanPatterns, delOptions)
  }
}