Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli-43): skip git ops when not a git repo #398

Merged
merged 3 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}