From 7ceb9cb7836210b2ab86c1274837ba3de82fe227 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Thu, 24 Jun 2021 00:02:40 +0200 Subject: [PATCH] feat: saving json data for future use --- package-lock.json | 2 +- package.json | 2 +- src/helpers.ts | 24 +++++++++++++++++++----- src/index.ts | 15 +++++++++++++-- test/helpers.spec.ts | 4 ++-- 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 66cf8f39..8d9e7409 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "mawesome", - "version": "1.0.34", + "version": "1.0.35", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 4993fe39..0778e92a 100644 --- a/package.json +++ b/package.json @@ -63,4 +63,4 @@ "volta": { "node": "14.15.4" } -} \ No newline at end of file +} diff --git a/src/helpers.ts b/src/helpers.ts index 87763825..3687671e 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -77,12 +77,26 @@ export function generateMd(data: string): Promise { }); } -export const OUTPUT_FILENAME: string = +export const MARKDOWN_FILENAME: string = core.getInput('output-filename') || 'README.md'; -export async function pushNewFile(markdown: string): Promise { - await fsp.writeFile(OUTPUT_FILENAME, markdown); + +type File = { + filename: string; + data: string; +}; + +export async function pushNewFiles(files: File[] = []): Promise { + 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(); } diff --git a/src/index.ts b/src/index.ts index 02b4669b..a120f63e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -41,7 +43,16 @@ export async function main(): Promise { 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 { diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index ffd5e7dd..d1b35deb 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -38,7 +38,7 @@ import { apiGetStar, paginate, generateMd, - pushNewFile, + pushNewFiles, } from '../src/helpers'; test('wait should wait', async (t) => { @@ -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'));