export async function validate()

in packages/apps-tools/src/cli/validate.ts [14:55]


export async function validate(config: Config, appDir?: string) {
  try {
    if (!appDir && !config.manifest) {
      exit(new Error(i18n('No directory or manifest file provided')));
    }

    if (config.manifest && !config.manifest.endsWith('.json')) {
      exit(new Error(i18n('Manifest file must be a JSON file')));
    }

    if (config.schema && !config.schema.endsWith('.json')) {
      exit(new Error(i18n('Schema file must be a JSON file')));
    }

    const ajv = new Ajv({strict: false});
    addFormats(ajv);

    const manifestFilePath = config.manifest ? config.manifest : path.join(appDir!, 'manifest.json');
    const manifest = await parseFile(manifestFilePath);
    let schema: AnySchemaObject;

    if (config.schema) {
      schema = isValidUrl(config.schema)
        ? await fetchSchema(config.schema)
        : await parseFile<AnySchemaObject>(config.schema);
    } else {
      schema = existsSync(tmpSchemaPath)
        ? JSON.parse(await readSchemaFromTmp())
        : await fetchSchemaAndWriteToTmp(DEFAULT_SCHEMA_URL);
    }

    const validateFn = ajv.compile(schema);
    const valid = validateFn(manifest);

    if (!valid) {
      throw new Error(validateFn.errors?.map(prepareError).join('\n'));
    }
    console.log(i18n('Manifest is valid!'));
  } catch (error) {
    exit(error);
  }
}