Skip to content

Commit

Permalink
feat: improve linter (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Jul 20, 2024
1 parent 9515c46 commit 1ca4d2c
Show file tree
Hide file tree
Showing 5 changed files with 978 additions and 229 deletions.
22 changes: 17 additions & 5 deletions apps/cli/src/command/lint.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import pluralize from 'pluralize';
import { ZodError } from 'zod';

import { check } from '../linter';
import { SignaleRenderer } from '../utils/listr';
import { LoadLocalConfigurationTask } from './diff.command';
import { BaseCommand } from './helper';

Expand All @@ -28,7 +29,15 @@ export const LintTask = (): ListrTask<{ local: ADCSDK.Configuration }> => ({

// normal case
const resourceType = pluralize.singular(error.path[0] as string);
const resourceName = ctx.local[error.path[0]][error.path[1]].name;
const resource = ctx.local[error.path[0]][error.path[1]];
const resourceName =
resourceType === 'global_rule' || resourceType === 'plugin_metadata'
? error.path[1]
: resourceType === 'ssl'
? resource.snis
: resourceType === 'consumer'
? resource.username
: resource.name;
err += `#${idx + 1} ${
error.message
} at ${resourceType}: "${resourceName}", field: "${(
Expand Down Expand Up @@ -59,10 +68,13 @@ export const LintCommand = new BaseCommand('lint')
.action(async () => {
const opts = LintCommand.optsWithGlobals();

const tasks = new Listr([
LoadLocalConfigurationTask(opts.file, {}),
LintTask(),
]);
const tasks = new Listr(
[LoadLocalConfigurationTask(opts.file, {}), LintTask()],
{
renderer: SignaleRenderer,
rendererOptions: { verbose: opts.verbose },
},
);

try {
await tasks.run();
Expand Down
15 changes: 15 additions & 0 deletions apps/cli/src/linter/exporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Export jsonschema file by:
*
* $ ts-node apps/cli/src/linter/exporter.ts
*
*/
import { writeFileSync } from 'fs';
import zodToJsonSchema from 'zod-to-json-schema';

import { ConfigurationSchema } from './schema';

writeFileSync(
'schema.json',
JSON.stringify(zodToJsonSchema(ConfigurationSchema), null, 2),
);
Loading

0 comments on commit 1ca4d2c

Please sign in to comment.