validate-growi-plugin-directive.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { GrowiPluginType } from '@growi/core/dist/consts';
  2. import { type GrowiPluginValidationData, GrowiPluginValidationError } from '../../../../model';
  3. import { importPackageJson } from './import-package-json';
  4. export const validateGrowiDirective = (projectDirRoot: string, expectedPluginType?: GrowiPluginType): GrowiPluginValidationData => {
  5. const pkg = importPackageJson(projectDirRoot);
  6. const { growiPlugin } = pkg;
  7. const data: GrowiPluginValidationData = { projectDirRoot, schemaVersion: NaN, growiPlugin };
  8. if (growiPlugin == null) {
  9. throw new GrowiPluginValidationError("The package.json does not have 'growiPlugin' directive.", data);
  10. }
  11. // schema version checking
  12. const schemaVersion = Number(growiPlugin.schemaVersion);
  13. data.schemaVersion = schemaVersion;
  14. if (Number.isNaN(schemaVersion) || schemaVersion < 4) {
  15. throw new GrowiPluginValidationError("The growiPlugin directive must have a valid 'schemaVersion' directive.", data);
  16. }
  17. const types: GrowiPluginType[] = growiPlugin.types;
  18. data.actualPluginTypes = types;
  19. if (types == null) {
  20. throw new GrowiPluginValidationError("The growiPlugin directive does not have 'types' directive.", data);
  21. }
  22. // type checking
  23. if (expectedPluginType != null) {
  24. data.expectedPluginType = expectedPluginType;
  25. if (!types.includes(expectedPluginType)) {
  26. throw new GrowiPluginValidationError("The growiPlugin directive does not have expected plugin type in 'types' directive.", data);
  27. }
  28. }
  29. return data;
  30. };