All files / gulpfile.ts bin.ts

0% Statements 0/60
0% Branches 0/10
0% Functions 0/11
0% Lines 0/56

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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                                                                                                                                                                                                 
#!/usr/bin/env node
 
process.env.INIT_CWD = process.cwd()
 
import chalk from 'chalk'
import * as spawn from 'cross-spawn'
import * as log from 'fancy-log'
import * as fs from 'fs'
import * as path from 'path'
import { TIMPLA_CONFIG as TC, TIMPLA_PROCESS as TP } from './internal'
 
const gulpModulePath = path.dirname(require.resolve('gulp'))
const gulpBinaryFile = path.join(gulpModulePath, '/bin/gulp.js')
const gulpFile = path.resolve(__dirname, '../lib')
 
const logLine = () => log(chalk.green(`==================================================`))
 
// Check if the user specified timplaWatch
let timplaWatch = TC.development && TC.development.timplaWatch
// If the user didn't specify anything, set timplaWatch to true
if (timplaWatch === undefined) {
  timplaWatch = true
}
 
const args = process.argv.slice(2)
 
// when no args are passed
const devMode = args.length === 0
 
// Prepend everything with npx
args.unshift('--gulpfile', gulpFile)
 
const defaultEnv = devMode ? 'development' : 'production'
process.env.NODE_ENV = process.env.NODE_ENV || defaultEnv
const spawnEnv = Object.create(process.env)
 
const run = (reload = false) => {
  // Used to check if pages should reload, on dev mode.
  spawnEnv.TIMPLA_DEV_RELOAD = reload
  const prc = spawn(gulpBinaryFile, args, { env: spawnEnv, stdio: 'inherit' })
  prc.on('close', (code, signal) => {
    if (code !== null) {
      process.exit(code)
    }
    if (signal) {
      if (signal === 'SIGKILL') {
        process.exit(137)
      }
      // eslint-disable-next-line
      log(chalk.green(`got signal ${signal}, exiting`))
      process.exit(1)
    }
    process.exit(0)
  })
  prc.on('error', err => {
    log.error(err)
    process.exit(1)
  })
  return prc
}
 
let proc = run()
 
const restartServer = (eventType: string, fileName: string) => {
  log(chalk.green(`\n> ${eventType}d ${fileName}, restarting the server...`))
  // Don't listen to 'close' now since otherwise parent gets killed by listener
  proc.removeAllListeners('close')
  proc.kill()
  proc = run(true)
}
 
if (devMode && timplaWatch) {
  const watchableFiles = ['eslint', 'tslint', 'babel', 'tsconfig', 'timplaconfig']
  const sessionWatchFiles = [...watchableFiles]
  fs.watch(TP.INIT_CWD, (eventType, fileName) => {
    if (watchableFiles.find(e => fileName.indexOf(e) > -1)) {
      // eslint-disable-next-line
      restartServer(eventType, fileName)
    }
  })
 
  // Check if it is an array
  if (timplaWatch instanceof Array) {
    timplaWatch.forEach(fileOrFolder => {
      const pathsToCheck = [path.resolve(TP.INIT_CWD, fileOrFolder), fileOrFolder]
      const firstValidPath = pathsToCheck.find(e => fs.existsSync(e))
      if (firstValidPath) {
        sessionWatchFiles.push(firstValidPath)
        fs.watch(firstValidPath, (eventType: string, fileName: string) => {
          restartServer(eventType, fileName)
        })
      }
    })
  }
 
  logLine()
  log(chalk.green(`Timpla started`))
  logLine()
  log(chalk.green(`========= Watching files for full-reload =========`))
  sessionWatchFiles.map(f => log(f))
  logLine()
} else if (devMode) {
  log(chalk.yellow(`Timpla: reload disabled`))
}