UNPKG

@sap/cds-dk

Version:

Command line client and development toolkit for the SAP Cloud Application Programming Model

56 lines (44 loc) 1.59 kB
const cp = require('child_process'); const path = require('path'); const fs = require('fs').promises; const util = require('util'); const os = require('os'); const cds = require('../cds'); const DEBUG = cds.debug('cli'); const GIT_IGNORE_FILE = '.gitignore'; class GitUtil { constructor() { this.execAsync = util.promisify(cp.exec); } async ensureFileIsGitignored(file, currentFolder = cds.root) { const gitIgnorePath = path.join(currentFolder, GIT_IGNORE_FILE); try { await this.execAsync(`git check-ignore -n -v ${file}`, { cwd: currentFolder }); return; // file is git ignored, nothing to do } catch (err) { if (err.code === 'ENOENT') { // git command not found DEBUG?.('git command not found'); return; } if (!err.stdout.match(/::/)) { // included or not a git repo DEBUG?.(err.message); return; } } console.log(`adding entry '${file}' to ${GIT_IGNORE_FILE}.`); let gitignore = await this.readFileSafely(gitIgnorePath); gitignore = gitignore + `${os.EOL}# added by cds${os.EOL}${file}${os.EOL}`; await fs.mkdir(path.dirname(gitIgnorePath), { recursive: true }); await fs.writeFile(gitIgnorePath, gitignore, 'utf8'); } async readFileSafely(file) { try { return await fs.readFile(file, 'utf8'); } catch (err) { return ''; } } } module.exports = new GitUtil();