Skip to content

Commit

Permalink
[cli] refactor install commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenVoich committed Apr 1, 2024
1 parent f9367bf commit fec80a0
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 115 deletions.
2 changes: 1 addition & 1 deletion cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {sources} from './commands/sources.js';
import {checkApiCompatibility, setNetwork, apiVersion, checkConfigFile, getNetworkFile, version} from './mops.js';
import {getNetwork} from './api/network.js';
import {whoami} from './commands/whoami.js';
import {installAll} from './commands/install-all.js';
import {installAll} from './commands/install/install-all.js';
import {search} from './commands/search.js';
import {add} from './commands/add.js';
import {cacheSize, cleanCache} from './cache.js';
Expand Down
4 changes: 2 additions & 2 deletions cli/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import chalk from 'chalk';
import {createLogUpdate} from 'log-update';
import {checkConfigFile, getGithubCommit, parseGithubURL, readConfig, writeConfig} from '../mops.js';
import {getHighestVersion} from '../api/getHighestVersion.js';
import {installMopsDep} from './install/install-mops-dep.js';
import {installFromGithub} from '../vessel.js';
import {install} from './install.js';
import {notifyInstalls} from '../notify-installs.js';
import {checkIntegrity} from '../integrity.js';
import {checkRequirements} from '../check-requirements.js';
Expand Down Expand Up @@ -93,7 +93,7 @@ export async function add(name : string, {verbose = false, dev = false, lock} :
await installFromGithub(pkgDetails.name, pkgDetails.repo, {verbose: verbose});
}
else if (!pkgDetails.path) {
let res = await install(pkgDetails.name, pkgDetails.version, {verbose: verbose});
let res = await installMopsDep(pkgDetails.name, pkgDetails.version, {verbose: verbose});
if (res === false) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import prompts from 'prompts';

import {checkApiCompatibility, writeConfig} from '../mops.js';
import {mainActor} from '../api/actors.js';
import {installAll} from './install-all.js';
import {installAll} from './install/install-all.js';
import {VesselConfig, readVesselConfig} from '../vessel.js';
import {Config, Dependencies} from '../types.js';
import {template} from './template.js';
Expand Down
52 changes: 0 additions & 52 deletions cli/commands/install-local.ts

This file was deleted.

32 changes: 11 additions & 21 deletions cli/commands/install-all.ts → cli/commands/install/install-all.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import process from 'node:process';
import chalk from 'chalk';
import {createLogUpdate} from 'log-update';
import {checkConfigFile, readConfig} from '../mops.js';
import {install} from './install.js';
import {installFromGithub} from '../vessel.js';
import {notifyInstalls} from '../notify-installs.js';
import {checkIntegrity} from '../integrity.js';
import {installLocal} from './install-local.js';
import {checkRequirements} from '../check-requirements.js';
import {checkConfigFile, readConfig} from '../../mops.js';
import {notifyInstalls} from '../../notify-installs.js';
import {checkIntegrity} from '../../integrity.js';
import {installDeps} from './install-deps.js';
import {checkRequirements} from '../../check-requirements.js';

type InstallAllOptions = {
verbose ?: boolean;
Expand All @@ -25,20 +23,12 @@ export async function installAll({verbose = false, silent = false, threads, lock
let deps = Object.values(config.dependencies || {});
let devDeps = Object.values(config['dev-dependencies'] || {});
let allDeps = [...deps, ...devDeps];
let installedPackages = {};

for (let {name, repo, path, version} of allDeps) {
if (repo) {
await installFromGithub(name, repo, {verbose, silent});
}
else {
let res = await (path ? installLocal(name, path, {silent, verbose}) : install(name, version, {silent, verbose, threads}));
if (res === false) {
return;
}
installedPackages = {...installedPackages, ...res};
}

let res = await installDeps(allDeps, {silent, verbose, threads});
if (!res) {
return;
}
let installedDeps = res;

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

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

await Promise.all([
notifyInstalls(Object.keys(installedPackages)),
notifyInstalls(Object.keys(installedDeps)),
checkIntegrity(lock),
]);

Expand Down
32 changes: 32 additions & 0 deletions cli/commands/install/install-dep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import path from 'node:path';
import {installFromGithub} from '../../vessel.js';
import {installMopsDep} from './install-mops-dep.js';
import {Dependency} from '../../types.js';
import {installLocalDep} from './install-local-dep.js';

type InstallDepOptions = {
verbose ?: boolean;
silent ?: boolean;
threads ?: number;
};

// install dependency
// returns false if failed
export async function installDep(dep : Dependency, {verbose, silent, threads} : InstallDepOptions = {}, parentPkgPath ?: string) : Promise<Record<string, string> | false> {
if (dep.repo) {
await installFromGithub(dep.name, dep.repo, {silent, verbose});
return {};
}
else if (dep.path) {
let depPath = dep.path;
if (parentPkgPath) {
depPath = path.resolve(parentPkgPath, dep.path);
}
return installLocalDep(dep.name, depPath, {silent, verbose});
}
else if (dep.version) {
return installMopsDep(dep.name, dep.version, {silent, verbose, threads});
}

return {};
}
31 changes: 31 additions & 0 deletions cli/commands/install/install-deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {Dependency} from '../../types.js';
import {installDep} from './install-dep.js';

type InstallDepsOptions = {
verbose ?: boolean;
silent ?: boolean;
threads ?: number;
};

// install all dependencies
// returns actual installed dependencies
// returns false if failed
export async function installDeps(deps : Dependency[], {verbose, silent, threads} : InstallDepsOptions = {}, parentPkgPath ?: string) : Promise<Record<string, string> | false> {
let installedDeps = {};
let ok = true;
for (const dep of deps) {
let res = await installDep(dep, {verbose, silent, threads}, parentPkgPath);
if (res) {
installedDeps = {...installedDeps, ...res};
}
else {
ok = false;
}
}

if (!ok) {
return false;
}

return installedDeps;
}
31 changes: 31 additions & 0 deletions cli/commands/install/install-local-dep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import process from 'node:process';
import path from 'node:path';
import {createLogUpdate} from 'log-update';
import {getRootDir, readConfig} from '../../mops.js';
import {installDeps} from './install-deps.js';

type InstallLocalDepOptions = {
verbose ?: boolean;
silent ?: boolean;
};

// skip install and just find non-local dependencies to install
// pkgPath should be relative to the current root dir or absolute
export async function installLocalDep(pkg : string, pkgPath = '', {verbose, silent} : InstallLocalDepOptions = {}) : Promise<Record<string, string> | false> {
if (!silent) {
let logUpdate = createLogUpdate(process.stdout, {showCursor: true});
logUpdate(`Local dependency ${pkg} = "${pkgPath}"`);

if (verbose) {
silent || logUpdate.done();
}
else {
logUpdate.clear();
}
}

// install dependencies
let dir = path.resolve(getRootDir(), pkgPath);
let config = readConfig(path.join(dir, 'mops.toml'));
return installDeps(Object.values(config.dependencies || {}), {silent, verbose}, pkgPath);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import process from 'node:process';
import path from 'node:path';
import fs from 'node:fs';
import path from 'node:path';
import {Buffer} from 'node:buffer';
import {createLogUpdate} from 'log-update';
import chalk from 'chalk';
import {checkConfigFile, formatDir, progressBar, readConfig} from '../mops.js';
import {getHighestVersion} from '../api/getHighestVersion.js';
import {storageActor} from '../api/actors.js';
import {parallel} from '../parallel.js';
import {installFromGithub} from '../vessel.js';
import {addCache, copyCache, isCached} from '../cache.js';
import {downloadFile, getPackageFilesInfo} from '../api/downloadPackageFiles.js';
import {installLocal} from './install-local.js';

export async function install(pkg : string, version = '', {verbose = false, silent = false, dep = false, threads = 12} = {}) : Promise<Record<string, string> | false> {
import {checkConfigFile, formatDir, progressBar, readConfig} from '../../mops.js';
import {getHighestVersion} from '../../api/getHighestVersion.js';
import {storageActor} from '../../api/actors.js';
import {parallel} from '../../parallel.js';
import {addCache, copyCache, isCached} from '../../cache.js';
import {downloadFile, getPackageFilesInfo} from '../../api/downloadPackageFiles.js';
import {installDeps} from './install-deps.js';

type InstallMopsDepOptions = {
verbose ?: boolean;
silent ?: boolean;
dep ?: boolean;
threads ?: number;
};

export async function installMopsDep(pkg : string, version = '', {verbose, silent, dep, threads} : InstallMopsDepOptions = {}) : Promise<Record<string, string> | false> {
threads = threads || 12;

if (!checkConfigFile()) {
return false;
}
Expand Down Expand Up @@ -95,31 +104,17 @@ export async function install(pkg : string, version = '', {verbose = false, sile
}

// install dependencies
let ok = true;
let config = readConfig(path.join(dir, 'mops.toml'));
let deps = Object.values(config.dependencies || {});
let installedDeps = {};
for (const {name, repo, version, path: depPath} of deps) {
if (repo) {
await installFromGithub(name, repo, {silent, verbose});
}
else {
let res = await (depPath ? installLocal(name, depPath, {silent, verbose}) : install(name, version, {silent, verbose}));
if (res) {
installedDeps = {...installedDeps, ...res};
}
else {
ok = false;
}
}
let res = await installDeps(Object.values(config.dependencies || {}), {silent, verbose});

if (!res) {
return false;
}
let installedDeps = res;

// add self to installed deps
if (!alreadyInstalled) {
installedDeps = {...installedDeps, [pkg]: version};
}

if (!ok) {
return false;
}
return installedDeps;
}
12 changes: 6 additions & 6 deletions cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"bun": "1.0.35",
"esbuild": "0.20.1",
"tsx": "4.7.1",
"typescript": "5.3.2"
"typescript": "5.4.3"
},
"overrides": {
"@dfinity/agent": "^0.19.3",
Expand Down
1 change: 1 addition & 0 deletions cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false,
"exactOptionalPropertyTypes": false,
"declaration": true
}
}

0 comments on commit fec80a0

Please sign in to comment.