<commit_before>// Licensed under the Apache License, Version 2.0 (the “License”); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an “AS IS” BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

import {CONFIGS_DIR, PACKAGE_DIR} from '../paths'
import {join} from 'path'
import {runBinary} from '../binaries'

const ESLINT_CONFIG_PATH = join(CONFIGS_DIR, 'eslint', 'index.js')
const ESLINT_ARGS = [
  '--ignore-pattern',
  '/build/',
  '--config',
  ESLINT_CONFIG_PATH,
  PACKAGE_DIR,
]

export default {
  command: 'lint',
  description: 'Check or fix code style.',
  builder: yargs => yargs.option('fix', {
    description: 'Fix lint errors.',
  }),
  handler: ctrineArgs => {
    let args = [...ESLINT_ARGS]

    if (ctrineArgs.fix)
      args.push('--fix')

    // Lint test files.
    // runBinary('eslint', args, ctrineArgs)
    // Lint all other files.
    runBinary('eslint', args, ctrineArgs)
  },
}
<commit_msg>Lint in two steps<commit_after>// Licensed under the Apache License, Version 2.0 (the “License”); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an “AS IS” BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

import {CONFIGS_DIR, PACKAGE_DIR} from '../paths'
import {EPILOG, PROLOG, SEPARATOR} from '../banner'
import {join} from 'path'
import {getModuleNameVersion} from '../modules'
import {runBinPiped} from '../binaries'

const ESLINT_CONFIG_PATH = join(CONFIGS_DIR, 'eslint', 'index.js')
const ESLINT_ARGS = [
  '--ignore-pattern',
  'build/**',
  '--config',
  ESLINT_CONFIG_PATH,
  PACKAGE_DIR,
]

function logBuffer(buffer:Buffer) {
  if (buffer.length)
    console.log(buffer.toString().replace(/\n+$/, ''))
}

export default {
  command: 'lint',
  description: 'Check or fix code style.',
  builder: yargs => yargs.option('fix', {
    description: 'Fix lint errors.',
  }),
  handler: env => {
    let sourceArgs = ['--ignore-pattern', '**/__tests__/**', ...ESLINT_ARGS]
    let testsArgs = [
      '--ignore-pattern',
      'src/**',
      '--ignore-pattern',
      '!**/__tests__',
      '--ignore-pattern',
      '!**/__tests__/**',
      ...ESLINT_ARGS
    ]

    if (env.fix) {
      sourceArgs.push('--fix')
      testsArgs.push('--fix')
    }

    console.log(PROLOG)
    console.log('Linter: %s', getModuleNameVersion('ESlint'))
    console.log(SEPARATOR)
    console.log()

    console.log('    [1/2] Linting sources...')

    let {
      stdout: outSources,
      stderr: errorSources,
    } = runBinPiped('eslint', sourceArgs, env)

    logBuffer(errorSources)
    logBuffer(outSources)

    console.log()
    console.log('    [2/2] Linting tests...')

    let {
      stdout: outTests,
      stderr: errorTests,
    } = runBinPiped('eslint', testsArgs, { jest: true, ...env})

    logBuffer(errorTests)
    logBuffer(outTests)

    console.log()
    console.log(EPILOG)
  },
}

{'fraction_non_alphanumeric': 0.14694335389792484, 'fraction_numerical': 0.0033651149747616375, 'mean_word_length': 3.1332560834298957, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '312692', 'n_tokens_mistral': 1145, 'n_tokens_neox': 1128, 'n_words': 340}