All files / tasks clean.ts

100% Statements 22/22
100% Branches 10/10
100% Functions 3/3
100% Lines 18/18

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 531x 1x   1x                 4x   1x 1x     3x 3x 3x 3x     3x 3x 3x   3x     3x   3x       1x                       2x      
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) {
    cb()
    return
  }
 
  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 isValidCleanPathConfig = cleanPatterns.every(e => {
    const resolvedRule = projectPath(e)
    const relativeRule = e
    const isInBlacklist =
      invalidCleanPaths.includes(relativeRule) || invalidCleanPaths.includes(resolvedRule)
 
    // If a user adds `../ etc.`
    const isDirectParent = isChildOf(TP.INIT_CWD, e) || isChildOf(TP.INIT_CWD, resolvedRule)
 
    return !isInBlacklist && !isDirectParent
  })
 
  if (!isValidCleanPathConfig) {
    throw 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}
        `
      )
    )
  } else {
    return del(cleanPatterns, delOptions)
  }
}