Skip to content
Yisroel Yakovson edited this page Jan 13, 2021 · 4 revisions

Working with ts-packrat

Here are steps that will work to create an npm package. Call it $PACKAGE.

Create the Package

  1. check that $PACKAGE isn't taken yet on npmjs.
  2. create a general directory for your $PACKAGE code. e.g. ~/packages/$PACKAGE-all. Then create a global variable $CODE just to follow the instructions below: CODE=~/packages/$PACKAGE-all/$PACKAGE.
  3. generate the package: npm js-packrat $CODE

Add Code

  1. install geenee: npm i -g yalc geenee
  2. set up your groupings and/or types to export: geenee settings $CODE
  3. create (in src/custom or in the helper section for your grouping files) any code needed
  4. make sure that you can build your lib correctly: npm run build.

Copying over Code

  1. if you are copying code from somewhere else, put it into src/custom.
  2. Try depcheck to see whether the copied code results in your needing to install packages.
  3. remember to run install if you manually insert any dependencies intoyour package json: cd $CODE && npm i

Running

One difference between a package and front end code or a cli is that you need a consumer of the package to run it. If you like, you can use yalc as a replacement for the npm symlinks proposed below.

  1. make sure your code compiles: cd $CODE && npm run build.
  2. link locally: cd $CODE && npm link
  3. set up a separate testing app $TEST that includes $PACKAGE. That could be as simple as:
    1. create a directory $TEST
    2. create a package.json file: cd $TEST && npm init -y
    3. link to $PACKAGE: cd $TEST && npm link $PACKAGE
    4. create a file $TEST/index.ts that imports a grouping or type to test and does something that gets printed with console.log
    5. run tsc $TEST/index.ts to confirm that it's working

Publishing

  1. create the GitHub repo <yourUserName>/$PACKAGE
  2. modify your README.md file to explain usage
  3. commit to git, push a branch, merge to main (whatever process you prefer with CI etc.)
  4. checkout main branch, pull and then publish: cd $CODE && git checkout main && git pull && npm publish
Clone this wiki locally