From d3d6ef7e79ec593eb6c3a9a381889e5e0e0847ff Mon Sep 17 00:00:00 2001 From: Max Gaurav Date: Fri, 29 Apr 2022 14:59:58 +0530 Subject: [PATCH 1/3] add file exists check to prevent override of existing .gitignore file within collection --- actions/new.action.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/actions/new.action.ts b/actions/new.action.ts index e962deed2..23648b0f1 100644 --- a/actions/new.action.ts +++ b/actions/new.action.ts @@ -203,7 +203,12 @@ const initializeGitRepository = async (dir: string) => { const createGitIgnoreFile = (dir: string, content?: string) => { const fileContent = content || defaultGitIgnore; const filePath = join(process.cwd(), dir, '.gitignore'); - return promisify(fs.writeFile)(filePath, fileContent); + return fileExists(filePath).then(exists => { + if (!exists) { + return promisify(fs.writeFile)(filePath, fileContent); + } + return; + }); }; const printCollective = () => { @@ -250,4 +255,14 @@ export const retrieveCols = () => { } }; +const fileExists = (path: string) => { + return promisify(fs.access)(path).then(() => true).catch((err: any) => { + if (err.code === 'ENOENT') { + return false; + } + + return Promise.reject(err); + }); +} + export const exit = () => process.exit(1); From 097d53f2678b6385dd43b14e036b3c2ff01fa712 Mon Sep 17 00:00:00 2001 From: Max Gaurav Date: Thu, 19 May 2022 09:59:32 +0530 Subject: [PATCH 2/3] change fileExists to be synchronous --- actions/new.action.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/actions/new.action.ts b/actions/new.action.ts index 23648b0f1..6817b51d2 100644 --- a/actions/new.action.ts +++ b/actions/new.action.ts @@ -203,12 +203,11 @@ const initializeGitRepository = async (dir: string) => { const createGitIgnoreFile = (dir: string, content?: string) => { const fileContent = content || defaultGitIgnore; const filePath = join(process.cwd(), dir, '.gitignore'); - return fileExists(filePath).then(exists => { - if (!exists) { - return promisify(fs.writeFile)(filePath, fileContent); - } + + if (fileExists(filePath)) { return; - }); + } + return promisify(fs.writeFile)(filePath, fileContent); }; const printCollective = () => { @@ -256,13 +255,16 @@ export const retrieveCols = () => { }; const fileExists = (path: string) => { - return promisify(fs.access)(path).then(() => true).catch((err: any) => { + try{ + fs.accessSync(path); + return true; + } catch (err: any) { if (err.code === 'ENOENT') { return false; } - return Promise.reject(err); - }); + throw err; + } } export const exit = () => process.exit(1); From 847192b70c0be08c6d07f3e68cf0b023f8c75dd2 Mon Sep 17 00:00:00 2001 From: Max Gaurav Date: Thu, 19 May 2022 10:11:59 +0530 Subject: [PATCH 3/3] fix formatting issue in add actions --- actions/new.action.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actions/new.action.ts b/actions/new.action.ts index 6817b51d2..044786eb0 100644 --- a/actions/new.action.ts +++ b/actions/new.action.ts @@ -255,7 +255,7 @@ export const retrieveCols = () => { }; const fileExists = (path: string) => { - try{ + try { fs.accessSync(path); return true; } catch (err: any) { @@ -265,6 +265,6 @@ const fileExists = (path: string) => { throw err; } -} +}; export const exit = () => process.exit(1);