From 971b832d9c5e8ee465e964229b5abb297f132eab Mon Sep 17 00:00:00 2001 From: Harsh Gupta Date: Mon, 5 Feb 2024 10:31:28 -0800 Subject: [PATCH] Auto Sync Registry if no codemod available (#195) Co-authored-by: Harsh --- src/executeMainThread.ts | 59 +++++++++++++++++++++---------------- src/handleListCliCommand.ts | 17 +++++++++-- src/messages.ts | 8 ++++- 3 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/executeMainThread.ts b/src/executeMainThread.ts index 9412cb5..33fc066 100644 --- a/src/executeMainThread.ts +++ b/src/executeMainThread.ts @@ -1,7 +1,7 @@ import * as readline from 'node:readline'; import yargs from 'yargs'; import { hideBin } from 'yargs/helpers'; -import { handleListNamesCommand } from './handleListCliCommand.js'; +import { handleListNamesAfterSyncing } from './handleListCliCommand.js'; import { CodemodDownloader } from './downloadCodemod.js'; import { Printer } from './printer.js'; import { handleLearnCliCommand } from './handleLearnCliCommand.js'; @@ -110,7 +110,6 @@ export const executeMainThread = async () => { } const argv = await Promise.resolve(argvObject.argv); - const fetchBuffer = async (url: string) => { const { data } = await Axios.get(url, { responseType: 'arraybuffer', @@ -132,6 +131,8 @@ export const executeMainThread = async () => { let telemetryService; let exit = () => {}; + const tarService = new TarService(fs as unknown as IFs); + if (!argv.telemetryDisable) { // hack to prevent appInsights from trying to read applicationinsights.json // this env should be set before appinsights is imported @@ -156,7 +157,7 @@ export const executeMainThread = async () => { if (String(argv._) === 'list') { try { - await handleListNamesCommand(printer); + await handleListNamesAfterSyncing(argv.useCache, printer, fileDownloadService, tarService, true); } catch (error) { if (!(error instanceof Error)) { return; @@ -173,30 +174,8 @@ export const executeMainThread = async () => { return; } - const tarService = new TarService(fs as unknown as IFs); - if (String(argv._) === 'syncRegistry') { - const codemodDownloader = new CodemodDownloader( - printer, - join(homedir(), '.intuita'), - argv.useCache, - fileDownloadService, - tarService, - ); - - try { - await codemodDownloader.syncRegistry(); - } catch (error) { - if (!(error instanceof Error)) { - return; - } - - printer.printOperationMessage({ - kind: 'error', - message: error.message, - }); - } - + await syncRegistryOperation(argv.useCache, printer, fileDownloadService, tarService) exit(); return; @@ -365,3 +344,31 @@ export const executeMainThread = async () => { exit(); }; + +export async function syncRegistryOperation( + useCache: boolean, + printer: Printer, + fileDownloadService: FileDownloadService, + tarService: TarService, +) { + const codemodDownloader = new CodemodDownloader( + printer, + join(homedir(), '.intuita'), + useCache, + fileDownloadService, + tarService, + ); + + try { + await codemodDownloader.syncRegistry(); + } catch (error) { + if (!(error instanceof Error)) { + return; + } + + printer.printOperationMessage({ + kind: 'error', + message: error.message, + }); + } +} diff --git a/src/handleListCliCommand.ts b/src/handleListCliCommand.ts index b8d51ca..09515cb 100644 --- a/src/handleListCliCommand.ts +++ b/src/handleListCliCommand.ts @@ -5,9 +5,12 @@ import { mkdir, readFile } from 'node:fs/promises'; import { homedir } from 'node:os'; import { join } from 'node:path'; import * as v from 'valibot'; -import type { PrinterBlueprint } from './printer.js'; +import type { Printer } from './printer.js'; +import { FileDownloadService } from './fileDownloadService.js'; +import { syncRegistryOperation } from './executeMainThread.js' +import { TarService } from './services/tarService.js'; -export const handleListNamesCommand = async (printer: PrinterBlueprint) => { +export const handleListNamesCommand = async (printer: Printer) => { const configurationDirectoryPath = join(homedir(), '.intuita'); await mkdir(configurationDirectoryPath, { recursive: true }); @@ -40,3 +43,13 @@ export const handleListNamesCommand = async (printer: PrinterBlueprint) => { printer.printOperationMessage({ kind: 'names', names }); }; + +export const handleListNamesAfterSyncing = async ( + useCache: boolean, + printer: Printer, + fileDownloadService: FileDownloadService, + tarService: TarService, +) => { + await syncRegistryOperation(useCache, printer, fileDownloadService, tarService) + await handleListNamesCommand(printer) +} \ No newline at end of file diff --git a/src/messages.ts b/src/messages.ts index 30ab27d..d152f73 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -53,6 +53,11 @@ export type ErrorMessage = Readonly<{ path?: string; }>; +export type StatusUpdateMessage = Readonly<{ + kind: 'status'; + message: string; +}>; + export type OperationMessage = | RewriteMessage | FinishMessage @@ -63,4 +68,5 @@ export type OperationMessage = | CopyMessage | MetadataPathMessage | NamesMessage - | ErrorMessage; + | ErrorMessage + | StatusUpdateMessage;