-
Notifications
You must be signed in to change notification settings - Fork 157
gitignore .apphosting/ #377
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
base: main
Are you sure you want to change the base?
Changes from all commits
b4cd6ec
ffcf43d
ca94ea2
a2b7335
747a05d
051fbb1
a040af0
9c03ce9
20d6565
70cf87d
0390e72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import assert from "assert"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import os from "os"; | ||
import { updateOrCreateGitignore } from "./index"; | ||
|
||
describe("update or create .gitignore", () => { | ||
let tmpDir: string; | ||
beforeEach(() => { | ||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "test-gitignore")); | ||
}); | ||
|
||
afterEach(() => { | ||
fs.rmSync(tmpDir, { recursive: true, force: true }); | ||
}); | ||
|
||
it(".gitignore file exists and is correctly updated with missing paths", () => { | ||
fs.writeFileSync(path.join(tmpDir, ".gitignore"), "existingpath/"); | ||
|
||
updateOrCreateGitignore(tmpDir, ["existingpath/", "newpath/"]); | ||
|
||
const gitignoreContent = fs.readFileSync(path.join(tmpDir, ".gitignore"), "utf-8"); | ||
assert.equal(`existingpath/\nnewpath/`, gitignoreContent); | ||
}); | ||
it(".gitignore file does not exist and is created", () => { | ||
updateOrCreateGitignore(tmpDir, ["chickenpath/", "newpath/"]); | ||
const gitignoreContent = fs.readFileSync(path.join(tmpDir, ".gitignore"), "utf-8"); | ||
assert.equal(`chickenpath/\nnewpath/`, gitignoreContent); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import { spawn } from "child_process"; | ||
import * as fs from "node:fs"; | ||
import * as path from "node:path"; | ||
|
||
// Output bundle metadata specifications to be written to bundle.yaml | ||
export interface OutputBundleConfig { | ||
|
@@ -139,3 +141,26 @@ export function getBuildOptions(): BuildOptions { | |
projectDirectory: process.cwd(), | ||
}; | ||
} | ||
|
||
/** | ||
* Updates or creates a .gitignore file with the given entries in the given path | ||
*/ | ||
export function updateOrCreateGitignore(dirPath: string, entries: string[]) { | ||
const gitignorePath = path.join(dirPath, ".gitignore"); | ||
|
||
if (!fs.existsSync(gitignorePath)) { | ||
console.log(`creating ${gitignorePath} with entries: ${entries.join("\n")}`); | ||
fs.writeFileSync(gitignorePath, entries.join("\n")); | ||
return; | ||
} | ||
|
||
let content = fs.readFileSync(gitignorePath, "utf-8"); | ||
for (const entry of entries) { | ||
if (!content.split("\n").includes(entry)) { | ||
console.log(`adding ${entry} to ${gitignorePath}`); | ||
content += `\n${entry}`; | ||
} | ||
} | ||
|
||
fs.writeFileSync(gitignorePath, content); | ||
Comment on lines
+157
to
+165
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation re-splits the file content on every iteration of the loop, which can be inefficient. Also, the way new entries are appended with A more robust and performant approach would be to read the existing entries into a let content = fs.readFileSync(gitignorePath, "utf-8");
const existingEntries = new Set(content.split("\n"));
const entriesToAdd = entries.filter(entry => !existingEntries.has(entry));
if (entriesToAdd.length > 0) {
entriesToAdd.forEach(entry => console.log(`adding ${entry} to ${gitignorePath}`));
if (content.length > 0 && !content.endsWith("\n")) {
content += "\n";
}
content += entriesToAdd.join("\n");
fs.writeFileSync(gitignorePath, content);
} |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two tests (
.apphosting gitignored without existing .gitignore file
and.apphosting gitignored in existing .gitignore file
) contain a lot of duplicated setup and execution logic. To improve maintainability and reduce redundancy, consider refactoring them into a single parameterized test. You can create an array of test cases and iterate over it, which will make the test suite cleaner and easier to extend in the future.