Skip to content

Commit fec80a0

Browse files
committed
[cli] refactor install commands
1 parent f9367bf commit fec80a0

File tree

12 files changed

+143
-115
lines changed

12 files changed

+143
-115
lines changed

cli/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {sources} from './commands/sources.js';
1010
import {checkApiCompatibility, setNetwork, apiVersion, checkConfigFile, getNetworkFile, version} from './mops.js';
1111
import {getNetwork} from './api/network.js';
1212
import {whoami} from './commands/whoami.js';
13-
import {installAll} from './commands/install-all.js';
13+
import {installAll} from './commands/install/install-all.js';
1414
import {search} from './commands/search.js';
1515
import {add} from './commands/add.js';
1616
import {cacheSize, cleanCache} from './cache.js';

cli/commands/add.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import chalk from 'chalk';
44
import {createLogUpdate} from 'log-update';
55
import {checkConfigFile, getGithubCommit, parseGithubURL, readConfig, writeConfig} from '../mops.js';
66
import {getHighestVersion} from '../api/getHighestVersion.js';
7+
import {installMopsDep} from './install/install-mops-dep.js';
78
import {installFromGithub} from '../vessel.js';
8-
import {install} from './install.js';
99
import {notifyInstalls} from '../notify-installs.js';
1010
import {checkIntegrity} from '../integrity.js';
1111
import {checkRequirements} from '../check-requirements.js';
@@ -93,7 +93,7 @@ export async function add(name : string, {verbose = false, dev = false, lock} :
9393
await installFromGithub(pkgDetails.name, pkgDetails.repo, {verbose: verbose});
9494
}
9595
else if (!pkgDetails.path) {
96-
let res = await install(pkgDetails.name, pkgDetails.version, {verbose: verbose});
96+
let res = await installMopsDep(pkgDetails.name, pkgDetails.version, {verbose: verbose});
9797
if (res === false) {
9898
return;
9999
}

cli/commands/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import prompts from 'prompts';
77

88
import {checkApiCompatibility, writeConfig} from '../mops.js';
99
import {mainActor} from '../api/actors.js';
10-
import {installAll} from './install-all.js';
10+
import {installAll} from './install/install-all.js';
1111
import {VesselConfig, readVesselConfig} from '../vessel.js';
1212
import {Config, Dependencies} from '../types.js';
1313
import {template} from './template.js';

cli/commands/install-local.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

cli/commands/install-all.ts renamed to cli/commands/install/install-all.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import process from 'node:process';
22
import chalk from 'chalk';
33
import {createLogUpdate} from 'log-update';
4-
import {checkConfigFile, readConfig} from '../mops.js';
5-
import {install} from './install.js';
6-
import {installFromGithub} from '../vessel.js';
7-
import {notifyInstalls} from '../notify-installs.js';
8-
import {checkIntegrity} from '../integrity.js';
9-
import {installLocal} from './install-local.js';
10-
import {checkRequirements} from '../check-requirements.js';
4+
import {checkConfigFile, readConfig} from '../../mops.js';
5+
import {notifyInstalls} from '../../notify-installs.js';
6+
import {checkIntegrity} from '../../integrity.js';
7+
import {installDeps} from './install-deps.js';
8+
import {checkRequirements} from '../../check-requirements.js';
119

1210
type InstallAllOptions = {
1311
verbose ?: boolean;
@@ -25,20 +23,12 @@ export async function installAll({verbose = false, silent = false, threads, lock
2523
let deps = Object.values(config.dependencies || {});
2624
let devDeps = Object.values(config['dev-dependencies'] || {});
2725
let allDeps = [...deps, ...devDeps];
28-
let installedPackages = {};
29-
30-
for (let {name, repo, path, version} of allDeps) {
31-
if (repo) {
32-
await installFromGithub(name, repo, {verbose, silent});
33-
}
34-
else {
35-
let res = await (path ? installLocal(name, path, {silent, verbose}) : install(name, version, {silent, verbose, threads}));
36-
if (res === false) {
37-
return;
38-
}
39-
installedPackages = {...installedPackages, ...res};
40-
}
26+
27+
let res = await installDeps(allDeps, {silent, verbose, threads});
28+
if (!res) {
29+
return;
4130
}
31+
let installedDeps = res;
4232

4333
let logUpdate = createLogUpdate(process.stdout, {showCursor: true});
4434

@@ -47,7 +37,7 @@ export async function installAll({verbose = false, silent = false, threads, lock
4737
}
4838

4939
await Promise.all([
50-
notifyInstalls(Object.keys(installedPackages)),
40+
notifyInstalls(Object.keys(installedDeps)),
5141
checkIntegrity(lock),
5242
]);
5343

cli/commands/install/install-dep.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import path from 'node:path';
2+
import {installFromGithub} from '../../vessel.js';
3+
import {installMopsDep} from './install-mops-dep.js';
4+
import {Dependency} from '../../types.js';
5+
import {installLocalDep} from './install-local-dep.js';
6+
7+
type InstallDepOptions = {
8+
verbose ?: boolean;
9+
silent ?: boolean;
10+
threads ?: number;
11+
};
12+
13+
// install dependency
14+
// returns false if failed
15+
export async function installDep(dep : Dependency, {verbose, silent, threads} : InstallDepOptions = {}, parentPkgPath ?: string) : Promise<Record<string, string> | false> {
16+
if (dep.repo) {
17+
await installFromGithub(dep.name, dep.repo, {silent, verbose});
18+
return {};
19+
}
20+
else if (dep.path) {
21+
let depPath = dep.path;
22+
if (parentPkgPath) {
23+
depPath = path.resolve(parentPkgPath, dep.path);
24+
}
25+
return installLocalDep(dep.name, depPath, {silent, verbose});
26+
}
27+
else if (dep.version) {
28+
return installMopsDep(dep.name, dep.version, {silent, verbose, threads});
29+
}
30+
31+
return {};
32+
}

cli/commands/install/install-deps.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {Dependency} from '../../types.js';
2+
import {installDep} from './install-dep.js';
3+
4+
type InstallDepsOptions = {
5+
verbose ?: boolean;
6+
silent ?: boolean;
7+
threads ?: number;
8+
};
9+
10+
// install all dependencies
11+
// returns actual installed dependencies
12+
// returns false if failed
13+
export async function installDeps(deps : Dependency[], {verbose, silent, threads} : InstallDepsOptions = {}, parentPkgPath ?: string) : Promise<Record<string, string> | false> {
14+
let installedDeps = {};
15+
let ok = true;
16+
for (const dep of deps) {
17+
let res = await installDep(dep, {verbose, silent, threads}, parentPkgPath);
18+
if (res) {
19+
installedDeps = {...installedDeps, ...res};
20+
}
21+
else {
22+
ok = false;
23+
}
24+
}
25+
26+
if (!ok) {
27+
return false;
28+
}
29+
30+
return installedDeps;
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import process from 'node:process';
2+
import path from 'node:path';
3+
import {createLogUpdate} from 'log-update';
4+
import {getRootDir, readConfig} from '../../mops.js';
5+
import {installDeps} from './install-deps.js';
6+
7+
type InstallLocalDepOptions = {
8+
verbose ?: boolean;
9+
silent ?: boolean;
10+
};
11+
12+
// skip install and just find non-local dependencies to install
13+
// pkgPath should be relative to the current root dir or absolute
14+
export async function installLocalDep(pkg : string, pkgPath = '', {verbose, silent} : InstallLocalDepOptions = {}) : Promise<Record<string, string> | false> {
15+
if (!silent) {
16+
let logUpdate = createLogUpdate(process.stdout, {showCursor: true});
17+
logUpdate(`Local dependency ${pkg} = "${pkgPath}"`);
18+
19+
if (verbose) {
20+
silent || logUpdate.done();
21+
}
22+
else {
23+
logUpdate.clear();
24+
}
25+
}
26+
27+
// install dependencies
28+
let dir = path.resolve(getRootDir(), pkgPath);
29+
let config = readConfig(path.join(dir, 'mops.toml'));
30+
return installDeps(Object.values(config.dependencies || {}), {silent, verbose}, pkgPath);
31+
}

cli/commands/install.ts renamed to cli/commands/install/install-mops-dep.ts

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
import process from 'node:process';
2-
import path from 'node:path';
32
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import {Buffer} from 'node:buffer';
45
import {createLogUpdate} from 'log-update';
56
import chalk from 'chalk';
6-
import {checkConfigFile, formatDir, progressBar, readConfig} from '../mops.js';
7-
import {getHighestVersion} from '../api/getHighestVersion.js';
8-
import {storageActor} from '../api/actors.js';
9-
import {parallel} from '../parallel.js';
10-
import {installFromGithub} from '../vessel.js';
11-
import {addCache, copyCache, isCached} from '../cache.js';
12-
import {downloadFile, getPackageFilesInfo} from '../api/downloadPackageFiles.js';
13-
import {installLocal} from './install-local.js';
14-
15-
export async function install(pkg : string, version = '', {verbose = false, silent = false, dep = false, threads = 12} = {}) : Promise<Record<string, string> | false> {
7+
import {checkConfigFile, formatDir, progressBar, readConfig} from '../../mops.js';
8+
import {getHighestVersion} from '../../api/getHighestVersion.js';
9+
import {storageActor} from '../../api/actors.js';
10+
import {parallel} from '../../parallel.js';
11+
import {addCache, copyCache, isCached} from '../../cache.js';
12+
import {downloadFile, getPackageFilesInfo} from '../../api/downloadPackageFiles.js';
13+
import {installDeps} from './install-deps.js';
14+
15+
type InstallMopsDepOptions = {
16+
verbose ?: boolean;
17+
silent ?: boolean;
18+
dep ?: boolean;
19+
threads ?: number;
20+
};
21+
22+
export async function installMopsDep(pkg : string, version = '', {verbose, silent, dep, threads} : InstallMopsDepOptions = {}) : Promise<Record<string, string> | false> {
23+
threads = threads || 12;
24+
1625
if (!checkConfigFile()) {
1726
return false;
1827
}
@@ -95,31 +104,17 @@ export async function install(pkg : string, version = '', {verbose = false, sile
95104
}
96105

97106
// install dependencies
98-
let ok = true;
99107
let config = readConfig(path.join(dir, 'mops.toml'));
100-
let deps = Object.values(config.dependencies || {});
101-
let installedDeps = {};
102-
for (const {name, repo, version, path: depPath} of deps) {
103-
if (repo) {
104-
await installFromGithub(name, repo, {silent, verbose});
105-
}
106-
else {
107-
let res = await (depPath ? installLocal(name, depPath, {silent, verbose}) : install(name, version, {silent, verbose}));
108-
if (res) {
109-
installedDeps = {...installedDeps, ...res};
110-
}
111-
else {
112-
ok = false;
113-
}
114-
}
108+
let res = await installDeps(Object.values(config.dependencies || {}), {silent, verbose});
109+
110+
if (!res) {
111+
return false;
115112
}
113+
let installedDeps = res;
116114

115+
// add self to installed deps
117116
if (!alreadyInstalled) {
118117
installedDeps = {...installedDeps, [pkg]: version};
119118
}
120-
121-
if (!ok) {
122-
return false;
123-
}
124119
return installedDeps;
125120
}

cli/package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)