2
0

validate.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { GrowiPluginType } from '~/consts';
  2. import { type GrowiPluginValidationData, GrowiPluginValidationError } from '~/model';
  3. import { importPackageJson } from './import';
  4. export const validatePackageJson = async(projectDirRoot: string, expectedPluginType?: GrowiPluginType): Promise<GrowiPluginValidationData> => {
  5. const pkg = await importPackageJson(projectDirRoot);
  6. const data: GrowiPluginValidationData = { projectDirRoot };
  7. const { growiPlugin } = pkg;
  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 'types' directive.", data);
  27. }
  28. }
  29. return data;
  30. };