diff --git a/src/extension.ts b/src/extension.ts index c6373a7..df930a1 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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 { } @@ -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 @@ -52,14 +49,14 @@ async function resolveWorkspaceFolder(gitIgnoreTemplate: GitignoreTemplate) { } } -function checkIfFileExists(path: string) { - return new Promise((resolve) => { - fs.stat(path, (err) => { +function getFileStats(path: string) { + return new Promise((resolve) => { + fs.stat(path, (err, stats) => { if (err) { // File does not exists return resolve(false); } - return resolve(true); + return resolve(stats); }); }); } @@ -67,8 +64,8 @@ function checkIfFileExists(path: string) { async function checkExistenceAndPromptForOperation(path: string, template: GitignoreTemplate): Promise { 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 }; } @@ -80,7 +77,7 @@ async function checkExistenceAndPromptForOperation(path: string, template: Gitig const typedString = operation.label; const type = GitignoreOperationType[typedString]; - return { path, template, type }; + return { path, template, type, stats }; } function promptForOperation() { @@ -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); diff --git a/src/interfaces.ts b/src/interfaces.ts index 7ac7329..94f93fc 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,5 +1,4 @@ -import { WriteStream } from "fs"; - +import * as fs from 'fs'; export interface GitignoreTemplate { name: string; @@ -11,7 +10,7 @@ export interface GitignoreTemplate { export interface GitignoreProvider { getTemplates(): Promise; download(operation: GitignoreOperation): Promise; - downloadToStream(operation: GitignoreOperation, stream: WriteStream): Promise; + downloadToStream(operation: GitignoreOperation, stream: fs.WriteStream): Promise; } export enum GitignoreOperationType { @@ -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; } diff --git a/src/providers/github-gitignore-api.ts b/src/providers/github-gitignore-api.ts index 24524e1..8b69d70 100644 --- a/src/providers/github-gitignore-api.ts +++ b/src/providers/github-gitignore-api.ts @@ -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'); } diff --git a/src/providers/github-gitignore-repository.ts b/src/providers/github-gitignore-repository.ts index e8055df..2e566fb 100644 --- a/src/providers/github-gitignore-repository.ts +++ b/src/providers/github-gitignore-repository.ts @@ -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'); }