All files / tasks init.ts

100% Statements 34/34
100% Branches 4/4
100% Functions 4/4
100% Lines 30/30

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 54 55 561x 1x 1x 1x 1x 1x 1x   1x 1x   10x   10x 3x     3x 2x 2x 2x     1x   1x 1x     2x   2x 2x 2x 2x 2x         2x 2x   2x 2x                 2x    
import chalk from 'chalk'
import * as log from 'fancy-log'
import * as gulp from 'gulp'
import * as replace from 'gulp-replace'
import merge = require('merge-stream')
import * as path from 'path'
import { projectPath, projectSrcPath } from '../internal'
 
const BASE_JS_TEMPLATES = './init_templates/js'
const EXCLUDE_TS_FILES = '!**/*.ts{,?}*'
 
const fileNamesOnly = (name: string) => name.split('/').splice(-1)[0]
 
const getJSInitTemplatePath = (...paths: any) => {
  return path.join(BASE_JS_TEMPLATES, ...paths)
}
 
export const init: (enableTS?: boolean) => gulp.TaskFunction = (enableTS = true) => () => {
  log(chalk.green('Generating default Timpla project files'))
  const configFiles = ['.eslintrc', '.prettierrc']
  const srcFiles = ['src/**/*', 'src/**/.gitkeep']
 
  if (enableTS) {
    configFiles.push('babel.config.js', 'tslint.json', 'tsconfig.json')
  } else {
    configFiles.push(getJSInitTemplatePath('babel.config.js'))
    srcFiles.push(getJSInitTemplatePath('src', '**/*'), EXCLUDE_TS_FILES)
  }
 
  const timplaConfigFile = enableTS ? '.timplaconfig.js' : getJSInitTemplatePath('.timplaconfig.js')
 
  const projectRoot = projectPath()
  const projectSrPath = projectSrcPath()
  const configStream = gulp.src(configFiles).pipe(gulp.dest(projectRoot))
  const srcStream = gulp.src(srcFiles).pipe(gulp.dest(projectSrPath))
  const timplaConfStream = gulp
    .src(timplaConfigFile)
    .pipe(replace('./lib/public', 'timpla'))
    .pipe(gulp.dest(projectRoot))
 
  const filesGenerated = [...configFiles, timplaConfigFile].map(fileNamesOnly)
  const filesGeneratedDesc = filesGenerated.join(', ')
 
  log(chalk.green(`Config files generated: ${filesGeneratedDesc}.`))
  log(
    chalk.yellow(`
To start the dev server:
`),
    chalk.magenta(`
npx timpla
`)
  )
 
  return merge(timplaConfStream, configStream, srcStream)
}