Skip to content

Commit

Permalink
use gh remote source files
Browse files Browse the repository at this point in the history
  • Loading branch information
cansirin committed Dec 26, 2023
1 parent 3b2298e commit 77c5b97
Show file tree
Hide file tree
Showing 47 changed files with 62 additions and 5,032 deletions.
64 changes: 62 additions & 2 deletions packages/odin-content/contentlayer.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { defineDocumentType, makeSource } from "contentlayer/source-files";
import { spawn } from "node:child_process";
import { defineDocumentType } from "contentlayer/source-files";
import { makeSource } from "contentlayer/source-remote-files";
import { bundleMDX } from "mdx-bundler";
import { getMDXComponent } from "mdx-bundler/client/index.js";
import * as ReactDOMServer from "react-dom/server";
Expand Down Expand Up @@ -29,9 +31,64 @@ const mdxToHtml = async (mdxSource: string) => {
return html;
};

const syncContentFromGit = async (contentDir: string) => {
const syncRun = async () => {
const gitUrl = "https://github.com/kamp-us/turkce-odin-project.git";
await runBashCommand(`
if [ -d "${contentDir}" ];
then
cd "${contentDir}"; git pull;
else
git clone --depth 1 --single-branch ${gitUrl} ${contentDir};
fi
`);
};

let wasCancelled = false;
let syncInterval: NodeJS.Timeout;

const syncLoop = async () => {
console.log("Syncing content files from git");

await syncRun();

if (wasCancelled) return;

// eslint-disable-next-line @typescript-eslint/no-misused-promises
syncInterval = setTimeout(syncLoop, 1000 * 60);
};

// Block until the first sync is done
await syncLoop();

return () => {
wasCancelled = true;
clearTimeout(syncInterval);
};
};

const runBashCommand = (command: string) =>
new Promise((resolve, reject) => {
const child = spawn(command, [], { shell: true });

child.stdout.setEncoding("utf8");
child.stdout.on("data", (data: Uint8Array | string) => process.stdout.write(data));

child.stderr.setEncoding("utf8");
child.stderr.on("data", (data: Uint8Array | string) => process.stderr.write(data));

child.on("close", function (code) {
if (code === 0) {
resolve(void 0);
} else {
reject(new Error(`Command failed with exit code ${code}`));
}
});
});

const Lesson = defineDocumentType(() => ({
name: "Lesson",
filePathPattern: "**/*.mdx",
filePathPattern: "**/*.md",
fields: {
title: {
type: "string",
Expand All @@ -52,6 +109,9 @@ const Lesson = defineDocumentType(() => ({
}));

export default makeSource({
syncFiles: syncContentFromGit,
contentDirPath: "curriculum",
contentDirInclude: ["."],
documentTypes: [Lesson],
disableImportAliasWarning: true,
});
Loading

0 comments on commit 77c5b97

Please sign in to comment.