From 06175197e41bff48e76b26fea5a7e056fdc94ca3 Mon Sep 17 00:00:00 2001 From: Viktor Varland Date: Mon, 10 May 2021 10:11:39 +0200 Subject: [PATCH 1/2] 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 --- src/commands/install.js | 3 ++- src/commands/types/commit.js | 7 +++++++ src/utils/files.js | 3 +++ src/utils/git.js | 4 ++++ src/utils/paths.js | 13 ++++++++++--- 5 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 src/utils/git.js diff --git a/src/commands/install.js b/src/commands/install.js index 02311cfa..76bcfde7 100755 --- a/src/commands/install.js +++ b/src/commands/install.js @@ -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') @@ -41,7 +42,7 @@ exports.installcmd = yargs => } } - if (config.hooks) { + if (gitEnabled() && config.hooks) { log.info('git-hooks > husky') husky({ command: 'install', diff --git a/src/commands/types/commit.js b/src/commands/types/commit.js index 437e01ae..8324c6c6 100755 --- a/src/commands/types/commit.js +++ b/src/commands/types/commit.js @@ -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') @@ -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, diff --git a/src/utils/files.js b/src/utils/files.js index a23c3320..1a45e471 100755 --- a/src/utils/files.js +++ b/src/utils/files.js @@ -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']) } @@ -209,5 +211,6 @@ module.exports = { pickFirstExists, resolveIgnoreFile, fileExists, + dirExists, relativePath, } diff --git a/src/utils/git.js b/src/utils/git.js new file mode 100644 index 00000000..10b7d8bb --- /dev/null +++ b/src/utils/git.js @@ -0,0 +1,4 @@ +const { dirExists } = require('./files.js') +const { GIT_DIR } = require('./paths.js') + +exports.gitEnabled = () => dirExists(GIT_DIR) diff --git a/src/utils/paths.js b/src/utils/paths.js index 0343cf23..721277eb 100755 --- a/src/utils/paths.js +++ b/src/utils/paths.js @@ -9,9 +9,13 @@ 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.json', + '.d2', + ].map(i => findup.sync.exists(path.join(directory, i))) return amiroot.includes(true) && directory }, { @@ -19,6 +23,8 @@ const PROJECT_ROOT = findup.sync( 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'] @@ -34,4 +40,5 @@ module.exports = { PROJECT_ROOT, TEMPLATE_DIR, DEPRECATED_CONFIGS, + GIT_DIR, } From 6d5c4962ee7a070a13d697bb187a79a72ae19ae0 Mon Sep 17 00:00:00 2001 From: Viktor Varland Date: Mon, 10 May 2021 10:29:34 +0200 Subject: [PATCH 2/2] 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. --- src/utils/paths.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/utils/paths.js b/src/utils/paths.js index 721277eb..c854fd2a 100755 --- a/src/utils/paths.js +++ b/src/utils/paths.js @@ -13,8 +13,7 @@ const PROJECT_ROOT = findup.sync( '.git', '.github', 'yarn.lock', - 'package.json', - '.d2', + 'package-lock.json', ].map(i => findup.sync.exists(path.join(directory, i))) return amiroot.includes(true) && directory },