Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
Auto Sync Registry if no codemod available (#195)
Browse files Browse the repository at this point in the history
Co-authored-by: Harsh <[email protected]>
  • Loading branch information
Harsh24893 and Harsh authored Feb 5, 2024
1 parent 79ffa3f commit 971b832
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
59 changes: 33 additions & 26 deletions src/executeMainThread.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
});
}
}
17 changes: 15 additions & 2 deletions src/handleListCliCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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)
}
8 changes: 7 additions & 1 deletion src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export type ErrorMessage = Readonly<{
path?: string;
}>;

export type StatusUpdateMessage = Readonly<{
kind: 'status';
message: string;
}>;

export type OperationMessage =
| RewriteMessage
| FinishMessage
Expand All @@ -63,4 +68,5 @@ export type OperationMessage =
| CopyMessage
| MetadataPathMessage
| NamesMessage
| ErrorMessage;
| ErrorMessage
| StatusUpdateMessage;

0 comments on commit 971b832

Please sign in to comment.