Skip to content

Commit cafec52

Browse files
committed
feat: support PREFER_APPEND_FILE_SYNC env
1 parent 0433a04 commit cafec52

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "simple-git-hooks-test-package",
3+
"version": "1.0.0",
4+
"simple-git-hooks": {
5+
"pre-commit": "prettier --write ."
6+
},
7+
"devDependencies": {
8+
"simple-pre-commit": "1.0.0"
9+
}
10+
}

simple-git-hooks.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,13 @@ function _setHook(hook, command, projectRoot=process.cwd()) {
201201
fs.mkdirSync(normalizedHookDirectory)
202202
}
203203

204-
fs.writeFileSync(hookPath, hookCommand)
204+
// use PREFER_APPEND_FILE_SYNC env to append hook command into file
205+
if (['1', 'true'].includes(process.env.PREFER_APPEND_FILE_SYNC)) {
206+
fs.appendFileSync(hookPath, `
207+
${hookCommand}`)
208+
} else {
209+
fs.writeFileSync(hookPath, hookCommand)
210+
}
205211
fs.chmodSync(hookPath, 0o0755)
206212

207213
console.info(`[INFO] Successfully set the ${hook} with command: ${command}`)

simple-git-hooks.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ describe("Simple Git Hooks tests", () => {
165165
const PROJECT_WITH_CUSTOM_CONF = path.normalize(
166166
path.join(testsFolder, "project_with_custom_configuration")
167167
);
168+
const PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV = path.normalize(
169+
path.join(testsFolder, "project_with_custom_append_file_sync_env")
170+
)
168171

169172
// Incorrect configurations
170173
const PROJECT_WITH_BAD_CONF_IN_PACKAGE_JSON_ = path.normalize(
@@ -589,6 +592,75 @@ describe("Simple Git Hooks tests", () => {
589592
})
590593
});
591594

595+
describe("Env var PREFER_APPEND_FILE_SYNC tests", () => {
596+
const GIT_USER_NAME = "github-actions";
597+
const GIT_USER_EMAIL = "github-actions@github.com";
598+
const PRE_COMMIT_FILE_PATH = path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks", "pre-commit");
599+
600+
const initializeGitRepository = (path) => {
601+
execSync(
602+
`git init \
603+
&& git config user.name ${GIT_USER_NAME} \
604+
&& git config user.email ${GIT_USER_EMAIL}`,
605+
{ cwd: path }
606+
);
607+
};
608+
609+
// replace pre commit content with 'test pre commit'
610+
const writePreCommitFile = () => {
611+
fs.writeFileSync(PRE_COMMIT_FILE_PATH, "test pre commit");
612+
}
613+
614+
beforeEach(() => {
615+
initializeGitRepository(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
616+
createGitHooksFolder(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
617+
});
618+
619+
describe("PREFER_APPEND_FILE_SYNC", () => {
620+
afterEach(() => {
621+
delete process.env.PREFER_APPEND_FILE_SYNC;
622+
});
623+
624+
it("should append to the hook file when PREFER_APPEND_FILE_SYNC is 1", () => {
625+
process.env.PREFER_APPEND_FILE_SYNC = 1;
626+
writePreCommitFile();
627+
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
628+
const installedHooks = getInstalledGitHooks(
629+
path.normalize(
630+
path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks")
631+
)
632+
);
633+
expect(installedHooks["pre-commit"]).toBe(`test pre commit
634+
${simpleGitHooks.PREPEND_SCRIPT}prettier --write .`);
635+
})
636+
637+
it("should append to the hook file when PREFER_APPEND_FILE_SYNC is true", () => {
638+
process.env.PREFER_APPEND_FILE_SYNC = "true";
639+
writePreCommitFile();
640+
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
641+
const installedHooks = getInstalledGitHooks(
642+
path.normalize(
643+
path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks")
644+
)
645+
);
646+
expect(installedHooks["pre-commit"]).toBe(`test pre commit
647+
${simpleGitHooks.PREPEND_SCRIPT}prettier --write .`);
648+
})
649+
650+
it("should write to the hook file when PREFER_APPEND_FILE_SYNC is not true", () => {
651+
writePreCommitFile();
652+
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
653+
const installedHooks = getInstalledGitHooks(
654+
path.normalize(
655+
path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks")
656+
)
657+
);
658+
expect(installedHooks["pre-commit"]).toBe(`${simpleGitHooks.PREPEND_SCRIPT}prettier --write .`);
659+
})
660+
})
661+
662+
})
663+
592664
afterEach(() => {
593665
[
594666
PROJECT_WITH_CONF_IN_SEPARATE_JS_ALT,

0 commit comments

Comments
 (0)