"lint" command for flipper-pkg tool

Summary:
Implemented json schema for flipper plugin package.json and used it for validation in "flipper-pkg lint" command.

Nice thing about json schema is that it not only allows to validate json, but also can be referenced using "$schema" property in json so IDEs like VSCode can find it and use for code completion, validation and to show properties documentation. I'm going to deploy the schema as a part of documentation website so it can be referenced as https://fbflipper.com/schemas/plugin-package/v2.json.

Also the "$schema" field can be used instead of "specVersion" to determine the specification according to which the plugin is defined. E.g., if specification version 3 would be created, it will be described in schema https://fbflipper.com/schemas/plugin-package/v3.json, etc.

Reviewed By: passy

Differential Revision: D21228294

fbshipit-source-id: f21351e584ef936a7d6b314436448489691f83a6
This commit is contained in:
Anton Nikolaev
2020-04-27 17:31:39 -07:00
committed by Facebook GitHub Bot
parent 01f8d80402
commit 21c574ac80
24 changed files with 1062 additions and 84 deletions

View File

@@ -0,0 +1,43 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import ajv from 'ajv';
import path from 'path';
import fs from 'fs-extra';
export default function (inputDirectory: string) {
const filePathExists: ajv.KeywordDefinition = {
errors: true,
compile: function validatePathExists(schema: any) {
function filePathExistsValidator(value: any, dataPath: any) {
const it = filePathExistsValidator as ajv.SchemaValidateFunction;
if (!schema) {
return true;
}
it.errors = [];
const tpl = {
keyword: 'filePathExists',
dataPath,
schemaPath: '',
params: [],
};
const fullPath = path.resolve(inputDirectory, value);
if (!fs.pathExistsSync(fullPath) || !fs.lstatSync(fullPath).isFile()) {
it.errors.push({
...tpl,
message: `should point to a valid file`,
});
}
return it.errors.length === 0;
}
return filePathExistsValidator;
},
};
return filePathExists;
}