Skip to content

Commit cbf9143

Browse files
committed
feat: bring validation schema inline instead of reading from a file
1 parent 8c617ca commit cbf9143

File tree

5 files changed

+154
-10
lines changed

5 files changed

+154
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"prepublishOnly": "npm run build",
3434
"lint": "eslint",
3535
"format": "prettier --write .",
36-
"quick:test": "node --import=@poppinss/ts-exec bin/test.ts"
36+
"quick:test": "node --import=@poppinss/ts-exec --enable-source-maps bin/test.ts"
3737
},
3838
"devDependencies": {
3939
"@adonisjs/eslint-config": "^3.0.0-next.1",
File renamed without changes.

schemas/main.ts

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,153 @@
77
* file that was distributed with this source code.
88
*/
99

10-
export const schemaRoot = import.meta.url
10+
export const schema = {
11+
$ref: '#/definitions/CommandMetaData',
12+
$schema: 'http://json-schema.org/draft-07/schema#',
13+
definitions: {
14+
CommandMetaData: {
15+
description: 'Command metdata required to display command help.',
16+
properties: {
17+
aliases: {
18+
description: 'Command aliases. The same command can be run using these aliases as well.',
19+
items: {
20+
type: 'string',
21+
},
22+
type: 'array',
23+
},
24+
args: {
25+
description: 'Args accepted by the command',
26+
items: {
27+
additionalProperties: false,
28+
properties: {
29+
allowEmptyValue: {
30+
description:
31+
'Whether or not to allow empty values. When set to false, the validation will fail if the argument is provided an empty string\n\nDefaults to false',
32+
type: 'boolean',
33+
},
34+
argumentName: {
35+
type: 'string',
36+
},
37+
default: {},
38+
description: {
39+
type: 'string',
40+
},
41+
name: {
42+
type: 'string',
43+
},
44+
required: {
45+
type: 'boolean',
46+
},
47+
type: {
48+
enum: ['string', 'spread'],
49+
type: 'string',
50+
},
51+
},
52+
required: ['name', 'argumentName', 'type'],
53+
type: 'object',
54+
},
55+
type: 'array',
56+
},
57+
commandName: {
58+
description: 'The name of the command',
59+
type: 'string',
60+
},
61+
description: {
62+
description: 'The command description to show on the help screen',
63+
type: 'string',
64+
},
65+
flags: {
66+
description: 'Flags accepted by the command',
67+
items: {
68+
additionalProperties: false,
69+
properties: {
70+
alias: {
71+
anyOf: [
72+
{
73+
type: 'string',
74+
},
75+
{
76+
items: {
77+
type: 'string',
78+
},
79+
type: 'array',
80+
},
81+
],
82+
},
83+
allowEmptyValue: {
84+
description:
85+
'Whether or not to allow empty values. When set to false, the validation will fail if the flag is mentioned but no value is provided\n\nDefaults to false',
86+
type: 'boolean',
87+
},
88+
default: {},
89+
description: {
90+
type: 'string',
91+
},
92+
flagName: {
93+
type: 'string',
94+
},
95+
name: {
96+
type: 'string',
97+
},
98+
required: {
99+
type: 'boolean',
100+
},
101+
showNegatedVariantInHelp: {
102+
description:
103+
'Whether or not to display the negated variant in the help output.\n\nApplicable for boolean flags only\n\nDefaults to false',
104+
type: 'boolean',
105+
},
106+
type: {
107+
enum: ['string', 'boolean', 'number', 'array'],
108+
type: 'string',
109+
},
110+
},
111+
required: ['name', 'flagName', 'type'],
112+
type: 'object',
113+
},
114+
type: 'array',
115+
},
116+
help: {
117+
anyOf: [
118+
{
119+
type: 'string',
120+
},
121+
{
122+
items: {
123+
type: 'string',
124+
},
125+
type: 'array',
126+
},
127+
],
128+
description: 'Help text for the command',
129+
},
130+
namespace: {
131+
description: 'Command namespace. The namespace is extracted from the command name',
132+
type: ['string', 'null'],
133+
},
134+
options: {
135+
$ref: '#/definitions/CommandOptions',
136+
description: 'Command configuration options',
137+
},
138+
},
139+
required: ['aliases', 'args', 'commandName', 'description', 'flags', 'namespace', 'options'],
140+
type: 'object',
141+
},
142+
CommandOptions: {
143+
description: 'Static set of command options',
144+
properties: {
145+
allowUnknownFlags: {
146+
description:
147+
'Whether or not to allow for unknown flags. If set to false, the command will not run when unknown flags are provided through the CLI\n\nDefaults to false',
148+
type: 'boolean',
149+
},
150+
staysAlive: {
151+
description:
152+
'When flag set to true, the kernel will not trigger the termination process unless the command explicitly calls the terminate method.\n\nDefaults to false',
153+
type: 'boolean',
154+
},
155+
},
156+
type: 'object',
157+
},
158+
},
159+
}

src/utils.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@
88
*/
99

1010
import { Validator } from 'jsonschema'
11-
import { readFile } from 'node:fs/promises'
1211
import { RuntimeException } from '@poppinss/utils/exception'
1312

14-
import { schemaRoot } from '../schemas/main.ts'
13+
import { schema } from '../schemas/main.ts'
1514
import type { AbstractBaseCommand, CommandMetaData, UIPrimitives } from './types.ts'
1615

17-
const schema = JSON.parse(
18-
await readFile(new URL('./command_metadata_schema.json', schemaRoot), 'utf8')
19-
)
20-
2116
/**
2217
* Helper to sort array of strings alphabetically.
2318
*/

tests/helpers.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ test.group('Helpers | validateCommandMetaData', () => {
6565
})
6666

6767
test('work fine when command metadata is complete', ({ assert }) => {
68-
assert.doesNotThrows(() =>
68+
assert.doesNotThrow(() =>
6969
validateCommandMetaData(
7070
{
7171
commandName: 'serve',
@@ -127,7 +127,7 @@ test.group('Helpers | validateCommand', () => {
127127
}
128128
}
129129

130-
assert.doesNotThrows(
130+
assert.doesNotThrow(
131131
() => validateCommand(MakeController, '"./foo.js" file'),
132132
'Invalid command exported from "./foo.js" file. requires property "aliases"'
133133
)

0 commit comments

Comments
 (0)