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
Commiting from project subfolder does not work #1196
Comments
Hey! Thanks for opening an issue, indeed it looks like a bug since we use the See:
🤔 Thinking out loud, I'm not sure how other tools approach this but it's a fair use-case to think people can commit from subdirectories. The problem is that if we want to implement this it can be tricky to start traversing each directory (up) until you find a file that contains a configuration file 😅 I think we should somehow get the absolute directory for the git repo and try to find the configuration files there instead of relying on the current working directory. |
Hey Carloscuesta It looks like NPM does like this
https://docs.npmjs.com/cli/v7/configuring-npm/folders Maybe we can implement the same way as they do. We can also limit to a certain amount of folders to make sure the calculation does not take forever. It's also important to note that for a monorepo, we usually commit in project subfolders (packages folders), but we might want a global gitmoji config for the whole project.
For this use case, we would need a global .gitmojirc.json file and recursively walking up folders to find it in the project root. Pierre |
I ran into this PR as well, so it used to work maybe |
There's definitely pretty straight forward ways to achieve this. Something like this should suffice to find either import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
function findWorkspaceRoot() {
try {
const stdout = execSync('git rev-parse --show-toplevel');
return stdout.toString().trim();
} catch (err) {
return;
}
}
enum ConfigType {
Package = 'package.json',
Config = '.gitmojirc.json',
}
function findConfigDir(type: ConfigType) {
const root = findWorkspaceRoot();
let currentDir = process.cwd();
while (true) {
const configPath = path.join(currentDir, type);
if (fs.existsSync(configPath)) {
if (type === ConfigType.Package) {
const packageJson = JSON.parse(fs.readFileSync(configPath, 'utf8'));
if (packageJson.gitmoji) {
return currentDir;
}
} else {
return currentDir;
}
}
if (currentDir === (root ?? path.dirname(currentDir))) {
break;
}
currentDir = path.dirname(currentDir);
}
throw new Error('Reached root without finding gitmoji config');
}
findConfigDir(ConfigType.Package); Basically crawl up the parent directories until you find a directory that has a config file that is usable. In an ideal scenario |
Discussion
Hello,
I want to use gitmoji-cli in my project. I set up the gitmoji config in package.json file.
When I commit from project root, where package.json file is located, everything works as expected.
However, when commiting from a subfolder of the project (for example, src folder), it does not find config and use the default config.
Am I doing something wrong or is this feature not available?
Thank you
Validations
The text was updated successfully, but these errors were encountered: