diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c8ec607..0bb1ca9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -17,7 +17,7 @@ build: - node bin/nxapi.js util validate-discord-titles - export VERSION=`[ "$CI_COMMIT_BRANCH" != "main" ] || node resources/build/ci-main-version.js` - - "node -e \"fs.writeFileSync('package.json', JSON.stringify((json => {json.version = process.env.VERSION || json.version; json.__nxapi_release = process.env.CI_COMMIT_TAG; return json;})(JSON.parse(fs.readFileSync('package.json', 'utf-8'))), null, 4) + '\\n', 'utf-8')\"" + - node resources/build/ci-package-json.js - PACKAGE=`npm --color="always" pack` - mv "$PACKAGE" nxapi.tgz artifacts: @@ -34,7 +34,7 @@ build-docker: - | [ "$GH_REGISTRY_IMAGE" != "" ] && docker login -u "$GH_REGISTRY_USER" -p "$GH_REGISTRY_PASSWORD" "$GH_REGISTRY" script: - - "node -e \"fs.writeFileSync('package.json', JSON.stringify((json => {json.__nxapi_release = process.env.CI_COMMIT_TAG; return json;})(JSON.parse(fs.readFileSync('package.json', 'utf-8'))), null, 4) + '\\n', 'utf-8')\"" + - node resources/build/ci-package-json.js - docker build --pull --no-cache --tag "$CI_REGISTRY_IMAGE:ref-$CI_COMMIT_REF_SLUG" . - | @@ -88,7 +88,7 @@ build-docker: build-app: stage: build script: - - "node -e \"fs.writeFileSync('package.json', JSON.stringify((json => {json.__nxapi_release = process.env.CI_COMMIT_TAG; return json;})(JSON.parse(fs.readFileSync('package.json', 'utf-8'))), null, 4) + '\\n', 'utf-8')\"" + - node resources/build/ci-package-json.js - npx electron-builder build --macos zip:x64 zip:arm64 --linux deb snap appimage --publish never - mv dist/app/package app @@ -113,7 +113,7 @@ build-windows: stage: build image: electronuserland/builder:wine script: - - "node -e \"fs.writeFileSync('package.json', JSON.stringify((json => {json.__nxapi_release = process.env.CI_COMMIT_TAG; return json;})(JSON.parse(fs.readFileSync('package.json', 'utf-8'))), null, 4) + '\\n', 'utf-8')\"" + - node resources/build/ci-package-json.js - npx electron-builder build --win nsis --publish never - mv dist/app/package app @@ -135,7 +135,7 @@ build-windows: publish-npm: stage: deploy script: - - "node -e \"fs.writeFileSync('package.json', JSON.stringify((json => {json.__nxapi_release = process.env.CI_COMMIT_TAG; return json;})(JSON.parse(fs.readFileSync('package.json', 'utf-8'))), null, 4) + '\\n', 'utf-8')\"" + - node resources/build/ci-package-json.js - echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc - npm --color="always" publish @@ -157,8 +157,7 @@ publish-gitlab: stage: deploy script: # Update the package.json and package-lock.json - - "node -e \"fs.writeFileSync('package.json', JSON.stringify((json => {json.name = process.env.GITLAB_NPM_PACKAGE_NAME; json.publishConfig = {access: 'public'}; json.__nxapi_release = process.env.CI_COMMIT_TAG; return json;})(JSON.parse(fs.readFileSync('package.json', 'utf-8'))), null, 4) + '\\n', 'utf-8')\"" - - "node -e \"fs.writeFileSync('package-lock.json', JSON.stringify((json => {json.name = process.env.GITLAB_NPM_PACKAGE_NAME; return json;})(JSON.parse(fs.readFileSync('package-lock.json', 'utf-8'))), null, 4) + '\\n', 'utf-8')\"" + - node resources/build/ci-package-json.js gitlab - echo "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}" | sed s/^https:// > .npmrc - npm --color="always" --registry=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/ publish @@ -181,8 +180,7 @@ publish-github: stage: deploy script: # Update the package.json and package-lock.json - - "node -e \"fs.writeFileSync('package.json', JSON.stringify((json => {json.name = process.env.GITHUB_NPM_PACKAGE_NAME; json.publishConfig = {access: 'public'}; json.repository = {type: 'git', url: 'https://github.com/' + process.env.GITHUB_REPOSITORY + '.git'}; json.__nxapi_release = process.env.CI_COMMIT_TAG; return json;})(JSON.parse(fs.readFileSync('package.json', 'utf-8'))), null, 4) + '\\n', 'utf-8')\"" - - "node -e \"fs.writeFileSync('package-lock.json', JSON.stringify((json => {json.name = process.env.GITHUB_NPM_PACKAGE_NAME; return json;})(JSON.parse(fs.readFileSync('package-lock.json', 'utf-8'))), null, 4) + '\\n', 'utf-8')\"" + - node resources/build/ci-package-json.js github - echo "//npm.pkg.github.com/:_authToken=${GITHUB_NPM_TOKEN}" > .npmrc - npm --color="always" --registry=https://npm.pkg.github.com/ publish @@ -206,7 +204,7 @@ publish-next: stage: deploy script: - export VERSION=`[ "$CI_COMMIT_BRANCH" != "main" ] || node resources/build/ci-main-version.js` - - "node -e \"fs.writeFileSync('package.json', JSON.stringify((json => {json.version = process.env.VERSION || json.version; json.__nxapi_release = process.env.CI_COMMIT_TAG; return json;})(JSON.parse(fs.readFileSync('package.json', 'utf-8'))), null, 4) + '\\n', 'utf-8')\"" + - node resources/build/ci-package-json.js - | if [ "$CI_COMMIT_BRANCH" = "main" -a "$VERSION" != "" ]; then @@ -219,8 +217,7 @@ publish-next: if [ "$GITLAB_NPM_PUBLISH_NEXT" = "true" -a "$GITLAB_NPM_PACKAGE_NAME" != "" ]; then echo "Publishing $VERSION to GitLab" - node -e "fs.writeFileSync('package.json', JSON.stringify((json => {json.name = process.env.GITLAB_NPM_PACKAGE_NAME; json.publishConfig = {access: 'public'}; return json;})(JSON.parse(fs.readFileSync('package.json', 'utf-8'))), null, 4) + '\n', 'utf-8')" - node -e "fs.writeFileSync('package-lock.json', JSON.stringify((json => {json.name = process.env.GITLAB_NPM_PACKAGE_NAME; return json;})(JSON.parse(fs.readFileSync('package-lock.json', 'utf-8'))), null, 4) + '\n', 'utf-8')" + node resources/build/ci-package-json.js gitlab echo "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}" | sed s/^https:// > .npmrc npm --color="always" --registry=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/ publish --tag next @@ -228,8 +225,7 @@ publish-next: if [ "$GITHUB_NPM_PUBLISH_NEXT" = "true" -a "$GITHUB_NPM_PACKAGE_NAME" != "" -a "$GITHUB_REPOSITORY" != "" -a "$GITHUB_NPM_TOKEN" != "" ]; then echo "Publishing $VERSION to GitHub" - node -e "fs.writeFileSync('package.json', JSON.stringify((json => {json.name = process.env.GITHUB_NPM_PACKAGE_NAME; json.publishConfig = {access: 'public'}; json.repository = {type: 'git', url: 'https://github.com/' + process.env.GITHUB_REPOSITORY + '.git'}; return json;})(JSON.parse(fs.readFileSync('package.json', 'utf-8'))), null, 4) + '\n', 'utf-8')" - node -e "fs.writeFileSync('package-lock.json', JSON.stringify((json => {json.name = process.env.GITHUB_NPM_PACKAGE_NAME; return json;})(JSON.parse(fs.readFileSync('package-lock.json', 'utf-8'))), null, 4) + '\n', 'utf-8')" + node resources/build/ci-package-json.js github echo "//npm.pkg.github.com/:_authToken=${GITHUB_NPM_TOKEN}" > .npmrc npm --color="always" --registry=https://npm.pkg.github.com/ publish --tag next diff --git a/resources/build/ci-main-version.js b/resources/build/ci-main-version.js index 2de861c..3306987 100644 --- a/resources/build/ci-main-version.js +++ b/resources/build/ci-main-version.js @@ -20,7 +20,7 @@ const [revision, branch_str, changed_files_str, tags_str, commit_count_str] = aw const branch = branch_str && branch_str !== 'HEAD' ? branch_str : null; const changed_files = changed_files_str.length ? changed_files_str.split('\n') : []; -const tags = tags_str.split('\n').filter(t => t.startsWith('tag: ')).map(t => t.substr(5)); +const tags = tags_str.split(/\n|, /).filter(t => t.startsWith('tag: ')).map(t => t.substr(5)); const last_tagged_version = tags.find(t => t.startsWith('v'))?.substr(1) ?? null; const last_version = last_tagged_version ?? pkg.version; const commit_count = parseInt(commit_count_str); diff --git a/resources/build/ci-package-json.js b/resources/build/ci-package-json.js new file mode 100644 index 0000000..00a54c0 --- /dev/null +++ b/resources/build/ci-package-json.js @@ -0,0 +1,41 @@ +import * as fs from 'node:fs/promises'; +import * as child_process from 'node:child_process'; +import * as util from 'node:util'; +import { fileURLToPath } from 'node:url'; + +const execFile = util.promisify(child_process.execFile); +const options = {cwd: fileURLToPath(new URL('../..', import.meta.url))}; +const git = (...args) => execFile('git', args, options).then(({stdout}) => stdout.toString().trim()); + +const pkg = JSON.parse(await fs.readFile(new URL('../../package.json', import.meta.url), 'utf-8')); + +const [revision, branch, changed_files] = await Promise.all([ + git('rev-parse', 'HEAD'), + git('rev-parse', '--abbrev-ref', 'HEAD'), + git('diff', '--name-only', 'HEAD'), +]); + +if (process.argv[2] === 'gitlab') { + pkg.name = process.env.GITLAB_NPM_PACKAGE_NAME ?? pkg.name; + pkg.publishConfig = {access: 'public'}; +} + +if (process.argv[2] === 'github') { + pkg.name = process.env.GITHUB_NPM_PACKAGE_NAME ?? pkg.name; + pkg.repository = { + type: 'git', + url: 'https://github.com/' + process.env.GITHUB_REPOSITORY + '.git', + }; + pkg.publishConfig = {access: 'public'}; +} + +pkg.version = process.env.VERSION || pkg.version; +pkg.__nxapi_release = process.env.CI_COMMIT_TAG; + +pkg.__nxapi_git = pkg.__nxapi_git ?? { + revision, + branch: branch && branch !== 'HEAD' ? branch : null, + changed_files: changed_files.length ? changed_files.split('\n') : [], +}; + +await fs.writeFile(new URL('../../package.json', import.meta.url), JSON.stringify(pkg, null, 4) + '\n', 'utf-8'); diff --git a/src/util/product.ts b/src/util/product.ts index 06f7146..f07329c 100644 --- a/src/util/product.ts +++ b/src/util/product.ts @@ -2,12 +2,9 @@ import process from 'node:process'; import * as path from 'node:path'; import { fileURLToPath } from 'node:url'; import * as fs from 'node:fs/promises'; -import * as child_process from 'node:child_process'; import * as util from 'node:util'; import createDebug from 'debug'; -const execFile = util.promisify(child_process.execFile); - const debug = createDebug('nxapi:util:product'); // @@ -42,9 +39,7 @@ const match = pkg.version.match(/^(\d+\.\d+\.\d+)-next\b/i); export const version: string = match?.[1] ?? pkg.version; export const release: string | null = embedded_release ?? pkg.__nxapi_release ?? null; -const execGit = (...args: string[]) => execFile('git', args, {cwd: dir}).then(({stdout}) => stdout.toString().trim()); - -export const git = typeof embedded_git !== 'undefined' ? embedded_git : await (async () => { +export const git = typeof embedded_git !== 'undefined' ? embedded_git : pkg.__nxapi_git ?? await (async () => { try { await fs.stat(path.join(dir, '.git')); } catch (err) { @@ -52,10 +47,14 @@ export const git = typeof embedded_git !== 'undefined' ? embedded_git : await (a return null; } + const child_process = await import('node:child_process'); + const execFile = util.promisify(child_process.execFile); + const git = (...args: string[]) => execFile('git', args, {cwd: dir}).then(({stdout}) => stdout.toString().trim()); + const [revision, branch, changed_files] = await Promise.all([ - execGit('rev-parse', 'HEAD'), - execGit('rev-parse', '--abbrev-ref', 'HEAD'), - execGit('diff', '--name-only', 'HEAD'), + git('rev-parse', 'HEAD'), + git('rev-parse', '--abbrev-ref', 'HEAD'), + git('diff', '--name-only', 'HEAD'), ]); return { @@ -66,7 +65,7 @@ export const git = typeof embedded_git !== 'undefined' ? embedded_git : await (a })(); export const dev = process.env.NODE_ENV !== 'production' && - (!!git || process.env.NODE_ENV === 'development'); + (!release || process.env.NODE_ENV === 'development'); export const product = 'nxapi ' + version + (!release && git ? '-' + git.revision.substr(0, 7) + (git.branch ? ' (' + git.branch + ')' : '') :