Skip to content

Commit

Permalink
respect .gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Jun 15, 2024
1 parent 941e99a commit 0042b66
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/renderer/components/ReplaceAllButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { EVENT_RUN_SNIPPET, REPLACE_ALL_TEST_RESULTS, SET_LOADING } from "../con
import { triggerEvent } from "../utils";

const ReplaceAllButton = () => {
const { language, rootPath, onlyPaths, skipPaths, snippetCode, testResults, dispatch } = useContext(AppContext);
const { language, rootPath, onlyPaths, skipPaths, respectGitignore, snippetCode, testResults, dispatch } = useContext(AppContext);

const replace = () => {
if (testResults.length > 0) {
dispatch({ type: REPLACE_ALL_TEST_RESULTS, testResults, rootPath });
} else {
dispatch({ type: SET_LOADING, loading: true, loadingText: "Running... it may take a while" });
triggerEvent(EVENT_RUN_SNIPPET, { language, rootPath, snippetCode, onlyPaths, skipPaths });
triggerEvent(EVENT_RUN_SNIPPET, { language, rootPath, snippetCode, onlyPaths, skipPaths, respectGitignore });
}
};

Expand Down
27 changes: 27 additions & 0 deletions src/renderer/components/RespectGitignoreCheckbox.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, { useContext, useEffect, useState } from "react";

import AppContext from "../context";
import { SET_RESPECT_GITIGNORE } from "../constants";
import { saveRespectGitignore } from "../utils";

const RespectGitignoreCheckbox = () => {
const { respectGitignore, dispatch } = useContext(AppContext);
const [value, setValue] = useState(respectGitignore);

useEffect(() => setValue(respectGitignore), [respectGitignore]);

const handleValueChanged = (event) => {
const respectGitignore = event.target.checked;
dispatch({ type: SET_RESPECT_GITIGNORE, respectGitignore });
saveRespectGitignore(respectGitignore);
};

return (
<label htmlFor="respectGitignore">
<input id="respectGitignore" type="checkbox" checked={value} onChange={handleValueChanged} />
Respect .gitignore
</label>
);
};

export default RespectGitignoreCheckbox;
4 changes: 3 additions & 1 deletion src/renderer/components/RunSnippet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { EVENT_SNIPPET_RUN, SET_LOADING } from "../constants";
import WorkingDir from "./WorkingDir";
import FilesToInclude from "./FilesToInclude";
import FilesToExclude from "./FilesToExclude";
import RespectGitignoreCheckbox from "./RespectGitIgnoreCheckbox";
import SearchButton from "./SearchButton";
import ReplaceAllButton from "./ReplaceAllButton";
import { showMessage } from "../utils";
Expand All @@ -29,7 +30,8 @@ export default () => {
<div className="run-snippet">
<div className="container-fluid mt-3 d-flex flex-row align-items-center">
<WorkingDir />
<div className="actions">
<div className="actions ml-2">
<RespectGitignoreCheckbox />
<SearchButton />
<ReplaceAllButton />
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/SearchButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { EVENT_TEST_SNIPPET, SET_LOADING } from "../constants";
import { triggerEvent } from "../utils";

const SearchButton = () => {
const { language, rootPath, onlyPaths, skipPaths, snippetCode, dispatch } = useContext(AppContext);
const { language, rootPath, onlyPaths, skipPaths, respectGitignore, snippetCode, dispatch } = useContext(AppContext);

const search = () => {
dispatch({ type: SET_LOADING, loading: true, loadingText: "Searching... it may take a while" });
triggerEvent(EVENT_TEST_SNIPPET, { language, rootPath, snippetCode, onlyPaths, skipPaths });
triggerEvent(EVENT_TEST_SNIPPET, { language, rootPath, snippetCode, onlyPaths, skipPaths, respectGitignore });
};

return (
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ export const SET_CURRENT_ACTION_INDEX = "SET_CURRENT_ACTION_INDEX";
export const SET_ROOT_PATH = "SET_ROOT_PATH";
export const SET_ONLY_PATHS = "SET_ONLY_PATHS";
export const SET_SKIP_PATHS = "SET_SKIP_PATHS";
export const SET_RESPECT_GITIGNORE = "SET_RESPECT_GITIGNORE";

export const LANGUAGE = "language";
export const PARSER = "parser";
export const ROOT_PATH = "root_path";
export const ONLY_PATHS = "only_paths";
export const SKIP_PATHS = "skip_paths";
export const RESPECT_GITIGNORE = "respect_gitignore";
2 changes: 1 addition & 1 deletion src/renderer/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ button.close:hover {
.new-snippet .nql-or-rules-select label {
margin-right: 10px;
}
.new-snippet .nql-or-rules-select input[type="radio"] {
input[type="radio"], input[type="checkbox"] {
margin-right: 5px;
}

Expand Down
3 changes: 2 additions & 1 deletion src/renderer/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { useReducerAsync } from "use-reducer-async";
import appReducer from "./reducer";
import appAction from "./action";
import AppContext from "./context";
import { getInited, getLanguage, getOnlyPaths, getRootPath, getSkipPaths } from "./utils";
import { getInited, getLanguage, getOnlyPaths, getRootPath, getSkipPaths, getRespectGitignore } from "./utils";

const initialState = {
inited: getInited(),
language: getLanguage(),
rootPath: getRootPath(),
onlyPaths: getOnlyPaths(),
skipPaths: getSkipPaths(),
respectGitignore: getRespectGitignore() === false ? false : true,
snippetsStore: {},
currentSnippetId: null,
generatedSnippets: [],
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
SET_ROOT_PATH,
SET_ONLY_PATHS,
SET_SKIP_PATHS,
SET_RESPECT_GITIGNORE,
SET_LANGUAGE,
SET_PARSER,
SET_INITED,
Expand Down Expand Up @@ -156,6 +157,7 @@ export default (state = {}, action) => {
rootPath: action.rootPath,
onlyPaths: action.onlyPaths,
skipPaths: action.skipPaths,
respectGitignore: action.respectGitignore,
};
}
case SET_ONLY_PATHS: {
Expand All @@ -170,6 +172,12 @@ export default (state = {}, action) => {
skipPaths: action.skipPaths,
};
}
case SET_RESPECT_GITIGNORE: {
return {
...state,
respectGitignore: action.respectGitignore,
};
}
default:
return state;
}
Expand Down
6 changes: 4 additions & 2 deletions src/renderer/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ const testSnippet = async (event) => {
return;
}
const {
detail: { snippetCode, rootPath, onlyPaths, skipPaths },
detail: { snippetCode, rootPath, onlyPaths, skipPaths, respectGitignore },
} = event;
const additionalArgs = buildAdditionalCommandArgs(language);
const synvertCommand = language === "ruby" ? runSynvertRuby : runSynvertJavascript;
Expand All @@ -210,6 +210,7 @@ const testSnippet = async (event) => {
rootPath,
onlyPaths,
skipPaths,
respectGitignore,
additionalArgs,
snippetCode,
binPath,
Expand Down Expand Up @@ -237,7 +238,7 @@ const runSnippet = async (event) => {
return;
}
const {
detail: { snippetCode, rootPath, onlyPaths, skipPaths },
detail: { snippetCode, rootPath, onlyPaths, skipPaths, respectGitignore },
} = event;
const additionalArgs = buildAdditionalCommandArgs(language);
const synvertCommand = language === "ruby" ? runSynvertRuby : runSynvertJavascript;
Expand All @@ -248,6 +249,7 @@ const runSnippet = async (event) => {
rootPath,
onlyPaths,
skipPaths,
respectGitignore,
additionalArgs,
snippetCode,
binPath,
Expand Down
7 changes: 5 additions & 2 deletions src/renderer/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import toast from "react-hot-toast";

import { ROOT_PATH, ONLY_PATHS, SKIP_PATHS, LANGUAGE, LANGUAGES, PARSER } from "./constants";
import { ROOT_PATH, ONLY_PATHS, SKIP_PATHS, RESPECT_GITIGNORE, LANGUAGE, LANGUAGES, PARSER } from "./constants";

const CUSTOM = "custom";
export const DEFAULT_VALUES = {
Expand Down Expand Up @@ -62,7 +62,7 @@ const getPreference = (section, key) => {
if (!preferences[section]) {
preferences[section] = {};
}
return preferences[section][key] || DEFAULT_VALUES[section][key];
return preferences[section][key] === undefined ? DEFAULT_VALUES[section][key] : preferences[section][key];
};

export const saveInited = (inited) => savePreference(CUSTOM, "inited", inited);
Expand Down Expand Up @@ -126,6 +126,9 @@ export const saveOnlyPaths = (path) => savePreference(CUSTOM, getRootPath() + ":
export const getSkipPaths = () => getPreference(CUSTOM, getRootPath() + ":" + SKIP_PATHS) || "";
export const saveSkipPaths = (path) => savePreference(CUSTOM, getRootPath() + ":" + SKIP_PATHS, path);

export const getRespectGitignore = () => getPreference(CUSTOM, getRootPath() + ":" + RESPECT_GITIGNORE);
export const saveRespectGitignore = (respectGitignore) => savePreference(CUSTOM, getRootPath() + ":" + RESPECT_GITIGNORE, respectGitignore);

export const convertSnippetsToStore = (snippets) =>
snippets.reduce(
(obj, snippet) => ({
Expand Down

0 comments on commit 0042b66

Please sign in to comment.