Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: rework npm publish flow #5

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.github/ @Mararok
40 changes: 0 additions & 40 deletions .github/actions/publish/action.yaml

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Pull Request
on:
pull_request:
branches: [ master, main ]
branches: [main]

jobs:
check:
Expand All @@ -10,10 +10,10 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Yarn install
uses: hexancore/yarn-ci-install@0.1.0
uses: hexancore/yarn-ci-install@ba9baf131eba84b6c86efb46375a530a3098bb04
- name: Lint
run: yarn lint
- name: Test
uses: ./.github/actions/test
- name: Build
run: yarn build
run: yarn build
70 changes: 0 additions & 70 deletions .github/workflows/prepare-release.yaml

This file was deleted.

6 changes: 1 addition & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
token: ${{ env.HEXANCORE_BOT_TOKEN }}
env:
HEXANCORE_BOT_TOKEN: ${{ secrets.HEXANCORE_BOT_TOKEN }}

- name: Yarn install
uses: hexancore/yarn-ci-install@0.1.0
uses: hexancore/yarn-ci-install@ba9baf131eba84b6c86efb46375a530a3098bb04

# Publish package
- name: Publish
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## 0.1.2

### Changed

- Release with gpg sign + generate provenance statements
Expand Down
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2023 Andrzej Wasiak <https://andrzejwasiak.pl>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 changes: 9 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,46 +1,25 @@
{
"name": "@hexancore/cli",
"version": "0.1.0",
"version": "0.1.2",
"engines": {
"node": ">=20"
},
"engine-strict": true,
"description": "CLI of Hexancore framework",
"author": {
"name": "Andrzej Wasiak",
"email": "[email protected]",
"url": "https://andrzejwasiak.pl"
},
"license": "MIT",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"typesVersions": {
"*": {
".": [
"./lib/index.d.ts"
]
}
},
"exports": {
".": {
"import": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"default": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
}
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/hexancore/cli.git"
"url": "https://github.com/hexancore/cli.git"
},
"publishConfig": {
"access": "public"
},
"homepage": "https://github.com/hexancore/cli.git",
"homepage": "https://github.com/hexancore/cli",
"packageManager": "[email protected]",
"scripts": {
"hcli": "node --optimize_for_size --max_old_space_size=460 --gc_interval=100 ./lib/index.js",
Expand Down Expand Up @@ -96,5 +75,10 @@
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "5.3.3"
}
},
"files": [
"bin",
"lib",
"templates"
]
}
70 changes: 70 additions & 0 deletions src/Command/Package/PackageBumpVersionCommandHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { ERR, OK, OKA, type AR, type R } from '@hexancore/common';
import { LocalDate, ZoneOffset } from '@js-joda/core';
import { inject, injectable } from 'inversify';
import { FilesystemHelper, type FileItem } from '../../Util';
import { Changelog } from '../../Util/Changelog/Changelog';

interface PackageBumpVersionCommandOptions {
dryRun?: true;
newVersion: string;
releaseDate?: string
}

