create-next-superclub
Version:
create next.js 14v for superclub-web
129 lines (108 loc) • 3.55 kB
JavaScript
const { execSync } = require("child_process");
const path = require("path");
const fs = require("fs");
const packageJson = require("../package.json");
const commander = require("commander");
const { green, cyan, yellow } = require("picocolors");
if (process.argv.length < 3) {
console.log("You have to provide a name to your app.");
console.log("For example :");
console.log(" npx create-my-boilerplate my-app");
process.exit(1);
}
const projectName = process.argv[2];
const currentPath = process.cwd();
const projectPath = path.join(currentPath, projectName);
const GIT_REPO = "https://github.com/lnuvy/next-14-js.git";
if (projectName !== ".") {
try {
fs.mkdirSync(projectPath);
} catch (err) {
console.log("Error!");
console.log(err);
console.log("============================");
if (err.code === "EEXIST") {
console.log(projectName);
console.log(
`The file ${projectName} already exist in the current directory, please give it another name.`
);
} else {
console.log(error);
}
process.exit(1);
}
}
function isInGitRepository() {
try {
execSync("git rev-parse --is-inside-work-tree", { stdio: "ignore" });
return true;
} catch (_) {}
return false;
}
function isInMercurialRepository() {
try {
execSync("hg --cwd . root", { stdio: "ignore" });
return true;
} catch (_) {}
return false;
}
function tryGitInit(root) {
let didInit = false;
try {
console.log(`${yellow("Git initializing...")}`);
execSync("git --version", { stdio: "ignore" });
if (isInGitRepository() || isInMercurialRepository()) {
return false;
}
execSync("git init", { stdio: "ignore" });
didInit = true;
execSync(`git remote add upstream ${GIT_REPO}`, { stdio: "ignore" });
execSync("git add -A", { stdio: "ignore" });
execSync('git commit -m "Initial commit from Create Next App"', {
stdio: "ignore",
});
execSync("git checkout -b installation", { stdio: "ignore" });
console.log(`${green("Git upstream remote added")}`);
return true;
} catch (e) {
console.log("Git init Error!");
console.log(e);
console.log("================================");
if (didInit) {
try {
fs.rmSync(path.join(root, ".git"), { recursive: true, force: true });
} catch (_) {}
}
return false;
}
}
async function main() {
try {
console.log(`${cyan("Hello, Runners! This is SuperClub Boilerplate!")}`);
const program = new commander.Command(packageJson.name)
.version(packageJson.version)
.arguments("<project-directory>")
.usage(`${green("<project-directory>")} [options]`)
.action((name) => {
console.log(name);
projectPath = name;
});
console.log(program);
execSync(`git clone --depth 1 ${GIT_REPO} ${projectPath}`);
if (projectName !== ".") {
process.chdir(projectPath); // cd입니다 clone을 마친 후 projectPath로 진입
}
console.log(`${green("Installing dependencies...")}`);
execSync("yarn install"); // package.json에 있는 의존성 설치
console.log(`${yellow("Removing useless files...")}`);
execSync("npx rimraf ./.git"); // 이제 보일러플레이트 git과 관련된 내용 제거
tryGitInit(projectPath); // git init and add upstream remote
console.log();
console.log();
console.log(`${green("The installation is done, this is ready to use !")}`);
} catch (error) {
console.log(error);
}
}
main();