Skip to content

Commit

Permalink
feat: Add download-google-sheets build-step
Browse files Browse the repository at this point in the history
  • Loading branch information
elwinschmitz committed Nov 22, 2023
1 parent 77e771f commit af0a521
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
74 changes: 74 additions & 0 deletions .azure/local-data/download-google-sheets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env node

const { google } = require('googleapis');
const fs = require('fs');

let credentials;

try {
// Required:
// GOOGLE_SHEETS_API_SERVICE_ACCOUNT='{"client_email":"user@example.org","private_key":"secret"}'
credentials = JSON.parse(process.env.GOOGLE_SHEETS_API_SERVICE_ACCOUNT);
} catch (error) {
console.error(
'Error parsing Google Sheets API service account credentials:',
error,
);
}

const auth = new google.auth.JWT(
credentials.client_email,
null,
credentials.private_key,
['https://www.googleapis.com/auth/spreadsheets.readonly'],
);

const sheets = google.sheets({
version: 'v4',
auth,
});

async function downloadSheet(spreadsheetId, range, outputFile) {
try {
const res = await sheets.spreadsheets.values.get({
spreadsheetId,
range,
});
const rows = res.data.values;
if (rows.length) {
fs.writeFileSync(outputFile, JSON.stringify(res.data));
console.log(`Google Sheet downloaded as: ${outputFile}`);
} else {
console.log('No data found.');
}
} catch (err) {
console.error('The API says: ', err);
}
}

//
// --------------------------------------------------------------------------
//

// Get variables from command-line arguments
const spreadsheetId = process.argv[2];

// Create the path(s) required to store the sheets
const path = `./www/data/${spreadsheetId}/values`;

if (!fs.existsSync(path)) {
fs.mkdirSync(path, { recursive: true });
}

// See "SheetName"-enum in: ../src/app/services/spreadsheet.service.ts
const ranges = [
'Referral Page',
'Categories',
'Sub-Categories',
'Offers',
'Q&As',
];

for (const range of ranges) {
downloadSheet(spreadsheetId, range, `${path}/${range}`);
}
11 changes: 6 additions & 5 deletions .github/workflows/poc-deploy-private-faq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ jobs:
- name: Set/override robots.txt
run: 'cp src/robots.private.txt www/robots.txt'

- name: Install Google Sheets API SDK
run: 'npm install googleapis@^128.0.0 --no-save'

- name: Download sheet data
env:
GOOGLE_SHEETS_API_SERVICE_ACCOUNT: ${{ secrets.GOOGLE_SHEETS_API_SERVICE_ACCOUNT }}
run: |
echo "Downloading sheet data..."
cp './data/test-sheet-id-1/values/Referral Page.json' './www/data/1QsmCBZ7j1sgOwo7sXjI8Jln_jw68oL1gyRvvzj3dHII/Referral Page'
cp './data/test-sheet-id-1/values/Categories.json' './www/data/1QsmCBZ7j1sgOwo7sXjI8Jln_jw68oL1gyRvvzj3dHII/Categories'
cp './data/test-sheet-id-1/values/Sub-Categories.json' './www/data/1QsmCBZ7j1sgOwo7sXjI8Jln_jw68oL1gyRvvzj3dHII/Sub-Categories'
cp './data/test-sheet-id-1/values/Offers.json' './www/data/1QsmCBZ7j1sgOwo7sXjI8Jln_jw68oL1gyRvvzj3dHII/Offers'
cp './data/test-sheet-id-1/values/Q&As.json' './www/data/1QsmCBZ7j1sgOwo7sXjI8Jln_jw68oL1gyRvvzj3dHII/Q&As'
node ./.azure/local-data/download-google-sheets.js 1QsmCBZ7j1sgOwo7sXjI8Jln_jw68oL1gyRvvzj3dHII
echo "Downloading sheet data: done ✅"
- name: Deploy to Private-Staging
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ coverage

# Third party code
node_modules

# Sensitive data
.credentials.json
13 changes: 12 additions & 1 deletion src/ngsw-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,18 @@
"maxAge": "7d",
"timeout": "8s"
},
"urls": ["https://sheets.googleapis.com/v4/spreadsheets/*"]
"urls": ["https://sheets.googleapis.com/v4/spreadsheets/**"]
},
{
"name": "localSheetsData",
"version": 1,
"cacheConfig": {
"strategy": "freshness",
"maxSize": 0,
"maxAge": "1u",
"timeout": "1u"
},
"urls": ["/data/**"]
}
],
"navigationUrls": ["/**", "!/**/*.*", "!/login", "!/logout", "!/.auth/**"]
Expand Down

0 comments on commit af0a521

Please sign in to comment.