diff --git a/lib/PromptSession.ts b/lib/PromptSession.ts index a0a000df6..ce29f857b 100644 --- a/lib/PromptSession.ts +++ b/lib/PromptSession.ts @@ -87,6 +87,9 @@ export class PromptSession { Util.log(" Generating project structure."); await projTemplate.generateFiles(process.cwd(), projectName, theme); + if (!config.skipGit) { + Util.gitInit(process.cwd(), projectName); + } // move cwd to project folder process.chdir(projectName); Util.log(Util.greenCheck() + " Project structure generated."); diff --git a/lib/Util.ts b/lib/Util.ts index aa9dd3b25..ba1abe4c5 100644 --- a/lib/Util.ts +++ b/lib/Util.ts @@ -317,6 +317,23 @@ class Util { return execSync(command, options); } + /** + * Initialize git for a project, located in the provided directory and commit it. + * @param parentRoot Parent directory root of the project. + * @param projectName Project name. + */ + public static gitInit(parentRoot, projectName) { + try { + const cwdir = path.join(parentRoot, projectName); + Util.exec("git init", { cwd: cwdir }); + Util.exec("git add .", { cwd: cwdir }); + Util.exec("git commit -m " + "\"Initial commit for project: " + projectName + "\"", { cwd: cwdir }); + Util.log("Git Initialized and Project '" + projectName + "' Committed"); + } catch (error) { + Util.error("Git initialization failed. Install Git in order to automatically commit the project.", "yellow"); + } + } + private static propertyByPath(object: any, propPath: string) { if (!propPath) { return object; diff --git a/lib/commands/new.ts b/lib/commands/new.ts index 1b2ec1c5b..481a2169f 100644 --- a/lib/commands/new.ts +++ b/lib/commands/new.ts @@ -39,7 +39,6 @@ command = { }, "skip-git": { alias: "sg", - default: false, describe: "Do not automatically initialize repository for the project with Git", type: "boolean" } @@ -97,19 +96,8 @@ command = { await projTemplate.generateFiles(process.cwd(), argv.name, theme); Util.log(Util.greenCheck() + " Project Created"); - this.git(argv["skip-git"], argv.name); - }, - git(skipGit, projectName) { - if (!skipGit) { - try { - const cwdir = "./" + projectName; - Util.exec("git init", { cwd: cwdir }); - Util.exec("git add .", { cwd: cwdir }); - Util.exec("git commit -m " + "\"Initial commit for project: " + projectName + "\"", { cwd: cwdir }); - Util.log(Util.greenCheck() + " Git Initialized and Project '" + projectName + "' committed"); - } catch (error) { - Util.error("Git initialization failed. Install Git in order to automatically commit the project.", "yellow"); - } + if (!argv["skip-git"] && !ProjectConfig.getConfig().skipGit) { + Util.gitInit(process.cwd(), argv.name); } } }; diff --git a/lib/config/defaults.json b/lib/config/defaults.json index 5135441d8..885b914a5 100644 --- a/lib/config/defaults.json +++ b/lib/config/defaults.json @@ -1,4 +1,5 @@ { "igPackageRegistry": "https://packages.infragistics.com/npm/js-licensed/", - "customTemplates": [] + "customTemplates": [], + "skipGit": false } \ No newline at end of file diff --git a/lib/types/Config.d.ts b/lib/types/Config.d.ts index e680c9eb1..33c8c425b 100644 --- a/lib/types/Config.d.ts +++ b/lib/types/Config.d.ts @@ -39,4 +39,6 @@ declare interface Config { customTemplates: string[]; /** Infragistics package registry Url */ igPackageRegistry: string; + /** Skip git initializion and commit of the new project */ + skipGit: boolean; } diff --git a/spec/acceptance/new-spec.ts b/spec/acceptance/new-spec.ts index e832e3c4d..36d9e1f66 100644 --- a/spec/acceptance/new-spec.ts +++ b/spec/acceptance/new-spec.ts @@ -67,7 +67,7 @@ describe("New command", () => { done(); }); - it("Skip Git", async done => { + it("Skip Git with command option", async done => { const projectName = "angularProj"; await cli.run(["new", projectName, "--framework=angular", "--type=igx-ts", "--skip-git"]); diff --git a/spec/unit/new-spec.ts b/spec/unit/new-spec.ts index 01cb0bec1..b552d0997 100644 --- a/spec/unit/new-spec.ts +++ b/spec/unit/new-spec.ts @@ -234,19 +234,19 @@ describe("Unit - New command", () => { }); spyOn(mockTemplate, "generateFiles"); - await newCmd.execute({ name: projectName, framework: "jq", type: "type", theme: "ig" }); + await newCmd.execute({ name: projectName, framework: "jq" }); expect(Util.exec).toHaveBeenCalledWith("git init", jasmine.any(Object)); expect(Util.exec).toHaveBeenCalledWith("git add .", jasmine.any(Object)); expect(Util.exec).toHaveBeenCalledWith("git commit -m " + "\"Initial commit for project: " + projectName + "\"", jasmine.any(Object)); expect(Util.log).toHaveBeenCalledWith( - jasmine.stringMatching("Git Initialized and Project '" + projectName + "' committed") + jasmine.stringMatching("Git Initialized and Project '" + projectName + "' Committed") ); done(); }); - it("Skip Git initialization", async done => { + it("Skip Git initialization with command option", async done => { const projectName = "projTitle"; const mockTemplate = { @@ -266,10 +266,40 @@ describe("Unit - New command", () => { getProjectLibrary: mockProjLib }); spyOn(mockTemplate, "generateFiles"); + spyOn(Util, "gitInit"); - await newCmd.execute({ "name": projectName, "framework": "jq", "type": "type", "theme": "ig", "skip-git": true }); + await newCmd.execute({ "name": projectName, "framework": "jq", "skip-git": true }); - expect(Util.exec).not.toHaveBeenCalled(); + expect(Util.gitInit).not.toHaveBeenCalled(); + done(); + }); + + it("Skip Git initialization with configuration option", async done => { + const projectName = "projTitle"; + + const mockTemplate = { + generateFiles: async (cwd: string, name: string, theme: string) => { + return true; + } + }; + const mockProjLib = { + getProject: () => { + return mockTemplate; + }, + projectType: "type", + themes: ["ig"] + }; + newCmd.template = jasmine.createSpyObj("TemplateManager", { + getFrameworkById: {}, + getProjectLibrary: mockProjLib + }); + spyOn(mockTemplate, "generateFiles"); + spyOn(ProjectConfig, "getConfig").and.returnValue({ skipGit: true }); + spyOn(Util, "gitInit"); + + await newCmd.execute({ name: projectName, framework: "jq" }); + + expect(Util.gitInit).not.toHaveBeenCalled(); done(); }); });