Skip to content

Commit ee1d536

Browse files
committed
feat: update check-locale-changes
1 parent bb737d2 commit ee1d536

File tree

5 files changed

+539
-17
lines changed

5 files changed

+539
-17
lines changed

.github/actions/check-locale-changes/action.yml

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: 'Check Locale Changes'
22
description: 'Check which enabled locales have changes and generate deployment matrix'
33
inputs:
44
trigger-type:
5-
description: 'Type of trigger: "manual", "auto", or "docs-pr"'
5+
description: 'Type of trigger: "manual" or "auto"'
66
required: true
77
manual-locales:
88
description: 'Comma-separated list of locales for manual trigger (optional)'
@@ -11,17 +11,73 @@ inputs:
1111
outputs:
1212
matrix-include:
1313
description: 'JSON array of locales to deploy with their config'
14-
value: ${{ steps.check-locales.outputs.matrix-include }}
14+
value: ${{ steps.generate-matrix.outputs.include }}
1515
has-changes:
1616
description: 'Whether any enabled locales have changes'
17-
value: ${{ steps.check-locales.outputs.has-changes }}
17+
value: ${{ steps.generate-matrix.outputs.has-changes }}
1818

1919
runs:
2020
using: 'composite'
2121
steps:
22-
- name: Check locale changes and generate matrix
23-
id: check-locales
22+
- name: Generate dynamic files config (for auto triggers)
23+
if: inputs.trigger-type == 'auto'
24+
id: generate-files-config
2425
shell: bash
2526
run: |
26-
# Use the unified Node.js script to handle everything
27-
node .github/scripts/check-locale-changes.js "${{ inputs.trigger-type }}" "${{ inputs.manual-locales }}" "true"
27+
# Use the dedicated script to generate files config
28+
files_yaml=$(node .github/scripts/generate-files-config.js)
29+
30+
echo "Generated files_yaml:"
31+
echo "$files_yaml"
32+
33+
# Save to output for next step
34+
{
35+
echo "files_yaml<<EOF"
36+
echo "$files_yaml"
37+
echo "EOF"
38+
} >> $GITHUB_OUTPUT
39+
40+
- name: Get changed files (for auto triggers)
41+
if: inputs.trigger-type == 'auto'
42+
id: changes
43+
uses: tj-actions/changed-files@v41
44+
with:
45+
files_yaml: ${{ steps.generate-files-config.outputs.files_yaml }}
46+
47+
- name: Generate deployment matrix
48+
id: generate-matrix
49+
shell: bash
50+
run: |
51+
# Prepare arguments for the matrix generation script
52+
trigger_type="${{ inputs.trigger-type }}"
53+
manual_locales="${{ inputs.manual-locales }}"
54+
55+
if [ "$trigger_type" == "manual" ]; then
56+
# For manual trigger, we don't need changes JSON
57+
output=$(node .github/scripts/generate-locale-matrix.js "$trigger_type" "$manual_locales")
58+
else
59+
# For auto triggers, create a minimal JSON with only the boolean change indicators
60+
changes_json=$(cat << 'EOF'
61+
{
62+
"core_any_changed": "${{ steps.changes.outputs.core_any_changed }}",
63+
"ar_any_changed": "${{ steps.changes.outputs.ar_any_changed }}",
64+
"de_any_changed": "${{ steps.changes.outputs.de_any_changed }}",
65+
"en_any_changed": "${{ steps.changes.outputs.en_any_changed }}",
66+
"es_any_changed": "${{ steps.changes.outputs.es_any_changed }}",
67+
"fr_any_changed": "${{ steps.changes.outputs.fr_any_changed }}",
68+
"ja_any_changed": "${{ steps.changes.outputs.ja_any_changed }}",
69+
"ru_any_changed": "${{ steps.changes.outputs.ru_any_changed }}",
70+
"zh-hans_any_changed": "${{ steps.changes.outputs.zh-hans_any_changed }}",
71+
"zh-hant_any_changed": "${{ steps.changes.outputs.zh-hant_any_changed }}"
72+
}
73+
EOF
74+
)
75+
76+
temp_file=$(mktemp)
77+
echo "$changes_json" > "$temp_file"
78+
output=$(node .github/scripts/generate-locale-matrix.js "$trigger_type" "" "$temp_file")
79+
rm -f "$temp_file"
80+
fi
81+
82+
# Parse the output (the script outputs two lines: include= and has-changes=)
83+
echo "$output" >> $GITHUB_OUTPUT

.github/scripts/check-locale-changes.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ function usage() {
3838
);
3939
console.log('');
4040
console.log('Arguments:');
41-
console.log(
42-
" trigger-type Type of trigger: 'manual', 'auto', or 'docs-pr'",
43-
);
41+
console.log(" trigger-type Type of trigger: 'manual' or 'auto'");
4442
console.log(
4543
' manual-locales Comma-separated list of locales (optional, for manual trigger)',
4644
);
@@ -280,7 +278,7 @@ function processManualTrigger(localeConfig, manualLocales) {
280278
}
281279

