Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Cache } from './cache';
import { GitignoreTemplate, GitignoreOperation, GitignoreOperationType, GitignoreProvider } from './interfaces';
import { GithubGitignoreRepositoryProvider } from './providers/github-gitignore-repository';


class CancellationError extends Error {

}
Expand All @@ -15,15 +14,13 @@ interface GitignoreQuickPickItem extends vscode.QuickPickItem {
template: GitignoreTemplate;
}


// Initialize
const config = vscode.workspace.getConfiguration('gitignore');
const cache = new Cache(config.get('cacheExpirationInterval', 3600));

// Create gitignore repository provider
const gitignoreRepository: GitignoreProvider = new GithubGitignoreRepositoryProvider(cache);
//const gitignoreRepository : GitignoreProvider = new GithubGitignoreApiProvider(cache);

// const gitignoreRepository: GitignoreProvider = new GithubGitignoreApiProvider(cache);

/**
* Resolves the workspace folder by
Expand Down Expand Up @@ -52,23 +49,23 @@ async function resolveWorkspaceFolder(gitIgnoreTemplate: GitignoreTemplate) {
}
}

function checkIfFileExists(path: string) {
return new Promise<boolean>((resolve) => {
fs.stat(path, (err) => {
function getFileStats(path: string) {
return new Promise<fs.Stats | false>((resolve) => {
fs.stat(path, (err, stats) => {
if (err) {
// File does not exists
return resolve(false);
}
return resolve(true);
return resolve(stats);
});
});
}

async function checkExistenceAndPromptForOperation(path: string, template: GitignoreTemplate): Promise<GitignoreOperation> {
path = joinPath(path, '.gitignore');

const exists = await checkIfFileExists(path);
if (!exists) {
const stats = await getFileStats(path);
if (!stats) {
// File does not exists -> we are fine to create it
return { path, template, type: GitignoreOperationType.Overwrite };
}
Expand All @@ -80,7 +77,7 @@ async function checkExistenceAndPromptForOperation(path: string, template: Gitig
const typedString = <keyof typeof GitignoreOperationType>operation.label;
const type = GitignoreOperationType[typedString];

return { path, template, type };
return { path, template, type, stats };
}

function promptForOperation() {
Expand Down Expand Up @@ -128,6 +125,7 @@ export function activate(context: vscode.ExtensionContext) {
url: t.download_url,
template: t
});

// TODO: use thenable for items
const selectedItem = await vscode.window.showQuickPick(items);

Expand Down
9 changes: 6 additions & 3 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { WriteStream } from "fs";

import * as fs from 'fs';

export interface GitignoreTemplate {
name: string;
Expand All @@ -11,7 +10,7 @@ export interface GitignoreTemplate {
export interface GitignoreProvider {
getTemplates(): Promise<GitignoreTemplate[]>;
download(operation: GitignoreOperation): Promise<void>;
downloadToStream(operation: GitignoreOperation, stream: WriteStream): Promise<void>;
downloadToStream(operation: GitignoreOperation, stream: fs.WriteStream): Promise<void>;
}

export enum GitignoreOperationType {
Expand All @@ -29,4 +28,8 @@ export interface GitignoreOperation {
* gitignore template file to use
*/
template: GitignoreTemplate;
/**
* If the file already exists, then the stats of the file.
*/
stats?: fs.Stats;
}
2 changes: 1 addition & 1 deletion src/providers/github-gitignore-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class GithubGitignoreApiProvider implements GitignoreProvider {
const file = fs.createWriteStream(operation.path, { flags: flags });

// If appending to the existing .gitignore file, write a NEWLINE as separator
if(flags === 'a') {
if(flags === 'a' && operation.stats?.size !== 0) {
file.write('\n');
}

Expand Down
2 changes: 1 addition & 1 deletion src/providers/github-gitignore-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class GithubGitignoreRepositoryProvider implements GitignoreProvider {
const file = fs.createWriteStream(operation.path, { flags: flags });

// If appending to the existing .gitignore file, write a NEWLINE as separator
if(flags === 'a') {
if(flags === 'a' && operation.stats?.size !== 0) {
file.write('\n');
}

Expand Down