Skip to content

Commit

Permalink
feat(pack): support .gitignore file
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Cui <[email protected]>
  • Loading branch information
BlackHole1 committed Jan 13, 2025
1 parent 9e435c6 commit 9c68812
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/cmd/pack.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from "node:path";
import * as tar from "tar";
import { afterEach, describe, expect, it } from "vitest";
import { fixture } from "../../tests/helper/fs";
import { exists, remove } from "../utils/fs";
Expand Down Expand Up @@ -44,6 +45,17 @@ describe("pack", () => {
await remove(tgz);
await pack(p, p);
await expect(exists(tgz)).resolves.toBe(true);

{
const result: string[] = [];
await tar.t({
f: tgz,
onReadEntry: (entry) => {
result.push(entry.path.replaceAll("\\", "/"));
},
});
expect(result).toContainEqual("package/package/.gitignore");
}
await remove(tgz);
});
});
2 changes: 1 addition & 1 deletion src/cmd/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function prePack(p: string, ignore: string[]) {

await Promise.all([
copyDir(p, path.join(workdir, "package"), (source, _) => {
const relative = path.relative(p, source);
const relative = path.relative(p, source).split(path.sep);
return !ignore.some(i => relative.includes(i));
}),
writeFile(path.join(workdir, "package.json"), packageJson),
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ export interface OOPackageSchema {
dependencies?: Record<string, string>;
scripts: Record<string, string>;
icon?: string;
files?: string[];
}
8 changes: 8 additions & 0 deletions src/utils/npm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ describe.concurrent("generatePackageJson", () => {
...content,
scripts: {},
icon: `./${path.join("package", "icon.png")}`,
files: [
"package",
"package/.gitignore",
],
});
});

Expand All @@ -69,6 +73,10 @@ describe.concurrent("generatePackageJson", () => {
expect(result).toEqual({
...content,
scripts: {},
files: [
"package",
"package/.gitignore",
],
});
});
});
Expand Down
5 changes: 5 additions & 0 deletions src/utils/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export async function generatePackageJson(dir: string, stringify = true): Promis

content.scripts = {};

content.files = [
"package",
"package/.gitignore",
];

if (stringify) {
return JSON.stringify(content, null, 2);
}
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/pack_success/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaa

0 comments on commit 9c68812

Please sign in to comment.