282280
/**
283-
* Process automatic/docs-pr trigger
281+
* Process automatic trigger
284282
*/
285283
function processAutoTrigger(localeConfig, changes) {
286284
let matrixInclude = [];
@@ -398,10 +396,8 @@ function main() {
398396
usage();
399397
}
400398

401-
if (!['manual', 'auto', 'docs-pr'].includes(triggerType)) {
402-
console.error(
403-
"Error: Invalid trigger-type. Must be 'manual', 'auto', or 'docs-pr'",
404-
);
399+
if (!['manual', 'auto'].includes(triggerType)) {
400+
console.error("Error: Invalid trigger-type. Must be 'manual' or 'auto'");
405401
usage();
406402
}
407403

@@ -419,7 +415,7 @@ function main() {
419415
if (triggerType === 'manual') {
420416
result = processManualTrigger(localeConfig, manualLocales);
421417
} else {
422-
// For auto and docs-pr triggers, generate files config and detect changes
418+
// For auto triggers, generate files config and detect changes
423419
filesYaml = generateFilesConfig(localeConfig);
424420
log('Generated files configuration for change detection');
425421

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Script to generate dynamic files configuration for change detection
5+
* Usage: node generate-files-config.js
6+
*/
7+
8+
const fs = require('node:fs');
9+
const path = require('node:path');
10+
11+
// Default values
12+
const SCRIPT_DIR = __dirname;
13+
const ROOT_DIR = path.resolve(SCRIPT_DIR, '../..');
14+
const LOCALE_CONFIG_FILE = path.join(ROOT_DIR, '.github/locales-config.json');
15+
16+
/**
17+
* Log messages with timestamp
18+
* @param {string} message
19+
*/
20+
function log(message) {
21+
const timestamp = new Date().toISOString().replace('T', ' ').substring(0, 19);
22+
console.error(`[${timestamp}] ${message}`);
23+
}
24+
25+
/**
26+
* Remove comments from JSON string
27+
* @param {string} jsonString
28+
* @returns {string}
29+
*/
30+
function stripJsonComments(jsonString) {
31+
// Remove single line comments starting with //
32+
return jsonString.replace(/^\s*\/\/.*$/gm, '');
33+
}
34+
35+
/**
36+
* Validate JSON content
37+
* @param {string} jsonString
38+
* @returns {boolean}
39+
*/
40+
function validateJson(jsonString) {
41+
try {
42+
JSON.parse(stripJsonComments(jsonString));
43+
return true;
44+
} catch (error) {
45+
return false;
46+
}
47+
}
48+
49+
/**
50+
* Main function to generate files configuration
51+
*/
52+
function main() {
53+
try {
54+
// Check if locale config file exists
55+
if (!fs.existsSync(LOCALE_CONFIG_FILE)) {
56+
console.error(
57+
`Error: Locale config file not found: ${LOCALE_CONFIG_FILE}`,
58+
);
59+
process.exit(1);
60+
}
61+
62+
// Read locale config
63+
let localeConfigContent;
64+
try {
65+
localeConfigContent = fs.readFileSync(LOCALE_CONFIG_FILE, 'utf8');
66+
} catch (error) {
67+
console.error('Error: Failed to read locale config file');
68+
process.exit(1);
69+
}
70+
71+
// Validate locale config JSON
72+
if (!validateJson(localeConfigContent)) {
73+
console.error('Error: Invalid JSON in locale config file');
74+
process.exit(1);
75+
}
76+
77+
const localeConfig = JSON.parse(stripJsonComments(localeConfigContent));
78+
79+
log('=== Files Config Generator ===');
80+
log(`Locale config file: ${LOCALE_CONFIG_FILE}`);
81+
82+
// Start with core files config
83+
let filesYaml = `core:
84+
- 'apps/docs/**'
85+
- 'packages/**'
86+
- '!apps/docs/content/**'
87+
- '!apps/docs/messages/**'`;
88+
89+
// Add each locale from config dynamically
90+
const locales = Object.keys(localeConfig);
91+
for (const locale of locales) {
92+
filesYaml += `
93+
${locale}:
94+
- 'apps/docs/content/${locale}/**'
95+
- 'apps/docs/messages/${locale}.json'`;
96+
}
97+
98+
log('Generated files_yaml configuration');
99+
100+
// Output the files_yaml configuration
101+
console.log(filesYaml);
102+
} catch (error) {
103+
console.error(`Error: ${error.message}`);
104+
process.exit(1);
105+
}
106+
}
107+
108+
// Run main function if script is executed directly
109+
if (require.main === module) {
110+
main();
111+
}
112+
113+
module.exports = { main };

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy