diff --git a/action.yml b/action.yml index dc97954..18ff0e6 100644 --- a/action.yml +++ b/action.yml @@ -7,7 +7,10 @@ inputs: repo: description: GitHub owner and repository names in the form of "owner/repo". required: false + url: + description: The URL in the browser pane of CodeSandbox. Eg. "https://fe8lf.sse.codesandbox.io/" + required: false runs: using: docker image: Dockerfile - args: ["${{ inputs.repo }}"] + args: ["--url", "${{ inputs.url }}", "${{ inputs.repo }}"] diff --git a/index.js b/index.js index 4292207..7bfdd9c 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,6 @@ // @ts-check const fs = require("fs"); -const arg = require("arg"); const puppeteer = require("puppeteer"); -const { GITHUB_REPOSITORY } = process.env; function inDocker() { return new Promise(resolve => @@ -10,33 +8,30 @@ function inDocker() { ); } -async function main() { - const args = arg({ - "-h": "--help", - "--help": Boolean - }); - - if (args["--help"]) { - console.log( - [ - `Usage: ${process.argv0} [options] repo`, - "Options:", - " -h, --help print command line options" - ].join("\n") - ); - return; - } - - const repo = args._[0] || GITHUB_REPOSITORY; - if (repo == null) { - throw new Error( - "sync-codesandbox requires a GITHUB_REPOSITORY environment variable like 'owner/repo'" - ); - } +/** + * @param {import("puppeteer").Page} page + * @param {URL} url + */ +async function initializeSandbox(page, url) { + const loadingTitle = "Sandbox - CodeSandbox"; + const loadingText = "Initializing Sandbox Container"; + await page.goto(url.href); + await page.waitForFunction( + `document.title !== "${loadingTitle}" && !document.body.textContent.includes("${loadingText}")` + ); +} +/** + * @param {string} repo GitHub owner and repository names in the form of "owner/repo". + * @param {URL} url The URL in the browser pane of CodeSandbox. + */ +async function main(repo, url) { const options = (await inDocker()) ? { args: ["--no-sandbox"] } : {}; const browser = await puppeteer.launch(options); const page = await browser.newPage(); + + if (url) await initializeSandbox(page, url); + const response = await page.goto(`https://codesandbox.io/s/github/${repo}`); await browser.close(); diff --git a/sync-codesandbox b/sync-codesandbox index f7ee670..7754383 100755 --- a/sync-codesandbox +++ b/sync-codesandbox @@ -1,8 +1,45 @@ #!/usr/bin/env node +const args = require("arg")({ + "-h": "--help", + "--help": Boolean, + "--url": String +}); + +if (args["--help"]) { + console.log( + [ + "Usage: sync-codesandbox [options] repo", + " repo GitHub owner and repository names.", + " eg. owner/repo", + "Options:", + " -h, --help Print command line options.", + " --url The URL in the browser pane of CodeSandbox.", + " eg. https://fe8lf.sse.codesandbox.io/" + ].join("\n") + ); + return; +} + +const repo = args._[0] || process.env.GITHUB_REPOSITORY; +if (!repo) { + throw new Error( + "sync-codesandbox requires a GITHUB_REPOSITORY environment variable like 'owner/repo'" + ); +} + +const url = + args["--url"] && + new URL( + [ + args["--url"].match(/^\s*https?:\/\//) ? "" : "https://", + args["--url"].trim() + ].join("") + ); + // NOTE: Unhandled promise rejection terminates Node.js process with non-zero exit code. process.on("unhandledRejection", event => { throw event; }); -require("./index.js")(); +require("./index.js")(repo, url);