Skip to content

Commit

Permalink
fix: don't drop .gitignore lines that aren't in a section (#88)
Browse files Browse the repository at this point in the history
* fix: include un-sectioned gitignore items

* fix: ignore build directory
  • Loading branch information
amcgee committed Sep 30, 2019
1 parent 7664c79 commit 7372a0c
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions cli/src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] }
Expand Down Expand Up @@ -36,13 +36,41 @@ 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
}

newSection.patterns = ignorePatterns
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)
Expand Down Expand Up @@ -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!')
Expand Down

0 comments on commit 7372a0c

Please sign in to comment.