Skip to content

Commit

Permalink
fix: rename constants, small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienMartel committed Jul 31, 2023
1 parent eaed0ac commit c0c26ed
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 47 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ module.exports = {

## Supported Environments

- [node]("./node.js")
- [next]("./next.js")
- svelte _(coming soon)_
- [Node.js]("./node.js")
- [Next.js]("./next.js")
- Svelte _(coming soon)_

## TODO

Expand Down
1 change: 0 additions & 1 deletion next.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module.exports = {
extends: [
'next/core-web-vitals',
'airbnb',
// "airbnb/hooks", // conflicts with next/core-web-vitals
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'eslint-config-prettier'
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"postinstall": "pnpm build",
"build": "tsc",
"prepublish": "pnpm build && pnpm release",
"release": "standard-version"
"release": "standard-version",
"prepare": "husky install"
},
"main": "./dist/index.js",
"bin": "./dist/index.js",
Expand Down
20 changes: 10 additions & 10 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
export const PACKAGE_NAME = "@monogram/eslint-config"
export const PACKAGE_NAME = '@monogram/eslint-config'

export const ESLINT_FILENAME = '.eslintrc.js'
export const ESLINT_FILENAME = '.eslintrc.js'

export const availableConfigs = ["next", "node"] as const
export type AvailableConfig = typeof availableConfigs[number]
export const AVAILABLE_CONFIGS = ['next', 'node'] as const
export type AvailableConfig = (typeof AVAILABLE_CONFIGS)[number]

export const packageManagers = ['yarn', 'pnpm', 'npm'] as const
export type PackageManager = typeof packageManagers[number]
export const PACKAGE_MANAGERS = ['yarn', 'pnpm', 'npm'] as const
export type PackageManager = (typeof PACKAGE_MANAGERS)[number]

type PackageManagerMap = Record<PackageManager, string>
type PackageManagerRecord = Record<PackageManager, string>

export const lockFiles: PackageManagerMap = {
export const LOCK_FILES: PackageManagerRecord = {
yarn: `${process.cwd()}/yarn.lock`,
pnpm: `${process.cwd()}/pnpm-lock.yaml`,
npm: `${process.cwd()}/package-lock.json`
} as const

export const installPrefixes: PackageManagerMap = {
export const INSTALL_PREFIXES: PackageManagerRecord = {
yarn: 'yarn add -D',
pnpm: 'pnpm add -D',
npm: 'npm i -D'
} as const
} as const
44 changes: 26 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
#!/usr/bin/env node
import { existsSync, writeFileSync } from 'node:fs'
import { execSync } from 'node:child_process'
import { select, confirm } from '@inquirer/prompts';
import { AvailableConfig, ESLINT_FILENAME, PACKAGE_NAME, PackageManager, availableConfigs, installPrefixes } from './constants';
import choosePackageManager from './package-manager';
import { select, confirm } from '@inquirer/prompts'
import {
AvailableConfig,
ESLINT_FILENAME,
PACKAGE_NAME,
PackageManager,
AVAILABLE_CONFIGS,
INSTALL_PREFIXES
} from './constants'
import choosePackageManager from './package-manager'

if (existsSync(`${process.cwd()}/${ESLINT_FILENAME}`)) {
confirm({ message: `Do you want to replace the current ${ESLINT_FILENAME} file?` })
.then((yes) => yes && handleCreate())
confirm({ message: `Do you want to replace the current ${ESLINT_FILENAME} file?` }).then(
(yes) => yes && handleCreate()
)
} else {
handleCreate()
}
Expand All @@ -23,36 +31,36 @@ async function handleCreate() {
async function chooseConfig() {
return select({
message: 'Which ESLint configuration do you want to install?',
choices: availableConfigs.map((config) => ({
choices: AVAILABLE_CONFIGS.map((config) => ({
value: config
})),
});
}))
})
}

async function installDependency(packageManager: PackageManager) {
const installPrefix = installPrefixes[packageManager]
const installPrefix = INSTALL_PREFIXES[packageManager]

const installCommand = `${installPrefix} eslint ${PACKAGE_NAME}`

console.log(`📦 Installing dependencies...`)

try {
execSync(installCommand)
} catch (error) {
console.error(error)
}
try {
execSync(installCommand)
} catch (error) {
console.error(error)
}
}

function createESLintConfig(whichConfig: AvailableConfig) {
const dataAsString = JSON.stringify(
{
extends: `${PACKAGE_NAME}/${whichConfig}`
},
extends: `${PACKAGE_NAME}/${whichConfig}`
},
null,
2
2
)

writeFileSync(`${process.cwd()}/${ESLINT_FILENAME}`, `module.exports = ${dataAsString}`)

console.log(`✅ ESLint configuration file created at ${process.cwd()}/${ESLINT_FILENAME}`)
}
}
31 changes: 17 additions & 14 deletions src/package-manager.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
import { existsSync } from "node:fs"
import { select, confirm } from "@inquirer/prompts"
import { PackageManager, lockFiles, packageManagers } from "./constants"
import { existsSync } from 'node:fs'
import { select, confirm } from '@inquirer/prompts'
import { PackageManager, LOCK_FILES, PACKAGE_MANAGERS } from './constants'

export default async function choosePackageManager(): Promise<PackageManager> {
const pm = findPackageManager()
const packageManager = findPackageManager()

if (pm === undefined) {
if (packageManager === undefined) {
return selectPackageManager()
}

const usePm = await confirm({ message: `Auto-detected package manager. Use ${pm} for installation?`, default: true })
const useAutoDetected = await confirm({
message: `Auto-detected package manager. Use ${packageManager} for installation?`,
default: true
})

if (usePm === false) {
if (useAutoDetected === false) {
return selectPackageManager()
}

return pm
return packageManager
}

function findPackageManager(): PackageManager | undefined {
return packageManagers.find((pm) => existsSync(lockFiles[pm]))
return PACKAGE_MANAGERS.find((packageManager) => existsSync(LOCK_FILES[packageManager]))
}

function selectPackageManager() {
return select({
message: 'Select a package manager you would like to use for the install',
choices: packageManagers.map((pm) => ({
value: pm
})),
});
}
choices: PACKAGE_MANAGERS.map((packageManager) => ({
value: packageManager
}))
})
}

0 comments on commit c0c26ed

Please sign in to comment.