Skip to content

Commit

Permalink
add InsertRootdirFilesPath2TSconfig()
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-murakami committed Aug 24, 2023
1 parent 867b4ad commit f81c1c2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
41 changes: 39 additions & 2 deletions bin/cli.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
/* eslint-disable no-console */
import { copyFileSync, existsSync } from 'node:fs'
import { join, dirname } from 'node:path'
import { cwd, argv } from 'node:process'
Expand All @@ -17,20 +18,23 @@ const rootDir = join(__dirname, '..')
const configDir = join(rootDir, 'template', 'config')
const currentDir = cwd()

// auto geration files
const file = {
eslintignore: '.eslintignore',
eslintrc: '.eslintrc.cjs',
prettierignore: '.prettierignore',
prettierrc: '.prettierrc',
}

// eslint-config-txsprefixer/template/config
const templateConfig = {
eslintignore: join(configDir, file.eslintignore),
eslintrc: join(configDir, file.eslintrc),
prettierignore: join(configDir, file.prettierignore),
prettierrc: join(configDir, file.prettierrc),
}

// package user's file generation path
const destination = {
eslintignore: join(currentDir, file.eslintignore),
eslintrc: join(currentDir, file.eslintrc),
Expand All @@ -55,6 +59,9 @@ program
if (options.eslint === true) await createESLintConfig()
if (options.prettier === true) await createPrettierConfig()
})
.action(() => {
InsertRootdirFilesPath2TSconfig()
})

// npx eslint-config-ts-prefixer barebone
program
Expand All @@ -66,10 +73,14 @@ program
await createPrettierConfig()
})

// run
/**
* Run
*/
program.parse(argv)

// functions
/**
* Functions
*/
async function createESLintConfig() {
await copyConfig('eslintrc')
await copyConfig('eslintignore')
Expand Down Expand Up @@ -107,3 +118,29 @@ async function copyConfig(filename) {
copyFileSync(templateConfig[filename], destination[filename])
}
}

function InsertRootdirFilesPath2TSconfig() {
// get rootDir's `tsconfig.json` contents
const tsconfigPath = join(rootDir, 'tsconfig.json')
const tsconfigContents = existsSync(tsconfigPath)
? fs.readFileSync(tsconfigPath, 'utf8')
: null
if (tsconfigContents) {
const tsconfig = JSON.parse(tsconfigContents)
// add "include" project root's configs avoid '@typescript-eslint/await-thenable's parse error https://elmah.io/tools/stack-trace-formatter/212c0a4849bc4054826e4055f5d167a7/
const configFiles = ['./**.js', './**.ts', './**.cjs', './**.mjs']
if (tsconfig.include) {
configFiles.forEach((globFilePath) => {
if (false === tsconfig.include.includes(globFilePath)) {
tsconfig.include.push(globFilePath)
}
})
} else {
// `tsconfig.json` dosn't have "include" fileld
tsconfig.include = configFiles
}
fs.writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, 2))
} else {
console.log('tsconfig.json not found in root directory')
}
}
2 changes: 1 addition & 1 deletion template/config/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @type {import('@types/eslint').Linter.Config} */
module.exports = {
root: true,
env:{},
env: {},
globals: {},
extends: ['ts-prefixer'],
parser: '@typescript-eslint/parser',
Expand Down

0 comments on commit f81c1c2

Please sign in to comment.