Skip to content

Commit 0090a01

Browse files
committed
feat: support PREFER_APPEND_FILE_SYNC env
1 parent 1efca92 commit 0090a01

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
@@ -202,7 +202,13 @@ function _setHook(hook, command, projectRoot=process.cwd()) {
202202
fs.mkdirSync(normalizedHookDirectory)
203203
}
204204

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

208214
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
@@ -173,6 +173,9 @@ describe("Simple Git Hooks tests", () => {
173173
const PROJECT_WITH_CUSTOM_CONF = path.normalize(
174174
path.join(testsFolder, "project_with_custom_configuration")
175175
);
176+
const PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV = path.normalize(
177+
path.join(testsFolder, "project_with_custom_append_file_sync_env")
178+
)
176179

177180
// Incorrect configurations
178181
const PROJECT_WITH_BAD_CONF_IN_PACKAGE_JSON_ = path.normalize(
@@ -627,6 +630,75 @@ describe("Simple Git Hooks tests", () => {
627630
})
628631
});
629632

633+
describe("Env var PREFER_APPEND_FILE_SYNC tests", () => {
634+
const GIT_USER_NAME = "github-actions";
635+
const GIT_USER_EMAIL = "github-actions@github.com";
636+
const PRE_COMMIT_FILE_PATH = path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks", "pre-commit");
637+
638+
const initializeGitRepository = (path) => {
639+
execSync(
640+
`git init \
641+
&& git config user.name ${GIT_USER_NAME} \
642+
&& git config user.email ${GIT_USER_EMAIL}`,
643+
{ cwd: path }
644+
);
645+
};
646+
647+
// replace pre commit content with 'test pre commit'
648+
const writePreCommitFile = () => {
649+
fs.writeFileSync(PRE_COMMIT_FILE_PATH, "test pre commit");
650+
}
651+
652+
beforeEach(() => {
653+
initializeGitRepository(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
654+
createGitHooksFolder(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
655+
});
656+
657+
describe("PREFER_APPEND_FILE_SYNC", () => {
658+
afterEach(() => {
659+
delete process.env.PREFER_APPEND_FILE_SYNC;
660+
});
661+
662+
it("should append to the hook file when PREFER_APPEND_FILE_SYNC is 1", () => {
663+
process.env.PREFER_APPEND_FILE_SYNC = 1;
664+
writePreCommitFile();
665+
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
666+
const installedHooks = getInstalledGitHooks(
667+
path.normalize(
668+
path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks")
669+
)
670+
);
671+
expect(installedHooks["pre-commit"]).toBe(`test pre commit
672+
${simpleGitHooks.PREPEND_SCRIPT}prettier --write .`);
673+
})
674+
675+
it("should append to the hook file when PREFER_APPEND_FILE_SYNC is true", () => {
676+
process.env.PREFER_APPEND_FILE_SYNC = "true";
677+
writePreCommitFile();
678+
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
679+
const installedHooks = getInstalledGitHooks(
680+
path.normalize(
681+
path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks")
682+
)
683+
);
684+
expect(installedHooks["pre-commit"]).toBe(`test pre commit
685+
${simpleGitHooks.PREPEND_SCRIPT}prettier --write .`);
686+
})
687+
688+
it("should write to the hook file when PREFER_APPEND_FILE_SYNC is not true", () => {
689+
writePreCommitFile();
690+
simpleGitHooks.setHooksFromConfig(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV);
691+
const installedHooks = getInstalledGitHooks(
692+
path.normalize(
693+
path.join(PROJECT_WITH_CUSTOM_APPEND_FILE_SYNC_ENV, ".git", "hooks")
694+
)
695+
);
696+
expect(installedHooks["pre-commit"]).toBe(`${simpleGitHooks.PREPEND_SCRIPT}prettier --write .`);
697+
})
698+
})
699+
700+
})
701+
630702
afterEach(() => {
631703
[
632704
PROJECT_WITH_CONF_IN_SEPARATE_JS_ALT,

0 commit comments

Comments
 (0)