Skip to content

Commit 1a22c40

Browse files
restore sample prep for marketing site
1 parent 44ba86a commit 1a22c40

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

scripts/prebuild.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ if [ -d "../defang" ]; then
1414
else
1515
DEFANG_PATH=$(readlink -f ./defang)
1616
fi
17+
if [ -d "../samples" ]; then
18+
SAMPLES_PATH=$(readlink -f ../samples)
19+
else
20+
SAMPLES_PATH=$(readlink -f ./samples)
21+
fi
1722

1823
cd "$DEFANG_PATH/src/cmd/gendocs" && go run main.go "$CLI_DOCS_PATH"
1924
cd "$CWD"
2025
node scripts/prep-cli-docs.js
26+
node scripts/prep-samples.js "$SAMPLES_PATH/samples"

scripts/prep-samples.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)