diff --git a/src/core/git/Git.ts b/src/core/git/Git.ts index 95c58b29a..b2865ae4d 100644 --- a/src/core/git/Git.ts +++ b/src/core/git/Git.ts @@ -1,6 +1,8 @@ import SFPLogger, { Logger, LoggerLevel } from '@flxbl-io/sfp-logger'; import simplegit, { SimpleGit } from 'simple-git'; import fs = require('fs-extra'); +import path = require('path'); +import ignore from 'ignore'; import GitIdentity from './GitIdentity'; const tmp = require('tmp'); @@ -134,8 +136,21 @@ export default class Git { SFPLogger.log(`Copying the repository to ${locationOfCopiedDirectory.name}`, LoggerLevel.INFO, logger); let repoDir = locationOfCopiedDirectory.name; - // Copy source directory to temp dir - fs.copySync(process.cwd(), repoDir); + // Read .gitignore file + const gitignore = ignore(); + const gitignorePath = `${process.cwd()}/.gitignore`; + if (fs.existsSync(gitignorePath)) { + const gitignoreContent = fs.readFileSync(gitignorePath).toString(); + gitignore.add(gitignoreContent); + } + + // Copy source directory to temp dir respecting .gitignore + fs.copySync(process.cwd(), repoDir, { + filter: (src) => { + const relativePath = path.relative(process.cwd(), src); + return !relativePath || !gitignore.ignores(relativePath); + } + }); //Initiate git on new repo on using the abstracted object let git = new Git(repoDir, logger); @@ -172,7 +187,7 @@ export default class Git { public raw(commands: string[]) { return this._git.raw(commands); - } + } public getRepositoryPath() { return this.repositoryLocation;