Last active
September 28, 2022 12:10
-
-
Save wojtekmaj/af1dd2d78075077d2110d0a8871d7fa2 to your computer and use it in GitHub Desktop.
Convert package to TypeScript automatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create tsconfig.json | |
echo "{ | |
\"compilerOptions\": { | |
\"declaration\": true, | |
\"esModuleInterop\": true, | |
\"isolatedModules\": true, | |
\"moduleResolution\": \"node\", | |
\"outDir\": \"dist\", | |
\"strict\": true | |
}, | |
\"include\": [\"src\"], | |
\"exclude\": [\"**/*.spec.ts\"] | |
}" > tsconfig.json | |
# Replace .babelrc with barebones config | |
echo "{ | |
\"presets\": [\"@babel/typescript\", \"@babel/env\"] | |
}" > .babelrc | |
# Remove manually created index.d.ts, if any | |
rm -rf index.d.ts | |
yarn dlx json -I -f package.json -e "this.files=this.files.filter(x=>x!=='index.d.ts')" | |
# Rename all js files in src directory to ts | |
git mv src/index.js src/index.ts | |
git mv src/index.spec.js src/index.spec.ts | |
# Modify package.json | |
npm pkg set 'source'='src/index.ts' | |
npm pkg set 'types'='src/index.ts' | |
npm pkg set 'scripts.build-esm'='tsc --outDir dist/esm --module esnext' | |
npm pkg set 'scripts.build-cjs'='tsc --outDir dist/cjs --module commonjs' | |
npm pkg set 'scripts.lint'='eslint src --ext .js,.ts' | |
npm pkg set 'scripts.tsc'='tsc --noEmit' | |
prev=$(npm pkg get scripts.test) | |
next=${prev/yarn lint &&/yarn lint && yarn tsc &&} | |
next=$(echo $next | sed 's/"//g') | |
npm pkg set 'scripts.test'=$next | |
# Remove and install dependencies | |
yarn remove @babel/cli | |
yarn add @babel/preset-typescript @types/jest @typescript-eslint/parser typescript --dev | |
yarn dedupe | |
# Configure ESLint to use @typescript-eslint/parser | |
yarn dlx json -I -f .eslintrc.json -e "this.parser=\"@typescript-eslint/parser\"" | |
# MANUAL STEPS: | |
# - Reorder new attributes in package.json manually |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment