Skip to content

Commit

Permalink
fix(cli-43): skip git ops when not a git repo (#398)
Browse files Browse the repository at this point in the history
* fix(cli-43): skip git ops when not a git repo

Introduce a gitEnabled() function used to identify if Git operations
like linting commits and installing hooks should be done.

Modifies how the PROJECT_ROOT is identified when there is no .git
directory by adding additional root-level files like yarn.lock and
package.json.

May be a risk for false positives in a monorepo, will need to monitor
that a bit.

Jira-Issue: CLI-43

* refactor: check for lock files

The yarn.lock should only exist at the root level even in workspaces,
and if someone is using NPM the same should be true for the
package-lock.json.

The .d2 directory is not a good indicator of the project root, so
removing that from the checks.
  • Loading branch information
varl authored May 10, 2021
2 parents be6c3ad + 6d5c496 commit 6488144
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const path = require('path')
const log = require('@dhis2/cli-helpers-engine').reporter
const { husky, isSupportedHook } = require('../tools/husky.js')
const { fileExists, deleteFile } = require('../utils/files.js')
const { gitEnabled } = require('../utils/git.js')
const { PROJECT_HOOKS_DIR, DEPRECATED_CONFIGS } = require('../utils/paths.js')
const { callback, exit } = require('../utils/run.js')

Expand Down Expand Up @@ -41,7 +42,7 @@ exports.installcmd = yargs =>
}
}

if (config.hooks) {
if (gitEnabled() && config.hooks) {
log.info('git-hooks > husky')
husky({
command: 'install',
Expand Down
7 changes: 7 additions & 0 deletions src/commands/types/commit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const log = require('@dhis2/cli-helpers-engine').reporter
const { commitlint } = require('../../tools/commitlint.js')
const { gitEnabled } = require('../../utils/git.js')
const { CONSUMING_ROOT } = require('../../utils/paths.js')
const { callback, exit } = require('../../utils/run.js')

Expand All @@ -23,6 +24,12 @@ exports.builder = yargs =>

exports.handler = argv => {
log.info(`commit-msg > commitlint`)

if (!gitEnabled()) {
log.print('Not a Git repository, skipping commit validation.')
return
}

commitlint({
config: argv.commitlintConfig,
file: argv.file,
Expand Down
3 changes: 3 additions & 0 deletions src/utils/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ const pickFirstExists = (files = [], customRoot) => {

const fileExists = fp => fs.existsSync(fp) && fs.statSync(fp).size !== 0

const dirExists = fp => fs.existsSync(fp) && fs.statSync(fp).isDirectory()

const resolveIgnoreFile = (ignoreFiles = []) => {
return pickFirstExists([...ignoreFiles, '.d2styleignore', '.gitignore'])
}
Expand All @@ -209,5 +211,6 @@ module.exports = {
pickFirstExists,
resolveIgnoreFile,
fileExists,
dirExists,
relativePath,
}
4 changes: 4 additions & 0 deletions src/utils/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { dirExists } = require('./files.js')
const { GIT_DIR } = require('./paths.js')

exports.gitEnabled = () => dirExists(GIT_DIR)
12 changes: 9 additions & 3 deletions src/utils/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ const TEMPLATE_DIR = path.join(PACKAGE_ROOT, 'templates')

const PROJECT_ROOT = findup.sync(
directory => {
const amiroot = ['.git', '.github', '.d2'].map(i =>
findup.sync.exists(path.join(directory, i))
)
const amiroot = [
'.git',
'.github',
'yarn.lock',
'package-lock.json',
].map(i => findup.sync.exists(path.join(directory, i)))
return amiroot.includes(true) && directory
},
{
cwd: CONSUMING_ROOT,
type: 'directory',
}
)

const GIT_DIR = path.join(PROJECT_ROOT, '.git')
const PROJECT_HOOKS_DIR = path.join(PROJECT_ROOT, '.hooks')

const STYLE_CONFIG_FILES = ['d2-style.config.js', 'd2-style.js']
Expand All @@ -34,4 +39,5 @@ module.exports = {
PROJECT_ROOT,
TEMPLATE_DIR,
DEPRECATED_CONFIGS,
GIT_DIR,
}

0 comments on commit 6488144

Please sign in to comment.