Ready To Go — publishing NPM packages the Right Way

Or what if the files property in your package.json file fails to include files you need?You need to test your preinstall/postinstall routines in a way that represents the reality of someone using your software.A lot of developers accidentally include extraneous files in their published packages, probably because they are too scared of modifying `.npmignore`..This means node_modules is heftier than it needs to be, full of packages with extraneous files.r2g solves the above problems, and will remove some of the headaches associated with publishing production-grade packages.A new workflowHere is what a better publishing workflow looks like$ npm test # run regular tests$ r2g test # we add this step$ npm publish If you are currently using np from Sindresorhus for NPM publishing, you can just add r2g test to the test routine, something like this:"scripts": { "test": "run regular tests && r2g test"}Here is the general solution that r2g usesThe solution does not replace your current CI/CD operation..It supplements (not supplants!) your current publishing routine with much needed behavior.Create a temp folder $HOME/.r2g/temp/projectCopy a blank package.json file to that folderTarball your package using npm pack and then install the tarball as a dependency of project, using npm i –production /path/to/your/package.tgzNow your package is installed here: $HOME/.r2g/temp/project/node_modules/xSince you don’t have your devDependencies, it’s unlikely that you can run your regular test suite — good — your regular test suite is no good anyway..You will create smoke-tests in a folder called .r2g in your package, your smoke tests are located here:packagex/ .r2g/ fixtures/ test-data.json tests/ smoke.test.1.js smoke.test.2.jsand when you end up testing your package with .r2g, these files get copied to here:$HOME/.r2g/temp/project node_modules/ x/ fixtures/ test-data.json tests/ smoke.test.1.js smoke.test.2.jsSo what we are doing is copying your files in the .r2g folder, to the main projects workspace..So your write your .r2g tests as if some other package was testing your package..Wowza!.All the files in tests are executed directly, and if they need to load non-executable files, they load files from the adjacent fixtures folder..Magisterial.Along with created tests in the .r2g folder, you can also smoke-test your package with a function exported from your main:exports.r2gSmokeTest = async () => { return true;} What if I want to re-use the tests that are in my regular test suite for my package?You can do that, and it’s done in a different phase in r2g’s cycle..This part is cool though..It’s actually the first phase, called phase S, for this phase, we rsync (copy) your project to a temp location, we npm packit, and we symlink the project to itself and so the project requires itself..The only change you need to make is require('x') your package with a non-relative path, instead of using require('../../../x-main.js').Your package is never touched!Lol, we never do anything in your current workspaces, we only rsync it to temporary folders..So you will never see random files pop up in your workspace that weren’t there before.. More details

Leave a Reply