Skip to content

Commit

Permalink
chore(ci): fix release action;
Browse files Browse the repository at this point in the history
* chore(ci): Add release-it script;
* fix(utils): redesigned logic for obtaining the global link;;
* chore(git): updated .gitignore;
chore(npm): updated .npmignore;
* chore(git): fix husky prepare script;
* chore(github): reworked npm release action step;
* chore(ci): add CHANGELOG.md contributors section generator;
* chore(deps): add `auto-changelog` to package.json;
  • Loading branch information
DigitalBrainJS committed Dec 20, 2022
1 parent f12d01e commit 46085e6
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 30 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/release.yml
Expand Up @@ -50,6 +50,8 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TYPE_ARG: ${{ fromJSON('{"auto":"", "patch":"--patch", "minor":"--minor", "major":"--major"}')[github.event.inputs.type] }}
BETA_ARG: ${{ github.event.inputs.beta == 'true' && '--preRelease=beta' || '' }}
NPM_ARG: ${{ github.event.inputs.npm == 'false' && '--no-npm' || '' }}
DRY_ARG: ${{ github.event.inputs.dry == 'true' && '--dry-run' || '' }}
run: npm run release -- --ci --verbose $NPM_ARG $TYPE_ARG $BETA_ARG $DRY_ARG
run: npm run release -- --ci --verbose $TYPE_ARG $BETA_ARG $DRY_ARG
- name: npm-release
if: ${{ github.event.inputs.dry == 'false' && github.event.inputs.npm == 'true' }}
run: npm publish
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -8,6 +8,6 @@ test/typescript/axios.js*
sauce_connect.log
test/module/**/package-lock.json
backup/
./.husky/
/.husky/
.npmrc
.env
30 changes: 11 additions & 19 deletions .npmignore
@@ -1,19 +1,11 @@
**/.*
*.iml
coverage/
examples/
node_modules/
typings/
sandbox/
test/
bower.json
CODE_OF_CONDUCT.md
COLLABORATOR_GUIDE.md
CONTRIBUTING.md
COOKBOOK.md
ECOSYSTEM.md
Gruntfile.js
karma.conf.js
webpack.*.js
sauce_connect.log
backup/
**/*
!/dist/**
!/lib/**
!index.js
!index.d.ts
!index.d.cts
!CHANGELOG.md
!LICENSE
!MIGRATION_GUIDE.md
!README.md
!SECURITY.md
13 changes: 11 additions & 2 deletions bin/check-build-version.js
Expand Up @@ -12,8 +12,17 @@ console.log(`Axios version: v${axios.VERSION}`);
console.log(`Axios build version: v${axiosBuild.VERSION}`);
console.log(`----------------------------`);

assert.strictEqual(version, axios.VERSION, `Version mismatch between package and Axios`);
assert.strictEqual(version, axiosBuild.VERSION, `Version mismatch between package and build`);
assert.strictEqual(
version,
axios.VERSION,
`Version mismatch between package and Axios ${version} != ${axios.VERSION}`
);

assert.strictEqual(
version,
axiosBuild.VERSION,
`Version mismatch between package and build ${version} != ${axiosBuild.VERSION}`
);

console.log('✔️ PASSED\n');

Expand Down
89 changes: 89 additions & 0 deletions bin/contributors.js
@@ -0,0 +1,89 @@
import axios from "../index.js";
import util from "util";
import cp from "child_process";
import Handlebars from "handlebars";
import fs from "fs/promises";

const exec = util.promisify(cp.exec);

const removeExtraLineBreaks = (str) => str.replace(/(?:\r\n|\r|\n){3,}/gm, '\r\n\r\n');

const cleanTemplate = template => template
.replace(/\n +/g, '\n')
.replace(/^ +/, '')
.replace(/\n\n\n+/g, '\n\n')
.replace(/\n\n$/, '\n');

const getUserInfo = ((userCache) => async (email) => {
if (userCache[email] !== undefined) {
return userCache[email];
}
try {
const {data: {items: [user]}} = await axios.get(`https://api.github.com/search/users?q=${email}`);

return (userCache[email] = user ? {
...user,
avatar_url_sm: user.avatar_url + '&s=16'
} : null);
} catch (err) {
console.warn(err);
return {};
}
})({});


const getReleaseInfo = async (version, useGithub) => {
version = 'v' + version.replace(/^v/, '');

const releases = JSON.parse((await exec(
`npx auto-changelog ${
version ? '--starting-version ' + version + ' --ending-version ' + version: ''
} --stdout --commit-limit false --template json`)).stdout
);

for(const release of releases) {
const authors = {};

const commits = [
...release.commits,
...release.fixes.map(fix => fix.commit),
...release.merges.map(fix => fix.commit)
].filter(Boolean);

for(const {author, email, insertions, deletions} of commits) {
const user = Object.assign({
name: author,
email
}, useGithub ? await getUserInfo(email) : null);

const entry = authors[author] = (authors[author] || {
insertions: 0, deletions: 0, ...user
});

entry.github = entry.login ? `https://github.com/${encodeURIComponent(entry.login)}` : '';

entry.insertions += insertions;
entry.deletions += deletions;
entry.points = entry.insertions + entry.deletions;
}

release.authors = Object.values(authors).sort((a, b) => b.points - a.points);
release.allCommits = commits;
}

return releases;
}

const renderContributorsList = async (version, useGithub = false, template) => {
const release = (await getReleaseInfo(version, useGithub))[0];

const compile = Handlebars.compile(String(await fs.readFile(template)))

const content = compile(release);

return removeExtraLineBreaks(cleanTemplate(content));
}

export {
renderContributorsList
}
47 changes: 47 additions & 0 deletions bin/injectContributorsList.js
@@ -0,0 +1,47 @@
import fs from 'fs/promises';
import path from 'path';
import {renderContributorsList} from './contributors.js';
import asyncReplace from 'string-replace-async';
import {fileURLToPath} from "url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const injectContributors = async (infile, injector) => {
console.log(`Checking contributors sections in ${infile}`);

infile = path.resolve(__dirname, infile);

const content = String(await fs.readFile(infile));
const headerRE = /^##\s+\[([-_\d.\w]+)]\s+-.+/mig;
const contributorsRE = /^\s*### Contributors/mi;

let tag;
let index = 0;

const newContent = await asyncReplace(content, headerRE, async (match, nextTag, offset) => {
const releaseContent = content.slice(index, offset);

const hasContributorsSection = contributorsRE.test(releaseContent);

const currentTag = tag;

tag = nextTag;
index = offset + match.length;

if(currentTag && !hasContributorsSection) {
console.log(`Adding contributors for ${currentTag}`);

return (await injector(currentTag)) + match;
}

return match;
});

await fs.writeFile(infile, newContent);
}


await injectContributors(
'../CHANGELOG.md',
(tag) => renderContributorsList(tag, true, path.resolve(__dirname, '../templates/contributors.hbs')
));
6 changes: 5 additions & 1 deletion lib/utils.js
Expand Up @@ -277,7 +277,11 @@ function findKey(obj, key) {
return null;
}

const _global = typeof self === "undefined" ? typeof global === "undefined" ? this : global : self;
const _global = (() => {
/*eslint no-undef:0*/
if (typeof globalThis !== "undefined") return globalThis;
return typeof self !== "undefined" ? self : (typeof window !== 'undefined' ? window : global)
})();

const isContextDefined = (context) => !isUndefined(context) && context !== _global;

Expand Down
88 changes: 86 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 46085e6

Please sign in to comment.