feat: wait for initialization of container

This commit is contained in:
Nebel 2020-03-26 00:15:34 +09:00
parent 50059bce2f
commit 5eba84d391
3 changed files with 62 additions and 27 deletions

View file

@ -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 }}"]

View file

@ -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();

View file

@ -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);