Skip to content

Commit

Permalink
feat: add color option. #35
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 4, 2024
1 parent fdbd91b commit ad3b10d
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Options:
--iconWidth Set this if icon is not square (default: 13)
--label The left label of the badge, usually static (default `coverage`).
--labelColor <Color RGB> or <Color Name> (default: '555')
--color <Color RGB> or <Color Name> (default: '')

Example:

Expand Down
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ inputs:
default: "1"
required: false

color:
description: "<Color RGB> or <Color Name> (default: '555')"
default: "555"
required: false

labelColor:
description: '<Color RGB> or <Color Name> (default: '555')'
description: "<Color RGB> or <Color Name> (default: '')"
default: "555"
required: false

Expand Down
87 changes: 84 additions & 3 deletions dist/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -37154,6 +37154,87 @@ var lib = __webpack_require__(9184);
var lib_default = /*#__PURE__*/__webpack_require__.n(lib);
// EXTERNAL MODULE: ./node_modules/@actions/core/lib/core.js
var core = __webpack_require__(3716);
;// ./node_modules/@babel/runtime/helpers/esm/toPrimitive.js

function toPrimitive(t, r) {
if ("object" != _typeof(t) || !t) return t;
var e = t[Symbol.toPrimitive];
if (void 0 !== e) {
var i = e.call(t, r || "default");
if ("object" != _typeof(i)) return i;
throw new TypeError("@@toPrimitive must return a primitive value.");
}
return ("string" === r ? String : Number)(t);
}

;// ./node_modules/@babel/runtime/helpers/esm/toPropertyKey.js


function toPropertyKey(t) {
var i = toPrimitive(t, "string");
return "symbol" == _typeof(i) ? i : i + "";
}

;// ./node_modules/@babel/runtime/helpers/esm/defineProperty.js

function _defineProperty(e, r, t) {
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
value: t,
enumerable: !0,
configurable: !0,
writable: !0
}) : e[r] = t, e;
}

;// ./node_modules/@babel/runtime/helpers/esm/objectSpread2.js

function ownKeys(e, r) {
var t = Object.keys(e);
if (Object.getOwnPropertySymbols) {
var o = Object.getOwnPropertySymbols(e);
r && (o = o.filter(function (r) {
return Object.getOwnPropertyDescriptor(e, r).enumerable;
})), t.push.apply(t, o);
}
return t;
}
function _objectSpread2(e) {
for (var r = 1; r < arguments.length; r++) {
var t = null != arguments[r] ? arguments[r] : {};
r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
_defineProperty(e, r, t[r]);
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
});
}
return e;
}

;// ./node_modules/@babel/runtime/helpers/esm/objectWithoutPropertiesLoose.js
function _objectWithoutPropertiesLoose(r, e) {
if (null == r) return {};
var t = {};
for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
if (e.includes(n)) continue;
t[n] = r[n];
}
return t;
}

;// ./node_modules/@babel/runtime/helpers/esm/objectWithoutProperties.js

function _objectWithoutProperties(e, t) {
if (null == e) return {};
var o,
r,
i = _objectWithoutPropertiesLoose(e, t);
if (Object.getOwnPropertySymbols) {
var s = Object.getOwnPropertySymbols(e);
for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]);
}
return i;
}

