Skip to content

Commit

Permalink
Avoid duplicated add json file to gitignore (#5)
Browse files Browse the repository at this point in the history
* Check if json already added before add to gitignore

* format codes
  • Loading branch information
M97Chahboun authored Mar 5, 2023
1 parent 1375f08 commit 2eff8ed
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';
import { Console } from "console";
import * as path from "path";
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import { extensions, commands, Uri,env, RelativePattern, ExtensionContext, workspace } from 'vscode';
import { extensions, commands, Uri, env, RelativePattern, ExtensionContext, workspace } from 'vscode';
import { GitExtension } from './git';
const fs = require('fs');
import Timer from './Timer';
Expand All @@ -21,16 +20,9 @@ export function activate(context: ExtensionContext) {
// This line of code will only be executed once when your extension is activated
const workspacePath = workspace.workspaceFolders![0].uri.path;
gitpath = path.join(workspacePath, ".git");
const gitIgnore = path.join(workspacePath, ".gitignore")
jsonPath = path.join(workspacePath, ".vscode/branch-timer.json");
gitBranch = getCurrentGitBranch(Uri.parse(gitpath));

fs.appendFile(gitIgnore, '.vscode/branch-timer.json', function (err:any) {
if (err){
console.log(err);
}
console.log('Saved!');
});
addToGitIgnore(workspacePath);
timer = new Timer(gitBranch!);
if (fs.existsSync(jsonPath)) {
var jsonFile: string = fs.readFileSync(jsonPath, 'utf8');
Expand Down Expand Up @@ -135,3 +127,21 @@ function getCurrentGitBranch(docUri: Uri): string | undefined {
return branchName;
}


function addToGitIgnore(workspacePath: string) {
var branchTimerPath = '.vscode/branch-timer.json';
const gitIgnore = path.join(workspacePath, ".gitignore")
if (fs.existsSync(gitIgnore)) {
var gitIgnoreFile: string = fs.readFileSync(gitIgnore, 'utf8');
if (!gitIgnoreFile.includes(branchTimerPath)) {
fs.appendFile(gitIgnore, branchTimerPath, function (err: any) {
if (err) {
console.log(err);
}
console.log('Added branch timer file to gitignore!');
});
} else {
console.log("Already Added");
}
}
}

0 comments on commit 2eff8ed

Please sign in to comment.