Skip to content

Commit b4cd6ec

Browse files
committed
Add common function to update .gitignore.
1 parent cbeec0b commit b4cd6ec

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

packages/@apphosting/common/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import assert from "assert";
2+
import fs from "fs";
3+
import yaml from "yaml";
4+
import path from "path";
5+
import os from "os";
6+
const importIndex = import("@apphosting/common/dist/index.js");
7+
8+
describe("update or create .gitignore", () => {
9+
let tmpDir: string;
10+
beforeEach(() => {
11+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "test-gitignore"));
12+
});
13+
14+
it(".gitignore file exists and is correctly updated with missing paths", async () => {
15+
const { UpdateOrCreateGitignore } = await importIndex;
16+
fs.writeFileSync(path.join(tmpDir, ".gitignore"), "existingpath/");
17+
18+
UpdateOrCreateGitignore(tmpDir, ["existingpath/", "newpath/"]);
19+
20+
const gitignoreContent = fs.readFileSync(path.join(tmpDir, ".gitignore"), "utf-8");
21+
assert.equal(`existingpath/\nnewpath/\n`, gitignoreContent);
22+
});
23+
it(".gitignore file does not exist and is created", async () => {
24+
const { UpdateOrCreateGitignore } = await importIndex;
25+
UpdateOrCreateGitignore(tmpDir, ["chickenpath/", "newpath/"]);
26+
const gitignoreContent = fs.readFileSync(path.join(tmpDir, ".gitignore"), "utf-8");
27+
assert.equal(`chickenpath/\nnewpath/`, gitignoreContent);
28+
});
29+
});

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 path from "node:path";
3+
import * as fs from "fs-extra";
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.includes(entry)) {
160+
console.log(`adding ${entry} to ${gitignorePath}`);
161+
content += `\n${entry}\n`;
162+
}
163+
}
164+
165+
fs.writeFileSync(gitignorePath, content);
166+
}

0 commit comments

Comments
 (0)