Skip to content

Commit

Permalink
Fix completion specs for linter
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandtshen committed Jan 21, 2021
1 parent 3b53b63 commit c3b1241
Show file tree
Hide file tree
Showing 18 changed files with 137 additions and 109 deletions.
100 changes: 64 additions & 36 deletions linter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Spec } = require('./schemas/Spec');
const fs = require('fs');
var path = require('path');

const bColors = {
OKGREEN : '\033[92m',
Expand All @@ -9,50 +10,77 @@ const bColors = {
BOLD : '\033[1m',
}

var file = process.argv.slice(2)[0];
if(!file) {
console.log("Enter the path of the spec to validate.");
return;
}
function validate(file) {
var fileParts = file.split('/');
var specName = fileParts[fileParts.length - 1].split('.')[0];

// Read JS from spec and compile
var data = fs.readFileSync(file, 'utf8');
eval(data);

var fileParts = file.split('/');
var specName = fileParts[fileParts.length - 1].split('.')[0];
console.log(`💨 Linting the ${specName.toUpperCase()} spec...`)

// Read JS from spec and compile
var data = fs.readFileSync(file, 'utf8');
eval(data);
// completionSpec variable made available via eval
const { error } = Spec.validate(completionSpec);

// completionSpec variable made available via eval
const { error } = Spec.validate(completionSpec);

if(error) {
console.log("❌ Autocompletion spec validation failed.");
console.log("------------------------------------------")
for(var i = 0; i < error.details.length; i++) {
const detail = error.details[i];
var curr = error._original;
var errorMessage = completionSpec.name;
for(var k = 0; k < detail.path.length; k++) {
//iterate down the path to grab all elements
var path = detail.path[k];
if(path === "subcommands" || path === "options" || path === "args" || path === "generators" || path === "suggestions" || path === "additionalSuggestions") {
errorMessage += '/' + path;
}
curr = curr[path];
if(curr.name != null) {
errorMessage += " " + JSON.stringify(curr.name);
}
}
errorMessage += '/' + detail.context.key;
console.log(`${bColors.FAIL}Error: ${detail.message} ${bColors.ENDC}`);
console.log("Path to error: " + `${bColors.WARNING}${errorMessage}${bColors.ENDC}`);
console.log("Current value:");
console.log(detail.context.value);
console.log('\n');
return false;
}
}
else {
console.log(`${bColors.OKGREEN}✅ The ${specName} autocomplete completion spec is working properly!${bColors.ENDC}\n`);
return true;
}
}

console.log(`${bColors.BOLD} ◧ FIG AUTOCOMPLETE LINTER V1 ${bColors.ENDC}\n`)
console.log(`💨 Linting the ${specName.toUpperCase()} spec...`)

if(error) {
console.log("❌ Autocompletion spec validation failed.");
console.log("------------------------------------------")
for(var i = 0; i < error.details.length; i++) {
const detail = error.details[i];
var curr = error._original;
var errorMessage = completionSpec.name;
for(var k = 0; k < detail.path.length; k++) {
//iterate down the path to grab all elements
var path = detail.path[k];
if(path === "subcommands" || path === "options" || path === "args" || path === "generators" || path === "suggestions" || path === "additionalSuggestions") {
errorMessage += '/' + path;

var file = process.argv.slice(2)[0];
if(!file) {
//validate the entire specs folder if no spec specified
fs.readdir('specs', function(err, files) {
if(err) {
console.log("Can't find /specs folder");
return;
}
for(var i = 0; i < files.length; i++) {
var file = files[i];
var spec = path.join('specs', file);
if(spec === "specs/.DS_Store") {
// Skip DS_Store
continue;
}
curr = curr[path];
if(curr.name != null) {
errorMessage += " " + JSON.stringify(curr.name);
if(!validate(spec)) {
return;
}
}
errorMessage += '/' + detail.context.key;
console.log(`${bColors.FAIL}Error: ${detail.message} ${bColors.ENDC}`);
console.log("Path to error: " + `${bColors.WARNING}${errorMessage}${bColors.ENDC}`);
console.log("Current value:");
console.log(detail.context.value);
}
})
return;
}
else {
console.log(`${bColors.OKGREEN}✅ Your autocomplete completion spec is working properly!${bColors.ENDC}`)
validate(file);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Fig Autocomplete Spec Linter",
"main": "linter.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node linter.js"
},
"author": "",
"license": "ISC",
Expand Down
4 changes: 2 additions & 2 deletions schemas/Arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const { Generator } = require('./Generator');
const Arg = Joi.object({
name: Joi.string(),
displayName: Joi.string(),
insertValue: Joi.string(),
insertValue: Joi.string().allow(''),
description: Joi.string().optional().allow(''),
icon: Joi.string(),
suggestions: Joi.array().items(Suggestion),
template: Joi.string().valid("filepaths", "folders"),
generators: Generator,
generators: Joi.alternatives().try(Generator, Joi.array().items(Generator)),
variadic: Joi.boolean()
})

Expand Down
2 changes: 1 addition & 1 deletion schemas/Option.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { Arg } = require('./Arg');
const { Suggestion } = require('./Suggestion');

const Option = Joi.object({
name: Joi.alternatives().try(Joi.string(), Joi.array().items(Joi.string())).required(),
name: Joi.alternatives().try(Joi.string().allow(''), Joi.array().items(Joi.string().allow(''))).required(),
displayName: Joi.string(),
insertValue: Joi.string(),
description: Joi.string().optional().allow(''),
Expand Down
6 changes: 2 additions & 4 deletions specs/brew.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ var generators = {
}
}



var completionSpec = {
name: "brew",
description: "Package manager for macOS",
Expand Down Expand Up @@ -73,11 +71,11 @@ var completionSpec = {
{
name: "cask",
insertValue: "cask ",
description: "Homebrew Cask provides a friendly CLI workflow for the administration of macOS applications distributed as binaries.",
description: "Homebrew Cask provides a friendly CLI workflow for the administration of macOS applications distributed as binaries.",
subcommands: [
{
name: "install",
insertValue: "install ",
insertValue: "install",
description: "Installs the given cask",
args: {
name: "<cask>",
Expand Down
2 changes: 1 addition & 1 deletion specs/cargo.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var completionSpec = {
},
],
args: {
template: "files",
template: "filepaths",
},
}
],
Expand Down
2 changes: 0 additions & 2 deletions specs/chmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var completionSpec = {
args: [
{
name: "mode",
optional: false,
suggestions: [
// Some of the most common chmod's (non-exhaustive)
{
Expand Down Expand Up @@ -41,7 +40,6 @@ var completionSpec = {
},
{
// Modifying
optional: false,
template: "filepaths"
}
],
Expand Down
8 changes: 6 additions & 2 deletions specs/curl.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ var completionSpec = {
name: ["-X","--request"],
description: "Specify request command to use",
args: {
suggestions: ["POST", "GET", "PATCH", "UPDATE"]
suggestions: [
{ name: "POST" }, { name: "GET" }, { name: "PATCH" }, { name: "UPDATE" }
]
},
},
{
name: ["-H","--header"],
description: "Pass custom header(s) to serve",
args: {
suggestions: ["'Content-Type: application/json'","'Content-Type: application/x-www-form-urlencoded'"]
suggestions: [
{name: "'Content-Type: application/json'"}, {name: "'Content-Type: application/x-www-form-urlencoded'"}
]
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion specs/fig.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var completionSpec = {
name: "Email",
description: "Email address of the user you want to invite",
variadic: true,
staticSuggestions: [
suggestions: [
{
name: "<email>",
insertValue: " ",
Expand Down
9 changes: 4 additions & 5 deletions specs/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,7 @@ var completionSpec = {
},
{
name: "ext:configure",
description:
"display information on how to use ext commands and extensions",
description: "display information on how to use ext commands and extensions",
options: [
{
name: ["-h", "--help"],
Expand All @@ -279,7 +278,7 @@ var completionSpec = {
},
],
args: {
generator: {
generators: {
script: "firebase ext:list",
postProcess: function (out) {
if (out.trim() == "") {
Expand Down Expand Up @@ -345,7 +344,7 @@ var completionSpec = {
},
],
args: {
generator: {
generators: {
script: "firebase ext:list",
postProcess: function (out) {
if (out.trim() == "") {
Expand All @@ -368,7 +367,7 @@ var completionSpec = {
name: "projects:addfirebase",
description: "add Firebase resources to a Google Cloud Platform project",
args: {
generator: {
generators: {
script: "zsh gcloud projects list",
postProcess: function (out) {
if (out.trim() === "") {
Expand Down
4 changes: 2 additions & 2 deletions specs/less.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var completionSpec = {

name: "less",
description: "qqopposite of more",
description: "opposite of more",
args: {
variadic: true,
template: "filpaths"
template: "filepaths"
}
}
Loading

0 comments on commit c3b1241

Please sign in to comment.