// EXTERNAL MODULE: ./node_modules/badgen/dist/index.js
var dist = __webpack_require__(572);
// EXTERNAL MODULE: external "fs"
Expand All @@ -37165,10 +37246,10 @@ var mini_svg_data_uri_default = /*#__PURE__*/__webpack_require__.n(mini_svg_data
var lodash_get = __webpack_require__(7935);
var lodash_get_default = /*#__PURE__*/__webpack_require__.n(lodash_get);
;// ./src/badges.ts
// Copied from `badgen` because it's not exported
var getIconString=function getIconString(path){return (0,external_fs_.readFileSync)(path,'utf8');};var defaultColorData={'#49c31a':[100],'#97c40f':[99.99,90],'#a0a127':[89.99,80],'#cba317':[79.99,60],'#ce0000':[59.99,0]};function badge(option,summary){var _ref=option||{},_ref$label=_ref.label,label=_ref$label===void 0?'coverage':_ref$label,_ref$style=_ref.style,style=_ref$style===void 0?'classic':_ref$style,_ref$jsonPath=_ref.jsonPath,jsonPath=_ref$jsonPath===void 0?'total.statements.pct':_ref$jsonPath;var pct=summary;pct=lodash_get_default()(summary,jsonPath,0);if(!isNaN(Number(pct))){pct=Number(pct);}if(typeof pct!=='number'){throw new Error("".concat(jsonPath," evaluates to ").concat(JSON.stringify(pct)," and is not a suitable path in the JSON coverage data"));}var colorData=defaultColorData;var color=Object.keys(colorData).find(function(value,idx){if(colorData[value].length===1&&pct>=colorData[value][0]){return true;}if(colorData[value].length===2&&pct<=colorData[value][0]&&pct>=colorData[value][1]){return true;}return false;});var badgenArgs={style:style,label:label,status:"".concat(pct<0?'Unknown':"".concat(pct,"%")),color:(color||'e5e5e5').replace(/^#/,'')};if(option.icon){var svgString=getIconString(option.icon);var svgDataUri=mini_svg_data_uri_default()(svgString);badgenArgs.icon=svgDataUri;}return (0,dist.badgen)(badgenArgs);}
var _excluded=["label","style","jsonPath"];// Copied from `badgen` because it's not exported
var getIconString=function getIconString(path){return (0,external_fs_.readFileSync)(path,'utf8');};var defaultColorData={'#49c31a':[100],'#97c40f':[99.99,90],'#a0a127':[89.99,80],'#cba317':[79.99,60],'#ce0000':[59.99,0]};function badge(option,summary){var _ref=option||{},_ref$label=_ref.label,label=_ref$label===void 0?'coverage':_ref$label,_ref$style=_ref.style,style=_ref$style===void 0?'classic':_ref$style,_ref$jsonPath=_ref.jsonPath,jsonPath=_ref$jsonPath===void 0?'total.statements.pct':_ref$jsonPath,otherOption=_objectWithoutProperties(_ref,_excluded);var pct=summary;pct=lodash_get_default()(summary,jsonPath,0);if(!isNaN(Number(pct))){pct=Number(pct);}if(typeof pct!=='number'){throw new Error("".concat(jsonPath," evaluates to ").concat(JSON.stringify(pct)," and is not a suitable path in the JSON coverage data"));}var colorData=defaultColorData;var color=Object.keys(colorData).find(function(value,idx){if(colorData[value].length===1&&pct>=colorData[value][0]){return true;}if(colorData[value].length===2&&pct<=colorData[value][0]&&pct>=colorData[value][1]){return true;}return false;});var badgenArgs=_objectSpread2(_objectSpread2({},otherOption),{},{style:style,label:label,status:"".concat(pct<0?'Unknown':"".concat(pct,"%")),color:(color||'e5e5e5').replace(/^#/,'')});if(option.icon){var svgString=getIconString(option.icon);var svgDataUri=mini_svg_data_uri_default()(svgString);badgenArgs.icon=svgDataUri;}console.log("badgenArgs",badgenArgs);return (0,dist.badgen)(badgenArgs);}
;// ./src/action.ts
;_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(){var _require,version,output,source,label,jsonPath,style,sourceData,svgStr;return _regeneratorRuntime().wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:_context.prev=0;_require=__webpack_require__(8330),version=_require.version;(0,core.info)("coverage-badges-cli v\x1B[32;1m".concat(version,"\x1B[0m"));output=external_path_default().resolve(process.cwd(),(0,core.getInput)('output')||'coverage/badges.svg');source=external_path_default().resolve(process.cwd(),(0,core.getInput)('source')||'coverage/coverage-summary.json');label=(0,core.getInput)('label')||'coverage';jsonPath=(0,core.getInput)('jsonPath')||'total.statements.pct';style=(0,core.getInput)('style')||'classic';lib_default().ensureDirSync(external_path_default().dirname(output));if(lib_default().existsSync(source)){_context.next=12;break;}(0,core.setFailed)("File \x1B[31m".concat(source,"\x1B[0m does not exist.\n please specify the file directory\n\x1B[35mnpm\x1B[0m coverage-badges-cli \x1B[33m--source\x1B[0m coverage/coverage-summary.json"));return _context.abrupt("return");case 12:(0,core.info)("Source Path: \x1B[32;1m".concat(source,"\x1B[0m"));(0,core.info)("Output Path: \x1B[32;1m".concat(output,"\x1B[0m"));sourceData=lib_default().readJSONSync(source);(0,core.startGroup)("Source Path: \x1B[32;1m".concat(source,"\x1B[0m"));(0,core.info)("".concat(JSON.stringify(sourceData,null,2)));(0,core.endGroup)();svgStr=badge({label:label,style:style,jsonPath:jsonPath},sourceData);(0,core.setOutput)('svg',svgStr);(0,core.startGroup)("SVG String: \x1B[32;1m".concat(output,"\x1B[0m"));(0,core.info)("".concat(svgStr));(0,core.endGroup)();lib_default().writeFileSync(output,svgStr);(0,core.info)("\nCreate Coverage Badges: \x1B[32;1m".concat(external_path_default().relative(process.cwd(),output),"\x1B[0m\n"));_context.next=30;break;case 27:_context.prev=27;_context.t0=_context["catch"](0);(0,core.setFailed)(_context.t0.message);case 30:case"end":return _context.stop();}},_callee,null,[[0,27]]);}))();
;_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(){var _require,version,output,source,label,labelColor,scale,jsonPath,style,sourceData,svgStr;return _regeneratorRuntime().wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:_context.prev=0;_require=__webpack_require__(8330),version=_require.version;(0,core.info)("coverage-badges-cli v\x1B[32;1m".concat(version,"\x1B[0m"));output=external_path_default().resolve(process.cwd(),(0,core.getInput)('output')||'coverage/badges.svg');source=external_path_default().resolve(process.cwd(),(0,core.getInput)('source')||'coverage/coverage-summary.json');label=(0,core.getInput)('label')||'coverage';labelColor=(0,core.getInput)('labelColor')||'555';scale=Number((0,core.getInput)('scale')||1)||1;jsonPath=(0,core.getInput)('jsonPath')||'total.statements.pct';style=(0,core.getInput)('style')||'classic';lib_default().ensureDirSync(external_path_default().dirname(output));if(lib_default().existsSync(source)){_context.next=14;break;}(0,core.setFailed)("File \x1B[31m".concat(source,"\x1B[0m does not exist.\n please specify the file directory\n\x1B[35mnpm\x1B[0m coverage-badges-cli \x1B[33m--source\x1B[0m coverage/coverage-summary.json"));return _context.abrupt("return");case 14:(0,core.info)("Source Path: \x1B[32;1m".concat(source,"\x1B[0m"));(0,core.info)("Output Path: \x1B[32;1m".concat(output,"\x1B[0m"));sourceData=lib_default().readJSONSync(source);(0,core.startGroup)("Source Path: \x1B[32;1m".concat(source,"\x1B[0m"));(0,core.info)("".concat(JSON.stringify(sourceData,null,2)));(0,core.endGroup)();svgStr=badge({label:label,labelColor:labelColor,style:style,jsonPath:jsonPath,scale:scale},sourceData);(0,core.setOutput)('svg',svgStr);(0,core.startGroup)("SVG String: \x1B[32;1m".concat(output,"\x1B[0m"));(0,core.info)("".concat(svgStr));(0,core.endGroup)();lib_default().writeFileSync(output,svgStr);(0,core.info)("\nCreate Coverage Badges: \x1B[32;1m".concat(external_path_default().relative(process.cwd(),output),"\x1B[0m\n"));_context.next=32;break;case 29:_context.prev=29;_context.t0=_context["catch"](0);(0,core.setFailed)(_context.t0.message);case 32:case"end":return _context.stop();}},_callee,null,[[0,29]]);}))();
})();

