All files / src/handlers/merge index.ts

91.3% Statements 21/23
83.33% Branches 5/6
66.67% Functions 2/3
94.44% Lines 17/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 3315x 15x 15x 15x 15x     15x 15x 15x   15x               40x 40x   40x 40x   40x 24x 8x       15x  
import { extname, basename } from 'path'
import { FileGenerator, Handler } from '@/internal'
import mergeJson from './merge-json'
import mergeIgnore from './merge-ignore'
import mergeYaml from './merge-yaml'
 
 
const jsonFileExts = ['.json']
const yamlFileExts = ['.yaml', '.yml']
const ignoreFilenames = ['.gitignore', '.npmignore']
 
const notSupportErrorMessage = (filename, projectPath): string => [
  `Merge files of this type(${filename}) are not supported by merge handler.`,
  'Please feedback this question to the template developer.',
  'The current file will be overwritten directly by the template file.',
  `Path: ${projectPath}`,
].join('\n')
 
 
const genFile: FileGenerator = async(file, resource) => {
  const { projectPath } = file
 
  const ext = extname(projectPath)
  const filename = basename(projectPath)
 
  if (jsonFileExts.includes(ext)) await mergeJson(file, resource)
  else if (yamlFileExts.includes(ext)) await mergeYaml(file, resource)
  else Eif (ignoreFilenames.includes(filename)) await mergeIgnore(file, resource)
  else throw new Error(notSupportErrorMessage(filename, projectPath))
}
 
export default new Handler(genFile)