-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.js
More file actions
93 lines (80 loc) · 2.78 KB
/
entrypoint.js
File metadata and controls
93 lines (80 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const core = require('@actions/core');
const fs = require('fs');
const path = require('path');
const { main: pushMain } = require('./scripts/push-json-to-google-sheets');
const { fetchSheetData: pullMain } = require('./scripts/pull-google-sheets-to-json');
// Function to list the contents of the root folder
const listRootFolderContents = () => {
const rootFolder = __dirname;
const files = fs.readdirSync(rootFolder);
core.info(`Root folder contents: ${files.join(', ')}`);
};
// Get inputs
const action = core.getInput('action');
const googleApiKeyJsonRaw = core.getInput('google_api_key_json');
const spreadsheetId = core.getInput('spreadsheet_id');
const localizationRoot = core.getInput('localization_root');
if (!googleApiKeyJsonRaw) {
core.setFailed('Google API Key JSON is required.');
process.exit(1);
}
if (!spreadsheetId) {
core.setFailed('Google Sheets Spreadsheet ID is required.');
process.exit(1);
}
if (!localizationRoot) {
core.setFailed('Localization root directory is required.');
process.exit(1);
}
// Parse the Google API Key JSON
let googleApiKeyJson;
try {
googleApiKeyJson = JSON.parse(googleApiKeyJsonRaw);
core.info('Parsed Google API Key JSON successfully.');
} catch (error) {
core.setFailed('Invalid Google API Key JSON.');
process.exit(1);
}
// Debug logging
core.info(`Action: ${action}`);
core.info(`Google API Key JSON: ${googleApiKeyJson ? 'Provided' : 'Not Provided'}`);
core.info(`Spreadsheet ID: ${spreadsheetId}`);
core.info(`Localization Root: ${localizationRoot}`);
// List the contents of the root folder
listRootFolderContents();
// Write the Google API key to a file
const googleApiKeyFilePath = path.join(__dirname, 'google-api-key.json');
try {
fs.writeFileSync(googleApiKeyFilePath, JSON.stringify(googleApiKeyJson));
core.info(`Google API key file created at: ${googleApiKeyFilePath}`);
} catch (error) {
core.setFailed(`Failed to write Google API key file: ${error.message}`);
process.exit(1);
}
process.env.GOOGLE_APPLICATION_CREDENTIALS = googleApiKeyFilePath;
process.env.SPREADSHEET_ID = spreadsheetId;
process.env.LOCALIZATION_ROOT = localizationRoot;
const cleanUp = () => {
if (fs.existsSync(googleApiKeyFilePath)) {
fs.unlinkSync(googleApiKeyFilePath);
core.info(`Google API key file deleted: ${googleApiKeyFilePath}`);
}
};
const runAction = async () => {
try {
if (action === 'push') {
await pushMain();
} else if (action === 'pull') {
await pullMain();
} else {
core.setFailed(`Unknown action: ${action}`);
process.exit(1);
}
} catch (error) {
core.setFailed(`Action failed with error: ${error.message}`);
process.exit(1);
} finally {
cleanUp();
}
};
runAction();