module.exports = __webpack_exports__;
Expand Down
5 changes: 3 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import fs from 'fs-extra';
import { setFailed, getInput, getBooleanInput, setOutput, info, startGroup, endGroup } from '@actions/core';
import { setFailed, getInput, setOutput, info, startGroup, endGroup } from '@actions/core';
import { badge, BadgeOption } from './badges';

;(async () => {
Expand All @@ -11,6 +11,7 @@ import { badge, BadgeOption } from './badges';
const source = path.resolve(process.cwd(), getInput('source') || 'coverage/coverage-summary.json');
const label = getInput('label') || 'coverage';
const labelColor = getInput('labelColor') || '555';
const color = getInput('color');
const scale = Number(getInput('scale') || 1) || 1;
const jsonPath = getInput('jsonPath') || 'total.statements.pct';
const style = (getInput('style') || 'classic') as BadgeOption['style'];
Expand All @@ -27,7 +28,7 @@ import { badge, BadgeOption } from './badges';
info(`${JSON.stringify(sourceData, null, 2)}`);
endGroup();

const svgStr = badge({ label, labelColor, style, jsonPath, scale } as BadgeOption, sourceData);
const svgStr = badge({ label, labelColor, color, style, jsonPath, scale } as BadgeOption, sourceData);

setOutput('svg', svgStr);

Expand Down
6 changes: 5 additions & 1 deletion src/badges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const defaultColorData: ColorData = {
}

export function badge(option: BadgeOption, summary: object) {
const { label = 'coverage', style = 'classic', jsonPath = 'total.statements.pct', ...otherOption } = (option || {}) as BadgenOptions
const { label = 'coverage', style = 'classic', jsonPath = 'total.statements.pct', color: optionColor, ...otherOption } = (option || {}) as BadgenOptions
let pct: any = summary;
pct = get(summary, jsonPath, 0);

Expand Down Expand Up @@ -66,6 +66,10 @@ export function badge(option: BadgeOption, summary: object) {
color: (color || 'e5e5e5').replace(/^#/, ''),
};

if (optionColor) {
badgenArgs.color = optionColor.replace(/^#/, '');
}

if(option.icon) {
const svgString = getIconString(option.icon) as string;
const svgDataUri = svgToTinyDataUri(svgString);
Expand Down
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ export default function run() {
export function cliHelp() {
console.log('\n Usage: coverage-badges [options] [--help|h]');
console.log('\n Options:\n');
console.log(' --version, -v ', 'Show version number');
console.log(' --help, -h ', 'Displays help information.');
console.log(' --output, -o ', 'Output directory.');
console.log(' --source, -s ', 'The path of the target file "coverage-summary.json".');
console.log(' --style ', 'Badges style: flat, flat-square.');
console.log(' --version, -v ','Show version number');
console.log(' --help, -h ','Displays help information.');
console.log(' --output, -o ','Output directory.');
console.log(' --source, -s ','The path of the target file "coverage-summary.json".');
console.log(' --style ','Badges style: flat, flat-square.');
console.log(' --type ','Coverage type: lines, statements, functions, branches.');
console.log(' --scale ','Set badge scale (default: 1)');
console.log(' --icon ','Path to icon file');
console.log(' --iconWidth ','Set this if icon is not square (default: 13)');
console.log(' --label ','The left label of the badge, usually static (default `coverage`).');
console.log(' --labelColor ','<Color RGB> or <Color Name> (default: "555")');
console.log(' --color ','<Color RGB> or <Color Name> (default: "")');
}

export function exampleHelp() {
Expand Down
8 changes: 7 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,17 @@ it('test badge case - custom label', async () => {
it('test badge case - label color', async () => {
const customLabel = "Custon Label";
const str = badge({ style: 'flat', status: '85%', labelColor: "ADF", label: customLabel }, mockSummary as any);
console.log("str", str)
expect(str.indexOf(`<text x="50" y="138" textLength="715">${customLabel}</text>`) > 0).toBeTruthy();
expect(str.indexOf(`<rect fill="#ADF" width="815" height="200"/>`) > 0).toBeTruthy();
});

it('test badge case - color', async () => {
const customLabel = "Custon Label";
const str = badge({ style: 'flat', status: '85%', color: "ADF", label: customLabel }, mockSummary as any);
expect(str.indexOf(`<text x="50" y="138" textLength="715">${customLabel}</text>`) > 0).toBeTruthy();
expect(str.indexOf(`<rect fill="#ADF" x="815" width="360" height="200"/>`) > 0).toBeTruthy();
});

it('test badge case - custom icon', async () => {
const customIcon = "./test/sample-logo.svg";
const processedIconString = `<image x="40" y="35" width="130" height="132" xlink:href="data:image/svg+xml,%3csvg xmlns=&apos;http://www.w3.org/2000/svg&apos; xml:space=&apos;preserve&apos; baseProfile=&apos;tiny&apos; overflow=&apos;visible&apos; version=&apos;1.2&apos; viewBox=&apos;0 0 256 257.5&apos;%3e%3ccircle cx=&apos;128&apos; cy=&apos;128.8&apos; r=&apos;120&apos; fill=&apos;none&apos; stroke=&apos;black&apos; stroke-miterlimit=&apos;10&apos; stroke-width=&apos;14&apos;/%3e%3c/svg%3e"/>`;
Expand Down

0 comments on commit ad3b10d

Please sign in to comment.