Skip to content

Instantly share code, notes, and snippets.

@book000
Last active May 6, 2024 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save book000/e895234219c6ecdbb747a46968b759ea to your computer and use it in GitHub Desktop.
Save book000/e895234219c6ecdbb747a46968b759ea to your computer and use it in GitHub Desktop.
# package.jsonが無ければエラーを表示して終了する
if (-not (Test-Path package.json)) {
Write-Host "Error: package.json not found." -ForegroundColor Red
exit 1
}
# package.jsonのeslintがv9以上だったら、v9では動作しないことを表示して終了する
$package_json = Get-Content -Path package.json -Raw | ConvertFrom-Json -AsHashtable
$eslint_version = $package_json.devDependencies.eslint
if ($eslint_version -ge "9.0.0") {
Write-Host "Error: This script eslint v9 or higher is not supported." -ForegroundColor Red
exit 1
}
# Remove old eslint files
Remove-Item .eslintignore
Remove-Item .eslintrc.yml
# get package manager (yarn or pnpm)
# yarn: yarn.lockの存在確認
# pnpm: pnpm-lock.yamlの存在確認
if (Test-Path yarn.lock) {
$package_manager = "yarn"
}
elseif (Test-Path pnpm-lock.yaml) {
$package_manager = "pnpm"
}
elseif (Test-Path package-lock.json) {
$package_manager = "npm"
}
else {
Write-Host "Error: package manager not found." -ForegroundColor Red
exit 1
}
Write-Host "Selected package manager: $package_manager"
# Remove unused packages
Write-Host "Remove unused packages..."
$remove_packages = @("@typescript-eslint/eslint-plugin", "@typescript-eslint/parser", "eslint", "eslint-config-prettier", "eslint-config-standard", "eslint-plugin-import", "eslint-plugin-n", "eslint-plugin-node", "eslint-plugin-promise", "eslint-plugin-unicorn")
foreach ($package in $remove_packages) {
if ($package_manager -eq "yarn") {
yarn remove $package
}
elseif ($package_manager -eq "pnpm") {
pnpm remove $package
}
else {
npm uninstall $package
}
}
# Install eslint and dependencies
Write-Host "Install eslint and dependencies..."
$install_packages = @("eslint", "eslint-config-standard", "eslint-plugin-import", "eslint-plugin-n", "eslint-plugin-promise", "@book000/eslint-config")
foreach ($package in $install_packages) {
if ($package_manager -eq "yarn") {
yarn add -D -E $package
}
elseif ($package_manager -eq "pnpm") {
pnpm add -D -E $package
}
else {
npm install -D -E $package
}
}
# Create eslint.config.mjs
Write-Host "Create eslint.config.mjs..."
"export { default } from '@book000/eslint-config';" | Out-File -FilePath eslint.config.mjs -Encoding utf8
# Update scripts (lint:eslint, fix:eslint)
Write-Host "Update package.json scripts..."
$package_json = Get-Content -Path package.json -Raw | ConvertFrom-Json -AsHashtable
$package_json.scripts."lint:eslint" = "eslint . -c eslint.config.mjs"
$package_json.scripts."fix:eslint" = "eslint . -c eslint.config.mjs --fix"
$package_json | ConvertTo-Json -Depth 100 | Out-File -FilePath package.json -Encoding utf8
# Add ignore eslint-config-standard to .depcheckrc.json (if exists)
if (Test-Path .depcheckrc.json) {
Write-Host "Add ignore eslint-config-standard to .depcheckrc.json..."
$depcheckrc = Get-Content -Path .depcheckrc.json -Raw | ConvertFrom-Json -AsHashtable
$ignore_packages = @("eslint-config-standard", "eslint-plugin-import", "eslint-plugin-n", "eslint-plugin-promise")
$depcheckrc.ignores = $depcheckrc.ignores | Where-Object { $_ -notin $ignore_packages }
$depcheckrc.ignores += $ignore_packages
$depcheckrc | ConvertTo-Json -Depth 100 | Out-File -FilePath .depcheckrc.json -Encoding utf8
}
else {
Write-Host "Create .depcheckrc.json..."
$depcheckrc = @{
ignores = @("eslint-config-standard", "eslint-plugin-import", "eslint-plugin-n", "eslint-plugin-promise")
}
$depcheckrc | ConvertTo-Json -Depth 100 | Out-File -FilePath .depcheckrc.json -Encoding utf8
}
# Upgrade interactive
Write-Host "Upgrade interactive..."
if ($package_manager -eq "yarn") {
yarn upgrade-interactive --latest
}
elseif ($package_manager -eq "pnpm") {
pnpm upgrade --interactive --latest
}
else {
npx npm-check --update
}
# Recreate lock file
Write-Host "Recreate lock file..."
if ($package_manager -eq "yarn") {
Remove-Item yarn.lock
yarn install
}
elseif ($package_manager -eq "pnpm") {
Remove-Item pnpm-lock.yaml
pnpm install
}
else {
Remove-Item package-lock.json
npm install
}
# Run eslint (auto fix)
Write-Host "Run eslint (auto fix)..."
if ($package_manager -eq "yarn") {
yarn run fix
}
elseif ($package_manager -eq "pnpm") {
pnpm run fix
}
else {
npm run fix
}
# Run fixpack
Write-Host "Run fixpack..."
npx fixpack
# Run depcheck
Write-Host "Run depcheck..."
npx depcheck
# Done
Write-Host "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment