Skip to content

Commit 175ad70

Browse files
Reintegrate and refine preview update logic, add setOutputs utility, and introduce getImages helper for dynamic content generation in README files.
1 parent 03a731c commit 175ad70

3 files changed

Lines changed: 202 additions & 80 deletions

File tree

dist/index.js

Lines changed: 163 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34379,6 +34379,8 @@ const github_1 = __nccwpck_require__(3228);
3437934379
const inputs_1 = __nccwpck_require__(9612);
3438034380
const core_1 = __nccwpck_require__(7484);
3438134381
const repository_1 = __nccwpck_require__(6629);
34382+
const preview_1 = __nccwpck_require__(1365);
34383+
const outputs_1 = __nccwpck_require__(8595);
3438234384
const previewUpdater = async () => {
3438334385
// Inputs
3438434386
const { token, configPath } = (0, inputs_1.parse)();
@@ -34399,44 +34401,34 @@ const previewUpdater = async () => {
3439934401
// Checkout branch
3440034402
const branchExists = await repo.branchExists();
3440134403
(0, core_1.info)(`Checkout ${branchExists ? 'existing' : 'new'} branch named "${repo.branchName()}"`);
34402-
// await repo.checkoutBranch(! branchExists)
34403-
//
34404-
// console.log('aaaa', branchExists)
34405-
//
34406-
// // Read file
34407-
// const content = readFile(config, config.path.readme)
34408-
// const preview = setPreview(content, config)
34409-
//
34410-
// if (content !== preview) {
34411-
// info(`Update readme in "${ config.path.readme }" file`)
34412-
// writeFile(config, config.path.readme, preview)
34413-
// } else {
34414-
// info(`File "${ config.path.readme }" is up to date`)
34415-
// }
34416-
//
34417-
// // Stage and commit changes
34418-
// await repo.stage()
34419-
// await repo.commit()
34420-
// await repo.push()
34421-
//
34422-
// // Create a Pull Request
34423-
// const pullRequest = await repo.createPullRequest()
34424-
//
34425-
// // Variables
34426-
// const pullRequestNumber: number = pullRequest.data.number
34427-
// const pullRequestUrl: string = pullRequest.data.html_url
34428-
//
34429-
// if (config.repository.pullRequest.assignees.length > 0) {
34430-
// await repo.assignee(pullRequestNumber, config.repository.pullRequest.assignees)
34431-
// }
34432-
//
34433-
// if (config.repository.pullRequest.labels.length > 0) {
34434-
// await repo.addLabels(pullRequestNumber, config.repository.pullRequest.labels)
34435-
// }
34436-
//
34437-
// info(`Preview created in pull request #${ pullRequestNumber }: ${ pullRequestUrl }`)
34438-
//
34439-
// setOutputs(repo.branchName(), pullRequestNumber, pullRequestUrl)
34404+
await repo.checkoutBranch(!branchExists);
34405+
// Read file
34406+
const content = (0, filesystem_1.readFile)(config, config.path.readme);
34407+
const preview = (0, preview_1.setPreview)(content, config);
34408+
if (content !== preview) {
34409+
(0, core_1.info)(`Update readme in "${config.path.readme}" file`);
34410+
(0, filesystem_1.writeFile)(config, config.path.readme, preview);
34411+
}
34412+
else {
34413+
(0, core_1.info)(`File "${config.path.readme}" is up to date`);
34414+
}
34415+
// Stage and commit changes
34416+
await repo.stage();
34417+
await repo.commit();
34418+
await repo.push();
34419+
// Create a Pull Request
34420+
const pullRequest = await repo.createPullRequest();
34421+
// Variables
34422+
const pullRequestNumber = pullRequest.data.number;
34423+
const pullRequestUrl = pullRequest.data.html_url;
34424+
if (config.repository.pullRequest.assignees.length > 0) {
34425+
await repo.assignee(pullRequestNumber, config.repository.pullRequest.assignees);
34426+
}
34427+
if (config.repository.pullRequest.labels.length > 0) {
34428+
await repo.addLabels(pullRequestNumber, config.repository.pullRequest.labels);
34429+
}
34430+
(0, core_1.info)(`Preview created in Pull Request #${pullRequestNumber}: ${pullRequestUrl}`);
34431+
(0, outputs_1.setOutputs)(repo.branchName(), pullRequestNumber, pullRequestUrl);
3444034432
};
3444134433
exports["default"] = previewUpdater;
3444234434

@@ -34581,6 +34573,73 @@ const exec = async (command) => {
3458134573
exports.exec = exec;
3458234574

3458334575

34576+
/***/ }),
34577+
34578+
/***/ 7828:
34579+
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
34580+
34581+
"use strict";
34582+
34583+
Object.defineProperty(exports, "__esModule", ({ value: true }));
34584+
exports.getImages = void 0;
34585+
const packageManagers_1 = __nccwpck_require__(2453);
34586+
const encodeUri = (value) => {
34587+
if (value === '') {
34588+
return '';
34589+
}
34590+
return encodeURIComponent(value);
34591+
};
34592+
const detectPackageManager = (config, visibility) => {
34593+
if ((0, packageManagers_1.hasComposer)(config)) {
34594+
return `composer${visibility} require`;
34595+
}
34596+
if ((0, packageManagers_1.hasNpm)(config)) {
34597+
return `npm${visibility} install`;
34598+
}
34599+
if ((0, packageManagers_1.hasYarn)(config)) {
34600+
return `yarn${visibility} add`;
34601+
}
34602+
return '';
34603+
};
34604+
const packageManager = (config) => {
34605+
const visibility = config.image.parameters.packageGlobal ? ' global' : '';
34606+
switch (config.image.parameters.packageManager) {
34607+
case 'composer':
34608+
return `composer${visibility} require`;
34609+
case 'npm':
34610+
return `npm${visibility} install`;
34611+
case 'yarn':
34612+
return `yarn${visibility} add`;
34613+
case 'auto':
34614+
return detectPackageManager(config, visibility);
34615+
default:
34616+
return '';
34617+
}
34618+
};
34619+
const packageName = (image) => image.packageManager !== 'none' ? image.packageName : '';
34620+
const render = (config, theme, suffix = '') => {
34621+
const image = config.image.parameters;
34622+
const params = new URLSearchParams({
34623+
theme: theme,
34624+
pattern: image.pattern,
34625+
style: image.style,
34626+
fontSize: image.fontSize,
34627+
images: image.icon,
34628+
packageManager: packageManager(config),
34629+
packageName: packageName(image),
34630+
description: image.description
34631+
});
34632+
return config.image.url.replace('{title}', encodeUri(image.title)) + '?' + params.toString() + suffix;
34633+
};
34634+
const format = (title, url) => `![${title}](${url})`;
34635+
const getImages = (config) => {
34636+
const light = format(config.image.parameters.title, render(config, 'light', '#gh-light-mode-only'));
34637+
const dark = format(config.image.parameters.title, render(config, 'dark', '#gh-dark-mode-only'));
34638+
return [light, dark];
34639+
};
34640+
exports.getImages = getImages;
34641+
34642+
3458434643
/***/ }),
3458534644

