Skip to content

Commit

Permalink
fix: eslintConfigTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
AntzyMo committed Oct 13, 2023
1 parent 7d07946 commit 50f3c31
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
"dependencies": {
"commander": "^11.0.0",
"diff": "^5.1.0",
"execa": "^8.0.1",
"kolorist": "^1.8.0",
"local-pkg": "^0.5.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 35 additions & 10 deletions src/ies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from 'node:path'
import * as Diff from 'diff'
import { execaCommand } from 'execa'
import { bold, green } from 'kolorist'
import { isPackageExists } from 'local-pkg'
import { bold, gray, green, red } from 'kolorist'
import { readFile, readdir, writeFile } from 'node:fs/promises'

export async function setupEslintConfig() {
Expand Down Expand Up @@ -32,24 +33,48 @@ async function addEslintConfig() {
const rootPkgPath = path.resolve(process.cwd(), 'package.json')
const pkgInfo = await readFile(rootPkgPath, { encoding: 'utf8' })

let importAntzyESlintConfig = ''
let eslintConfigTemplate = ''
if (pkgInfo.includes('"type": "module"')) {
importAntzyESlintConfig = "import { antzy } from '@antzy/eslint-config'"
eslintConfigTemplate = "import { antzy } from '@antzy/eslint-config'\n\nexport default antzy()"
} else {
importAntzyESlintConfig = "const { antzy } = require('@antzy/eslint-config')"
eslintConfigTemplate = "const { antzy } = require('@antzy/eslint-config')\n\nmodule.exports = antzy()"
}

const eslintConfigTemplate = `
${importAntzyESlintConfig}\n\nexport default antzy()
`

const eslintFileUrl = path.join(process.cwd(), 'eslint.config.js')
await writeFile(eslintFileUrl, eslintConfigTemplate.trim()).catch(() => process.exit())

console.log('')
console.log(green(`+ 1 | ${importAntzyESlintConfig}`))
console.log(green(`+ 2 | export deafult antzy()\n`))
printDiff('', eslintConfigTemplate)

console.log('The above configuration has been added to:', bold(green('eslint.config.js')))
console.log('')
}

function printDiff(form: string, to: string) {
const diffs = Diff.diffLines(form, to)
let no = 0
let output = ''

for (const diff of diffs) {
let lines = diff.value.trimEnd().split('\n')
if (!('added' in diff)) {
if (lines.length > 3) {
no = diff.count - 3
lines = lines.slice(-3)
}
}

for (const line of lines) {
if (!diff.added) no += 1
if (diff.added) {
output += green(`+ | ${line}\n`)
} else if (diff.removed) {
output += red(`- ${no.toString().padStart(3, ' ')} | ${line}\n`)
} else {
output += gray(` ${no.toString().padStart(3, ' ')} | ${line}\n`)
}
}
}

console.log(output.trimEnd())
}

0 comments on commit 50f3c31

Please sign in to comment.