From 8965ec80990ffa709c06376b296a71a47c43fea1 Mon Sep 17 00:00:00 2001 From: Sarabveer Singh Date: Mon, 4 Dec 2023 00:14:18 -0500 Subject: [PATCH] ci: add actions job to fetch everyday --- .github/workflows/fetch.yaml | 25 +++++++++++++++++++++++++ lib/writeFile.js | 19 ++++++++++++++----- 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/fetch.yaml diff --git a/.github/workflows/fetch.yaml b/.github/workflows/fetch.yaml new file mode 100644 index 0000000..46e4c0f --- /dev/null +++ b/.github/workflows/fetch.yaml @@ -0,0 +1,25 @@ +name: Fetch latest Hukamnama +on: + workflow_dispatch: + schedule: + - cron: '30 2 * * *' + - cron: '30 5 * * *' + +jobs: + test_schedule: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + # Setup .npmrc file to publish to GitHub Packages + - uses: actions/setup-node@v3 + with: + registry-url: 'https://npm.pkg.github.com' + scope: '@gurbaninow' + - run: npm ci + - run: npm run fetch + name: Fetch + - uses: stefanzweifel/git-auto-commit-action@v5 + with: + file_pattern: '*.json' diff --git a/lib/writeFile.js b/lib/writeFile.js index 92cee67..110e0a5 100644 --- a/lib/writeFile.js +++ b/lib/writeFile.js @@ -8,11 +8,20 @@ const writeFile = ( { date: dateStr, ids, sttmIds } ) => { // Write file // Error if already exists - writeFileSync( - `./archive/${date[ 0 ]}/${date[ 1 ]}/${date[ 2 ]}.json`, - `${JSON.stringify( { sttm2_ids: sttmIds, shabad_ids: ids }, null, 2 )}\n`, - { flag: 'wx' }, - ) + try { + writeFileSync( + `./archive/${date[ 0 ]}/${date[ 1 ]}/${date[ 2 ]}.json`, + `${JSON.stringify( { sttm2_ids: sttmIds, shabad_ids: ids }, null, 2 )}\n`, + { flag: 'wx' }, + ) + } catch ( error ) { + if ( error.code === 'EEXIST' ) { + // eslint-disable-next-line no-console + console.error( error.message ) + } else { + throw error + } + } } export default writeFile