Build a new package
So you wanna build a new package.... This guide will explain some of the key files and configs you will need to put in place for your new package. This guide uses the logger package as an example.
Overview
Directory Stucture
A package needs to live in the packages/ folder. The name of the folder should be descriptive of what that package contains and should be the plural for example:
logger- GOODutils- GOODtype-package- BADvery-specific-type- BAD
Note the casing should be kebab case. (lowercase and dash delimited)
This is an example directory structure your package should follow as well as some of the files you may require.
packages/
| - logger
| - __tests__ // Test folder for testing package code
| - lefthook.yml // git hooks (pre-commit lint, pre-push audit)
| - src/ // Holds the the package code
| - index.ts // main entry point for package
| - .eslintrc.js // linting
| - .npmrc // contains the the info needed to access the @bhag-business-platform registry
| - ci.yml // will hold the ci definition for the package
| - tsconfig.json // development config for TypeScript
| - tsconfig.build.json // build config for TypeScript
| - package.json // dependencies
| - jest.config.js // test config
| - README.md // docs about the package
Files
There are a number of config files thatr are required in ordere to successfully publish your new package. Lets take a look through each on and explain what it does and what it should contain.
lefthook.yml
This file defines the Lefthook git hooks for this package. The root lefthook.yml picks it up. yarn run lint runs on commit when files under this package's folder are staged, and yarn run audit runs on push when the push changes files in this folder. A failed audit blocks the push and shows the /fix-audit command to run. Without this file, lint and audit are only enforced in CI, not locally. root must be the package's path from the repo root, with a trailing slash, and job names must be unique in the repo.
For a package at packages/logger, it should contain the following:
pre-commit:
jobs:
- name: packages/logger lint
root: packages/logger/
run: yarn run lint
pre-push:
jobs:
- name: packages/logger audit
root: packages/logger/
run: yarn run audit
env:
AUDIT_REGISTRY_ERROR: warn
fail_text: 'Fix it before you push: claude "/fix-audit packages/logger", then commit and push again.'.eslintrc.js
This file sets up the eslint configuration for the package. It should contain the following:
const mainEslintConfig = require('../../platform/config/lint/eslint');
module.exports = {
...mainEslintConfig,
ignorePatterns: [
...mainEslintConfig.ignorePatterns,
"dist/*"
]
} Note here it extends the main eslint config to prevent repetition of config.
.npmrc
This file specifies our private NPM package registry location as well as what auth token to use.
This allows the package to install packages from our private Gitlab package registry, for example packages such as @bhag-business-platform/luna-utils
It should contain the following:
@bhag-business-platform:registry=https://gitlab.com/api/v4/projects/71835281/packages/npm/
//gitlab.com/api/v4/projects/71835281/packages/npm/:_authToken=${LUNA_PACKAGE_REGISTRY_PAT}
jest.config.js
This file specified the jest testing config. It should contain the following:
const mainJestConfig = require('../../platform/config/test/jest');
module.exports = {
...mainJestConfig,
globalSetup: "./global-setup.js"
};
Note here - this config also extends a base jest config and overrides a few values.
Package.json
This file contains where to publish your package, your scripts, dependancies and anything else your package needs to build and run. At a minimum the file must contain the following:
{
"name": "@bhag-business-platform/luna-logger",
"version": "1.9.5",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"license": "MIT",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"test": "jest --passWithNoTests",
"lint": "eslint .",
"audit": "../../platform/scripts/run-node-audit.sh",
"audit:ignore": "yarn audit --json > .yarn-audit.ignore"
},
"publishConfig": {
"registry": "https://gitlab.com/api/v4/projects/71835281/packages/npm/"
},
"dependencies": {
},
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/node": "^14.14.19",
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"eslint": "^8.13.0",
"eslint-plugin-security": "^1.5.0",
"typescript": "^4.6.3",
"jest": "^27.5.1",
"ts-jest": "^27.1.4"
}
}Note the publishConfig specifies where to publish the package. Note the name must follow the convention of @bhag-business-platform/package-name as it is scoped to the gitlab project.
Package Versioning
When first creating the package, your package should be version 1.0.0.
Subsequent updates should follow loosly the semver versioning guide
tsconfig.json
This file contains the base TypeScript config for this package. It should contain the following:
"compilerOptions": {
"target": "es2019",
"module": "commonjs",
"allowJs": true,
"checkJs": true,
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
"sourceMap": true,
"outDir": "./dist"
},
"include": ["src/**/*","__tests__/**/*"]
}
tsconfig.build.json
This file contains the TypeScript config for building the package. It extends the base but omits tests. It should contain the following:
{
"extends": "./tsconfig.json",
"exclude": ["__tests__/**/*"]
}
ci.yml
This file contains the Gitlab CI configuration for this service. It will use anchors and templates that you can use.
package-logger:install:
extends:
- .luna-node-package-install-template
variables:
PACKAGE_DIR: logger
package-logger:audit:
extends:
- .luna-node-package-audit-template
variables:
PACKAGE_DIR: logger
package-logger:build:
extends:
- .luna-node-package-build-template
variables:
PACKAGE_DIR: logger
package-logger:test:
extends:
- .luna-node-package-test-template
variables:
PACKAGE_DIR: logger
package-logger:lint:
extends:
- .luna-node-package-lint-template
variables:
PACKAGE_DIR: logger
package-logger:publish-check:
extends:
- .luna-node-package-publish-check-template
variables:
PACKAGE_DIR: logger
package-logger:publish-main:
extends:
- .luna-node-package-publish-latest-template
variables:
PACKAGE_DIR: logger
dependencies:
- package-logger:build
package-logger:publish-env:
extends:
- .luna-node-package-publish-tag-template
variables:
PACKAGE_DIR: logger
dependencies:
- package-logger:build
Note you will find the template definitions of what each job does in platform/ci/packages/anchors.yml
README.md
This file contains documentation about the package - it can use images and other files from the docs/ folder. See the ui-components package README.md for a good example of a README for a package
Vewing published packages
You can view the published packages on gitlab in the Package Registry.
You may need to use the filter option to find the specific package or version you are looking for.
You will also see tags to speicify what the latest version is for that package on an environment. The latest tag is for package that are published on the main branch. The env-xxx tag specifies the latest package version on the env-xxx environment. e.g. env-polaris These tags are usful when installing or upgrading your packages in dependencies.
Published Versions
Different versions of your package will be published depending on what branch you're pushing / merging.
For merge-request a publish check will occur. It won't actually publish the package, but will check if merging your MR into main will allow you to publish the version of your package.
For env/* branches, for example, env/polaris a auto generated version will be published. This is based on the env branch, the version in package.json as well as the pipeline id. It will appear as something like 1.0.0-env-polairs.12345. This will be a unique version for each pipeline ran. This version will also be tagged as env-polaris to represent the latest version on the polaris environment.
For main the version published will be the version in package.json. The package will also be tagged as latest to represent the latest version of the package
Installing your package
To install your package in other services / packages / clients. Please ensure you have set up your LUNA_PACKAGE_REGISTRY_PAT and you have a .npmrc file that specifies where to get the package from (our gitlab private package registry)
You can then install the package as follows:
yarn install @bhag-business-platform/luna-logger@latestThis will install the latest version of the @bhag-business-platform/luna-logger package.
Alternativley - you can install the package you published to an environment by changing the @latest tag to the latest env tag.
yarn install @bhag-business-platform/luna-logger@env-polarisUpgrading your package
If you want to upgrade your package to the latest version, run this command:
yarn upgrade @bhag-business-platform/luna-logger@latestAlternativley - you can upgrade the package you published to an environment by changing the @latest tag to the latest env tag.
Please note - for packages / services / clients - we should only use the main tags when in main. env-* tags or versions are used for testing.
