Skip to content

Commit

Permalink
feat: add skipGit configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
Lipata committed Mar 1, 2018
1 parent 9d1768e commit cc9ca96
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 21 deletions.
3 changes: 3 additions & 0 deletions lib/PromptSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
17 changes: 17 additions & 0 deletions lib/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 2 additions & 14 deletions lib/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ command = {
},
"skip-git": {
alias: "sg",
default: false,
describe: "Do not automatically initialize repository for the project with Git",
type: "boolean"
}
Expand Down Expand Up @@ -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);
}
}
};
Expand Down
3 changes: 2 additions & 1 deletion lib/config/defaults.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"igPackageRegistry": "https://packages.infragistics.com/npm/js-licensed/",
"customTemplates": []
"customTemplates": [],
"skipGit": false
}
2 changes: 2 additions & 0 deletions lib/types/Config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion spec/acceptance/new-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);

Expand Down
40 changes: 35 additions & 5 deletions spec/unit/new-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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();
});
});

0 comments on commit cc9ca96

Please sign in to comment.