Skip to content
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

fix(install): reinstall same dependencies #47

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 29 additions & 0 deletions src/cmd/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ describe.sequential("install all", () => {
publish(path.join(remoteStorage, "c-0.0.2"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "d-0.0.1"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "e-0.0.1"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "e-0.0.2"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "f-0.0.1"), ctx.registry.endpoint, "fake-token"),
publish(path.join(remoteStorage, "e-0.0.3"), ctx.registry.endpoint, "fake-token"),
]);
}

Expand Down Expand Up @@ -155,6 +158,8 @@ describe.sequential("install all", () => {
"a-0.0.1",
"b-0.0.1",
"c-0.0.1",
"f-0.0.1",
"e-0.0.3",
]));

expect(result.deps).toStrictEqual({
Expand Down Expand Up @@ -200,6 +205,27 @@ describe.sequential("install all", () => {
target: expect.any(String),
meta: expect.any(Object),
},
"e-0.0.2": {
name: "e",
version: "0.0.2",
isAlreadyExist: false,
target: expect.any(String),
meta: expect.any(Object),
},
"e-0.0.3": {
name: "e",
version: "0.0.3",
isAlreadyExist: false,
target: expect.any(String),
meta: expect.any(Object),
},
"f-0.0.1": {
name: "f",
version: "0.0.1",
isAlreadyExist: false,
target: expect.any(String),
meta: expect.any(Object),
},
});

const fileList = await globby(`**/${ooPackageName}`, {
Expand All @@ -215,6 +241,9 @@ describe.sequential("install all", () => {
"c-0.0.2/package.oo.yaml",
"d-0.0.1/package.oo.yaml",
"e-0.0.1/package.oo.yaml",
"e-0.0.2/package.oo.yaml",
"e-0.0.3/package.oo.yaml",
"f-0.0.1/package.oo.yaml",
]));
});
});
Expand Down
64 changes: 64 additions & 0 deletions src/utils/fs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { readdir } from "node:fs/promises";
import path from "node:path";
import { globby } from "globby";
import { beforeEach, describe, expect, it } from "vitest";
import { fixture } from "../../tests/helper/fs";
import { copyDir, remove, tempDir } from "./fs";

beforeEach(async (ctx) => {
const temp = await tempDir();

ctx.workdir = temp;

return async () => {
await remove(temp);
};
});

describe.concurrent("copyDir", () => {
it("should copy dir", async (ctx) => {
const p = fixture("fs_copy_dir");
const target = path.join(ctx.workdir, "target");
await copyDir(p, target);
expect(await readdir(target)).toEqual(await readdir(p));
});

it("should copy dir with filter", async (ctx) => {
const p = fixture("fs_copy_dir");
const target = path.join(ctx.workdir, "target");

const files = await globby("**/3.txt", {
cwd: p,
dot: true,
onlyFiles: false,
gitignore: true,
absolute: true,
});

await copyDir(p, target, (source, _) => {
if (source === p) {
return true;
}

return files[0].includes(source);
});

const files2 = await globby("**", {
cwd: target,
dot: true,
onlyFiles: false,
gitignore: true,
absolute: false,
});

expect(files2).toContain(path.join("bar", "baz", "3.txt"));
});

// see: https://github.com/nodejs/node/pull/53534
it.skip("should copy dir with dir already exists", async (ctx) => {
const p = fixture("fs_copy_dir");
const target = path.join(ctx.workdir, "target");
const ps = Array.from({ length: 100 }).fill(0).map(() => copyDir(p, target));
await Promise.all(ps);
});
});
15 changes: 12 additions & 3 deletions src/utils/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ export async function transformNodeModules(dir: string) {
absolute: true,
});

const map = new Map<string, {
source: string;
name: string;
version: string;
}>();

const ps = result
// Filter out paths that do not conform to this rule: node_modules/PACKAGE_NAME/package/package.oo.yaml
.filter((p) => {
Expand All @@ -137,12 +143,15 @@ export async function transformNodeModules(dir: string) {
.map(async (p) => {
const source = path.dirname(p);
const { name, version } = await generatePackageJson(source, false);
return {

map.set(`${name}-${version}`, {
source,
name,
version,
};
});
});

return await Promise.all(ps);
await Promise.all(ps);

return Array.from(map.values());
}
Empty file.
Empty file.
Empty file.
11 changes: 10 additions & 1 deletion tests/fixtures/install_all/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ entry
a: 0.0.1
c: 0.0.2
d: 0.0.1
e: 0.0.2
b: 0.0.1
e: 0.0.1
c: 0.0.1
e: 0.0.2
b: 0.0.1
f: 0.0.1
e: 0.0.1
e: 0.0.3
```

#### Local Storage (Already exists locally)
Expand All @@ -26,11 +32,14 @@ b: 0.0.1
c: 0.0.2
d: 0.0.1
e: 0.0.1
e: 0.0.2
e: 0.0.3
f: 0.0.1
```

### Installation Logic

1. Only install the `a:0.0.1` `b:0.0.1` `c:0.0.2` `d:0.0.1` `e:0.0.1` dependencies.
1. Only install the `a:0.0.1` `b:0.0.1` `c:0.0.2` `d:0.0.1` `e:0.0.1` `e:0.0.2` `f:0.0.1` `e:0.0.3` dependencies.
2. Do not install the `c:0.0.1` dependency as it is already installed locally.
3. Do install the `e:0.0.1` dependency as it is sub-dependency of `b:0.0.1`.
1. _oopm_ don't know about this sub-dependency as it is not mentioned in the `entry` package.
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/install_all/entry/package.oo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ dependencies:
a: 0.0.1
b: 0.0.1
c: 0.0.1
f: 0.0.1
e: 0.0.3
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ version: 0.0.1
dependencies:
c: 0.0.2
d: 0.0.1
e: 0.0.2
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
name: c
version: 0.0.2
dependencies:
e: 0.0.2
b: 0.0.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: e
version: 0.0.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: e
version: 0.0.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: f
version: 0.0.1
dependencies:
e: 0.0.1