Skip to content

Commit ab711f1

Browse files
committed
Add description property to wireit script configs
Adds an optional `description` string field to wireit script configurations, allowing developers to document what each script does. - schema.json: adds `description` property with markdownDescription for IDE autocomplete/hover support - src/config.ts: adds `description: string | undefined` to BaseScriptConfig - src/analyzer.ts: adds #processDescription() method and wires it into script config parsing - src/test/json-schema.test.ts: adds test cases for valid/invalid values - src/test/util/package-json.ts: adds description to test PackageJson type Closes #1015
1 parent b9011ec commit ab711f1

5 files changed

Lines changed: 76 additions & 0 deletions

File tree

schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
"markdownDescription": "The wireit config for the npm script with this name.\n\nThe npm script should just run `wireit` with no args and its actual command should be put in the `command` property of this object.\n\nFor more info see: https://github.com/google/wireit#cleaning-output",
99
"additionalProperties": true,
1010
"properties": {
11+
"description": {
12+
"markdownDescription": "A human-readable description of what this script does.\n\nFor example:\n\n```json\n\"description\": \"Compile TypeScript sources to JavaScript\"\n```",
13+
"type": "string"
14+
},
1115
"clean": {
1216
"markdownDescription": "By default, `output` files are deleted before the command is run.\n\nSet `clean` to false to prevent this.\n\nSome commands, like `tsc --build`, have their own incremental run logic and only write those output files that have changed. In that case, it can be beneficial to only delete output files when one of the input files has been deleted. In that case, set `clean` to \"if-file-deleted\".\n\nFor more info see: https://github.com/google/wireit#cleaning-output",
1317
"enum": [true, false, "if-file-deleted"]

src/analyzer.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ export class Analyzer {
503503
declaringFile: packageJson.jsonFile,
504504
services: [],
505505
env: {},
506+
description: undefined,
506507
};
507508
Object.assign(placeholder, remainingConfig);
508509
}
@@ -632,6 +633,11 @@ export class Analyzer {
632633
);
633634

634635
const env = this.#processEnv(placeholder, packageJson, syntaxInfo, command);
636+
const description = this.#processDescription(
637+
placeholder,
638+
packageJson,
639+
syntaxInfo,
640+
);
635641

636642
if (placeholder.failures.length > 0) {
637643
// A script with locally-determined errors doesn't get upgraded to
@@ -658,6 +664,7 @@ export class Analyzer {
658664
declaringFile: packageJson.jsonFile,
659665
services: [],
660666
env,
667+
description,
661668
};
662669
Object.assign(placeholder, remainingConfig);
663670
}
@@ -945,6 +952,39 @@ export class Analyzer {
945952
return defaultValue;
946953
}
947954

955+
#processDescription(
956+
placeholder: UnvalidatedConfig,
957+
packageJson: PackageJson,
958+
syntaxInfo: ScriptSyntaxInfo,
959+
): string | undefined {
960+
if (syntaxInfo.wireitConfigNode == null) {
961+
return undefined;
962+
}
963+
const node = findNodeAtLocation(syntaxInfo.wireitConfigNode, [
964+
'description',
965+
]);
966+
if (node === undefined) {
967+
return undefined;
968+
}
969+
if (typeof node.value === 'string') {
970+
return node.value;
971+
}
972+
placeholder.failures.push({
973+
type: 'failure',
974+
reason: 'invalid-config-syntax',
975+
script: placeholder,
976+
diagnostic: {
977+
severity: 'error',
978+
message: `Must be a string`,
979+
location: {
980+
file: packageJson.jsonFile,
981+
range: {length: node.length, offset: node.offset},
982+
},
983+
},
984+
});
985+
return undefined;
986+
}
987+
948988
#processFiles(
949989
placeholder: UnvalidatedConfig,
950990
packageJson: PackageJson,

src/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ interface BaseScriptConfig extends ScriptReference {
207207
*/
208208
configAstNode: NamedAstNode | undefined;
209209

210+
/**
211+
* A human-readable description of what this script does.
212+
*/
213+
description: string | undefined;
214+
210215
/** The parsed JSON file that declared this script. */
211216
declaringFile: JsonFile;
212217
failures: Failure[];

src/test/json-schema.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ test('a script with all fields set is valid', () => {
8282
wireit: {
8383
a: {
8484
command: 'b',
85+
description: 'A description.',
8586
dependencies: ['c', {script: 'c', cascade: false}],
8687
files: ['d'],
8788
output: ['e'],
@@ -266,4 +267,29 @@ test('dependencies[i].cascade must be boolean', () => {
266267
);
267268
});
268269

270+
test('description is valid when set to a string', () => {
271+
shouldValidate({
272+
wireit: {
273+
a: {
274+
command: 'b',
275+
description: 'A human-readable description of this script.',
276+
},
277+
},
278+
});
279+
});
280+
281+
test('description must be a string', () => {
282+
expectValidationErrors(
283+
{
284+
wireit: {
285+
a: {
286+
command: 'b',
287+
description: 123,
288+
},
289+
},
290+
},
291+
['instance.wireit.a.description is not of a type(s) string'],
292+
);
293+
});
294+
269295
test.run();

src/test/util/package-json.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface PackageJson {
1616
wireit?: {
1717
[scriptName: string]: {
1818
command?: string;
19+
description?: string;
1920
dependencies?: Array<string | {script: string; cascade?: boolean}>;
2021
files?: string[];
2122
output?: string[];

0 commit comments

Comments
 (0)