From 9ab37ad9634792c94bc83b0e2369e2c7e1aa4d74 Mon Sep 17 00:00:00 2001 From: Sasha Date: Wed, 20 Dec 2023 19:24:20 +0200 Subject: [PATCH] fix: print list of files from local directory (#173) --- package.json | 7 +++--- pnpm-lock.yaml | 15 +++++++++---- src/executeMainThread.ts | 2 +- src/handleListCliCommand.ts | 44 +++++++++++++++++++++++-------------- 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index e0610e2..da66bbc 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "@babel/traverse": "7.23.2", "@effect/schema": "0.27.0", "@intuita-inc/filemod": "1.1.0", - "@intuita-inc/utilities": "1.0.2", + "@intuita-inc/utilities": "1.1.0", "@svgr/hast-util-to-babel-ast": "^7.0.0", "applicationinsights": "^2.9.1", "ast-types": "^0.14.2", @@ -54,6 +54,7 @@ "unified": "^10.1.2", "unist-util-filter": "^5.0.1", "unist-util-visit": "^5.0.0", + "valibot": "^0.24.1", "yargs": "^17.6.2" }, "main": "./dist/index.cjs", @@ -68,6 +69,7 @@ "@types/yargs": "^17.0.13", "@typescript-eslint/eslint-plugin": "6.9.1", "@typescript-eslint/parser": "6.9.1", + "@vitest/coverage-v8": "^1.0.1", "esbuild": "^0.17.14", "eslint": "^8.28.0", "eslint-config-prettier": "^8.5.0", @@ -75,8 +77,7 @@ "sinon": "^17.0.0", "ts-node": "^10.9.1", "typescript": "5.2.2", - "vitest": "^1.0.1", - "@vitest/coverage-v8": "^1.0.1" + "vitest": "^1.0.1" }, "packageManager": "pnpm@8.6.7", "scripts": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6a6f369..6c7acb3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -24,8 +24,8 @@ dependencies: specifier: 1.1.0 version: 1.1.0 '@intuita-inc/utilities': - specifier: 1.0.2 - version: 1.0.2(effect@2.0.0-next.59)(fast-check@3.14.0) + specifier: 1.1.0 + version: 1.1.0(effect@2.0.0-next.59)(fast-check@3.14.0) '@svgr/hast-util-to-babel-ast': specifier: ^7.0.0 version: 7.0.0 @@ -89,6 +89,9 @@ dependencies: unist-util-visit: specifier: ^5.0.0 version: 5.0.0 + valibot: + specifier: ^0.24.1 + version: 0.24.1 yargs: specifier: ^17.6.2 version: 17.6.2 @@ -2005,8 +2008,8 @@ packages: glob: 10.3.10 dev: false - /@intuita-inc/utilities@1.0.2(effect@2.0.0-next.59)(fast-check@3.14.0): - resolution: {integrity: sha512-qhJ6W2Ia4/ivLyPrR1qXJVszG8FRzlV8G54ozSHRpSCmAwrYsJCNZ4zA8TxVXZD9Fk3dJLP+bi0CCEvnLC+7hw==} + /@intuita-inc/utilities@1.1.0(effect@2.0.0-next.59)(fast-check@3.14.0): + resolution: {integrity: sha512-2/H2/v22Wuxb8SYB8QUB8AsdZoAr4Y0pAa0Rs+aym2DWOEysJB4aHsgF1YH2JqYiz59rQeIVAiYnELIqv/XZKg==} dependencies: '@effect/schema': 0.52.0(effect@2.0.0-next.59)(fast-check@3.14.0) transitivePeerDependencies: @@ -5598,6 +5601,10 @@ packages: convert-source-map: 2.0.0 dev: true + /valibot@0.24.1: + resolution: {integrity: sha512-Toclbuy20XsECZiueh2dkQ63he2AGaBIj/FJRDAFti2kueFldm9bjJzSYvPaL5CE1HXDMRhq7olak8at7xCz5A==} + dev: false + /vfile-location@4.1.0: resolution: {integrity: sha512-YF23YMyASIIJXpktBa4vIGLJ5Gs88UB/XePgqPmTa7cDA+JeO3yclbpheQYCHjVHBn/yePzrXuygIL+xbvRYHw==} dependencies: diff --git a/src/executeMainThread.ts b/src/executeMainThread.ts index 1b33bd7..b4a6923 100644 --- a/src/executeMainThread.ts +++ b/src/executeMainThread.ts @@ -149,7 +149,7 @@ export const executeMainThread = async () => { if (String(argv._) === 'list') { try { - await handleListNamesCommand(fileDownloadService, printer); + await handleListNamesCommand(printer); } catch (error) { if (!(error instanceof Error)) { return; diff --git a/src/handleListCliCommand.ts b/src/handleListCliCommand.ts index 5ba5033..7e47383 100644 --- a/src/handleListCliCommand.ts +++ b/src/handleListCliCommand.ts @@ -1,30 +1,42 @@ +import { isNeitherNullNorUndefined } from '@intuita-inc/utilities'; +import * as fs from 'fs'; +import { glob } from 'glob'; +import { mkdir, readFile } from 'node:fs/promises'; import { homedir } from 'node:os'; import { join } from 'node:path'; -import { mkdir } from 'node:fs/promises'; -import * as S from '@effect/schema/Schema'; -import type { FileDownloadService } from './fileDownloadService.js'; +import * as v from 'valibot'; import type { PrinterBlueprint } from './printer.js'; -export const handleListNamesCommand = async ( - fileDownloadService: FileDownloadService, - printer: PrinterBlueprint, -) => { +export const handleListNamesCommand = async (printer: PrinterBlueprint) => { const intuitaDirectoryPath = join(homedir(), '.intuita'); await mkdir(intuitaDirectoryPath, { recursive: true }); - const path = join(intuitaDirectoryPath, 'names.json'); - - const buffer = await fileDownloadService.download( - 'https://intuita-public.s3.us-west-1.amazonaws.com/codemod-registry/names.json', - path, + const configFiles = await glob('**/config.json', { + absolute: true, + cwd: intuitaDirectoryPath, + fs, + nodir: true, + }); + + const codemodNames = await Promise.allSettled( + configFiles.map(async (cfg) => { + const configJson = await readFile(cfg, 'utf8'); + + const parsedConfig = v.safeParse( + v.object({ name: v.string() }), + JSON.parse(configJson), + ); + return parsedConfig.success ? parsedConfig.output.name : null; + }), ); - const data = buffer.toString('utf8'); - - const parsedJson = JSON.parse(data); + const onlyValid = codemodNames + .map((x) => (x.status === 'fulfilled' ? x.value : null)) + .filter(isNeitherNullNorUndefined) + .sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())); - const names = S.parseSync(S.array(S.string))(parsedJson); + const names = v.parse(v.array(v.string()), onlyValid); printer.printOperationMessage({ kind: 'names', names }); };