fix: update yaml-format.js to use dynamic chalk imports
- Convert all functions to async to support chalk ES module import - Replace string.replace with manual regex processing for async formatYamlContent calls - This resolves the ERR_REQUIRE_ESM error in GitHub Actions format step 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -4,14 +4,24 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const yaml = require('js-yaml');
|
const yaml = require('js-yaml');
|
||||||
const { execSync } = require('child_process');
|
const { execSync } = require('child_process');
|
||||||
const chalk = require('chalk');
|
|
||||||
|
// Dynamic import for ES module
|
||||||
|
let chalk;
|
||||||
|
|
||||||
|
// Initialize ES modules
|
||||||
|
async function initializeModules() {
|
||||||
|
if (!chalk) {
|
||||||
|
chalk = (await import('chalk')).default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* YAML Formatter and Linter for BMAD-METHOD
|
* YAML Formatter and Linter for BMAD-METHOD
|
||||||
* Formats and validates YAML files and YAML embedded in Markdown
|
* Formats and validates YAML files and YAML embedded in Markdown
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function formatYamlContent(content, filename) {
|
async function formatYamlContent(content, filename) {
|
||||||
|
await initializeModules();
|
||||||
try {
|
try {
|
||||||
// First try to fix common YAML issues
|
// First try to fix common YAML issues
|
||||||
let fixedContent = content
|
let fixedContent = content
|
||||||
@@ -62,7 +72,8 @@ function formatYamlContent(content, filename) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function processMarkdownFile(filePath) {
|
async function processMarkdownFile(filePath) {
|
||||||
|
await initializeModules();
|
||||||
const content = fs.readFileSync(filePath, 'utf8');
|
const content = fs.readFileSync(filePath, 'utf8');
|
||||||
let modified = false;
|
let modified = false;
|
||||||
let newContent = content;
|
let newContent = content;
|
||||||
@@ -77,22 +88,34 @@ function processMarkdownFile(filePath) {
|
|||||||
|
|
||||||
// Find YAML code blocks
|
// Find YAML code blocks
|
||||||
const yamlBlockRegex = /```ya?ml\n([\s\S]*?)\n```/g;
|
const yamlBlockRegex = /```ya?ml\n([\s\S]*?)\n```/g;
|
||||||
|
let match;
|
||||||
|
const replacements = [];
|
||||||
|
|
||||||
newContent = newContent.replace(yamlBlockRegex, (match, yamlContent) => {
|
while ((match = yamlBlockRegex.exec(newContent)) !== null) {
|
||||||
const formatted = formatYamlContent(yamlContent, filePath);
|
const [fullMatch, yamlContent] = match;
|
||||||
if (formatted === null) {
|
const formatted = await formatYamlContent(yamlContent, filePath);
|
||||||
return match; // Keep original if parsing failed
|
if (formatted !== null) {
|
||||||
|
// Remove trailing newline that js-yaml adds
|
||||||
|
const trimmedFormatted = formatted.replace(/\n$/, '');
|
||||||
|
|
||||||
|
if (trimmedFormatted !== yamlContent) {
|
||||||
|
modified = true;
|
||||||
|
console.log(chalk.green(`✓ Formatted YAML in ${filePath}`));
|
||||||
|
}
|
||||||
|
|
||||||
|
replacements.push({
|
||||||
|
start: match.index,
|
||||||
|
end: match.index + fullMatch.length,
|
||||||
|
replacement: `\`\`\`yaml\n${trimmedFormatted}\n\`\`\``
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Remove trailing newline that js-yaml adds
|
|
||||||
const trimmedFormatted = formatted.replace(/\n$/, '');
|
// Apply replacements in reverse order to maintain indices
|
||||||
|
for (let i = replacements.length - 1; i >= 0; i--) {
|
||||||
if (trimmedFormatted !== yamlContent) {
|
const { start, end, replacement } = replacements[i];
|
||||||
modified = true;
|
newContent = newContent.slice(0, start) + replacement + newContent.slice(end);
|
||||||
}
|
}
|
||||||
|
|
||||||
return `\`\`\`yml\n${trimmedFormatted}\n\`\`\``;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (modified) {
|
if (modified) {
|
||||||
fs.writeFileSync(filePath, newContent);
|
fs.writeFileSync(filePath, newContent);
|
||||||
@@ -101,9 +124,10 @@ function processMarkdownFile(filePath) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function processYamlFile(filePath) {
|
async function processYamlFile(filePath) {
|
||||||
|
await initializeModules();
|
||||||
const content = fs.readFileSync(filePath, 'utf8');
|
const content = fs.readFileSync(filePath, 'utf8');
|
||||||
const formatted = formatYamlContent(content, filePath);
|
const formatted = await formatYamlContent(content, filePath);
|
||||||
|
|
||||||
if (formatted === null) {
|
if (formatted === null) {
|
||||||
return false; // Syntax error
|
return false; // Syntax error
|
||||||
@@ -116,7 +140,8 @@ function processYamlFile(filePath) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function lintYamlFile(filePath) {
|
async function lintYamlFile(filePath) {
|
||||||
|
await initializeModules();
|
||||||
try {
|
try {
|
||||||
// Use yaml-lint for additional validation
|
// Use yaml-lint for additional validation
|
||||||
execSync(`npx yaml-lint "${filePath}"`, { stdio: 'pipe' });
|
execSync(`npx yaml-lint "${filePath}"`, { stdio: 'pipe' });
|
||||||
@@ -128,7 +153,8 @@ function lintYamlFile(filePath) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function main() {
|
async function main() {
|
||||||
|
await initializeModules();
|
||||||
const args = process.argv.slice(2);
|
const args = process.argv.slice(2);
|
||||||
const glob = require('glob');
|
const glob = require('glob');
|
||||||
|
|
||||||
@@ -170,13 +196,13 @@ function main() {
|
|||||||
try {
|
try {
|
||||||
let changed = false;
|
let changed = false;
|
||||||
if (ext === '.md') {
|
if (ext === '.md') {
|
||||||
changed = processMarkdownFile(filePath);
|
changed = await processMarkdownFile(filePath);
|
||||||
} else if (ext === '.yml' || ext === '.yaml' || basename.includes('roomodes') || basename.includes('.yml') || basename.includes('.yaml')) {
|
} else if (ext === '.yml' || ext === '.yaml' || basename.includes('roomodes') || basename.includes('.yml') || basename.includes('.yaml')) {
|
||||||
// Handle YAML files and special cases like .roomodes
|
// Handle YAML files and special cases like .roomodes
|
||||||
changed = processYamlFile(filePath);
|
changed = await processYamlFile(filePath);
|
||||||
|
|
||||||
// Also run linting
|
// Also run linting
|
||||||
const lintPassed = lintYamlFile(filePath);
|
const lintPassed = await lintYamlFile(filePath);
|
||||||
if (!lintPassed) hasErrors = true;
|
if (!lintPassed) hasErrors = true;
|
||||||
} else {
|
} else {
|
||||||
// Skip silently for unsupported files
|
// Skip silently for unsupported files
|
||||||
@@ -205,7 +231,10 @@ function main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (require.main === module) {
|
if (require.main === module) {
|
||||||
main();
|
main().catch(error => {
|
||||||
|
console.error('Error:', error);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { formatYamlContent, processMarkdownFile, processYamlFile };
|
module.exports = { formatYamlContent, processMarkdownFile, processYamlFile };
|
||||||
Reference in New Issue
Block a user