import Fuse from 'fuse.js'; import { globSync } from 'glob'; import inquirer from 'inquirer'; import path from 'path'; export async function importCommon( folder: string, name: string, ): Promise<{ filePaths: string[]; associations: { taskId: string; pullerId: string; tableId: string; resultKey: string; } | null; } | null> { let paths = globSync(path.join(folder, `**/*.yaml`), { windowsPathsNoEscape: true }) || []; paths = paths.filter( (v) => !v.endsWith('/variables.yaml') && !v.endsWith('/main.yaml') && !v.endsWith('/output.yaml') && !v.endsWith('/deployment.yaml') && !v.endsWith('Task.yaml') && !v.endsWith('Connection.yaml') && !v.endsWith('Report.yaml') && !v.endsWith('Table.yaml') && !v.endsWith('Schema.yaml'), ); const files = paths.map((p) => { return { path: p, fileName: path.basename(p).replace(/([a-z0-9])([A-Z])/g, '$1 $2'), }; }); const fuse = new Fuse(files, { includeScore: true, keys: [ 'fileName', // will be assigned a `weight` of 1 { name: 'path', weight: 2, }, ], threshold: 0.8, isCaseSensitive: false, ignoreLocation: true, distance: 100, }); const chosenFiles = fuse .search(name) .sort((a: any, b: any) => parseFloat(a.score) - parseFloat(b.score)); if (chosenFiles.length === 0) { console.log(`There is no such component named ${name} in ${folder}`); return null; } if (chosenFiles.length > 1) { const choices = chosenFiles.map((x: any, i: number) => { const p = x.item.path; const base = path.basename(p, '.yaml'); const type = ( base.match( /_(Puller|Pusher|Transformation|Validation|Dependency|Mapping|Webhook)$/i, )?.[1] || 'component' ).toLowerCase(); const prettyName = base .replace( /_(Puller|Pusher|Transformation|Validation|Dependency|Mapping|Webhook)$/i, '', ) .replace(/[_\-]+/g, ' ') .trim(); return { name: `${i + 1}. Type: ${type}\n Name: ${prettyName}\n File Path: ${p}`, value: i, }; }); const answer = await inquirer.prompt<{ value: number[] }>([ { type: 'checkbox', name: 'value', message: 'Please select one or more components below:', choices, validate: (val: number[]) => val && val.length ? true : 'Please select at least one item', }, ]); return { filePaths: (answer.value || []).map((idx) => chosenFiles[idx].item.path), associations: null, }; } return { filePaths: [chosenFiles[0].item.path], associations: null }; }