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
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh committed Feb 2, 2024
1 parent 79ffa3f commit 12bd8b8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 28 deletions.
56 changes: 32 additions & 24 deletions src/executeMainThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,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 +158,7 @@ export const executeMainThread = async () => {

if (String(argv._) === 'list') {
try {
await handleListNamesCommand(printer);
await handleListNamesCommand(argv, printer, fileDownloadService, tarService, true);
} catch (error) {
if (!(error instanceof Error)) {
return;
Expand All @@ -173,30 +175,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, printer, fileDownloadService, tarService)
exit();

return;
Expand Down Expand Up @@ -365,3 +345,31 @@ export const executeMainThread = async () => {

exit();
};

export async function syncRegistryOperation(
argv: any,
printer: Printer,
fileDownloadService: FileDownloadService,
tarService: TarService,
) {
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,
});
}
}
23 changes: 20 additions & 3 deletions src/handleListCliCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ 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';

export const handleListNamesCommand = async (printer: PrinterBlueprint) => {
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 (
argv: any,
printer: Printer,
fileDownloadService: FileDownloadService,
tarService: TarService,
syncRegistry: boolean,
) => {
const configurationDirectoryPath = join(homedir(), '.intuita');

await mkdir(configurationDirectoryPath, { recursive: true });
Expand Down Expand Up @@ -38,5 +47,13 @@ export const handleListNamesCommand = async (printer: PrinterBlueprint) => {

const names = v.parse(v.array(v.string()), onlyValid);

// Sync with registry if there are no codemods available
if (syncRegistry && Object.keys(names).length === 0) {
printer.printOperationMessage({ kind: 'status', message: "There were no codemod synced, hence syncing it with registry" });
await syncRegistryOperation(argv, printer, fileDownloadService, tarService)

await handleListNamesCommand(argv, printer, fileDownloadService, tarService, false)
}

printer.printOperationMessage({ kind: 'names', names });
};
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 12bd8b8

Please sign in to comment.