Skip to content

Commit

Permalink
Add query params
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Mar 7, 2024
1 parent dcd6eda commit 154efa1
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/fsInitializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import importFromGist from "./gist.ts";

const defaultGem = "octokit";

const mainRbHeader = (gem: string) => `# This is a Ruby WASI playground
const mainRbHeader = (gem: string | null) => `# This is a Ruby WASI playground
# You can run any Ruby code here and see the result
# You can also install gems using a Gemfile and the "Bundle install" button.
require "${gem}"
`;
${gem ? `\nrequire "${gem}"\n` : ""}`;

const mainRb = `${mainRbHeader(defaultGem)}
# RunRuby.dev comes with a WASI-compatible Faraday adapter
Expand Down Expand Up @@ -36,11 +34,16 @@ gem "${gem}"
`
);

const buildStarter = ({ gem, main }: { gem: string, main: string }) => {
return [
const buildStarter = ({ gem, main }: { gem: string | null, main: string }) => {
const result = [
{ filename: "main.rb", content: main },
{ filename: "Gemfile", content: gemfileContents(gem) }
];

if (gem) {
result.push({ filename: "Gemfile", content: gemfileContents(gem) })
}

return result;
};

export const composeInitialFS = async () => {
Expand All @@ -56,10 +59,19 @@ export const composeInitialFS = async () => {
}
}

const gem = getQueryParam("gem");
if (gem) {
return { files: buildStarter({ gem, main: mainRbHeader(gem) }) };
return buildMainRB();
};

const buildMainRB = () => {
const initialGem = getQueryParam("gem");
const initialCode = getQueryParam("code");

if (!initialGem && !initialCode) {
return { files: buildStarter({ gem: defaultGem, main: mainRb }) };
}

return { files: buildStarter({ gem: defaultGem, main: mainRb }) };
const code = initialCode ? decodeURIComponent(atob(initialCode)) : "";
const main = mainRbHeader(initialGem) + "\n" + code;

return { files: buildStarter({ gem: initialGem, main }) };
};

0 comments on commit 154efa1

Please sign in to comment.