This commit is contained in:
Stefan Pejcic
2024-05-08 19:52:27 +02:00
parent 9c8d080b57
commit 80303fadd5
2509 changed files with 0 additions and 594172 deletions

View File

@@ -1,3 +0,0 @@
export const GITHUB_ORG = "refinedev";
export const GITHUB_REPO = "refine";
export const GITHUB_BRANCH = "master";

View File

@@ -1,76 +0,0 @@
import got from "got";
import tar from "tar";
import { Stream } from "stream";
import { promisify } from "util";
import { join } from "path";
import { createWriteStream, promises as fs } from "fs";
const pipeline = promisify(Stream.pipeline);
const TEMP_PREFIX = ".refine-example.temp";
async function downloadTar(url: string) {
const tempFile = join(process.cwd(), `${TEMP_PREFIX}-${Date.now()}`);
try {
await pipeline(got.stream(url), createWriteStream(tempFile));
return tempFile;
} catch (err) {
try {
await fs.unlink(tempFile);
} catch (err) {
// ignore
}
return undefined;
}
}
export async function downloadAndExtract({
root,
name,
branch,
repo,
org,
}: {
root: string;
name: string;
branch: string;
repo: string;
org: string;
}) {
const tempFile = await downloadTar(
`https://codeload.github.com/${org}/${repo}/tar.gz/${branch}`,
);
if (!tempFile) {
return "download-failed";
}
try {
await tar.x({
file: tempFile,
cwd: root,
strip: 3,
filter: (p) => {
if (p.includes(`${repo}-${branch}/examples/${name}/`)) {
return true;
}
return false;
},
});
} catch (err) {
try {
await fs.unlink(tempFile);
} catch (err) {
// ignore
}
return "extract-failed";
}
try {
await fs.unlink(tempFile);
} catch (err) {
// ignore
}
return "success";
}

View File

@@ -1,24 +0,0 @@
import got from "got";
async function isUrlOk(url: string): Promise<boolean> {
const res = await got.head(url).catch((e) => e);
return res.statusCode === 200;
}
export async function existsInRepo({
organization,
repository,
example,
branch,
}: {
organization: string;
repository: string;
example: string;
branch: string;
}): Promise<boolean> {
return isUrlOk(
`https://api.github.com/repos/${organization}/${repository}/contents/examples/${encodeURIComponent(
example,
)}?ref=${branch}`,
);
}

View File

@@ -1,68 +0,0 @@
import execa from "execa";
import path from "path";
import rimraf from "rimraf";
function isInGitRepository(root: string): boolean {
try {
execa.commandSync("git rev-parse --is-inside-work-tree", {
stdio: "ignore",
cwd: root,
});
return true;
} catch (_) {}
return false;
}
function isInMercurialRepository(root: string): boolean {
try {
execa.commandSync("hg --cwd . root", { stdio: "ignore", cwd: root });
return true;
} catch (_) {}
return false;
}
export function gitInit(root: string, message: string) {
let didInit = false;
try {
execa.commandSync("git --version", { stdio: "ignore", cwd: root });
} catch (_) {
return "git-not-found";
}
if (isInGitRepository(root) || isInMercurialRepository(root)) {
return "already-in-repository";
}
try {
execa.commandSync("git init", { stdio: "ignore", cwd: root });
} catch (e) {
return "git-init-failed";
}
didInit = true;
try {
execa.commandSync("git checkout -b main", {
stdio: "ignore",
cwd: root,
});
execa.commandSync("git add -A", { stdio: "ignore", cwd: root });
execa.sync("git", ["commit", `--message="${message}"`], {
stdio: "ignore",
cwd: root,
});
} catch (e) {
if (didInit) {
try {
rimraf.sync(path.join(root, ".git"));
} catch (_) {}
}
return "git-commit-failed";
}
return "success";
}

View File

