From 9dbb52807fe2794639871092b2b8ba91c76c33b0 Mon Sep 17 00:00:00 2001 From: Al Calzone Date: Sun, 16 Dec 2018 12:53:09 +0100 Subject: [PATCH] Initialize the git repo automatically Fixes #13 --- CHANGELOG.md | 1 + build/cli.js | 20 +++++++++++++++++++- build/lib/questions.js | 8 ++++++++ src/cli.ts | 21 ++++++++++++++++++++- src/lib/questions.ts | 9 +++++++++ 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d4eec31..b2c108c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## __WORK_IN_PROGRESS__ +* (AlCalzone) Initialize the git repo automatically (fixes #13) * (AlCalzone) When using TypeScript, perform an initial build run (fixes #27) * (AlCalzone) When formatting files, clear lines with only whitespace (fixes #30) * (AlCalzone) Enable resolveJsonModule for JS files (fixes #26) diff --git a/build/cli.js b/build/cli.js index a3f4715a..ca43c2a5 100644 --- a/build/cli.js +++ b/build/cli.js @@ -12,7 +12,7 @@ const tools_1 = require("./lib/tools"); const rootDir = path.resolve(yargs.argv.target || process.cwd()); /** Asks a series of questions on the CLI */ async function ask() { - let answers = {}; + let answers = { cli: true }; for (const q of questions_1.questionsAndText) { // Headlines if (typeof q === "string") { @@ -63,6 +63,8 @@ function logProgress(message) { const installDependencies = !yargs.argv.noInstall || !!yargs.argv.install; /** Whether an initial build should be performed */ let buildTypeScript; +/** Whether the initial commit should be performed automatically */ +let gitCommit; /** CLI-specific functionality for creating the adapter directory */ async function setupProject_CLI(answers, files) { const rootDirName = path.basename(rootDir); @@ -78,6 +80,19 @@ async function setupProject_CLI(answers, files) { await tools_1.executeCommand(tools_1.isWindows ? "npm.cmd" : "npm", ["run", "build"], { cwd: targetDir, stdout: "ignore" }); } } + if (gitCommit) { + logProgress("Initializing git repo"); + // As described here: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/ + const gitCommandArgs = [ + ["init"], + ["add", "."], + ["commit", "-m", `"Initial commit"`], + ["remote", "add", "origin", `https://github.com/${answers.authorGithub}/ioBroker.${answers.adapterName}`], + ]; + for (const args of gitCommandArgs) { + await tools_1.executeCommand("git", args, { cwd: targetDir, stdout: "ignore", stderr: "ignore" }); + } + } console.log(); console.log(ansi_colors_1.blueBright("All done! Have fun programming! ") + ansi_colors_1.red("♥")); } @@ -89,6 +104,9 @@ async function setupProject_CLI(answers, files) { if (buildTypeScript) maxSteps++; } + gitCommit = answers.gitCommit === "yes"; + if (gitCommit) + maxSteps++; logProgress("Generating files"); const files = await createAdapter_1.createFiles(answers); await setupProject_CLI(answers, files); diff --git a/build/lib/questions.js b/build/lib/questions.js index bafc8002..8189027b 100644 --- a/build/lib/questions.js +++ b/build/lib/questions.js @@ -149,6 +149,14 @@ exports.questionsAndText = [ message: "What's your email address?", action: actionsAndTransformers_1.checkEmail, }, + { + condition: { name: "cli", value: true }, + type: "select", + name: "gitCommit", + message: "Initialize the GitHub repo automatically?", + initial: "no", + choices: ["yes", "no"], + }, { type: "select", name: "license", diff --git a/src/cli.ts b/src/cli.ts index b4b2c566..052ee186 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -12,7 +12,7 @@ const rootDir = path.resolve(yargs.argv.target || process.cwd()); /** Asks a series of questions on the CLI */ async function ask() { - let answers: Record = {}; + let answers: Record = {cli: true}; for (const q of questionsAndText) { // Headlines if (typeof q === "string") { @@ -65,6 +65,8 @@ function logProgress(message: string) { const installDependencies = !yargs.argv.noInstall || !!yargs.argv.install; /** Whether an initial build should be performed */ let buildTypeScript: boolean; +/** Whether the initial commit should be performed automatically */ +let gitCommit: boolean; /** CLI-specific functionality for creating the adapter directory */ async function setupProject_CLI(answers: Answers, files: File[]) { @@ -84,6 +86,21 @@ async function setupProject_CLI(answers: Answers, files: File[]) { await executeCommand(isWindows ? "npm.cmd" : "npm", ["run", "build"], { cwd: targetDir, stdout: "ignore" }); } } + + if (gitCommit) { + logProgress("Initializing git repo"); + // As described here: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/ + const gitCommandArgs = [ + ["init"], + ["add", "."], + ["commit", "-m", `"Initial commit"`], + ["remote", "add", "origin", `https://github.com/${answers.authorGithub}/ioBroker.${answers.adapterName}`], + ]; + for (const args of gitCommandArgs) { + await executeCommand("git", args, { cwd: targetDir, stdout: "ignore", stderr: "ignore" }); + } + } + console.log(); console.log(blueBright("All done! Have fun programming! ") + red("♥")); } @@ -96,6 +113,8 @@ async function setupProject_CLI(answers: Answers, files: File[]) { buildTypeScript = answers.language === "TypeScript"; if (buildTypeScript) maxSteps++; } + gitCommit = answers.gitCommit === "yes"; + if (gitCommit) maxSteps++; logProgress("Generating files"); const files = await createFiles(answers); diff --git a/src/lib/questions.ts b/src/lib/questions.ts index f3331fcd..ac1ad82e 100644 --- a/src/lib/questions.ts +++ b/src/lib/questions.ts @@ -171,6 +171,14 @@ export const questionsAndText: (Question | string)[] = [ message: "What's your email address?", action: checkEmail, }, + { + condition: { name: "cli", value: true }, + type: "select", + name: "gitCommit", + message: "Initialize the GitHub repo automatically?", + initial: "no", + choices: ["yes", "no"], + }, { type: "select", name: "license", @@ -209,6 +217,7 @@ export interface Answers { adminReact?: string; indentation?: string; quotes?: "single" | "double"; + gitCommit?: "yes" | "no"; } export function checkAnswers(answers: Partial): void {