Skip to content

Instantly share code, notes, and snippets.

@j1mmie
Last active February 21, 2023 00:05
Show Gist options
  • Save j1mmie/5c347bad361bfe071ba5f572bdef57a8 to your computer and use it in GitHub Desktop.
Save j1mmie/5c347bad361bfe071ba5f572bdef57a8 to your computer and use it in GitHub Desktop.
Automatically generate package.json exports section for TypeScript
# 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