Skip to content

Commit

Permalink
Add docs and samples to JSON (#948)
Browse files Browse the repository at this point in the history
So we can display them in the editor
  • Loading branch information
GarboMuffin authored Aug 25, 2023
1 parent b0f8c20 commit 7d2c1b4
Showing 1 changed file with 51 additions and 38 deletions.
89 changes: 51 additions & 38 deletions development/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ExtensionFile extends BuildFile {
}

class HomepageFile extends BuildFile {
constructor(extensionFiles, extensionImages, docs, samples, mode) {
constructor(extensionFiles, extensionImages, withDocs, samples, mode) {
super(pathUtil.join(__dirname, "homepage-template.ejs"));

/** @type {Record<string, ExtensionFile>} */
Expand All @@ -128,8 +128,8 @@ class HomepageFile extends BuildFile {
/** @type {Record<string, string>} */
this.extensionImages = extensionImages;

/** @type {Record<string, DocsFile>} */
this.docs = docs;
/** @type {Map<string, SampleFile[]>} */
this.withDocs = withDocs;

/** @type {SampleFile[]} */
this.samples = samples;
Expand Down Expand Up @@ -178,27 +178,13 @@ class HomepageFile extends BuildFile {
.slice(0, 5)
.map((i) => i[0]);

/** @type {Map<string, SampleFile[]>} */
const samplesBySlug = new Map();
for (const sample of this.samples) {
for (const url of sample.getExtensionURLs()) {
const slug = new URL(url).pathname.substring(1).replace(".js", "");

if (samplesBySlug.has(slug)) {
samplesBySlug.get(slug).push(sample);
} else {
samplesBySlug.set(slug, [sample]);
}
}
}

const extensionMetadata = Object.fromEntries(
featuredExtensionSlugs.map((slug) => [
slug,
{
...this.extensionFiles[slug].getMetadata(),
hasDocumentation: !!this.docs[slug],
samples: samplesBySlug.get(slug) || [],
hasDocumentation: this.withDocs.has(slug),
samples: this.samples.get(slug) || [],
},
])
);
Expand All @@ -217,14 +203,20 @@ class HomepageFile extends BuildFile {
}

class JSONMetadataFile extends BuildFile {
constructor(extensionFiles, extensionImages) {
constructor(extensionFiles, extensionImages, withDocs, samples) {
super(null);

/** @type {Record<string, ExtensionFile>} */
this.extensionFiles = extensionFiles;

/** @type {Record<string, string>} */
this.extensionImages = extensionImages;

/** @type {Set<string>} */
this.withDocs = withDocs;

/** @type {Map<string, SampleFile[]>} */
this.samples = samples;
}

getType() {
Expand Down Expand Up @@ -252,6 +244,13 @@ class JSONMetadataFile extends BuildFile {
if (metadata.original.length) {
extension.original = metadata.original;
}
if (this.withDocs.has(extensionSlug)) {
extension.docs = true;
}
const samples = this.samples.get(extensionSlug);
if (samples) {
extension.samples = samples.map((i) => i.getTitle());
}

extensions.push(extension);
}
Expand Down Expand Up @@ -477,15 +476,37 @@ class Builder {
build.files[`/images/${filename}`] = new ImageFileClass(absolutePath);
}

/** @type {Set<string>} */
const extensionsWithDocs = new Set();

/** @type {Map<string, SampleFile[]>} */
const samples = new Map();
for (const [filename, absolutePath] of recursiveReadDirectory(
this.samplesRoot
)) {
if (!filename.endsWith(".sb3")) {
continue;
}

const file = new SampleFile(absolutePath);
for (const url of file.getExtensionURLs()) {
const slug = new URL(url).pathname.substring(1).replace(".js", "");
if (samples.has(slug)) {
samples.get(slug).push(file);
} else {
samples.set(slug, [file]);
}
}
build.files[`/samples/${filename}`] = file;
}

if (this.mode !== "desktop") {
for (const [filename, absolutePath] of recursiveReadDirectory(
this.websiteRoot
)) {
build.files[`/${filename}`] = new BuildFile(absolutePath);
}

/** @type {Record<string, DocsFile>} */
const docs = {};
for (const [filename, absolutePath] of recursiveReadDirectory(
this.docsRoot
)) {
Expand All @@ -494,23 +515,10 @@ class Builder {
}
const extensionSlug = filename.split(".")[0];
const file = new DocsFile(absolutePath, extensionSlug);
docs[extensionSlug] = file;
extensionsWithDocs.add(extensionSlug);
build.files[`/${extensionSlug}.html`] = file;
}

/** @type {SampleFile[]} */
const samples = [];
for (const [filename, absolutePath] of recursiveReadDirectory(
this.samplesRoot
)) {
if (!filename.endsWith(".sb3")) {
continue;
}
const file = new SampleFile(absolutePath);
build.files[`/samples/${filename}`] = file;
samples.push(file);
}

const scratchblocksPath = pathUtil.join(
__dirname,
"../node_modules/scratchblocks/build/scratchblocks.min.js"
Expand All @@ -522,15 +530,20 @@ class Builder {
build.files["/index.html"] = new HomepageFile(
extensionFiles,
extensionImages,
docs,
extensionsWithDocs,
samples,
this.mode
);
build.files["/sitemap.xml"] = new SitemapFile(build);
}

build.files["/generated-metadata/extensions-v0.json"] =
new JSONMetadataFile(extensionFiles, extensionImages);
new JSONMetadataFile(
extensionFiles,
extensionImages,
extensionsWithDocs,
samples
);

for (const [oldPath, newPath] of Object.entries(compatibilityAliases)) {
build.files[oldPath] = build.files[newPath];
Expand Down

0 comments on commit 7d2c1b4

Please sign in to comment.