Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/forms / danger / Makefile.ts
Size: Mime:
/**
 * @file @todo use changed files to build script
 * @todo get output from master... compare to current...
 */
// import { getChangedFiles } from './Makefile.git'
// const changedFilesList = getChangedFiles()
import chalk from 'chalk'
import { resolve } from 'path'
import { writeFileSync } from 'fs'

const _scripts = {
  prettier:
    'npx prettier --write src/**/*.{ts,tsx} && prettier --write src/*.{ts,tsx}',
  styledlint: `npx stylelint './src/new-forms/**/*.(tsx|ts)' --config './stylelint.config.js'`,
  tslint:
    'npx tslint --format codeFrame --project tsconfig.json --config tslint.json',
  // 'test:ci': 'yarn test --runInBand',
  eslint: 'npx eslint src/{new-forms,validators}/*.{ts,tsx} --ext tsx --ext ts',
  test: `yarn test:ci`,
  typescript: 'npx tsc',
}
Object.keys(_scripts).forEach(key => {
  _scripts[key] = _scripts[key].split(' ')
  _scripts[key] = [_scripts[key][0], _scripts[key].slice(1)]
})

process.env.CI = 'true'

const scripts = (_scripts as any) as {
  [key: string]: [string, ReadonlyArray<string>]
}

import { spawn, Output } from './Makefile.shell'
import { SpawnOptions } from 'child_process'
const colorSpawn = (command: string, args: any) => {
  const options: SpawnOptions = {
    // stdio: 'inherit',
    env: process.env,
  }
  return spawn(command, args, options)
}

const fromExceptionToStdout = (exception: ExceptionFromChildProcess) => {
  if (typeof exception === 'object' && exception.stdout) {
    return exception.stdout.toString()
  } else {
    return exception
  }
}

export type ExceptionFromChildProcess = Error & { stdout?: string } | string
function handleException(exception: ExceptionFromChildProcess) {
  // const output = fromExceptionToStdout(exception)
  // const text = chalk.dim('// caught \\/')
  // const exceptionColoured = chalk.yellow(output as any)
  // console.debug(text)
  // console.error(exceptionColoured)
}

const results: { [key: string]: any } = {}

// @todo handleSuccessFor = (name) => handleSuccess
function handleSuccess(result: any) {
  // console.log('✅ ', result)
}

function logTitle(key: string) {
  console.log('\n\n\n')
  console.log(chalk.dim('==================================================='))
  console.log(chalk.bold('' + key))
  console.log(chalk.dim('==================================================='))
  console.log('\n\n\n')
}
function logResults() {
  Object.keys(results).forEach(key => {
    logTitle(key)
    const value = results[key]

    if (Array.isArray(value)) {
      value.forEach(x => console.log(x.length))
    } else {
      console.log(value.length)
    }
  })
}
function persistResults() {
  const outputPath = resolve(__dirname, './output.json')
  writeFileSync(outputPath, JSON.stringify(results, undefined, 2))
}
function fromResultToStdout(result: Output) {
  if (result.stdout && result.stderr) {
    return result.stdout.toString() + '\n' + result.stderr.toString()
  } else if (result.stdout) {
    return result.stdout.toString()
  } else if (result.stderr) {
    return result.stderr.toString()
  } else {
    return 'no output!!!'
  }
}

function run(key: string) {
  const script: [string, ReadonlyArray<string>] = scripts[key]
  const running = colorSpawn(script[0], script[1])

  return new Promise(resolve => {
    running
      .then(result => {
        handleSuccess(result)
        results[key] = fromResultToStdout(result)
        resolve(results[key])
      })
      .catch(exception => {
        handleException(exception)
        results[key] = fromExceptionToStdout(exception)
        resolve(results[key])
      })
  })
}

async function danger() {
  // @todo need to capture with colours in subprocess
  // log.shush()
  // had to do this instead of exec for colors...
  console.time('danger')

  // 75305.972ms
  // 63578.819ms
  // await run('tslint')
  // await run('test')
  // await run('eslint')
  // await run('styledlint')
  // await run('typescript')

  // 52761.767ms
  // 40774.407ms
  await Promise.all(
    ['tslint', 'test', 'eslint', 'styledlint', 'typescript'].map(run)
  )

  console.timeEnd('danger')

  console.time('logAndSave')
  logResults()
  persistResults()
  console.timeEnd('logAndSave')

  // log.unShush()
  console.log('\n...;')
}

danger()

process.on('unhandledRejection', (reason, p) => {
  console.log(
    'Possibly Unhandled Rejection at: Promise ',
    p,
    ' reason: ',
    reason
  )
})
process.on('unhandledException' as any, exception => {
  console.log('ignoring subprocess')
})