Skip to content

Commit

Permalink
ci: configure gulp (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wroud authored Sep 8, 2024
1 parent cf5ef0c commit f6d4755
Show file tree
Hide file tree
Showing 10 changed files with 2,767 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ jobs:
git config user.email [email protected]
- name: Run ci:prepublish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: yarn workspaces foreach -A --no-private run ci:prepublish

- name: Release
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"@vitest/coverage-v8": "^2",
"@wroud/tests-runner": "workspace:^",
"commitizen": "^4",
"conventional-changelog-conventionalcommits": "^8",
"cz-conventional-changelog": "^3",
"happy-dom": "^15",
"husky": "^9",
Expand Down
35 changes: 35 additions & 0 deletions packages/ci/gulp/RestrictEmptyCommits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Bumper } from "conventional-recommended-bump";

export class RestrictEmptyCommits extends Bumper {
loadPreset(preset) {
this.preset = preset;

this.whatBump = async (commits) => {
const { whatBump } = await this.getPreset();

return whatBump(commits);
};

this.tagGetter = async () => {
const { tags } = await this.getPreset();

return this.getLastSemverTag(tags);
};

this.commitsGetter = async function* commitsGetter() {
const { commits, parser } = await this.getPreset();

yield* this.getCommits(commits, parser);
};

return this;
}
bump(whatBump = this.whatBump) {
return super.bump((commits) => {
if (commits.length === 0) {
return null;
}
return whatBump(commits);
});
}
}
26 changes: 26 additions & 0 deletions packages/ci/gulp/combineStreams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PassThrough } from "stream";

// Function to create a combined readable stream from two streams
export function combineStreams(...streams) {
const passThrough = new PassThrough();
let currentStreamIndex = 0;

function pipeNext() {
if (currentStreamIndex < streams.length) {
const currentStream = streams[currentStreamIndex];
currentStream.pipe(passThrough, { end: false });
currentStream.on("end", () => {
currentStreamIndex++;
pipeNext();
});
currentStream.on("error", (error) => {
passThrough.emit("error", error);
});
} else {
passThrough.end();
}
}

pipeNext();
return passThrough;
}
1 change: 1 addition & 0 deletions packages/ci/gulp/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./prepublish.js";
100 changes: 100 additions & 0 deletions packages/ci/gulp/prepublish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { task } from "gulp";
import { execa } from "execa";
import { temporaryFile } from "tempy";
import { rename } from "fs/promises";
import conventionalGithubReleaser from "conventional-github-releaser";
import conventionalChangelog from "conventional-changelog";
import createPreset from "conventional-changelog-conventionalcommits";
import { RestrictEmptyCommits } from "./RestrictEmptyCommits.js";
import { pipeline } from "stream/promises";
import { combineStreams } from "./combineStreams.js";
import { createReadStream, createWriteStream } from "fs";

const tagPrefix = "di-v";
const commitPath = ".";
const changeLogFile = "CHANGELOG.md";
// print output of commands into the terminal
const stdio = "inherit";

async function bumpVersion(preset) {
const bumper = new RestrictEmptyCommits(process.cwd())
.loadPreset(preset)
.tag({
prefix: tagPrefix,
})
.commits({ path: commitPath });

const recommendation = await bumper.bump();

if (!recommendation.releaseType) {
return null;
}

await execa("yarn", ["version", recommendation.releaseType], {
stdio,
});

const { stdout } = await execa`grep '"version"' package.json`
.pipe`awk -F '"' '{print $4}'`;
return stdout;
}

async function changelog(preset, version) {
const changelogStream = conventionalChangelog(
{
config: preset,
tagPrefix,
},
undefined,
{ path: commitPath },
);

const combinedStream = combineStreams(
changelogStream,
createReadStream(changeLogFile),
);

const tmp = temporaryFile();

await pipeline(combinedStream, createWriteStream(tmp, { flags: "w" }));
await rename(tmp, changeLogFile);

return version;
}

async function commitTagPush(version) {
const commitMsg = `chore: release ${version}`;
await execa("git", ["add", "package.json", "CHANGELOG.md"], { stdio });
await execa("git", ["commit", "--message", commitMsg], { stdio });
await execa("git", ["tag", `${tagPrefix}${version}`], { stdio });
await execa("git", ["push", "--follow-tags"], { stdio });
}

async function githubRelease() {
await new Promise((resolve, reject) => {
conventionalGithubReleaser(
{ type: "oauth", token: process.env.GITHUB_TOKEN },
{ preset, tagPrefix, commitPath },
(err, success) => {
if (err) {
reject(err);
} else {
resolve(success);
}
},
);
});
}

task("ci:prepublish", async () => {
const preset = createPreset();
const version = await bumpVersion(preset);

if (version === null) {
return;
}

await changelog(preset, version);
await commitTagPush(version);
await githubRelease();
});
15 changes: 13 additions & 2 deletions packages/ci/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@
"get-version": "./bin/getVersion.js"
},
"devDependencies": {
"@tsconfig/node20": "^20",
"@tsconfig/node20": "^20"
},
"exports": {
"./gulp": "./gulp/index.js"
},
"dependencies": {
"conventional-changelog": "^6",
"conventional-changelog-cli": "^5",
"conventional-changelog-conventionalcommits": "^8",
"conventional-recommended-bump": "^10"
"conventional-github-releaser": "^3",
"conventional-recommended-bump": "^10",
"execa": "^9",
"gulp": "^5",
"tempy": "^3"
}
}
1 change: 1 addition & 0 deletions packages/di/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@wroud/ci/gulp";
8 changes: 2 additions & 6 deletions packages/di/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@
"!.tsbuildinfo"
],
"scripts": {
"ci:version": "yarn version $(yarn get-version conventionalcommits di-v .)",
"ci:changelog": "conventional-changelog -p conventionalcommits -i CHANGELOG.md -s -t di-v --commit-path .",
"ci:tag": "VERSION=$(grep '\"version\"' package.json | awk -F '\"' '{print $4}') && git add package.json CHANGELOG.md && git commit -m \"Release di-v$VERSION\" && git tag -a \"di-v$VERSION\" -m \"Release version $VERSION\" && git push && git push origin tag \"di-v$VERSION\"",
"ci:prepublish": "yarn run ci:version || exit 0 && yarn run ci:changelog && yarn run ci:tag",
"ci:prepublish": "yarn gulp ci:prepublish",
"test": "tests-runner",
"test:ci": "CI=true yarn run test",
"build": "tsc -b",
Expand All @@ -61,8 +58,7 @@
"@wroud/tests-runner": "workspace:^",
"@wroud/tsconfig": "workspace:^",
"concurrently": "^8",
"conventional-changelog-cli": "^5",
"conventional-changelog-conventionalcommits": "^8",
"gulp": "^5",
"rimraf": "^6",
"tslib": "^2",
"typescript": "^5",
Expand Down
Loading

0 comments on commit f6d4755

Please sign in to comment.