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 / scripts.ts
Size: Mime:
import { SpawnScriptArgs } from './shell'
import { APP_ROOT_DIR } from './config'

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',
  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)]
})

export const scripts = (_scripts as any) as SpawnScriptArgs

import { resolve } from 'path'
import { writeFileSync, readFileSync, unlinkSync } from 'fs'
import { getChangedFiles } from './git'

/**
 * @todo this will need to be changing tsconfig, not tslint
 */
export function runTslintFor(relativeChangedFilesList: string[]) {
  const tslintPath = resolve(APP_ROOT_DIR, './tslint.json')
  const tslintBackupPath = resolve(APP_ROOT_DIR, './tslint.backup.json')
  const tslintConfigBuffer = readFileSync(tslintPath).toString('utf-8')
  const tslintConfig = JSON.parse(tslintConfigBuffer)
  writeFileSync(tslintBackupPath, tslintConfigBuffer)

  const tslint =
    'npx tslint --format codeFrame --project tsconfig.json --config tslint.json'

  console.error('@todo need to handle running this here')
  console.log(tslint)

  /**
   * @see https://github.com/palantir/tslint/blob/master/src/configuration.ts
   * @alias configIgnoringEverythingExceptFilesWeWant
   * @todo !!!!!! NEED TO CHANGE FROM EXCLUDE TO INCLUDE (black to white)
   */
  tslintConfig.linterOptions = {
    exclude: [
      'src/inputs/**/*',
      'src/forms/**/*',
      'config/**/*.js',
      'node_modules/**/*.ts',
      'coverage/lcov-report/*.js',
    ],
  }
  // update the file for use by script (and then restore original)
  // this is because the `cli args` do not support what we want
  const configStringified = JSON.stringify(tslintConfig, undefined, 2)
  writeFileSync(tslintPath, configStringified)
}

export function runTscFor(relativeChangedFilesList: string[]) {
  const tsconfigPath = resolve(APP_ROOT_DIR, './tsconfig.json')
  const tsconfigBackupPath = resolve(APP_ROOT_DIR, './tsconfig.backup.json')
  const tsconfigConfigBuffer = readFileSync(tsconfigPath).toString('utf-8')
  const tsconfigConfig = JSON.parse(tsconfigConfigBuffer)
  writeFileSync(tsconfigBackupPath, tsconfigConfigBuffer)

  const tsconfig = 'npx tsc'

  tsconfigConfig.linterOptions = {
    include: relativeChangedFilesList,
  }
  // update the file for use by script (and then restore original)
  // this is because the `cli args` do not support what we want
  const configStringified = JSON.stringify(tsconfigConfig, undefined, 2)
  writeFileSync(tsconfigPath, configStringified)

  console.error('@todo need to handle running this here')
  console.log(tsconfig)

  // restore from backup
  writeFileSync(tsconfigPath, tsconfigConfigBuffer)
  unlinkSync(tsconfigBackupPath)
}

export async function generateScriptsForChangedfiles() {
  const changedFilesList = await getChangedFiles()
  console.log(changedFilesList)

  const relativeChangedFilesList = changedFilesList.map(
    file => 'src' + file.split('src/').pop()
  )

  // runTslintFor(relativeChangedFilesList)

  const styledlint = relativeChangedFilesList.map(
    file => `npx stylelint './${file}' --config './stylelint.config.js'`
  )
  const prettier = relativeChangedFilesList.map(
    file => `npx prettier --write ${file}`
  )

  const eslint = relativeChangedFilesList.map(
    file => `npx eslint ${file} --ext tsx --ext ts`
  )

  const test = `yarn test:ci --onlyChanged`

  const typescript = runTscFor(relativeChangedFilesList)

  return {
    prettier,
    styledlint,
    // tslint,
    eslint,
    test,
    typescript,
  }
}