Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@apphosting/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"scripts": {
"build": "tsc",
"test": "ts-mocha -p tsconfig.json 'src/**/*.spec.ts' 'src/*.spec.ts'",
"localregistry:start": "npx verdaccio --config ../publish-dev/verdaccio-config.yaml",
"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"
},
Expand Down
29 changes: 29 additions & 0 deletions packages/@apphosting/common/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import assert from "assert";
import fs from "fs";
import yaml from "yaml";

Check failure on line 3 in packages/@apphosting/common/src/index.spec.ts

View workflow job for this annotation

GitHub Actions / Lint

'yaml' is defined but never used
import path from "path";
import os from "os";
const importIndex = import("@apphosting/common/dist/index.js");

describe("update or create .gitignore", () => {
let tmpDir: string;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "test-gitignore"));
});

it(".gitignore file exists and is correctly updated with missing paths", async () => {
const { UpdateOrCreateGitignore } = await importIndex;
fs.writeFileSync(path.join(tmpDir, ".gitignore"), "existingpath/");

UpdateOrCreateGitignore(tmpDir, ["existingpath/", "newpath/"]);

Check failure on line 18 in packages/@apphosting/common/src/index.spec.ts

View workflow job for this annotation

GitHub Actions / Lint

A function with a name starting with an uppercase letter should only be used as a constructor

const gitignoreContent = fs.readFileSync(path.join(tmpDir, ".gitignore"), "utf-8");
assert.equal(`existingpath/\nnewpath/\n`, gitignoreContent);
});
it(".gitignore file does not exist and is created", async () => {
const { UpdateOrCreateGitignore } = await importIndex;
UpdateOrCreateGitignore(tmpDir, ["chickenpath/", "newpath/"]);

Check failure on line 25 in packages/@apphosting/common/src/index.spec.ts

View workflow job for this annotation

GitHub Actions / Lint

A function with a name starting with an uppercase letter should only be used as a constructor
const gitignoreContent = fs.readFileSync(path.join(tmpDir, ".gitignore"), "utf-8");
assert.equal(`chickenpath/\nnewpath/`, gitignoreContent);
});
});
25 changes: 25 additions & 0 deletions packages/@apphosting/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { spawn } from "child_process";
import * as path from "node:path";
import * as fs from "fs-extra";

// Output bundle metadata specifications to be written to bundle.yaml
export interface OutputBundleConfig {
Expand Down Expand Up @@ -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.includes(entry)) {
console.log(`adding ${entry} to ${gitignorePath}`);
content += `\n${entry}\n`;
}
}

fs.writeFileSync(gitignorePath, content);
}
Loading