|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const YAML = require('yaml'); |
| 4 | + |
| 5 | +const samplesDir = process.argv[2]; |
| 6 | + |
| 7 | +// categories are directories in the current directory (i.e. we're running in samples/ and we might have a samples/ruby/ directory) |
| 8 | +const directories = fs.readdirSync(samplesDir).filter(file => fs.statSync(path.join(samplesDir, file)).isDirectory()); |
| 9 | + |
| 10 | +let jsonArray = []; |
| 11 | + |
| 12 | +directories.forEach((sample) => { |
| 13 | + const directoryName = sample; |
| 14 | + console.log(`@@ Adding ${sample}`); |
| 15 | + let readme; |
| 16 | + try { |
| 17 | + readme = fs.readFileSync(path.join(samplesDir, sample, 'README.md'), 'utf8'); |
| 18 | + } catch (error) { |
| 19 | + readme = `# ${sample}`; |
| 20 | + } |
| 21 | + |
| 22 | + // The readme should contain lines that start with the following: |
| 23 | + // Title: |
| 24 | + // Short Description: |
| 25 | + // Tags: |
| 26 | + // Languages: |
| 27 | + // |
| 28 | + // We want to extract the title, short description, tags, and languages from the readme. Tags and languages are comma separated lists. |
| 29 | + const title = readme.match(/Title: (.*)/)[1]; |
| 30 | + const shortDescription = readme.match(/Short Description: (.*)/)[1]; |
| 31 | + const tags = readme.match(/Tags: (.*)/)[1].split(',').map(tag => tag.trim()); |
| 32 | + const languages = readme.match(/Languages: (.*)/)[1].split(',').map(language => language.trim()); |
| 33 | + |
| 34 | + let configs = []; |
| 35 | + try { |
| 36 | + composeFile = fs.readFileSync(path.join(samplesDir, sample, 'compose.yaml'), 'utf8'); |
| 37 | + compose = YAML.parse(composeFile); |
| 38 | + |
| 39 | + for (var name in compose.services) { |
| 40 | + service = compose.services[name] |
| 41 | + if (Array.isArray(service.environment)) { |
| 42 | + service.environment.forEach(env => { |
| 43 | + if (!env.includes("=")) { |
| 44 | + configs.push(env); |
| 45 | + } |
| 46 | + }); |
| 47 | + } else { |
| 48 | + for (var name in service.environment) { |
| 49 | + value = service.environment[name]; |
| 50 | + if (value === null || value === undefined || value === "") { |
| 51 | + configs.push(name); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } catch (error) { |
| 57 | + // Ignore if the sample doesn't have a compose file |
| 58 | + if (error.code != 'ENOENT') { |
| 59 | + console.log(`failed to parese compose for configs for sample`, sample, error); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + const sampleSummary = { |
| 64 | + name: directoryName, |
| 65 | + category: languages?.[0], |
| 66 | + readme, |
| 67 | + directoryName, |
| 68 | + title, |
| 69 | + shortDescription, |
| 70 | + tags, |
| 71 | + languages, |
| 72 | + }; |
| 73 | + if (configs.length > 0) { |
| 74 | + sampleSummary.configs = configs; |
| 75 | + } |
| 76 | + jsonArray.push(sampleSummary); |
| 77 | + |
| 78 | + console.log(`@@ Added ${sample}`); |
| 79 | +}); |
| 80 | + |
| 81 | +const stringified = JSON.stringify(jsonArray, null, 2); |
| 82 | + |
| 83 | +// fs.writeFileSync(path.join(__dirname, '..', 'samples.json'), stringified); |
| 84 | + |
| 85 | +// we're going to open up the ../docs/samples.md file and replce [] with the stringified JSON |
| 86 | + |
| 87 | +// const samplesMd = path.join(__dirname, '..', 'docs', 'samples.md'); |
| 88 | +// let samplesMdContents = fs.readFileSync(samplesMd, 'utf8'); |
| 89 | +// samplesMdContents += `<Samples samples={${stringified}} />`; |
| 90 | +// fs.writeFileSync(samplesMd, samplesMdContents); |
| 91 | + |
| 92 | +// save the json to the samples.json file in static |
| 93 | +fs.writeFileSync(path.join(__dirname, '..', 'static', 'samples-v2.json'), stringified); |
0 commit comments