From a8268c680abc9f1f25222c516f9b5efc803c4f81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Seku=C5=82a?= Date: Thu, 26 Jan 2023 08:53:53 +0100 Subject: [PATCH] [AAE-11496] Publish libraries to npm (#2880) * [AAE-11496] Publish libraries to npm registry * update * update * added md * deprecate old version --- .travis.yml | 9 ++++ angular.json | 3 +- docs/extending/publish-to-npm.md | 37 ++++++++++++++ package.json | 1 + scripts/ci/npm/publish-libs.sh | 83 ++++++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 docs/extending/publish-to-npm.md create mode 100755 scripts/ci/npm/publish-libs.sh diff --git a/.travis.yml b/.travis.yml index 46c685906..a6099b881 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,6 +35,8 @@ stages: if: branch = master AND type = push - name: e2e if: type = cron || type = pull_request + - name: Release Libraries + if: type = push - name: Trigger DW if: branch = develop AND (type = cron OR (type = api AND commit_message =~ /\[trigger aca\]/)) @@ -190,6 +192,13 @@ jobs: name: Release Tag script: ./scripts/travis/release/git-tag.sh + - stage: Release Libraries + name: Release Libraries + script: + - npm ci + - ./scripts/ci/npm/publish-libs.sh + cache: false + - name: Publish to Dockerhub script: ./scripts/travis/deploy/publish.sh "content-ce" "$DOCKER_HUB_REPOSITORY_DOMAIN" "$DOCKER_HUB_USERNAME" "$DOCKER_HUB_PASSWORD" diff --git a/angular.json b/angular.json index e0b14dd72..cc8383985 100644 --- a/angular.json +++ b/angular.json @@ -430,7 +430,8 @@ "production": { "tsConfig": "projects/aca-shared/tsconfig.lib.prod.json" } - } + }, + "defaultConfiguration": "production" }, "test": { "builder": "@angular-devkit/build-angular:karma", diff --git a/docs/extending/publish-to-npm.md b/docs/extending/publish-to-npm.md new file mode 100644 index 000000000..d61ebff8c --- /dev/null +++ b/docs/extending/publish-to-npm.md @@ -0,0 +1,37 @@ +--- +Title: Publish to npm +--- + +# Create a new library + +New library can be created using standard `ng` command + +e.g. if we would like to create `aca-new-lib` we need to run the following: + +``` +ng generate @schematics/angular:library aca-new-lib +``` + +# Publish library + +## Build library +In order to publish new library, we need to build it first. You need to add build of your library to `build-libs` command in `package.json` + +``` +"build-libs": "ng build aca-shared && ng build aca-new-lib", +``` + +## Update publish script +If we would like to publish the new library we need to update `scripts/ci/npm/publish-libs.sh` + +The only thing that need to be done is to update `PROJECTS` variable by adding your library name i.e. + +``` +PROJECTS=( + 'aca-shared', + 'aca-new-lib' +); +``` +# Caveats +- The versions of libraries are updated automatically by the script (should not be done manually) +- In travis setting you can find `PUBLISH_PROJECTS` variable, which can be used for enabling dry mode when running the publish command diff --git a/package.json b/package.json index 310bb2b24..0c0241101 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "prebuild": "mkdir -p ./app/.tmp && cp ./app/src/app.config.json.tpl ./app/.tmp/app.config.json", "build": "ng build content-ce", "build.release": "npm run build -- --configuration=production,release", + "build-libs": "ng build aca-shared", "test": "ng test", "test:ci": "ng test adf-office-services-ext && ng test content-ce --code-coverage", "lint": "NODE_OPTIONS=--max_old_space_size=4096 ng lint && npm run spellcheck && npm run e2e.typecheck", diff --git a/scripts/ci/npm/publish-libs.sh b/scripts/ci/npm/publish-libs.sh new file mode 100755 index 000000000..256bc9c20 --- /dev/null +++ b/scripts/ci/npm/publish-libs.sh @@ -0,0 +1,83 @@ +#!/usr/bin/env bash + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +ROOT_DIR=${DIR}/../../.. +DIST_DIR=${ROOT_DIR}/dist/@alfresco +PROJECTS_DIR=${ROOT_DIR}/projects + +DRY_RUN="" +if [ "$PUBLISH_PROJECTS" = "false" ]; then + DRY_RUN=true + echo "PUBLISH_PROJECTS is set to false, run in dry mode" +fi + +VERSION_IN_PACKAGE_JSON=`node -p "require('$ROOT_DIR/package.json')".version;`; + +if [[ $TRAVIS_BRANCH =~ ^master.*?$ ]] ; then + NEW_LIBRARY_VERSION=VERSION_IN_PACKAGE_JSON +else + NEW_LIBRARY_VERSION="${VERSION_IN_PACKAGE_JSON}-${TRAVIS_BUILD_NUMBER}" +fi + +if [[ $TRAVIS_BRANCH =~ ^master(-patch.*)?$ ]] +then + # Pre-release versions + if [[ $VERSION_IN_PACKAGE_JSON =~ ^[0-9]*\.[0-9]*\.[0-9]*-A\.[0-9]*$ ]]; + then + TAG_NPM=next + # Stable major versions + else + TAG_NPM=latest + fi +fi + +if [[ $TRAVIS_BRANCH =~ ^develop(-patch.*)?$ ]] +then + TAG_NPM=alpha +fi + +echo -e "Branch is '$TRAVIS_BRANCH', therefore publish with '$TAG_NPM' tag\n" + +PROJECTS=( + 'aca-shared' +); + +for PROJECT in "${PROJECTS[@]}" +do + echo "Update ${PROJECT} version to ${NEW_LIBRARY_VERSION}" + + cd $PROJECTS_DIR/${PROJECT} + npm version ${NEW_LIBRARY_VERSION}; +done + +echo -e "\n\nBuild projects" +cd ${ROOT_DIR} + +npm run build-libs + +for PROJECT in "${PROJECTS[@]}" +do + cd $DIST_DIR/${PROJECT} + PREVIOUS_LIBRARY_VERSION=$(npm view @alfresco/${PROJECT} version) + + if [ "$DRY_RUN" = "true" ] ; then + echo -e "Publish with dry mode for project: $PROJECT\n" + echo -e "npm publish --dry-run --tag $TAG_NPM \n" + + npm publish --dry-run --tag $TAG_NPM + + echo -e "Deprecating old version of @alfresco/$PROJECT the old version is $PREVIOUS_LIBRARY_VERSION\n" + + echo "npm deprecate @alfresco/$PROJECT@$PREVIOUS_LIBRARY_VERSION 'Upgrade to @latest or $NEW_LIBRARY_VERSION'" + else + echo -e "Publish $PROJECT\n" + echo -e "npm publish --tag $TAG_NPM\n" + + echo 'strict-ssl=false' >> .npmrc + echo 'registry=http://${NPM_REGISTRY_ADDRESS}' >> .npmrc + echo '//${NPM_REGISTRY_ADDRESS}/:_authToken="${NPM_REGISTRY_TOKEN}"' >> .npmrc + + npm publish --tag $TAG_NPM + npm deprecate "@alfresco/$PROJECT@$PREVIOUS_LIBRARY_VERSION" "Upgrade to @latest or $NEW_LIBRARY_VERSION" + fi +done