@injectable()
export class PackageBumpVersionCommandHandler {
public constructor(
@inject(FilesystemHelper) private fs: FilesystemHelper
) {
}

public execute(options: PackageBumpVersionCommandOptions): AR<void> {
return this.fs.readJson('package.json').onOk(packageJson => {
packageJson.version = options.newVersion;
const repositoryUrl = this.extractReposiotryUrlFromPackageJson(packageJson);
if (repositoryUrl.isError()) {
return ERR(repositoryUrl.e);
}
const releaseDate: string = options.releaseDate ?? LocalDate.now(ZoneOffset.UTC).toString();

const updatedChangelog = this.updateChangelog(repositoryUrl.v, options.newVersion, releaseDate);
return this.save([
{ path: 'package.json', content: JSON.stringify(packageJson, null, 2) },
{ path: 'CHANGELOG.md', content: () => updatedChangelog },
], options.dryRun);
});
}

private extractReposiotryUrlFromPackageJson(packageJson: Record<string, any>): R<string> {
const repositoryUrl = packageJson.repository?.url;
if (typeof repositoryUrl !== 'string') {
return ERR('core.cli.package.empty_repository_url');
}
return OK(repositoryUrl.replace('.git', ''));
}

private updateChangelog(repositoryUrl: string, newVersion: string, releaseDate): AR<string> {
return this.fs.getFileLines('CHANGELOG.md').onOk((lines) => {
const changelog = new Changelog(repositoryUrl, lines);
changelog.updateWithNewVersion(newVersion, releaseDate);
return changelog.toString();
});
}

private save(files: FileItem[], dryRun: boolean): AR<void> {
if (dryRun) {
return OKA(files).onEachAsArray((item) => {
if (typeof item.content === 'function') {
return item.content().onOk((content) => {
console.log('Files:\n' + `### ${item.path} ###\n${content}\n### END ###\n\n`);
});
} else {
console.log('Files:\n' + `### ${item.path} ###\n${item.content}\n### END ###\n\n`);
return OK(undefined);
}

}).onOk(() => { });
}

return this.fs.outputFiles(files);
}
}
26 changes: 20 additions & 6 deletions src/Command/registerCommands.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Command, CommandUnknownOpts } from '@commander-js/extra-typings';
import { StdErrors, type AR } from '@hexancore/common';
import type { Container } from 'inversify';
import { HcModuleHelper, ProjectHelper, PromptHelper, printError, styles } from '../Util';
import { FilesystemHelper, HcModuleHelper, ProjectHelper, PromptHelper, printError, styles } from '../Util';
import { MakeModuleCommandHandler } from './Make/MakeModuleCommandHandler';
import { MakeModuleMessageCommandHandler } from './Make/MakeModuleMessageCommandHandler';
import { PulumiMakeProjectCommandHandler } from './Pulumi/MakeProject/PulumiMakeProjectCommandHandler';
import { PackageBumpVersionCommandHandler } from './Package/PackageBumpVersionCommandHandler';

async function actionWrapper(action: () => AR<void>): Promise<void> {
(await action()).onErr((e) => {
Expand Down Expand Up @@ -34,13 +35,13 @@ function makeModuleMessage(cli: Command, c: Container): void {
.action((options) => actionWrapper(() => command.execute(options)));
}

function make(cli: Command, c: Container): void {
const make = cli.command('make').description('display subcommand list');
function makeCommands(cli: Command, c: Container): void {
const make = cli.command('make').description('display subcommand list');
makeModule(make, c);
makeModuleMessage(make, c);
}

function pulumi(cli: Command, c: Container): void {
function pulumiCommands(cli: Command, c: Container): void {
const pulumi = cli.command('pulumi').description('display subcommand list');
const make = pulumi.command('make').description('[project]');

Expand All @@ -52,12 +53,25 @@ function pulumi(cli: Command, c: Container): void {
.action((options) => actionWrapper(() => makeProjectCommand.execute(options)));
}

function packageCommands(cli: Command, c: Container): void {
const commandGroup = cli.command('package').description('display subcommand list');
commandGroup.command('bump-version')
.description('Bumps package version')
.option('--dryRun', 'Prints updated files', false)
.argument('newVersion', 'new version of package')
.action((newVersion, options) => actionWrapper(() => {
const handler = new PackageBumpVersionCommandHandler(c.get(FilesystemHelper));
return handler.execute({ newVersion, ...options });
}));
}

export default function (cli: Command, c: Container): void {
cli.configureHelp({
subcommandTerm: (cmd: CommandUnknownOpts) => {
return styles.primary(cmd.name());
}
});
make(cli, c);
pulumi(cli, c);
makeCommands(cli, c);
pulumiCommands(cli, c);
packageCommands(cli, c);
}
Loading