fixes on nodejs script

- add missing headers generation
- sort variables (for testing, may be disabled)
- skip environments when no file is actually present
This commit is contained in:
2026-07-02 16:48:39 +02:00
parent 650d62a29c
commit cb6d7d91a5
+34 -8
View File
@@ -6,7 +6,7 @@ import stripJsonComments from 'strip-json-comments';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
const interpolationVariableRegex = /^{{(.*?)}}$/ const interpolationVariableRegex = /^{{(.*?)}}$/
const DEFAULT_ENV_VAR_VALUE = 'EDIT_VALUE_HERE' const DEFAULT_VAR_VALUE = 'EDIT_VALUE_HERE'
const VARIABLE_NAME_VALUE_SEPARATOR = '=' const VARIABLE_NAME_VALUE_SEPARATOR = '='
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
@@ -221,6 +221,22 @@ export function mergeRequestConfig(base, updates) {
delete merged.auth; delete merged.auth;
} }
if (Object.prototype.hasOwnProperty.call(updates, 'headers')) {
const headers = [...(Array.isArray(base.headers) ? base.headers : [])];
const byName = new Map();
for (const header of headers) {
if (header && header.name) {
byName.set(String(header.name), header);
}
}
for (const header of updates.headers) {
if (header && header.name) {
byName.set(String(header.name), header);
}
}
merged.headers = [...byName.values()];
}
if (Array.isArray(updates.variables)) { if (Array.isArray(updates.variables)) {
const variables = [...(Array.isArray(base.variables) ? base.variables : [])]; const variables = [...(Array.isArray(base.variables) ? base.variables : [])];
const byName = new Map(); const byName = new Map();
@@ -352,7 +368,7 @@ export function buildRequestContent(request, requestName, requestConfig = {}, do
commentedVariableDefinitions.push({ name: normalized, value }); commentedVariableDefinitions.push({ name: normalized, value });
}; };
const addReferencedVariables = (value, fallbackValue = 'YOUR_VALUE_HERE') => { const addReferencedVariables = (value, fallbackValue = DEFAULT_VAR_VALUE) => {
for (const placeholder of collectPlaceholders(String(value))) { for (const placeholder of collectPlaceholders(String(value))) {
addVariable(placeholder, fallbackValue); addVariable(placeholder, fallbackValue);
} }
@@ -479,14 +495,20 @@ export function buildRequestContent(request, requestName, requestConfig = {}, do
if (commentedVariableDefinitions.length > 0) { if (commentedVariableDefinitions.length > 0) {
lines.push(`# Other variables for ${requestName}`); lines.push(`# Other variables for ${requestName}`);
for (const variable of commentedVariableDefinitions) { for (const variable of commentedVariableDefinitions.sort((a, b) => {
const nameComparison = a.name.localeCompare(b.name);
return nameComparison !== 0 ? nameComparison : a.value.localeCompare(b.value);
})) {
lines.push(`# @${sanitizeVarName(variable.name)}${VARIABLE_NAME_VALUE_SEPARATOR}${formatVariableValue(variable.value, { renderValue })}`); lines.push(`# @${sanitizeVarName(variable.name)}${VARIABLE_NAME_VALUE_SEPARATOR}${formatVariableValue(variable.value, { renderValue })}`);
} }
} }
if (parameterVariableDefinitions.length > 0) { if (parameterVariableDefinitions.length > 0) {
lines.push(`# Parameter variables for ${requestName}`); lines.push(`# Parameter variables for ${requestName}`);
for (const variable of parameterVariableDefinitions) { for (const variable of parameterVariableDefinitions.sort((a, b) => {
const nameComparison = a.name.localeCompare(b.name);
return nameComparison !== 0 ? nameComparison : a.value.localeCompare(b.value);
})) {
lines.push(`@${sanitizeVarName(variable.name)}${VARIABLE_NAME_VALUE_SEPARATOR}${formatVariableValue(variable.value, { renderValue })}`); lines.push(`@${sanitizeVarName(variable.name)}${VARIABLE_NAME_VALUE_SEPARATOR}${formatVariableValue(variable.value, { renderValue })}`);
} }
} }
@@ -523,7 +545,10 @@ export function buildRequestContent(request, requestName, requestConfig = {}, do
if (variableDefinitions.length > 0) { if (variableDefinitions.length > 0) {
lines.push(`# Variables for ${requestName}`); lines.push(`# Variables for ${requestName}`);
for (const variable of variableDefinitions) { for (const variable of variableDefinitions.sort((a, b) => {
const nameComparison = a.name.localeCompare(b.name);
return nameComparison !== 0 ? nameComparison : a.value.localeCompare(b.value);
})) {
lines.push(`@${sanitizeVarName(variable.name)}${VARIABLE_NAME_VALUE_SEPARATOR}${formatVariableValue(variable.value, { renderValue })}`); lines.push(`@${sanitizeVarName(variable.name)}${VARIABLE_NAME_VALUE_SEPARATOR}${formatVariableValue(variable.value, { renderValue })}`);
} }
} }
@@ -598,7 +623,8 @@ function writeEnvironmentTemplates(sourceDir, outputRoot) {
const visit = (currentDir) => { const visit = (currentDir) => {
const entries = fs.readdirSync(currentDir, { withFileTypes: true }); const entries = fs.readdirSync(currentDir, { withFileTypes: true });
const hasEnvironmentsDir = entries.some((entry) => entry.isDirectory() && entry.name === 'environments'); const hasEnvironmentsDir = entries.some((entry) => entry.isDirectory() && entry.name === 'environments' &&
fs.readdirSync(path.join(currentDir, 'environments'), { withFileTypes: true }).some((envEntry) => envEntry.isFile() && /\.ya?ml$/i.test(envEntry.name)));
if (hasEnvironmentsDir) { if (hasEnvironmentsDir) {
targets.push(currentDir); targets.push(currentDir);
@@ -647,7 +673,7 @@ function writeEnvironmentTemplates(sourceDir, outputRoot) {
} }
} }
const templateContent = variableNames.length > 0 ? `${variableNames.map(v => `${v}=${DEFAULT_ENV_VAR_VALUE}`).join('\n')}\n` : ''; const templateContent = variableNames.length > 0 ? `${variableNames.map(v => `${v}=${DEFAULT_VAR_VALUE}`).join('\n')}\n` : '';
fs.writeFileSync(path.join(targetDir, '.env.template'), templateContent, 'utf8'); fs.writeFileSync(path.join(targetDir, '.env.template'), templateContent, 'utf8');
dotenvVariablesByTarget.set(targetDir, new Set(variableNames)); dotenvVariablesByTarget.set(targetDir, new Set(variableNames));
} }
@@ -761,7 +787,7 @@ function main() {
throw new Error('No collections found in workspace.yml'); throw new Error('No collections found in workspace.yml');
} }
const outputBaseRoot = path.join(workspaceRoot, 'autogen', 'httpyac'); const outputBaseRoot = path.join(workspaceRoot, 'autogen', 'httpyac_node');
cleanFolder(outputBaseRoot); cleanFolder(outputBaseRoot);
for (const collection of collections) { for (const collection of collections) {