Skip to content

Commit

Permalink
fix: update gitignore on init (#71)
Browse files Browse the repository at this point in the history
* fix: update .gitignore on init

* chore: better gitignore parsing
  • Loading branch information
amcgee committed Sep 27, 2019
1 parent c9c90a6 commit e91d71f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"lodash": "^4.17.11",
"moment": "^2.24.0",
"parse-author": "^2.0.0",
"parse-gitignore": "^1.0.1",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
Expand Down
45 changes: 45 additions & 0 deletions cli/src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,47 @@ const { reporter, exec } = require('@dhis2/cli-helpers-engine')
const path = require('path')
const fs = require('fs-extra')
const chalk = require('chalk')
const gitignore = require('parse-gitignore')

const makePaths = require('../lib/paths')

const ignorePatterns = ['node_modules', '.d2', 'src/locales']

const parseGitignore = gitignoreFile => {
const newSection = { name: 'DHIS2 Platform', patterns: [] }
if (fs.existsSync(gitignoreFile)) {
const content = fs.readFileSync(gitignoreFile)
const parsed = gitignore.parse(content)

const existingSection = parsed.sections.filter(
section => section.name === newSection.name
)[0]

if (existingSection) {
newSection.patterns = existingSection.patterns
}

ignorePatterns.forEach(pattern => {
if (!parsed.patterns.includes(pattern)) {
newSection.patterns.push(pattern)
}
})

if (existingSection) {
existingSection.patterns = newSection.patterns
} else {
if (newSection.patterns.length) {
parsed.sections.push(newSection)
}
}

return parsed.sections
}

newSection.patterns = ignorePatterns
return [newSection]
}

const handler = async ({ force, name, cwd, lib }) => {
cwd = cwd || process.cwd()
cwd = path.join(cwd, name)
Expand Down Expand Up @@ -135,6 +173,13 @@ 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))
)

reporter.print('')
reporter.info('SUCCESS!')
const cdCmd = name != '.' ? `cd ${name} && ` : ''
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6595,6 +6595,11 @@ parse-author@^2.0.0:
dependencies:
author-regex "^1.0.0"

parse-gitignore@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/parse-gitignore/-/parse-gitignore-1.0.1.tgz#8b9dc57f17b810d495c5dfa62eb07caffe7758c7"
integrity sha512-UGyowyjtx26n65kdAMWhm6/3uy5uSrpcuH7tt+QEVudiBoVS+eqHxD5kbi9oWVRwj7sCzXqwuM+rUGw7earl6A==

parse-json@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-4.0.0.tgz#be35f5425be1f7f6c747184f98a788cb99477ee0"
Expand Down

0 comments on commit e91d71f

Please sign in to comment.