Skip to content

Commit 84a1be5

Browse files
authored
Add common function to update .gitignore. (#375)
1 parent 00669d3 commit 84a1be5

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

packages/@apphosting/common/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@apphosting/common",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Shared library code for App Hosting framework adapters",
55
"author": {
66
"name": "Firebase",
@@ -17,6 +17,7 @@
1717
},
1818
"scripts": {
1919
"build": "tsc",
20+
"test": "ts-mocha -p tsconfig.json 'src/**/*.spec.ts' 'src/*.spec.ts'",
2021
"localregistry:start": "npx verdaccio --config ../publish-dev/verdaccio-config.yaml",
2122
"localregistry:publish": "(npm view --registry=http://localhost:4873 @apphosting/common && npm unpublish --@apphosting:registry=http://localhost:4873 --force); npm publish --@apphosting:registry=http://localhost:4873"
2223
},
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import assert from "assert";
2+
import fs from "fs";
3+
import path from "path";
4+
import os from "os";
5+
import { updateOrCreateGitignore } from "./index";
6+
7+
describe("update or create .gitignore", () => {
8+
let tmpDir: string;
9+
beforeEach(() => {
10+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "test-gitignore"));
11+
});
12+
13+
afterEach(() => {
14+
fs.rmSync(tmpDir, { recursive: true, force: true });
15+
});
16+
17+
it(".gitignore file exists and is correctly updated with missing paths", () => {
18+
fs.writeFileSync(path.join(tmpDir, ".gitignore"), "existingpath/");
19+
20+
updateOrCreateGitignore(tmpDir, ["existingpath/", "newpath/"]);
21+
22+
const gitignoreContent = fs.readFileSync(path.join(tmpDir, ".gitignore"), "utf-8");
23+
assert.equal(`existingpath/\nnewpath/`, gitignoreContent);
24+
});
25+
it(".gitignore file does not exist and is created", () => {
26+
updateOrCreateGitignore(tmpDir, ["chickenpath/", "newpath/"]);
27+
const gitignoreContent = fs.readFileSync(path.join(tmpDir, ".gitignore"), "utf-8");
28+
assert.equal(`chickenpath/\nnewpath/`, gitignoreContent);
29+
});
30+
});

packages/@apphosting/common/src/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { spawn } from "child_process";
2+
import * as fs from "node:fs";
3+
import * as path from "node:path";
24

35
// Output bundle metadata specifications to be written to bundle.yaml
46
export interface OutputBundleConfig {
@@ -139,3 +141,26 @@ export function getBuildOptions(): BuildOptions {
139141
projectDirectory: process.cwd(),
140142
};
141143
}
144+
145+
/**
146+
* Updates or creates a .gitignore file with the given entries in the given path
147+
*/
148+
export function updateOrCreateGitignore(dirPath: string, entries: string[]) {
149+
const gitignorePath = path.join(dirPath, ".gitignore");
150+
151+
if (!fs.existsSync(gitignorePath)) {
152+
console.log(`creating ${gitignorePath} with entries: ${entries.join("\n")}`);
153+
fs.writeFileSync(gitignorePath, entries.join("\n"));
154+
return;
155+
}
156+
157+
let content = fs.readFileSync(gitignorePath, "utf-8");
158+
for (const entry of entries) {
159+
if (!content.split("\n").includes(entry)) {
160+
console.log(`adding ${entry} to ${gitignorePath}`);
161+
content += `\n${entry}`;
162+
}
163+
}
164+
165+
fs.writeFileSync(gitignorePath, content);
166+
}

0 commit comments

Comments
 (0)