|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { inspect } from "util"; |
| 4 | +import { mkdir, writeFile } from "fs/promises"; |
| 5 | + |
| 6 | +import inquirer from "inquirer"; |
| 7 | +import { Octokit } from "@octokit/core"; |
| 8 | +import { createOAuthDeviceAuth } from "@octokit/auth-oauth-device"; |
| 9 | +import clipboardy from "clipboardy"; |
| 10 | + |
| 11 | +import command from "./lib/command.js"; |
| 12 | +import createBranchProtection from "./lib/create-branch-protection.js"; |
| 13 | +import createCoc from "./lib/create-coc.js"; |
| 14 | +import createContributing from "./lib/create-contributing.js"; |
| 15 | +import createIssueTemplates from "./lib/create-issue-templates.js"; |
| 16 | +import createLicense from "./lib/create-license.js"; |
| 17 | +import createPackageJson from "./lib/create-package-json.js"; |
| 18 | +import createPullRequest from "./lib/create-pull-request.js"; |
| 19 | +import createReadme from "./lib/create-readme.js"; |
| 20 | +import createReleaseAction from "./lib/create-release-action.js"; |
| 21 | +import createTestAction from "./lib/create-test-action.js"; |
| 22 | +import createRepository from "./lib/create-repository.js"; |
| 23 | +import createScript from "./lib/create-script.js"; |
| 24 | +import prompts from "./lib/prompts.js"; |
| 25 | +import writePrettyFile from "./lib/write-pretty-file.js"; |
| 26 | + |
| 27 | +main(); |
| 28 | + |
| 29 | +async function main() { |
| 30 | + const { repoIsPrivate } = await inquirer.prompt({ |
| 31 | + name: "repoIsPrivate", |
| 32 | + type: "list", |
| 33 | + message: "Do you want to create a public or private repository?", |
| 34 | + choices: ["public", "private"], |
| 35 | + filter: (input) => input === "private", |
| 36 | + }); |
| 37 | + |
| 38 | + const auth = createOAuthDeviceAuth({ |
| 39 | + clientId: "e93735961b3b72ca5c02", // Create Octoherd Script OAuth app by @octoherd |
| 40 | + scopes: repoIsPrivate ? ["repo"] : ["public_repo"], |
| 41 | + async onVerification({ verification_uri, user_code }) { |
| 42 | + console.log("Open %s", verification_uri); |
| 43 | + |
| 44 | + await clipboardy.write(user_code); |
| 45 | + console.log("Paste code: %s (copied to your clipboard)", user_code); |
| 46 | + |
| 47 | + await inquirer.prompt({ |
| 48 | + name: "grant_access", |
| 49 | + type: "confirm", |
| 50 | + message: "Press <enter> when ready", |
| 51 | + }); |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | + const { token } = await auth({ type: "oauth" }); |
| 56 | + const octokit = new Octokit({ auth: token }); |
| 57 | + octokit.hook.before("request", async (options) => { |
| 58 | + const { method, url, ...parameters } = octokit.request.endpoint.parse( |
| 59 | + options |
| 60 | + ); |
| 61 | + console.log(`> ${method} ${url.replace("https://api.github.com", "")}`); |
| 62 | + for (const [name, value] of Object.entries(parameters.headers)) { |
| 63 | + console.log(` ${name}: ${value}`); |
| 64 | + } |
| 65 | + if (parameters.body) { |
| 66 | + console.log(``); |
| 67 | + for (const [name, value] of Object.entries(parameters.body)) { |
| 68 | + console.log(` ${name}: ${inspect(value)}`); |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + // get user information |
| 74 | + const { |
| 75 | + data: { id: userId, login, name, email, blog: website, twitter_username }, |
| 76 | + } = await octokit.request("GET /user"); |
| 77 | + |
| 78 | + try { |
| 79 | + const answers = await prompts({ |
| 80 | + login, |
| 81 | + name, |
| 82 | + email, |
| 83 | + website, |
| 84 | + twitter_username, |
| 85 | + }); |
| 86 | + |
| 87 | + const [owner, repo] = answers.repository.split("/"); |
| 88 | + const isUserRepo = answers.repository.startsWith(login); |
| 89 | + |
| 90 | + // create project folder and chdir into it |
| 91 | + console.log(`Creating ${answers.path}`); |
| 92 | + await mkdir(answers.path, { recursive: true }); |
| 93 | + process.chdir(answers.path); |
| 94 | + |
| 95 | + await command("git init"); |
| 96 | + await command("git checkout -b main"); |
| 97 | + |
| 98 | + createLicense(answers.licenseName); |
| 99 | + console.log(`LICENSE created`); |
| 100 | + |
| 101 | + createCoc(answers.cocEmail); |
| 102 | + console.log(`CODE_OF_CONDUCT.md created`); |
| 103 | + |
| 104 | + createContributing({ owner, repo, packageName: answers.packageName }); |
| 105 | + console.log(`CONTRIBUTING.md created`); |
| 106 | + |
| 107 | + await createReadme({ |
| 108 | + addWip: true, |
| 109 | + repository: answers.repository, |
| 110 | + repo, |
| 111 | + description: answers.description, |
| 112 | + }); |
| 113 | + console.log(`README.md created`); |
| 114 | + |
| 115 | + await command("git add LICENSE"); |
| 116 | + await command("git commit -m 'docs(LICENSE): ISC'"); |
| 117 | + |
| 118 | + await command("git add CODE_OF_CONDUCT.md"); |
| 119 | + await command( |
| 120 | + "git commit -m 'docs(CODE_OF_CONDUCT): Contributor Covenant'" |
| 121 | + ); |
| 122 | + |
| 123 | + await command("git add CONTRIBUTING.md"); |
| 124 | + await command("git commit -m 'docs(CONTRIBUTING): initial version'"); |
| 125 | + |
| 126 | + await command("git add README.md"); |
| 127 | + await command("git commit -m 'docs(README): initial version'"); |
| 128 | + |
| 129 | + await createIssueTemplates(answers.packageName); |
| 130 | + await command("git add .github/ISSUE_TEMPLATE"); |
| 131 | + await command("git commit -m 'docs(ISSUE_TEMPLATES): initial version'"); |
| 132 | + |
| 133 | + const { |
| 134 | + data: { id: repositoryId }, |
| 135 | + } = await createRepository(octokit, { |
| 136 | + isUserRepo, |
| 137 | + owner, |
| 138 | + repo, |
| 139 | + description: answers.description, |
| 140 | + repoIsPrivate, |
| 141 | + }); |
| 142 | + |
| 143 | + await command( |
| 144 | + `git remote add origin [email protected]:${answers.repository}.git` |
| 145 | + ); |
| 146 | + await command(`git push -u origin HEAD`); |
| 147 | + await command(`git checkout -b initial-version`); |
| 148 | + |
| 149 | + createReadme({ |
| 150 | + repo, |
| 151 | + description: answers.description, |
| 152 | + }); |
| 153 | + |
| 154 | + await command(`git commit README.md -m 'docs(README): remove WIP note'`); |
| 155 | + await command(`git push -u origin HEAD`); |
| 156 | + |
| 157 | + let ownerId = userId; |
| 158 | + if (!isUserRepo) { |
| 159 | + const { data } = await octokit.request("GET /orgs/{org}", { |
| 160 | + org: owner, |
| 161 | + }); |
| 162 | + ownerId = data.id; |
| 163 | + } |
| 164 | + |
| 165 | + await createPullRequest(octokit, { |
| 166 | + owner, |
| 167 | + repo, |
| 168 | + ownerId, |
| 169 | + repositoryId, |
| 170 | + }); |
| 171 | + |
| 172 | + await createPackageJson(owner, repo, answers); |
| 173 | + console.log("Install dependencies"); |
| 174 | + const dependencies = ["@octoherd/cli"]; |
| 175 | + |
| 176 | + await command(`npm install ${dependencies.join(" ")}`); |
| 177 | + |
| 178 | + await command(`git add package.json`); |
| 179 | + await command(`git commit -m 'build(package): initial version'`); |
| 180 | + await command(`git add package-lock.json`); |
| 181 | + await command(`git commit -m 'build(package): lock file'`); |
| 182 | + |
| 183 | + console.log("Create branch protection for main"); |
| 184 | + try { |
| 185 | + await createBranchProtection(octokit, { owner, repo }); |
| 186 | + } catch (error) { |
| 187 | + if (error.status !== 403) throw error; |
| 188 | + |
| 189 | + console.log( |
| 190 | + "Branch protection could not be enabled, because the repository is private and belongs to an organization using the free plan" |
| 191 | + ); |
| 192 | + } |
| 193 | + |
| 194 | + const ignorePaths = ["node_modules"]; |
| 195 | + await writePrettyFile(".gitignore", ignorePaths.join("\n")); |
| 196 | + await command(`git add .gitignore`); |
| 197 | + await command( |
| 198 | + `git commit -m 'build(gitignore): ${ignorePaths.join(", ")}'` |
| 199 | + ); |
| 200 | + |
| 201 | + console.log("create script.js and cli.js"); |
| 202 | + await createScript(answers); |
| 203 | + await writeFile( |
| 204 | + // we cannot use writePrettyFile, it blows because of the #! in the first line |
| 205 | + "cli.js", |
| 206 | + `#!/usr/bin/env node |
| 207 | +
|
| 208 | +import { script } from "./script.js"; |
| 209 | +import { run } from "@octoherd/cli/run"; |
| 210 | +
|
| 211 | +run(script); |
| 212 | +` |
| 213 | + ); |
| 214 | + |
| 215 | + await command(`git add script.js cli.js`); |
| 216 | + await command(`git commit -m 'feat: initial version'`); |
| 217 | + |
| 218 | + await createReadme({ |
| 219 | + addBadges: true, |
| 220 | + repo, |
| 221 | + description: answers.description, |
| 222 | + packageName: answers.packageName, |
| 223 | + repository: answers.repository, |
| 224 | + }); |
| 225 | + await command(`git commit README.md -m 'docs(README): badges'`); |
| 226 | + |
| 227 | + await createReadme({ |
| 228 | + addBadges: true, |
| 229 | + addUsage: true, |
| 230 | + owner, |
| 231 | + repo, |
| 232 | + description: answers.description, |
| 233 | + packageName: answers.packageName, |
| 234 | + repository: answers.repository, |
| 235 | + scriptOptions: answers.scriptOptions, |
| 236 | + }); |
| 237 | + await command(`git commit README.md -m 'docs(README): usage'`); |
| 238 | + |
| 239 | + console.log("Create actions"); |
| 240 | + await createReleaseAction({ owner }); |
| 241 | + await command(`git add .github/workflows/release.yml`); |
| 242 | + await command(`git commit -m 'ci(release): initial version'`); |
| 243 | + |
| 244 | + await createTestAction(); |
| 245 | + await command(`git add .github/workflows/test.yml`); |
| 246 | + await command(`git commit -m 'ci(test): initial version'`); |
| 247 | + |
| 248 | + await command(`git push`); |
| 249 | + |
| 250 | + console.log(`Your new repository is here: |
| 251 | +https://github.com/${answers.repository} |
| 252 | +
|
| 253 | +To change into the new directory, do |
| 254 | +$ cd ${answers.path}`); |
| 255 | + } catch (error) { |
| 256 | + console.log(error); |
| 257 | + } |
| 258 | + |
| 259 | + console.log("All done."); |
| 260 | +} |
0 commit comments