Skip to content

Commit 9f5f56f

Browse files
authored
feat(install): add return val in installAll / installDeps (#11)
Signed-off-by: Kevin Cui <[email protected]>
1 parent eba2caa commit 9f5f56f

File tree

3 files changed

+133
-12
lines changed

3 files changed

+133
-12
lines changed

src/cmd/install.test.ts

+78-2
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,58 @@ describe.sequential("install all", () => {
144144
// Copy `entry` to workdir
145145
await copyDir(path.join(p, "entry"), ctx.workdir);
146146

147-
await install({
147+
const result = await install({
148148
all: true,
149149
token: "fake-token",
150150
workdir: ctx.workdir,
151151
distDir,
152152
});
153153

154+
expect(result.deps).toStrictEqual({
155+
"a-0.0.1": {
156+
name: "a",
157+
version: "0.0.1",
158+
isAlreadyExist: false,
159+
target: expect.any(String),
160+
meta: expect.any(Object),
161+
},
162+
"b-0.0.1": {
163+
name: "b",
164+
version: "0.0.1",
165+
isAlreadyExist: false,
166+
target: expect.any(String),
167+
meta: expect.any(Object),
168+
},
169+
"c-0.0.1": {
170+
name: "c",
171+
version: "0.0.1",
172+
isAlreadyExist: true,
173+
target: expect.any(String),
174+
meta: expect.any(Object),
175+
},
176+
"c-0.0.2": {
177+
name: "c",
178+
version: "0.0.2",
179+
isAlreadyExist: false,
180+
target: expect.any(String),
181+
meta: expect.any(Object),
182+
},
183+
"d-0.0.1": {
184+
name: "d",
185+
version: "0.0.1",
186+
isAlreadyExist: false,
187+
target: expect.any(String),
188+
meta: expect.any(Object),
189+
},
190+
"e-0.0.1": {
191+
name: "e",
192+
version: "0.0.1",
193+
isAlreadyExist: true,
194+
target: expect.any(String),
195+
meta: expect.any(Object),
196+
},
197+
});
198+
154199
const fileList = await fg.glob(`**/${ooPackageName}`, {
155200
cwd: distDir,
156201
onlyFiles: true,
@@ -194,7 +239,7 @@ describe.sequential("install deps", () => {
194239
// Copy `entry` to workdir
195240
await copyDir(path.join(p, "entry"), ctx.workdir);
196241

197-
await install({
242+
const result = await install({
198243
deps: [
199244
{ name: "a" },
200245
{ name: "b" },
@@ -206,6 +251,37 @@ describe.sequential("install deps", () => {
206251
distDir,
207252
});
208253

254+
expect(result.deps).toStrictEqual({
255+
"a-0.0.2": {
256+
name: "a",
257+
version: "0.0.2",
258+
isAlreadyExist: false,
259+
target: expect.any(String),
260+
meta: expect.any(Object),
261+
},
262+
"b-0.0.2": {
263+
name: "b",
264+
version: "0.0.2",
265+
isAlreadyExist: false,
266+
target: expect.any(String),
267+
meta: expect.any(Object),
268+
},
269+
"c-0.0.1": {
270+
name: "c",
271+
version: "0.0.1",
272+
isAlreadyExist: false,
273+
target: expect.any(String),
274+
meta: expect.any(Object),
275+
},
276+
"d-0.0.1": {
277+
name: "d",
278+
version: "0.0.1",
279+
isAlreadyExist: false,
280+
target: expect.any(String),
281+
meta: expect.any(Object),
282+
},
283+
});
284+
209285
const deps = (await generatePackageJson(ctx.workdir, false)).dependencies;
210286
expect(deps).toEqual({
211287
a: "0.0.2",

src/cmd/install.ts

+44-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Dep, DepRaw, Deps, InstallFileResult } from "../types";
1+
import type { Dep, DepRaw, Deps, InstallAllResult, InstallFileResult, InstallPackageResult } from "../types";
22
import path from "node:path";
33
import { execa } from "execa";
44
import { ooPackageName } from "../const";
@@ -35,9 +35,10 @@ export interface InstallPackageOptions extends InstallBasicOptions {
3535

3636
export type InstallOptions = InstallAllOptions | InstallFileOptions | InstallPackageOptions;
3737

38-
export async function install(options: InstallAllOptions | InstallPackageOptions): Promise<void>;
38+
export async function install(options: InstallAllOptions): Promise<InstallAllResult>;
39+
export async function install(options: InstallPackageOptions): Promise<InstallPackageResult>;
3940
export async function install(options: InstallFileOptions): Promise<InstallFileResult>;
40-
export async function install(options: InstallOptions): Promise<InstallFileResult | void> {
41+
export async function install(options: InstallOptions): Promise<InstallFileResult | InstallPackageResult | InstallAllResult> {
4142
if ("file" in options) {
4243
return await installFile(options);
4344
}
@@ -97,7 +98,7 @@ export async function installFile(options: InstallFileOptions): Promise<InstallF
9798

9899
// oopm install foo
99100
// oopm install [email protected]
100-
export async function installPackage(options: InstallPackageOptions) {
101+
export async function installPackage(options: InstallPackageOptions): Promise<InstallPackageResult> {
101102
const libraryMeta = await generatePackageJson(options.workdir, false);
102103

103104
const alreadyInstalled: Deps = [];
@@ -144,18 +145,22 @@ export async function installPackage(options: InstallPackageOptions) {
144145
await Promise.all(p);
145146
}
146147

147-
await _install({
148+
const deps = await _install({
148149
alreadyInstalled,
149150
needInstall,
150151
save: options.save,
151152
workdir: options.workdir,
152153
distDir: options.distDir,
153154
token: options.token,
154155
});
156+
157+
return {
158+
deps,
159+
};
155160
}
156161

157162
// oopm install
158-
export async function installAll(options: InstallAllOptions) {
163+
export async function installAll(options: InstallAllOptions): Promise<InstallAllResult> {
159164
const libraryMeta = await generatePackageJson(options.workdir, false);
160165

161166
const alreadyInstalled: Deps = [];
@@ -180,14 +185,18 @@ export async function installAll(options: InstallAllOptions) {
180185
await Promise.all(p);
181186
}
182187

183-
await _install({
188+
const deps = await _install({
184189
alreadyInstalled,
185190
needInstall,
186191
save: false,
187192
workdir: options.workdir,
188193
distDir: options.distDir,
189194
token: options.token,
190195
});
196+
197+
return {
198+
deps,
199+
};
191200
}
192201

193202
interface _InstallOptions {
@@ -199,7 +208,7 @@ interface _InstallOptions {
199208
needInstall: Deps;
200209
}
201210

202-
async function _install(options: _InstallOptions) {
211+
async function _install(options: _InstallOptions): Promise<InstallPackageResult["deps"]> {
203212
const temp = await tempDir();
204213
await initPackageJson(temp, options.needInstall, options.token);
205214

@@ -218,6 +227,20 @@ async function _install(options: _InstallOptions) {
218227

219228
await mkdir(options.distDir);
220229

230+
const targets: InstallPackageResult["deps"] = {};
231+
232+
for (const dep of options.alreadyInstalled) {
233+
const target = path.join(options.distDir, `${dep.name}-${dep.version}`);
234+
235+
targets[`${dep.name}-${dep.version}`] = {
236+
name: dep.name,
237+
version: dep.version,
238+
target,
239+
isAlreadyExist: true,
240+
meta: await generatePackageJson(target, false),
241+
};
242+
}
243+
221244
const ps = info
222245
.filter((i) => {
223246
if (options.alreadyInstalled.length === 0) {
@@ -230,7 +253,17 @@ async function _install(options: _InstallOptions) {
230253
})
231254
.map(async (i) => {
232255
const target = path.join(options.distDir, `${i.name}-${i.version}`);
233-
if (!await exists(target)) {
256+
const isAlreadyExist = await exists(target);
257+
258+
targets[`${i.name}-${i.version}`] = {
259+
name: i.name,
260+
version: i.version,
261+
target,
262+
isAlreadyExist,
263+
meta: await generatePackageJson(i.source, false),
264+
};
265+
266+
if (!isAlreadyExist) {
234267
return copyDir(i.source, target);
235268
}
236269
});
@@ -242,4 +275,6 @@ async function _install(options: _InstallOptions) {
242275
await Promise.all(ps);
243276

244277
await remove(temp);
278+
279+
return targets;
245280
}

src/types.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@ export interface InstallFileResult {
1414
target: string;
1515
meta: Omit<OOPackageSchema, "scripts">;
1616
isOverwrite: boolean;
17-
};
17+
}
18+
19+
export interface InstallPackageResult {
20+
deps: Record<`${Dep["name"]}-${Dep["version"]}`, Dep & {
21+
target: string;
22+
meta: Omit<OOPackageSchema, "scripts">;
23+
isAlreadyExist: boolean;
24+
}>;
25+
}
26+
27+
export interface InstallAllResult extends InstallPackageResult {}
1828

1929
export interface OOPackageSchema {
2030
name: string;

0 commit comments

Comments
 (0)