|
| 1 | +import fs from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | + |
| 4 | +import toml from '@iarna/toml'; |
| 5 | +import Ajv from 'ajv'; |
| 6 | + |
| 7 | +const schema = { |
| 8 | + type: 'object', |
| 9 | + properties: { |
| 10 | + api_version: { |
| 11 | + type: 'string', |
| 12 | + pattern: '^(202\\d{1}-\\d{2}|unstable)$', // Matches "202X-XX" format |
| 13 | + }, |
| 14 | + extensions: { |
| 15 | + type: 'array', |
| 16 | + items: { |
| 17 | + type: 'object', |
| 18 | + properties: { |
| 19 | + description: { type: 'string' }, |
| 20 | + handle: { type: 'string' }, |
| 21 | + name: { type: 'string' }, |
| 22 | + type: { type: 'string' }, |
| 23 | + // build: { |
| 24 | + // type: 'object', |
| 25 | + // properties: { |
| 26 | + // command: { type: 'string' }, |
| 27 | + // path: { type: 'string' }, |
| 28 | + // additionalProperties: true, // Allow other properties in build |
| 29 | + // }, |
| 30 | + // required: ['command', 'path'], |
| 31 | + // }, |
| 32 | + // ui: { |
| 33 | + // type: 'object', |
| 34 | + // properties: { |
| 35 | + // paths: { |
| 36 | + // type: 'object', |
| 37 | + // properties: { |
| 38 | + // create: { type: 'string' }, |
| 39 | + // details: { type: 'string' }, |
| 40 | + // additionalProperties: { type: 'string' }, // Allow other paths |
| 41 | + // }, |
| 42 | + // additionalProperties: true, // Allow other keys in paths |
| 43 | + // }, |
| 44 | + // additionalProperties: true, // Allow other properties in ui |
| 45 | + // }, |
| 46 | + // additionalProperties: true, // Allow other keys in ui |
| 47 | + // }, |
| 48 | + // targeting: { |
| 49 | + // type: 'array', |
| 50 | + // items: { |
| 51 | + // type: 'object', |
| 52 | + // properties: { |
| 53 | + // export: { type: 'string' }, |
| 54 | + // input_query: { type: 'string' }, |
| 55 | + // target: { type: 'string' }, |
| 56 | + // additionalProperties: true, // Allow other properties in targeting item |
| 57 | + // }, |
| 58 | + // required: ['export', 'input_query', 'target'], |
| 59 | + // additionalProperties: true, // Allow other keys in targeting item |
| 60 | + // }, |
| 61 | + // }, |
| 62 | + // additionalProperties: true, // Allow other properties in extension |
| 63 | + }, |
| 64 | + required: ['description', 'handle', 'name', 'type', 'build'], |
| 65 | + additionalProperties: true, // Allow other keys in extension |
| 66 | + }, |
| 67 | + }, |
| 68 | + additionalProperties: true, // Allow other top-level properties |
| 69 | + }, |
| 70 | + required: ['api_version'], |
| 71 | + // required: ['api_version', 'extensions'], |
| 72 | + additionalProperties: true, // Allow other top-level keys |
| 73 | +}; |
| 74 | + |
| 75 | +const ajv = new Ajv(); |
| 76 | +const compiledAjv = ajv.compile(schema); |
| 77 | + |
| 78 | +async function findExtensionTomlFiles(directory) { |
| 79 | + const files = await fs.readdir(directory); |
| 80 | + const matchingFiles = []; |
| 81 | + |
| 82 | + for (const file of files) { |
| 83 | + if (file === 'node_modules') { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + const fullPath = path.join(directory, file); |
| 88 | + const stats = await fs.stat(fullPath); |
| 89 | + |
| 90 | + if (stats.isFile() && file === 'shopify.extension.toml') { |
| 91 | + matchingFiles.push(fullPath); |
| 92 | + } else if (stats.isDirectory()) { |
| 93 | + // Recursively search subdirectories |
| 94 | + const subDirectoryFiles = await findExtensionTomlFiles(fullPath); |
| 95 | + matchingFiles.push(...subDirectoryFiles); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return matchingFiles; |
| 100 | +} |
| 101 | + |
| 102 | +async function testSchema(filename) { |
| 103 | + await fs.readFile(filename, 'utf8') |
| 104 | + .then(data => { |
| 105 | + const parsedData = toml.parse(data); |
| 106 | + const isValid = compiledAjv(parsedData); |
| 107 | + |
| 108 | + if (isValid) { |
| 109 | + console.log(`Validation successful for ${filename}`); |
| 110 | + } else { |
| 111 | + console.error(`Validation failed for ${filename}:`); |
| 112 | + console.error(compiledAjv.errors); |
| 113 | + |
| 114 | + process.exit(-1) |
| 115 | + } |
| 116 | + }) |
| 117 | + .catch(_ => { |
| 118 | + throw new Error(`Error reading file ${filename}.`); |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +async function test() { |
| 123 | + const filenames = await findExtensionTomlFiles('./'); |
| 124 | + |
| 125 | + filenames.forEach(filename => { |
| 126 | + testSchema(filename); |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | +await test(); |
0 commit comments