From 5937405a0f67a1f676565469be0ba67c4da751fe Mon Sep 17 00:00:00 2001 From: Edouard Bozon Date: Sun, 11 Feb 2024 09:43:07 +0100 Subject: [PATCH 1/2] feat: add plugin (wip) --- nx.json | 11 ++++ packages/semver/package.json | 1 + packages/semver/project.json | 23 ------- packages/semver/src/index.ts | 1 + packages/semver/src/plugin/index.ts | 97 +++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 23 deletions(-) create mode 100644 packages/semver/src/plugin/index.ts diff --git a/nx.json b/nx.json index 636ada4e2..0cf552451 100644 --- a/nx.json +++ b/nx.json @@ -51,5 +51,16 @@ ], "production": ["default", "!{projectRoot}/src/test-setup.[jt]s"] }, + "plugins": [ + { + "plugin": "@jscutlery/semver", + "options": { + "npmPublish": true, + "githubRelease": true, + "noVerify": true, + "commitMessageFormat": "release({projectName}): 🎸 cut release to {version}" + } + } + ], "parallel": 1 } diff --git a/packages/semver/package.json b/packages/semver/package.json index 4c8b796c3..10dd6bcc0 100644 --- a/packages/semver/package.json +++ b/packages/semver/package.json @@ -6,6 +6,7 @@ "main": "src/index.js", "executors": "./executors.json", "generators": "./generators.json", + "private": false, "nx-migrations": { "migrations": "./migrations.json" }, diff --git a/packages/semver/project.json b/packages/semver/project.json index 1acc7cabc..b4dd7f67b 100644 --- a/packages/semver/project.json +++ b/packages/semver/project.json @@ -5,29 +5,6 @@ "projectType": "library", "generators": {}, "targets": { - "version": { - "executor": "./dist/packages/semver:version", - "options": { - "push": true, - "noVerify": true, - "postTargets": ["build", "publish", "github"], - "commitMessageFormat": "release({projectName}): 🎸 cut release to {version}" - } - }, - "github": { - "executor": "./dist/packages/semver:github", - "options": { - "tag": "{tag}", - "notes": "{notes}" - } - }, - "publish": { - "executor": "ngx-deploy-npm:deploy", - "options": { - "access": "public", - "distFolderPath": "dist/packages/semver" - } - }, "lint": { "executor": "@nx/linter:eslint", "options": { diff --git a/packages/semver/src/index.ts b/packages/semver/src/index.ts index e69de29bb..c9d6fec91 100644 --- a/packages/semver/src/index.ts +++ b/packages/semver/src/index.ts @@ -0,0 +1 @@ +export { createNodes } from './plugin'; diff --git a/packages/semver/src/plugin/index.ts b/packages/semver/src/plugin/index.ts new file mode 100644 index 000000000..0b9c24d4b --- /dev/null +++ b/packages/semver/src/plugin/index.ts @@ -0,0 +1,97 @@ +import { + CreateNodes, + ProjectConfiguration, + TargetConfiguration, + readJsonFile, +} from '@nx/devkit'; +import { PackageJson } from 'nx/src/utils/package-json'; +import { dirname, resolve } from 'path'; + +interface Options { + buildTarget?: string; + npmPublish?: boolean; + githubRelease?: boolean; + noVerify?: boolean; + commitMessageFormat?: string; +} + +export const createNodes: CreateNodes = [ + '**/package.json', + (packageJsonPath, opts) => { + opts ??= {}; + opts.buildTarget ??= 'build'; + opts.npmPublish ??= true; + opts.githubRelease ??= true; + + const projectRoot = dirname(packageJsonPath); + // Do not augment project if package.json is private. + const packageJson = readJsonFile(packageJsonPath); + if (packageJson.private === undefined || packageJson.private === true) { + return {}; + } + + const projectConfig = readJsonFile( + resolve(projectRoot, 'project.json'), + ); + const outputPath: string | undefined = + projectConfig.targets?.[opts.buildTarget]?.options?.outputPath; + + if (!outputPath) { + throw new Error( + `The project "${projectRoot}" does not have a build target.`, + ); + } + + const targets: [target: string, config: TargetConfiguration][] = [ + [ + 'version', + { + executor: '@jscutlery/semver:version', + options: { + ...(opts.githubRelease ? { push: true } : {}), + ...(opts.noVerify ? { noVerify: opts.noVerify } : {}), + ...(opts.commitMessageFormat + ? { commitMessageFormat: opts.commitMessageFormat } + : {}), + postTargets: [ + ...(opts.npmPublish ? [opts.buildTarget, 'npm'] : []), + ...(opts.githubRelease ? ['github'] : []), + ], + }, + }, + ], + ]; + + if (opts.npmPublish) { + targets.push([ + 'npm', + { + command: `npm publish ${outputPath}`, + }, + ]); + } + + if (opts.githubRelease) { + targets.push([ + 'github', + { + executor: '@jscutlery/semver:github', + options: { + notes: '{notes}', + tag: '{tag}', + }, + }, + ]); + } + + return { + projects: { + [projectRoot]: { + targets: targets.reduce((acc, [target, config]) => { + return { ...acc, [target]: config }; + }, {}), + }, + }, + }; + }, +]; From 5cd96aaba68acdb46ad680d599b56b7f5f024dfd Mon Sep 17 00:00:00 2001 From: Edouard Bozon Date: Sun, 11 Feb 2024 09:44:49 +0100 Subject: [PATCH 2/2] feat: add plugin (wip) --- packages/semver/src/plugin/index.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/semver/src/plugin/index.ts b/packages/semver/src/plugin/index.ts index 0b9c24d4b..f01917985 100644 --- a/packages/semver/src/plugin/index.ts +++ b/packages/semver/src/plugin/index.ts @@ -4,7 +4,6 @@ import { TargetConfiguration, readJsonFile, } from '@nx/devkit'; -import { PackageJson } from 'nx/src/utils/package-json'; import { dirname, resolve } from 'path'; interface Options { @@ -24,12 +23,6 @@ export const createNodes: CreateNodes = [ opts.githubRelease ??= true; const projectRoot = dirname(packageJsonPath); - // Do not augment project if package.json is private. - const packageJson = readJsonFile(packageJsonPath); - if (packageJson.private === undefined || packageJson.private === true) { - return {}; - } - const projectConfig = readJsonFile( resolve(projectRoot, 'project.json'), );