@el-j/google-sheet-translations 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +258 -0
  3. package/dist/constants.d.ts +6 -0
  4. package/dist/constants.d.ts.map +1 -0
  5. package/dist/constants.js +9 -0
  6. package/dist/constants.js.map +1 -0
  7. package/dist/getSpreadSheetData.d.ts +7 -0
  8. package/dist/getSpreadSheetData.d.ts.map +1 -0
  9. package/dist/getSpreadSheetData.js +121 -0
  10. package/dist/getSpreadSheetData.js.map +1 -0
  11. package/dist/index.d.ts +18 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +34 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/types.d.ts +22 -0
  16. package/dist/types.d.ts.map +1 -0
  17. package/dist/types.js +3 -0
  18. package/dist/types.js.map +1 -0
  19. package/dist/utils/auth.d.ts +8 -0
  20. package/dist/utils/auth.d.ts.map +1 -0
  21. package/dist/utils/auth.js +19 -0
  22. package/dist/utils/auth.js.map +1 -0
  23. package/dist/utils/configurationHandler.d.ts +29 -0
  24. package/dist/utils/configurationHandler.d.ts.map +1 -0
  25. package/dist/utils/configurationHandler.js +23 -0
  26. package/dist/utils/configurationHandler.js.map +1 -0
  27. package/dist/utils/dataConverter/convertFromDataJsonFormat.d.ts +8 -0
  28. package/dist/utils/dataConverter/convertFromDataJsonFormat.d.ts.map +1 -0
  29. package/dist/utils/dataConverter/convertFromDataJsonFormat.js +47 -0
  30. package/dist/utils/dataConverter/convertFromDataJsonFormat.js.map +1 -0
  31. package/dist/utils/dataConverter/convertToDataJsonFormat.d.ts +10 -0
  32. package/dist/utils/dataConverter/convertToDataJsonFormat.d.ts.map +1 -0
  33. package/dist/utils/dataConverter/convertToDataJsonFormat.js +51 -0
  34. package/dist/utils/dataConverter/convertToDataJsonFormat.js.map +1 -0
  35. package/dist/utils/dataConverter/findLocalChanges.d.ts +9 -0
  36. package/dist/utils/dataConverter/findLocalChanges.d.ts.map +1 -0
  37. package/dist/utils/dataConverter/findLocalChanges.js +39 -0
  38. package/dist/utils/dataConverter/findLocalChanges.js.map +1 -0
  39. package/dist/utils/fileWriter.d.ts +23 -0
  40. package/dist/utils/fileWriter.d.ts.map +1 -0
  41. package/dist/utils/fileWriter.js +119 -0
  42. package/dist/utils/fileWriter.js.map +1 -0
  43. package/dist/utils/getFileLastModified.d.ts +7 -0
  44. package/dist/utils/getFileLastModified.d.ts.map +1 -0
  45. package/dist/utils/getFileLastModified.js +23 -0
  46. package/dist/utils/getFileLastModified.js.map +1 -0
  47. package/dist/utils/isDataJsonNewer.d.ts +8 -0
  48. package/dist/utils/isDataJsonNewer.d.ts.map +1 -0
  49. package/dist/utils/isDataJsonNewer.js +40 -0
  50. package/dist/utils/isDataJsonNewer.js.map +1 -0
  51. package/dist/utils/localeFilter.d.ts +17 -0
  52. package/dist/utils/localeFilter.d.ts.map +1 -0
  53. package/dist/utils/localeFilter.js +49 -0
  54. package/dist/utils/localeFilter.js.map +1 -0
  55. package/dist/utils/localeNormalizer.d.ts +37 -0
  56. package/dist/utils/localeNormalizer.d.ts.map +1 -0
  57. package/dist/utils/localeNormalizer.js +145 -0
  58. package/dist/utils/localeNormalizer.js.map +1 -0
  59. package/dist/utils/readDataJson.d.ts +8 -0
  60. package/dist/utils/readDataJson.d.ts.map +1 -0
  61. package/dist/utils/readDataJson.js +29 -0
  62. package/dist/utils/readDataJson.js.map +1 -0
  63. package/dist/utils/sheetProcessor.d.ts +22 -0
  64. package/dist/utils/sheetProcessor.d.ts.map +1 -0
  65. package/dist/utils/sheetProcessor.js +86 -0
  66. package/dist/utils/sheetProcessor.js.map +1 -0
  67. package/dist/utils/spreadsheetUpdater.d.ts +25 -0
  68. package/dist/utils/spreadsheetUpdater.d.ts.map +1 -0
  69. package/dist/utils/spreadsheetUpdater.js +196 -0
  70. package/dist/utils/spreadsheetUpdater.js.map +1 -0
  71. package/dist/utils/syncManager.d.ts +23 -0
  72. package/dist/utils/syncManager.d.ts.map +1 -0
  73. package/dist/utils/syncManager.js +62 -0
  74. package/dist/utils/syncManager.js.map +1 -0
  75. package/dist/utils/validateEnv.d.ts +9 -0
  76. package/dist/utils/validateEnv.d.ts.map +1 -0
  77. package/dist/utils/validateEnv.js +29 -0
  78. package/dist/utils/validateEnv.js.map +1 -0
  79. package/dist/utils/wait.d.ts +9 -0
  80. package/dist/utils/wait.d.ts.map +1 -0
  81. package/dist/utils/wait.js +21 -0
  82. package/dist/utils/wait.js.map +1 -0
  83. package/package.json +91 -0
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleBidirectionalSync = handleBidirectionalSync;
4
+ const findLocalChanges_1 = require("./dataConverter/findLocalChanges");
5
+ const spreadsheetUpdater_1 = require("./spreadsheetUpdater");
6
+ const isDataJsonNewer_1 = require("./isDataJsonNewer");
7
+ const readDataJson_1 = require("./readDataJson");
8
+ /**
9
+ * Checks if local changes need to be synced to the spreadsheet and performs the sync if needed
10
+ * @param doc Google Spreadsheet document
11
+ * @param dataJsonPath Path to the languageData.json file
12
+ * @param translationsOutputDir Directory containing translation output files
13
+ * @param syncLocalChanges Whether sync is enabled
14
+ * @param autoTranslate Whether to auto-generate Google Translate formulas
15
+ * @param spreadsheetData Current data from the spreadsheet
16
+ * @param waitSeconds Time to wait between API calls
17
+ * @param localeMapping Mapping from normalized locale codes to original spreadsheet headers
18
+ * @returns Sync operation result
19
+ */
20
+ async function handleBidirectionalSync(doc, dataJsonPath, translationsOutputDir, syncLocalChanges, autoTranslate, spreadsheetData, waitSeconds, localeMapping = {}) {
21
+ const result = {
22
+ shouldRefresh: false,
23
+ hasChanges: false
24
+ };
25
+ // Check if languageData.json exists and read it
26
+ const localData = (0, readDataJson_1.readDataJson)(dataJsonPath);
27
+ const dataJsonExists = localData !== null;
28
+ // Check if we need to sync local changes to the spreadsheet
29
+ const shouldSyncToSheet = syncLocalChanges &&
30
+ dataJsonExists &&
31
+ (0, isDataJsonNewer_1.isDataJsonNewer)(dataJsonPath, translationsOutputDir);
32
+ if (!shouldSyncToSheet || !localData) {
33
+ return result;
34
+ }
35
+ console.log("Local languageData.json is newer than translation files. Checking for changes...");
36
+ // Find differences between local data and spreadsheet data
37
+ const changes = (0, findLocalChanges_1.findLocalChanges)(localData, spreadsheetData);
38
+ // Check if there are any actual changes
39
+ const hasChanges = Object.keys(changes).length > 0 &&
40
+ Object.keys(changes).some(locale => Object.keys(changes[locale]).length > 0);
41
+ if (!hasChanges) {
42
+ console.log("No local changes found that need to be synced to the spreadsheet.");
43
+ return result;
44
+ }
45
+ const localesCount = Object.keys(changes).length;
46
+ const keysCount = Object.values(changes)
47
+ .flatMap(l => Object.values(l))
48
+ .flatMap(s => Object.keys(s)).length;
49
+ console.log(`Found local changes: ${localesCount} locale(s), ~${keysCount} key(s) to sync to the spreadsheet.`);
50
+ // Update the spreadsheet with the changes, passing the autoTranslate option and locale mapping
51
+ try {
52
+ await (0, spreadsheetUpdater_1.updateSpreadsheetWithLocalChanges)(doc, changes, waitSeconds, autoTranslate, localeMapping);
53
+ result.shouldRefresh = true;
54
+ result.hasChanges = true;
55
+ }
56
+ catch (err) {
57
+ console.error("Failed to sync local changes to spreadsheet:", err);
58
+ // Do not set shouldRefresh; return unchanged result to avoid stale refresh loop
59
+ }
60
+ return result;
61
+ }
62
+ //# sourceMappingURL=syncManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"syncManager.js","sourceRoot":"","sources":["../../src/utils/syncManager.ts"],"names":[],"mappings":";;AA2BA,0DA6DC;AAtFD,uEAAoE;AACpE,6DAAyE;AACzE,uDAAoD;AACpD,iDAA8C;AAU9C;;;;;;;;;;;GAWG;AACI,KAAK,UAAU,uBAAuB,CAC5C,GAAsB,EACtB,YAAoB,EACpB,qBAA6B,EAC7B,gBAAyB,EACzB,aAAsB,EACtB,eAAgC,EAChC,WAAmB,EACnB,gBAAw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C,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IACtC,OAAO,CAAC,GAAG,CAAC,wBAAwB,YAAY,gBAAgB,SAAS,qCAAqC,CAAC,CAAC;IAEhH,+FAA+F;IAC/F,IAAI,CAAC;QACJ,MAAM,IAAA,sDAAiC,EAAC,GAAG,EAAE,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,aAAa,CAAC,CAAC;QACjG,MAAM,CAAC,aAAa,GAAG,IAAI,CAAC;QAC5B,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC;IAC1B,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACd,OAAO,CAAC,KAAK,CAAC,8CAA8C,EAAE,GAAG,CAAC,CAAC;QACnE,gFAAgF;IACjF,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { GoogleEnvVars } from "../types";
2
+ /**
3
+ * Validates required environment variables for Google Sheets API
4
+ * @returns Object with validated environment variables
5
+ * @throws Error if any required environment variable is missing
6
+ */
7
+ export declare function validateEnv(): GoogleEnvVars;
8
+ export default validateEnv;
9
+ //# sourceMappingURL=validateEnv.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateEnv.d.ts","sourceRoot":"","sources":["../../src/utils/validateEnv.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE9C;;;;GAIG;AACH,wBAAgB,WAAW,IAAI,aAAa,CAuB3C;AAED,eAAe,WAAW,CAAC"}
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateEnv = validateEnv;
4
+ /**
5
+ * Validates required environment variables for Google Sheets API
6
+ * @returns Object with validated environment variables
7
+ * @throws Error if any required environment variable is missing
8
+ */
9
+ function validateEnv() {
10
+ // Check for required environment variables
11
+ const requiredVars = [
12
+ "GOOGLE_CLIENT_EMAIL",
13
+ "GOOGLE_PRIVATE_KEY",
14
+ "GOOGLE_SPREADSHEET_ID",
15
+ ];
16
+ const missingVars = requiredVars.filter((varName) => !process.env[varName]);
17
+ if (missingVars.length > 0) {
18
+ throw new Error(`Missing required environment variables: ${missingVars.join(", ")}\n
19
+ \nMake sure these are set in your .env file or environment.`);
20
+ }
21
+ // Return validated environment variables
22
+ return {
23
+ GOOGLE_CLIENT_EMAIL: process.env.GOOGLE_CLIENT_EMAIL,
24
+ GOOGLE_PRIVATE_KEY: process.env.GOOGLE_PRIVATE_KEY,
25
+ GOOGLE_SPREADSHEET_ID: process.env.GOOGLE_SPREADSHEET_ID,
26
+ };
27
+ }
28
+ exports.default = validateEnv;
29
+ //# sourceMappingURL=validateEnv.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateEnv.js","sourceRoot":"","sources":["../../src/utils/validateEnv.ts"],"names":[],"mappings":";;AAOA,kCAuBC;AA5BD;;;;GAIG;AACH,SAAgB,WAAW;IAC1B,2CAA2C;IAC3C,MAAM,YAAY,GAA+B;QAChD,qBAAqB;QACrB,oBAAoB;QACpB,uBAAuB;KACvB,CAAC;IAEF,MAAM,WAAW,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC;IAE5E,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC5B,MAAM,IAAI,KAAK,CACd,2CAA2C,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC;4DACR,CACzD,CAAC;IACH,CAAC;IAED,yCAAyC;IACzC,OAAO;QACN,mBAAmB,EAAE,OAAO,CAAC,GAAG,CAAC,mBAA6B;QAC9D,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,kBAA4B;QAC5D,qBAAqB,EAAE,OAAO,CAAC,GAAG,CAAC,qBAA+B;KAClE,CAAC;AACH,CAAC;AAED,kBAAe,WAAW,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Creates a promise that resolves after a specified number of seconds
3
+ * @param seconds - The number of seconds to wait
4
+ * @param reason - A description of why we're waiting
5
+ * @returns A promise that resolves after the specified delay
6
+ */
7
+ export declare function wait(seconds: number, reason: string): Promise<void>;
8
+ export default wait;
9
+ //# sourceMappingURL=wait.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wait.d.ts","sourceRoot":"","sources":["../../src/utils/wait.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAUnE;AAED,eAAe,IAAI,CAAC"}
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.wait = wait;
4
+ /**
5
+ * Creates a promise that resolves after a specified number of seconds
6
+ * @param seconds - The number of seconds to wait
7
+ * @param reason - A description of why we're waiting
8
+ * @returns A promise that resolves after the specified delay
9
+ */
10
+ function wait(seconds, reason) {
11
+ console.log("wait", seconds, reason);
12
+ return new Promise((resolve) => {
13
+ const timeoutId = setTimeout(() => {
14
+ resolve();
15
+ }, seconds * 1000);
16
+ // This ensures the timeout is properly cleared if the promise is garbage collected
17
+ timeoutId.unref?.();
18
+ });
19
+ }
20
+ exports.default = wait;
21
+ //# sourceMappingURL=wait.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wait.js","sourceRoot":"","sources":["../../src/utils/wait.ts"],"names":[],"mappings":";;AAMA,oBAUC;AAhBD;;;;;GAKG;AACH,SAAgB,IAAI,CAAC,OAAe,EAAE,MAAc;IACnD,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IACrC,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;QACpC,MAAM,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE;YACjC,OAAO,EAAE,CAAC;QACX,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC,CAAC;QAEnB,mFAAmF;QACnF,SAAS,CAAC,KAAK,EAAE,EAAE,CAAC;IACrB,CAAC,CAAC,CAAC;AACJ,CAAC;AAED,kBAAe,IAAI,CAAC"}
package/package.json ADDED
@@ -0,0 +1,91 @@
1
+ {
2
+ "name": "@el-j/google-sheet-translations",
3
+ "version": "1.0.0",
4
+ "description": "A package to manage translations stored in Google Spreadsheets",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "require": "./dist/index.js",
10
+ "types": "./dist/index.d.ts"
11
+ }
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "LICENSE",
19
+ "README.md"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsc",
23
+ "clean": "rimraf dist",
24
+ "prebuild": "npm run clean",
25
+ "prepare": "npm run build",
26
+ "test": "jest",
27
+ "lint": "eslint src/ --ext .ts --max-warnings 0",
28
+ "dev": "tsc --watch",
29
+ "release": "semantic-release",
30
+ "release:dry-run": "semantic-release --dry-run",
31
+ "docs:dev": "vitepress dev website",
32
+ "docs:build": "vitepress build website",
33
+ "docs:preview": "vitepress preview website"
34
+ },
35
+ "keywords": [
36
+ "google-sheets",
37
+ "translations",
38
+ "i18n",
39
+ "nextjs",
40
+ "typescript",
41
+ "auto-translation",
42
+ "machine-translation",
43
+ "google-translate",
44
+ "localization",
45
+ "internationalization",
46
+ "spreadsheet",
47
+ "google-api",
48
+ "google-spreadsheet"
49
+ ],
50
+ "author": "el-j",
51
+ "license": "MIT",
52
+ "dependencies": {
53
+ "google-auth-library": "^10.1.0",
54
+ "google-spreadsheet": "^4.1.5"
55
+ },
56
+ "peerDependencies": {
57
+ "typescript": ">=4.0.0"
58
+ },
59
+ "devDependencies": {
60
+ "@eslint/js": "^10.0.1",
61
+ "@semantic-release/changelog": "^6.0.3",
62
+ "@semantic-release/commit-analyzer": "^13.0.1",
63
+ "@semantic-release/git": "^10.0.1",
64
+ "@semantic-release/github": "^12.0.6",
65
+ "@semantic-release/npm": "^13.1.5",
66
+ "@semantic-release/release-notes-generator": "^14.1.0",
67
+ "@types/jest": "^30.0.0",
68
+ "@types/node": "^24.0.15",
69
+ "@typescript-eslint/eslint-plugin": "^8.56.1",
70
+ "@typescript-eslint/parser": "^8.56.1",
71
+ "conventional-changelog-conventionalcommits": "^9.3.0",
72
+ "dotenv": "17.2.0",
73
+ "eslint": "^10.0.3",
74
+ "jest": "^30.0.4",
75
+ "jest-mock-extended": "^4.0.0",
76
+ "rimraf": "^6.0.1",
77
+ "semantic-release": "^25.0.3",
78
+ "ts-jest": "^29.4.0",
79
+ "ts-node": "^10.9.2",
80
+ "typescript": "^5.8.3",
81
+ "vitepress": "^1.6.4"
82
+ },
83
+ "engines": {
84
+ "node": ">=14.18.0"
85
+ },
86
+ "overrides": {
87
+ "google-spreadsheet": {
88
+ "google-auth-library": "^10.1.0"
89
+ }
90
+ }
91
+ }