You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.0KB

  1. const CUR_VER_PREFIX = '0.5';
  2. const fs = require("fs");
  3. const Ajv = require('ajv'); //https://github.com/epoberezkin/ajv
  4. const ajv = new Ajv({});
  5. const validate = ajv.compile(require('./manifest.json'));
  6. describe("json", function() {
  7. it("manifest files should be valid against the schema", function(/*done*/) {//TODO call done on Promise.all done
  8. fs.readdir('plugins', function(err, files) {
  9. if (err){
  10. fail("unable to read plugins dir");
  11. }
  12. for (let index in files) {
  13. const fileName = files[index];
  14. const filePath = `plugins/${fileName}`;
  15. if(!filePath.toLowerCase().endsWith('.json')){
  16. fail("manifests should have .json extension");
  17. }
  18. fs.readFile(filePath, 'utf8', (err, fileContent) => {
  19. if (err){
  20. fail("unable to read manifest file");
  21. }
  22. let manifestObj;
  23. try {
  24. const obj = JSON.parse(fileContent);
  25. const valid = validate(obj);
  26. if (!valid) {
  27. validate.errors.map(e=>e.message+=` in ${filePath}`)
  28. fail(validate.errors);
  29. }
  30. if(!(/^[a-zA-Z0-9_\-]*$/).test(obj.slug)){
  31. fail(`slug does not match regex in ${filePath}`);
  32. }
  33. if(fileName.replace('.json','') !== obj.slug){
  34. fail(`slug '${obj.slug}' does not match fileName: ${fileName}`);
  35. }
  36. if(obj.version && !obj.version.startsWith(CUR_VER_PREFIX)){
  37. fail(`version '${obj.version}' must start with '${CUR_VER_PREFIX}'`);
  38. }
  39. } catch(err){
  40. fail(`Invalid JSON: ${filePath}\n${err}`);
  41. }
  42. });
  43. }
  44. // done(); //TODO use promises
  45. });
  46. });
  47. });