UNPKG

2.03 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const fs = require('fs')
4const path = require('path')
5const yargs = require('yargs')
6const execSync = require('child_process').execSync
7
8const argv = yargs.usage('Usage: $0 [options]').option('fix', {
9 describe: 'auto-fix problems',
10 type: 'boolean',
11}).argv
12
13function scanFor(file, maxDepth = 3) {
14 let pathToFile = path.join(process.cwd(), file)
15 while (pathToFile.length > file.length + 1 && !fs.existsSync(pathToFile) && maxDepth >= 0) {
16 const parentDir = path.dirname(pathToFile.substr(0, pathToFile.length - file.length))
17 pathToFile = path.join(parentDir, file)
18 maxDepth--
19 }
20
21 return fs.existsSync(pathToFile) ? pathToFile : ''
22}
23
24const TSCONFIG_PATH = scanFor('tsconfig.json')
25const TSLINT_PATH = scanFor('node_modules/.bin/tslint')
26const ESLINT_PATH = scanFor('node_modules/.bin/eslint')
27const PRETTIER_PATH = scanFor('node_modules/.bin/prettier')
28const TSLINTRC_PATH = scanFor('tslint.json')
29const ESLINTRC_PATH = scanFor('.eslintrc')
30
31if (!TSLINT_PATH && !ESLINT_PATH) {
32 throw new Error('Must have either eslint or tslint installed')
33}
34
35if (!PRETTIER_PATH) {
36 throw new Error('Must have prettier installed')
37}
38
39if (!TSLINTRC_PATH && !ESLINTRC_PATH) {
40 throw new Error('Must have either eslint or tslint config')
41}
42
43function exec(command) {
44 try {
45 execSync(command, {stdio: 'inherit'})
46 return true
47 } catch (err) {
48 process.stderr.write(err.message + '\n')
49 return false
50 }
51}
52
53const directories = `{packages/*/,./}{src/**/,lib/**/,bin/**/,test/**/,}`
54
55const lintFixArg = argv.fix ? '--fix' : ''
56const lintCommand = TSCONFIG_PATH
57 ? `${TSLINT_PATH} --project . -c ${TSLINTRC_PATH}`
58 : `${ESLINT_PATH} -c ${ESLINTRC_PATH} '${directories}*.js'`
59const lintPassed = exec(`${lintCommand} ${lintFixArg}`)
60const prettierFixArg = argv.fix ? '--write' : '--list-different'
61const prettierFiles = `${directories}*.{ts,css,scss,md,json}`
62const prettierPassed = exec(`${PRETTIER_PATH} ${prettierFixArg} '${prettierFiles}'`)
63process.exit(lintPassed && prettierPassed ? 0 : 1)