mirror of
https://github.com/kou029w/sync-codesandbox.git
synced 2025-01-18 15:38:04 +00:00
feat: wait for initialization of container
This commit is contained in:
parent
50059bce2f
commit
5eba84d391
3 changed files with 62 additions and 27 deletions
|
@ -7,7 +7,10 @@ inputs:
|
||||||
repo:
|
repo:
|
||||||
description: GitHub owner and repository names in the form of "owner/repo".
|
description: GitHub owner and repository names in the form of "owner/repo".
|
||||||
required: false
|
required: false
|
||||||
|
url:
|
||||||
|
description: The URL in the browser pane of CodeSandbox. Eg. "https://fe8lf.sse.codesandbox.io/"
|
||||||
|
required: false
|
||||||
runs:
|
runs:
|
||||||
using: docker
|
using: docker
|
||||||
image: Dockerfile
|
image: Dockerfile
|
||||||
args: ["${{ inputs.repo }}"]
|
args: ["--url", "${{ inputs.url }}", "${{ inputs.repo }}"]
|
||||||
|
|
45
index.js
45
index.js
|
@ -1,8 +1,6 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const arg = require("arg");
|
|
||||||
const puppeteer = require("puppeteer");
|
const puppeteer = require("puppeteer");
|
||||||
const { GITHUB_REPOSITORY } = process.env;
|
|
||||||
|
|
||||||
function inDocker() {
|
function inDocker() {
|
||||||
return new Promise(resolve =>
|
return new Promise(resolve =>
|
||||||
|
@ -10,33 +8,30 @@ function inDocker() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
/**
|
||||||
const args = arg({
|
* @param {import("puppeteer").Page} page
|
||||||
"-h": "--help",
|
* @param {URL} url
|
||||||
"--help": Boolean
|
*/
|
||||||
});
|
async function initializeSandbox(page, url) {
|
||||||
|
const loadingTitle = "Sandbox - CodeSandbox";
|
||||||
if (args["--help"]) {
|
const loadingText = "Initializing Sandbox Container";
|
||||||
console.log(
|
await page.goto(url.href);
|
||||||
[
|
await page.waitForFunction(
|
||||||
`Usage: ${process.argv0} [options] repo`,
|
`document.title !== "${loadingTitle}" && !document.body.textContent.includes("${loadingText}")`
|
||||||
"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 {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 options = (await inDocker()) ? { args: ["--no-sandbox"] } : {};
|
||||||
const browser = await puppeteer.launch(options);
|
const browser = await puppeteer.launch(options);
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
|
|
||||||
|
if (url) await initializeSandbox(page, url);
|
||||||
|
|
||||||
const response = await page.goto(`https://codesandbox.io/s/github/${repo}`);
|
const response = await page.goto(`https://codesandbox.io/s/github/${repo}`);
|
||||||
await browser.close();
|
await browser.close();
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,45 @@
|
||||||
#!/usr/bin/env node
|
#!/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.
|
// NOTE: Unhandled promise rejection terminates Node.js process with non-zero exit code.
|
||||||
process.on("unhandledRejection", event => {
|
process.on("unhandledRejection", event => {
|
||||||
throw event;
|
throw event;
|
||||||
});
|
});
|
||||||
|
|
||||||
require("./index.js")();
|
require("./index.js")(repo, url);
|
||||||
|
|
Loading…
Add table
Reference in a new issue