From d3d6ef7e79ec593eb6c3a9a381889e5e0e0847ff Mon Sep 17 00:00:00 2001 From: Max Gaurav Date: Fri, 29 Apr 2022 14:59:58 +0530 Subject: [PATCH] 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);