import fs from 'fs'; import path from 'path'; import { globSync } from 'glob'; import { stringify } from 'yaml'; const BASE_FILES = ['main.yaml', 'ObjectAssociations.yaml', 'variables.yaml']; function ensureBaseFiles(sourceRoot: string, destRoot: string) { const fallbacks: Record = { 'main.yaml': { externals: [] }, 'ObjectAssociations.yaml': {}, 'variables.yaml': { variables: {} }, }; if (!fs.existsSync(destRoot)) fs.mkdirSync(destRoot, { recursive: true }); for (const f of BASE_FILES) { const dst = path.join(destRoot, f); if (fs.existsSync(dst)) continue; const src = path.join(sourceRoot, f); if (fs.existsSync(src)) fs.copyFileSync(src, dst); else fs.writeFileSync(dst, stringify(fallbacks[f], { lineWidth: 0 })); } } export async function includeAll(folder: string) { const resolved = path.resolve(folder); if (!fs.existsSync(resolved)) { return { filePaths: [], associations: null }; } const stat = fs.statSync(resolved); const isFile = stat.isFile(); // Find source root (folder that contains variables.yaml) const variablePaths = (isFile ? globSync(path.join(path.dirname(resolved), `**/variables.yaml`), { windowsPathsNoEscape: true, }) : globSync(path.join(resolved, `**/variables.yaml`), { windowsPathsNoEscape: true, })) || []; if (!variablePaths.length) { console.error( `Please choose a folder path that contains the variables.yaml file, e.g.: ~/workspaces/shopify/partials`, ); return { filePaths: [], associations: null }; } const sourceRoot = path.dirname(variablePaths[0]); // Ensure base files exist in the current project (if missing) // (We do NOT include these files as externals) // eslint-disable-next-line @typescript-eslint/no-var-requires const { getProjectPath } = require('../../../helpers/cluserHelper'); ensureBaseFiles(sourceRoot, getProjectPath()); const filePaths = isFile ? [resolved] : globSync(path.join(resolved, `**/*.yaml`), { windowsPathsNoEscape: true, }) || []; return { filePaths: filePaths.filter((p) => !BASE_FILES.includes(path.basename(p))), associations: null, }; }