Skip to content

Commit c87a49d

Browse files
committed
Include scripts in dist folder
1 parent ddcd6d7 commit c87a49d

File tree

9 files changed

+2423
-75
lines changed

9 files changed

+2423
-75
lines changed

.github/workflows/localization-pull-action.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ jobs:
1515
with:
1616
node-version: 20
1717

18-
- name: Install dependencies
19-
run: npm install
20-
2118
- name: Pull Google Sheets to JSON
2219
uses: ./ # Use the local action
2320
with:

.github/workflows/localization-push-action.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ jobs:
1515
with:
1616
node-version: 20
1717

18-
- name: Install dependencies
19-
run: npm install
20-
2118
- name: Push JSON to Google Sheets
2219
uses: ./ # Use the local action
2320
with:

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ Thumbs.db
3232

3333
# Dist directory
3434
dist/
35-
!dist/index.js
35+
!dist/index.js
36+
!dist/scripts/
37+
!dist/scripts/**

dist/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27603,9 +27603,9 @@ process.env.LOCALIZATION_ROOT = localizationRoot;
2760327603

2760427604
try {
2760527605
if (action === 'push') {
27606-
execSync('node ./scripts/push-json-to-google-sheets.js', { stdio: 'inherit' });
27606+
execSync('node ./dist/scripts/push-json-to-google-sheets.js', { stdio: 'inherit' });
2760727607
} else if (action === 'pull') {
27608-
execSync('node ./scripts/pull-google-sheets-to-json.js', { stdio: 'inherit' });
27608+
execSync('node ./dist/scripts/pull-google-sheets-to-json.js', { stdio: 'inherit' });
2760927609
} else {
2761027610
core.setFailed(`Unknown action: ${action}`);
2761127611
process.exit(1);
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const { google } = require('googleapis');
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
const processSheet = async (sheetName, rows, localizationRoot) => {
6+
if (!rows || rows.length === 0) {
7+
console.log(`No data found in sheet: ${sheetName}`);
8+
return;
9+
}
10+
11+
const headers = rows[0];
12+
const languages = headers.slice(1); // Exclude the 'Key' column
13+
const translations = rows.slice(1); // Exclude the header row
14+
15+
// Write translations back into JSON files
16+
const localizationDir = sheetName === 'RootLocalization' ? localizationRoot : `${localizationRoot}/${sheetName}`;
17+
if (!fs.existsSync(localizationDir)) fs.mkdirSync(localizationDir);
18+
19+
const jsonFiles = {};
20+
for (const lang of languages) {
21+
const filePath = `${localizationDir}/${lang}.json`;
22+
if (fs.existsSync(filePath)) {
23+
jsonFiles[lang] = JSON.parse(fs.readFileSync(filePath, 'utf8'));
24+
} else {
25+
jsonFiles[lang] = { Culture: lang, Texts: {} };
26+
}
27+
}
28+
29+
for (const row of translations) {
30+
const key = row[0];
31+
if (!key) continue; // Skip empty keys
32+
for (let i = 1; i < row.length; i++) {
33+
const lang = languages[i - 1];
34+
const value = row[i];
35+
if (value) { // Only add non-empty values
36+
if (jsonFiles[lang].Texts) {
37+
jsonFiles[lang].Texts[key] = value;
38+
} else {
39+
if (!jsonFiles[lang].texts) {
40+
jsonFiles[lang].Texts = {};
41+
} else {
42+
jsonFiles[lang].Texts = jsonFiles[lang].texts;
43+
delete jsonFiles[lang].texts;
44+
}
45+
jsonFiles[lang].Texts[key] = value;
46+
}
47+
}
48+
}
49+
}
50+
51+
// Save each language's JSON file
52+
for (const [lang, data] of Object.entries(jsonFiles)) {
53+
fs.writeFileSync(`${localizationDir}/${lang}.json`, JSON.stringify(data, null, 2));
54+
}
55+
};
56+
57+
const fetchSheetData = async () => {
58+
// Load credentials dynamically from environment variable
59+
const keyFilePath = process.env.GOOGLE_APPLICATION_CREDENTIALS;
60+
const localizationRoot = process.env.LOCALIZATION_ROOT;
61+
62+
if (!keyFilePath || !fs.existsSync(keyFilePath)) {
63+
throw new Error('Google API credentials file not found. Ensure GOOGLE_APPLICATION_CREDENTIALS is set.');
64+
}
65+
66+
if (!localizationRoot || !fs.existsSync(localizationRoot)) {
67+
throw new Error('Localization root folder not found. Ensure LOCALIZATION_ROOT is set.');
68+
}
69+
70+
const auth = new google.auth.GoogleAuth({
71+
keyFile: keyFilePath,
72+
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
73+
});
74+
const sheets = google.sheets({ version: 'v4', auth });
75+
76+
const spreadsheetId = process.env.SPREADSHEET_ID;
77+
78+
// Get the list of sheet names
79+
const sheetInfo = await sheets.spreadsheets.get({
80+
spreadsheetId,
81+
});
82+
const sheetNames = sheetInfo.data.sheets.map(sheet => sheet.properties.title);
83+
84+
for (const sheetName of sheetNames) {
85+
const range = `${sheetName}!A1:Z`; // Adjust range based on data size
86+
87+
// Fetch data from Google Sheets
88+
const response = await sheets.spreadsheets.values.get({
89+
spreadsheetId,
90+
range,
91+
});
92+
93+
const rows = response.data.values;
94+
await processSheet(sheetName, rows, localizationRoot);
95+
}
96+
97+
console.log('Data successfully pulled and JSON files updated');
98+
};
99+
100+
if (require.main === module) {
101+
fetchSheetData();
102+
}
103+
104+
module.exports = { processSheet, fetchSheetData };
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
const { google } = require('googleapis');
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
const parseLocalizationFiles = (folderPath) => {
6+
const files = fs.readdirSync(folderPath).filter(file => file.endsWith('.json'));
7+
8+
if (files.length === 0) {
9+
console.log(`No localization files found in folder: ${folderPath}`);
10+
return { languages: [], translations: {} };
11+
}
12+
13+
const languages = [];
14+
const translations = {};
15+
16+
for (const file of files) {
17+
const lang = path.basename(file, '.json'); // Extract language code from file name
18+
languages.push(lang);
19+
20+
const jsonData = JSON.parse(fs.readFileSync(`${folderPath}/${file}`, 'utf8'));
21+
const texts = jsonData.texts || jsonData.Texts || {}; // Use 'texts' or 'Texts' if they exist, otherwise default to {}
22+
for (const [key, value] of Object.entries(texts)) {
23+
if (!translations[key]) translations[key] = {};
24+
if (value) { // Only add non-empty values
25+
translations[key][lang] = value;
26+
}
27+
}
28+
}
29+
30+
return { languages, translations };
31+
};
32+
33+
const processFolder = async (folderPath, sheetName, sheets, existingSheetNames, spreadsheetId) => {
34+
const { languages, translations } = parseLocalizationFiles(folderPath);
35+
36+
if (languages.length === 0) {
37+
return;
38+
}
39+
40+
// Prepare rows for Google Sheets
41+
const rows = [['Key', ...languages]]; // Header row
42+
for (const [key, values] of Object.entries(translations)) {
43+
const row = [key];
44+
for (const lang of languages) {
45+
row.push(values[lang] || ''); // Add translation or empty if missing
46+
}
47+
rows.push(row);
48+
}
49+
50+
// Create new sheet if it does not exist
51+
if (!existingSheetNames.includes(sheetName)) {
52+
await sheets.spreadsheets.batchUpdate({
53+
spreadsheetId,
54+
resource: {
55+
requests: [
56+
{
57+
addSheet: {
58+
properties: {
59+
title: sheetName,
60+
},
61+
},
62+
},
63+
],
64+
},
65+
});
66+
}
67+
68+
// Push data to Google Sheets
69+
const range = `${sheetName}!A1`;
70+
await sheets.spreadsheets.values.update({
71+
spreadsheetId,
72+
range,
73+
valueInputOption: 'RAW',
74+
resource: { values: rows },
75+
});
76+
};
77+
78+
const main = async () => {
79+
// Load credentials dynamically from environment variable
80+
const keyFilePath = process.env.GOOGLE_APPLICATION_CREDENTIALS;
81+
const localizationRoot = process.env.LOCALIZATION_ROOT;
82+
83+
// Debug logging
84+
console.log('Google API Key File Path:', keyFilePath);
85+
console.log('Localization Root:', localizationRoot);
86+
87+
if (!keyFilePath || !fs.existsSync(keyFilePath)) {
88+
throw new Error('Google API credentials file not found. Ensure GOOGLE_APPLICATION_CREDENTIALS is set.');
89+
}
90+
91+
if (!localizationRoot || !fs.existsSync(localizationRoot)) {
92+
throw new Error('Localization root folder not found. Ensure LOCALIZATION_ROOT is set.');
93+
}
94+
95+
const auth = new google.auth.GoogleAuth({
96+
keyFile: keyFilePath,
97+
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
98+
});
99+
const sheets = google.sheets({ version: 'v4', auth });
100+
101+
const spreadsheetId = process.env.SPREADSHEET_ID;
102+
103+
// Get the list of existing sheet names
104+
const sheetInfo = await sheets.spreadsheets.get({
105+
spreadsheetId,
106+
});
107+
const existingSheetNames = sheetInfo.data.sheets.map(sheet => sheet.properties.title);
108+
109+
// Process the root folder and push data to the RootLocalization sheet if localization files exist
110+
const rootFiles = fs.readdirSync(localizationRoot).filter(file => file.endsWith('.json'));
111+
if (rootFiles.length > 0) {
112+
await processFolder(localizationRoot, 'RootLocalization', sheets, existingSheetNames, spreadsheetId);
113+
}
114+
115+
// Process subfolders
116+
const subfolders = fs.readdirSync(localizationRoot).filter(subfolder => fs.lstatSync(`${localizationRoot}/${subfolder}`).isDirectory());
117+
for (const subfolder of subfolders) {
118+
await processFolder(`${localizationRoot}/${subfolder}`, subfolder, sheets, existingSheetNames, spreadsheetId);
119+
}
120+
121+
console.log('Data successfully uploaded to Google Sheets');
122+
};
123+
124+
if (require.main === module) {
125+
main();
126+
}
127+
128+
module.exports = { processFolder, parseLocalizationFiles };

entrypoint.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ process.env.LOCALIZATION_ROOT = localizationRoot;
4646

4747
try {
4848
if (action === 'push') {
49-
execSync('node ./scripts/push-json-to-google-sheets.js', { stdio: 'inherit' });
49+
execSync('node ./dist/scripts/push-json-to-google-sheets.js', { stdio: 'inherit' });
5050
} else if (action === 'pull') {
51-
execSync('node ./scripts/pull-google-sheets-to-json.js', { stdio: 'inherit' });
51+
execSync('node ./dist/scripts/pull-google-sheets-to-json.js', { stdio: 'inherit' });
5252
} else {
5353
core.setFailed(`Unknown action: ${action}`);
5454
process.exit(1);

0 commit comments

Comments
 (0)