@@ -1,211 +0,0 @@
import ora from "ora";
import path from "path";
import chalk from "chalk";
import boxen from "boxen";
import { gitInit } from "./git-init";
import { makeDir } from "./make-dir";
import { existsInRepo } from "./exists-in-repo";
import { downloadAndExtract } from "./download-and-extract";
import { findPM, installDependencies } from "./install-dependencies";
import { GITHUB_ORG, GITHUB_REPO, GITHUB_BRANCH } from "./constants";
const run = async (
example: string | boolean | undefined,
destination?: string,
) => {
const pm = findPM();
if (typeof example !== "string") {
ora("You must specify an example name").fail();
console.log(
boxen(
[
chalk`You can find {bold refine} examples at:`,
"",
chalk`{dim.cyan github.com/}{cyan refinedev/refine/tree/master/examples}`,
].join("\n"),
{
title: chalk`No example provided`,
titleAlignment: "center",
borderStyle: "round",
borderColor: "gray",
padding: 1,
textAlignment: "center",
margin: 1,
float: "center",
},
),
);
process.exit(1);
}
const root = path.resolve(destination || example);
const existSpinner = ora("Checking if example exists in refine").start();
const found = await existsInRepo({
organization: GITHUB_ORG,
repository: GITHUB_REPO,
example,
branch: GITHUB_BRANCH,
});
if (found) {
existSpinner.succeed("Example found in refine repository");
} else {
existSpinner.fail(
`Could not locate an example named ${chalk.red(`"${example}"`)}`,
);
console.log(
boxen(
[
chalk`You can find {bold refine} examples at:`,
"",
chalk`{dim.cyan github.com/}{cyan refinedev/refine/tree/master/examples}`,
].join("\n"),
{
title: chalk`Example not found`,
titleAlignment: "center",
borderStyle: "round",
borderColor: "gray",
padding: 1,
textAlignment: "center",
margin: 1,
float: "center",
},
),
);
process.exit(1);
}
const cdPath = root.includes(path.resolve(process.cwd()))
? root.replace(path.resolve(process.cwd()), ".")
: root;
const dirSpinner = ora(`Creating directory ${chalk.cyan(cdPath)}.`).start();
const dirStatus = await makeDir(root);
if (dirStatus === "already") {
dirSpinner.warn(
`Directory ${chalk.cyan(
cdPath,
)} already exists. Files will be overwritten.`,
);
} else if (dirStatus === "failed") {
dirSpinner.fail(`Failed to create directory ${chalk.cyan(cdPath)}.`);
process.exit(1);
} else {
dirSpinner.succeed(`Directory ${chalk.cyan(cdPath)} created.`);
}
const downloadSpinner = ora(
`Downloading files for example ${chalk.cyan(
example,
)}. This might take a moment.`,
).start();
const downloadStatus = await downloadAndExtract({
root,
name: example,
branch: GITHUB_BRANCH,
repo: GITHUB_REPO,
org: GITHUB_ORG,
});
if (downloadStatus === "download-failed") {
downloadSpinner.fail(
`Failed to download files for example ${chalk.cyan(example)}.`,
);
process.exit(1);
}
if (downloadStatus === "extract-failed") {
downloadSpinner.fail(
`Failed to extract files for example ${chalk.cyan(example)}.`,
);
process.exit(1);
}
downloadSpinner.succeed(
`Files downloaded and extracted for example ${chalk.cyan(example)}.`,
);
const installSpinner = ora(
"Installing packages. This might take a couple of minutes.",
).start();
const installStatus = await installDependencies(root);
if (installStatus) {
installSpinner.succeed("Packages installed successfully.");
} else {
installSpinner.fail(
"Failed to install packages. You can try again manually.",
);
}
const gitSpinner = ora(
`Initializing Git in ${chalk.cyan(cdPath)}.`,
).start();
const gitStatus = gitInit(root, "Initial commit from Create Refine App");
if (gitStatus === "git-not-found") {
gitSpinner.warn(
`Git was not found in your PATH. Skipping Git initialization.`,
);
}
if (gitStatus === "already-in-repository") {
gitSpinner.warn(
`Directory ${chalk.cyan(
cdPath,
)} is already a Git repository. Skipping Git initialization.`,
);
}
if (gitStatus === "git-init-failed") {
gitSpinner.warn(
`Failed to initialize Git repository in ${chalk.cyan(cdPath)}.`,
);
}
if (gitStatus === "git-commit-failed") {
gitSpinner.warn(
`Failed to commit initial commit to Git repository in ${chalk.cyan(
cdPath,
)}.`,
);
}
if (gitStatus === "success") {
gitSpinner.succeed("Created Git repository with initial commit.");
}
const pmRun = pm === "yarn" ? "" : "run ";
console.log(
boxen(
[
chalk`Created {cyan ${example}} at {cyan ${cdPath}}`,
"",
chalk`Start using your new {bold refine} app by running:`,
"",
chalk` {bold cd} {cyan ${cdPath}}`,
chalk` {bold ${pm} ${pmRun}}{cyan start}`,
].join("\n"),
{
// title: `create-refine-app${version ? ` v${version}` : ""}`,
title: chalk`{bold.green Success!}`,
titleAlignment: "center",
borderStyle: "round",
padding: 1,
float: "center",
margin: 1,
borderColor: "gray",
},
),
);
};
export default run;

View File

@@ -1,23 +0,0 @@
import execa from "execa";
import whichPMRuns from "which-pm-runs";
export function findPM() {
try {
const { name } = whichPMRuns() || {};
return name ?? "npm";
} catch (err) {
return "npm";
}
}
export async function installDependencies(root: string) {
try {
await execa(findPM(), ["install"], {
cwd: root,
stdio: "ignore",
});
return true;
} catch (err) {
return false;
}
}

View File

@@ -1,16 +0,0 @@
import fs from "fs";
export async function makeDir(
root: string,
options = { recursive: true },
): Promise<"already" | "success" | "failed"> {
try {
if (fs.existsSync(root)) {
return "already";
}
await fs.promises.mkdir(root, options);
return "success";
} catch (err) {
return "failed";
}
}