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

Commit

Permalink
fix: print list of files from local directory (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
r4zendev authored Dec 20, 2023
1 parent 3e70853 commit 9ab37ad
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -68,15 +69,15 @@
"@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",
"eslint-plugin-prettier": "^4.2.1",
"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": "[email protected]",
"scripts": {
Expand Down
15 changes: 11 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/executeMainThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 28 additions & 16 deletions src/handleListCliCommand.ts
Original file line number Diff line number Diff line change
@@ -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 });
};

0 comments on commit 9ab37ad

Please sign in to comment.