Skip to content

Commit

Permalink
implement config cli
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-murakami committed Nov 14, 2022
1 parent 1d97867 commit b4973ff
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 8 deletions.
89 changes: 89 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env node
const fs = require('node:fs')
const path = require('node:path')
const process = require('node:process')

const { Command } = require('commander')
const inquirer = require('inquirer')
const program = new Command()

const rootDir = path.join(__dirname, '..')
const configDir = path.join(rootDir, 'template', 'config')
const currentDir = process.cwd()

const file = {
eslintignore: '.eslintignore',
eslintrc: '.eslintrc.js',
prettierrc: '.prettierrc',
}

const templateConfig = {
eslintignore: path.join(configDir, file.eslintignore),
eslintrc: path.join(configDir, file.eslintrc),
prettierrc: path.join(configDir, file.prettierrc),
}

const destination = {
eslintignore: path.join(currentDir, file.eslintignore),
eslintrc: path.join(currentDir, file.eslintrc),
prettierrc: path.join(currentDir, file.prettierrc),
}

program.name('eslint-config-ts-prefixer')
program
.command('config')
.description(
`create ${file.prettierrc}/${file.eslintrc}/${file.eslintignore} files your current directory.`
)
.option('--prettier', `create ${file.prettierrc} only`)
.option('--eslint', `create ${file.eslintrc}/${file.eslintignore} only`)
.action(async (options) => {
if (Object.keys(options).length === 0) {
await createESLintConfig()
await createPrettierConfig()
return
}
if (options.eslint === true) await createESLintConfig()
if (options.prettier === true) await createPrettierConfig()
})

// run
program.parse()

// functions
async function createESLintConfig() {
await copyConfig('eslintrc')
await copyConfig('eslintignore')
}

async function createPrettierConfig() {
await copyConfig('prettierrc')
}

async function copyConfig(filename) {
if (fs.existsSync(destination[filename])) {
const answer = await inquirer.prompt({
choices: [
{
key: 'y',
name: 'Overwrite',
value: 'overwrite',
},
{
key: 'n',
name: 'Abort',
value: 'abort',
},
],
message: `Your ${filename} already exists.`,
name: 'overwrite',
type: 'expand',
})

if (answer.overwrite === 'overwrite') {
fs.copyFileSync(templateConfig[filename], destination[filename])
}
} else {
fs.copyFileSync(templateConfig[filename], destination[filename])
}
}
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "eslint-config-ts-prefixer",
"version": "0.0.1",
"version": "0.0.2",
"description": "ESLint rules that specialized TypeScript",
"main": "index.js",
"bin": "./bin/cli.js",
"scripts": {
"lint": "eslint index.js",
"prettier": "prettier --write \"**/*.+(json|yml|css|md|mdx)\""
Expand All @@ -17,9 +18,11 @@
"url": "https://github.com/laststance/eslint-config-ts-prefixer/issues"
},
"homepage": "https://github.com/laststance/eslint-config-ts-prefixer",
"dependencies": {},
"dependencies": {
"commander": "^9.4.1",
"inquirer": "^8.0.0"
},
"devDependencies": {
"typescript": "^4.8.4",
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.42.1",
"eslint": "^8.27.0",
Expand All @@ -28,7 +31,8 @@
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-sort-keys-fix": "^1.1.2",
"prettier": "^2.7.1"
"prettier": "^2.7.1",
"typescript": "^4.8.4"
},
"peerDependencies": {
"typescript": "^4.0.0"
Expand Down
1 change: 1 addition & 0 deletions template/config/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
7 changes: 7 additions & 0 deletions template/config/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.export = {
extends: 'ts-prefixer',
globals: {},
plugins: [],
rules: {},
settings: {},
}
4 changes: 4 additions & 0 deletions template/config/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"semi": false
}
Loading

0 comments on commit b4973ff

Please sign in to comment.