From 7372a0c29460e9beadd1825670abb5497f06aa9d Mon Sep 17 00:00:00 2001 From: Austin McGee <947888+amcgee@users.noreply.github.com> Date: Mon, 30 Sep 2019 14:24:05 +0200 Subject: [PATCH] fix: don't drop .gitignore lines that aren't in a section (#88) * fix: include un-sectioned gitignore items * fix: ignore build directory --- cli/src/commands/init.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/cli/src/commands/init.js b/cli/src/commands/init.js index f51312699..3495947e9 100644 --- a/cli/src/commands/init.js +++ b/cli/src/commands/init.js @@ -6,7 +6,7 @@ const gitignore = require('parse-gitignore') const makePaths = require('../lib/paths') -const ignorePatterns = ['node_modules', '.d2', 'src/locales'] +const ignorePatterns = ['node_modules', '.d2', 'src/locales', 'build'] const parseGitignore = gitignoreFile => { const newSection = { name: 'DHIS2 Platform', patterns: [] } @@ -36,6 +36,21 @@ const parseGitignore = gitignoreFile => { } } + const defaultSection = { + name: null, + patterns: parsed.patterns.filter(pattern => { + if ( + parsed.sections.some(section => + section.patterns.includes(pattern) + ) + ) { + return false + } + return true + }), + } + parsed.sections.unshift(defaultSection) + return parsed.sections } @@ -43,6 +58,19 @@ const parseGitignore = gitignoreFile => { return [newSection] } +const writeGitignore = (gitignoreFile, sections) => { + const format = section => { + if (section.name === null && section.patterns.length) { + return section.patterns.join('\n') + '\n\n' + } + if (section.patterns.length) { + return gitignore.format(section) + } + return '' + } + fs.writeFileSync(gitignoreFile, gitignore.stringify(sections, format)) +} + const handler = async ({ force, name, cwd, lib }) => { cwd = cwd || process.cwd() cwd = path.join(cwd, name) @@ -175,10 +203,8 @@ const handler = async ({ force, name, cwd, lib }) => { const gitignoreFile = path.join(paths.base, '.gitignore') reporter.info('Updating .gitignore...') - fs.writeFileSync( - gitignoreFile, - gitignore.stringify(parseGitignore(gitignoreFile)) - ) + const sections = parseGitignore(gitignoreFile) + writeGitignore(gitignoreFile, sections) reporter.print('') reporter.info('SUCCESS!')