Skip to content

Commit

Permalink
feat: saving json data for future use
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub Actions committed Jun 23, 2021
1 parent cee1906 commit 7ceb9cb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@
"volta": {
"node": "14.15.4"
}
}
}
24 changes: 19 additions & 5 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,26 @@ export function generateMd(data: string): Promise<string> {
});
}

export const OUTPUT_FILENAME: string =
export const MARKDOWN_FILENAME: string =
core.getInput('output-filename') || 'README.md';
export async function pushNewFile(markdown: string): Promise<any> {
await fsp.writeFile(OUTPUT_FILENAME, markdown);

type File = {
filename: string;
data: string;
};

export async function pushNewFiles(files: File[] = []): Promise<any> {
if (!files.length) return;

await git.pull();
await git.add(OUTPUT_FILENAME);
await git.commit(`chore(${OUTPUT_FILENAME}): updated ${OUTPUT_FILENAME}`);

await Promise.all(
files.map(async ({ filename, data }) => {
await fsp.writeFile(filename, data);
await git.add(filename);
await git.commit(`chore(${filename}): updated ${filename}`);
})
);

await git.push();
}
15 changes: 13 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as core from '@actions/core';
import { data } from 'remark';

import {
renderer,
paginate,
REPO_USERNAME,
generateMd,
pushNewFile,
pushNewFiles,
MARKDOWN_FILENAME,
} from './helpers';

import type { SortedLanguageList, Stars, Star } from './types';
Expand Down Expand Up @@ -41,7 +43,16 @@ export async function main(): Promise<any> {

const markdown: string = await generateMd(rendered);

await pushNewFile(markdown);
await pushNewFiles([
{
filename: MARKDOWN_FILENAME,
data: markdown,
},
{
filename: 'data.json',
data: JSON.stringify(sortedByLanguages, null, 2),
},
]);
}

export async function run(): Promise<any> {
Expand Down
4 changes: 2 additions & 2 deletions test/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
apiGetStar,
paginate,
generateMd,
pushNewFile,
pushNewFiles,
} from '../src/helpers';

test('wait should wait', async (t) => {
Expand Down Expand Up @@ -97,7 +97,7 @@ test('generateMd should create TOC', async (t) => {
});

test('should push', async (t) => {
await pushNewFile('# title');
await pushNewFiles([{filename: "README.md", data: '# title'}]);
t.true(writeFile.calledWith('README.md', '# title'));
t.true(pull.called);
t.true(add.calledWith('README.md'));
Expand Down

0 comments on commit 7ceb9cb

Please sign in to comment.