3458634645
/***/ 9612:
@@ -34611,6 +34670,72 @@ const parse = () => {
3461134670
exports.parse = parse;
3461234671

3461334672

34673+
/***/ }),
34674+
34675+
/***/ 8595:
34676+
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
34677+
34678+
"use strict";
34679+
34680+
Object.defineProperty(exports, "__esModule", ({ value: true }));
34681+
exports.setOutputs = void 0;
34682+
const core_1 = __nccwpck_require__(7484);
34683+
const setOutputs = (branchName, pullRequestNumber, pullRequestUrl) => {
34684+
(0, core_1.setOutput)('branchName', branchName);
34685+
(0, core_1.setOutput)('pullRequestNumber', pullRequestNumber);
34686+
(0, core_1.setOutput)('pullRequestUrl', pullRequestUrl);
34687+
};
34688+
exports.setOutputs = setOutputs;
34689+
34690+
34691+
/***/ }),
34692+
34693+
/***/ 2453:
34694+
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
34695+
34696+
"use strict";
34697+
34698+
Object.defineProperty(exports, "__esModule", ({ value: true }));
34699+
exports.hasYarn = exports.hasNpm = exports.hasComposer = void 0;
34700+
const filesystem_1 = __nccwpck_require__(9742);
34701+
const hasComposer = (config) => (0, filesystem_1.fileExists)(config, 'composer.json');
34702+
exports.hasComposer = hasComposer;
34703+
const hasNpm = (config) => (0, filesystem_1.fileExists)(config, 'package.json');
34704+
exports.hasNpm = hasNpm;
34705+
const hasYarn = (config) => (0, filesystem_1.fileExists)(config, 'yarn.lock');
34706+
exports.hasYarn = hasYarn;
34707+
34708+
34709+
/***/ }),
34710+
34711+
/***/ 1365:
34712+
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
34713+
34714+
"use strict";
34715+
34716+
Object.defineProperty(exports, "__esModule", ({ value: true }));
34717+
exports.setPreview = void 0;
34718+
const image_1 = __nccwpck_require__(7828);
34719+
const hasHeader = (content) => content.match(/^#\s+/);
34720+
const cleanUp = (content) => content
34721+
.replace(/^(#\s+.+\n+)(!\[.+]\(.*\)\n?){1,2}\n?/, '$1\n')
34722+
.replace(/^(#\s+.+\n+)(<img\s.*\/>\n?){1,2}\n?/, '$1\n');
34723+
const titleCase = (title) => title
34724+
.replace(/([A-Z])/g, '$1')
34725+
.toLowerCase()
34726+
.replace(/(^|\s|-|_)\S/g, (match) => match.toUpperCase())
34727+
.replace(/[-_]/g, ' ');
34728+
const setPreview = (content, config) => {
34729+
if (!hasHeader(content)) {
34730+
const title = titleCase(config.image.parameters.title);
34731+
content = `# ${title}\n\n${content}`;
34732+
}
34733+
const images = (0, image_1.getImages)(config).join('\n');
34734+
return cleanUp(content).replace(/^(#\s+.+\n\n)/, '$1' + images + '\n\n');
34735+
};
34736+
exports.setPreview = setPreview;
34737+
34738+
3461434739
/***/ }),
3461534740

3461634741
/***/ 3678:
@@ -34681,7 +34806,6 @@ class Repository {
3468134806
async checkoutBranch(isNew) {
3468234807
try {
3468334808
this._newBranch = isNew;
34684-
console.log('bbb', isNew);
3468534809
await (0, filesystem_1.exec)(`git switch ${isNew ? '-c' : ''} "${this.branchName()}"`);
3468634810
}
3468734811
catch (error) {

src/main.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { cwd, readConfig } from './utils/filesystem'
1+
import { cwd, readConfig, readFile, writeFile } from './utils/filesystem'
22
import { context, getOctokit } from '@actions/github'
33
import { parse } from './utils/inputs'
44
import { info } from '@actions/core'
55
import { Config } from './types/config'
66
import { Repository } from './utils/repository'
7+
import { setPreview } from './utils/preview'
8+
import { setOutputs } from './utils/outputs'
79

810
const previewUpdater = async () => {
911
// Inputs
@@ -33,44 +35,42 @@ const previewUpdater = async () => {
3335
// Checkout branch
3436
const branchExists = await repo.branchExists()
3537
info(`Checkout ${ branchExists ? 'existing' : 'new' } branch named "${ repo.branchName() }"`)
36-
// await repo.checkoutBranch(! branchExists)
37-
//
38-
// console.log('aaaa', branchExists)
39-
//
40-
// // Read file
41-
// const content = readFile(config, config.path.readme)
42-
// const preview = setPreview(content, config)
43-
//
44-
// if (content !== preview) {
45-
// info(`Update readme in "${ config.path.readme }" file`)
46-
// writeFile(config, config.path.readme, preview)
47-
// } else {
48-
// info(`File "${ config.path.readme }" is up to date`)
49-
// }
50-
//
51-
// // Stage and commit changes
52-
// await repo.stage()
53-
// await repo.commit()
54-
// await repo.push()
55-
//
56-
// // Create a Pull Request
57-
// const pullRequest = await repo.createPullRequest()
58-
//
59-
// // Variables
60-
// const pullRequestNumber: number = pullRequest.data.number
61-
// const pullRequestUrl: string = pullRequest.data.html_url
62-
//
63-
// if (config.repository.pullRequest.assignees.length > 0) {
64-
// await repo.assignee(pullRequestNumber, config.repository.pullRequest.assignees)
65-
// }
66-
//
67-
// if (config.repository.pullRequest.labels.length > 0) {
68-
// await repo.addLabels(pullRequestNumber, config.repository.pullRequest.labels)
69-
// }
70-
//
71-
// info(`Preview created in pull request #${ pullRequestNumber }: ${ pullRequestUrl }`)
72-
//
73-
// setOutputs(repo.branchName(), pullRequestNumber, pullRequestUrl)
38+
await repo.checkoutBranch(! branchExists)
39+
40+
// Read file
41+
const content = readFile(config, config.path.readme)
42+
const preview = setPreview(content, config)
43+
44+
if (content !== preview) {
45+
info(`Update readme in "${ config.path.readme }" file`)
46+
writeFile(config, config.path.readme, preview)
47+
} else {
48+
info(`File "${ config.path.readme }" is up to date`)
49+
}
50+
51+
// Stage and commit changes
52+
await repo.stage()
53+
await repo.commit()
54+
await repo.push()
55+
56+
// Create a Pull Request
57+
const pullRequest = await repo.createPullRequest()
58+
59+
// Variables
60+
const pullRequestNumber: number = pullRequest.data.number
61+
const pullRequestUrl: string = pullRequest.data.html_url
62+
63+
if (config.repository.pullRequest.assignees.length > 0) {
64+
await repo.assignee(pullRequestNumber, config.repository.pullRequest.assignees)
65+
}
66+
67+
if (config.repository.pullRequest.labels.length > 0) {
68+
await repo.addLabels(pullRequestNumber, config.repository.pullRequest.labels)
69+
}
70+
71+
info(`Preview created in Pull Request #${ pullRequestNumber }: ${ pullRequestUrl }`)
72+
73+
setOutputs(repo.branchName(), pullRequestNumber, pullRequestUrl)
7474
}
7575

7676
export default previewUpdater

src/utils/repository.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ export class Repository
5757
try {
5858
this._newBranch = isNew
5959

60-
console.log('bbb', isNew)
61-
6260
await exec(`git switch ${ isNew ? '-c' : '' } "${ this.branchName() }"`)
6361
} catch (error) {
6462
// @ts-ignore

0 commit comments

Comments
 (0)