Last active
February 21, 2023 00:05
-
-
Save j1mmie/5c347bad361bfe071ba5f572bdef57a8 to your computer and use it in GitHub Desktop.
Automatically generate package.json exports section for TypeScript
This file contains hidden or 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
# Requires jj - https://github.com/tidwall/jj | |
# Can be modified to use jq | |
# Probably tons of unhandled edge cases | |
# | |
# Usage: | |
# In a directory that looks like this: | |
# | |
# . | |
# ├── generate-exports.sh | |
# ├── package.json | |
# └── src | |
# ├── some-folder | |
# │ └── SomeScript.ts | |
# ├── another-folder | |
# │ └── AnotherScript.ts | |
# └── LastScript.ts | |
# | |
# Execute generate-exports.sh: | |
# | |
# > sh generate-exports.sh | |
# | |
# The resulting package.json will look like this: | |
# { | |
# "exports": { | |
# ./some-folder/SomeScript": "./src/some-folder/SomeScript.js", | |
# ./another-folder/AnotherScript": "./src/another-folder/AnotherScript.js", | |
# ./LastScript": "./src/LastScript.js" | |
# } | |
# } | |
JSON={`find src -name "*.ts" | | |
sed 's/^\(src\/\)//' | | |
sed 's/\(\.ts\)$//' | | |
sed 's/.*/ ".\/&": ".\/src\/&.js"/' | | |
paste -sd "," -`} | |
jj -i package.json -o package.json -p -r -v "$JSON" exports |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment