Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Version comparison #50

Merged
merged 5 commits into from
Sep 14, 2022
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fnpm",
"version": "0.3.10",
"version": "0.4.0",
"private": true,
"repository": {
"type": "git",
Expand All @@ -18,9 +18,9 @@
},
"devDependencies": {
"eslint-config-custom": "*",
"ora": "6.1.2",
"prettier": "latest",
"turbo": "latest",
"ora": "6.1.2"
"turbo": "latest"
},
"engines": {
"npm": ">=7.0.0",
Expand Down
12 changes: 7 additions & 5 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fnpm-io/cli",
"version": "0.3.10",
"version": "0.4.0",
"description": "FNPM CLI Tool.",
"private": false,
"main": "build/bin/cli.js",
Expand All @@ -27,13 +27,15 @@
"dependencies": {
"axios": "0.27.2",
"chalk": "5.0.1",
"execa": "^6.1.0",
"execa": "6.1.0",
"glob": "8.0.3",
"markdown-table": "^3.0.2",
"markdown-table": "3.0.2",
"ora": "6.1.2",
"pacote": "13.6.2",
"prompts": "^2.4.2",
"read-package-json-fast": "2.0.3"
"prompts": "2.4.2",
"read-package-json-fast": "2.0.3",
"npm-pick-manifest": "7.0.2",
"compare-versions": "5.0.1"
},
"devDependencies": {
"@types/glob": "7.2.0",
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/commands/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ const tests = [
group: 3,
},
{
name: "fnpm install (no cache)",
name: "FNPM install (no cache)",
command: "fnpm install",
pre: "npm cache clean -f && fnpm clear",
spinner: ora(chalk.green(`Running "fnpm install (no cache)"...`)).stop(),
spinner: ora(chalk.green(`Running "FNPM install (no cache)"...`)).stop(),
group: 1,
},
{
name: "fnpm install (with cache)",
name: "FNPM install (with cache)",
command: "fnpm install",
pre: "rm -rf node_modules",
spinner: ora(chalk.green(`Running "fnpm install (with cache)"...`)).stop(),
spinner: ora(chalk.green(`Running "FNPM install (with cache)"...`)).stop(),
group: 3,
},
{
Expand Down
20 changes: 14 additions & 6 deletions packages/cli/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@ export default async function create(args: string[]) {
return;
}

const command = args[0];
// Get global config path
const npmPath = await execa("npm", ["config", "get", "prefix"]).then(
(res) => res.stdout
);

let command = args[0];

// If command doesn't start with create- then add it
if (!command.startsWith("create-")) {
command = `create-${command}`;
}

args.shift();

const spinner = ora(`Searching ${command} in NPM Registry...`).start();
const manifest = await pacote.manifest(command);
spinner.succeed();
spinner.text = `Found ${command} in NPM Registry`;

// Check if the package is already installed

const { install } = await prompts({
type: "confirm",
name: "install",
Expand All @@ -37,11 +50,6 @@ export default async function create(args: string[]) {
});

if (install) {
// Get global config path
const npmPath = await execa("npm", ["config", "get", "prefix"]).then(
(res) => res.stdout
);

const globalPath = path.join(npmPath, "lib", "node_modules", manifest.name);

const __downloading = ora(`Downloading ${manifest.name}...`).start();
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import checkVersion from "../utils/checkVersion.js";
import run from "./run.js";
import { update } from "../utils/readConfig.js";
import create from "./create.js";
import remove from "./remove.js";

export async function commands(args: string[]) {
const [command, ...rest] = args;
Expand Down Expand Up @@ -39,6 +40,9 @@ export async function commands(args: string[]) {
case "create":
await create(rest);
break;
case "remove":
await remove(rest);
break;
default:
console.log(`Unknown command: ${command}`);
}
Expand Down
124 changes: 90 additions & 34 deletions packages/cli/src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,38 @@ import { mkdir, rm, readdir, writeFile } from "fs/promises";
import { exec } from "child_process";
import path from "path";
import { existsSync, readFileSync } from "fs";
import { getDeps } from "../utils/getDeps.js";
import pacote from "pacote";
import { performance } from "perf_hooks";
import { satisfies } from "compare-versions";
import { getDeps } from "../utils/getDeps.js";
import { installBins } from "../utils/addBinaries.js";
import { getDepsWorkspaces } from "../utils/getDepsWorkspaces.js";
import { installLocalDep } from "../utils/installLocalDep.js";
import { createModules } from "../utils/createModules.js";
import { hardLink } from "../utils/hardLink.js";
import getParamsDeps from "../utils/parseDepsParams.js";
import readConfig from "../utils/readConfig.js";
import parseTime from "../utils/parseTime.js";

let pkgs: {
type pkg = {
name: string;
version: string;
spec: string;
tarball: string;
parent?: string;
}[] = [];
};

let pkgs: pkg[] = [];

const __DOWNLOADING: string[] = [];
const __DOWNLOADED: string[] = [];
const __INSTALLED: {
name: string;
version: string;
}[] = [];

const __DOWNLOADING: string[] = [];
const __DOWNLOADED: string[] = [];
const __SKIPPED: string[] = [];

const userFnpmCache = readConfig().cache;
const downloadFile = ".fnpm";

Expand All @@ -55,6 +63,7 @@ export default async function install(opts: string[]) {
const deps = getDeps(pkg).concat(wsDeps).concat(addDeps);

const __fetch = ora(chalk.green("Fetching packages...")).start();
const __fetch_start = performance.now();

await Promise.all(
deps.map(async (dep) => {
Expand All @@ -74,28 +83,76 @@ export default async function install(opts: string[]) {
pkgs.push({
name: dep.name,
version: manifest.version,
spec: dep.version,
tarball: manifest.dist.tarball,
parent: dep.parent || undefined,
});
})
);

__fetch.succeed(chalk.green("Fetched all packages!"));
const __fetch_end = performance.now();
__fetch.succeed(
chalk.green(
`Fetched packages in ${chalk.gray(parseTime(__fetch_start, __fetch_end))}`
)
);

const __install = ora(chalk.green("Installing packages...")).start();
const __install_start = performance.now();

await Promise.all(
pkgs.map(async (pkg) => {
await installPkg(pkg, pkg.parent, __install);
})
);

__install.succeed(chalk.green("Installed all packages!"));
const __install_end = performance.now();
__install.succeed(
chalk.green(
`Installed packages in ${chalk.gray(
parseTime(__install_start, __install_end)
)}`
)
);

// Get all __SKIPPED packages, check if they are in __INSTALLED and install them if not
await Promise.all(
[...__SKIPPED].map(async (pkg) => {
const isInstalled = __INSTALLED.find((i) => i.name === pkg);

if (!isInstalled) {
const manifest = await pacote.manifest(`${pkg}@latest`, {
registry: REGISTRY,
});

ora(chalk.gray(`Installing ${pkg}@latest...`)).info();
await installPkg(
{
name: pkg,
version: manifest.version,
spec: "latest",
tarball: manifest.dist.tarball,
},
undefined,
undefined
);
}

return;
})
);

const __binaries = ora(chalk.blue("Installing binaries...")).start();
const __binaries_start = performance.now();
await installBins();

__binaries.succeed(chalk.blue("Installed binaries!"));
const __binaries_end = performance.now();
__binaries.succeed(
chalk.blue(
`Installed binaries in ${chalk.gray(
parseTime(__binaries_start, __binaries_end)
)}`
)
);

// If addDeps is not empty, add them to package.json using flag
if (addDeps.length > 0) {
Expand Down Expand Up @@ -153,7 +210,7 @@ export async function installPkg(
spinner?: Ora
) {
const cacheFolder = `${userFnpmCache}/${manifest.name}/${manifest.version}`;
// Check if package is already installed

if (
__INSTALLED.find(
(pkg) => pkg.name === manifest.name && pkg.version === manifest.version
Expand All @@ -162,6 +219,23 @@ export async function installPkg(
return;
}

const pkgInstalled =
manifest.spec &&
__INSTALLED.find(
(pkg) =>
pkg.name === manifest.name && satisfies(pkg.version, manifest.spec)
);

if (pkgInstalled) {
return;
}

// Check if spec is * and add it to __SKIPPED
if (manifest.spec === "*") {
__SKIPPED.push(manifest.name);
return;
}

// Check if package is already in root node_modules
const isSuitable = __INSTALLED.find((pkg) => pkg.name === manifest.name);

Expand All @@ -177,18 +251,6 @@ export async function installPkg(
name: manifest.name,
version: manifest.version,
});
} else {
ora(
chalk.yellow(
`Package ${
manifest.name
} is already installed in root node_modules! (${chalk.gray(
isSuitable.version
)} -> ${chalk.gray(manifest.version)}) Installing in ${chalk.grey(
pkgProjectDir.replace(process.cwd(), "")
)}...`
)
).warn();
}

// Check if parent exists
Expand Down Expand Up @@ -216,21 +278,11 @@ export async function installPkg(
readFileSync(`${cacheFolder}/${downloadFile}`, "utf-8")
);

const thisPath = path.join(
process.cwd(),
"node_modules",
parent ? parent : ""
);

for (const dep of Object.keys(cachedDeps)) {
const name = dep;
const version = Object.keys(cachedDeps[dep])[0];
const { tarball, pathname = path } = cachedDeps[dep][version];

const depPath = path.join(thisPath, "node_modules", name);

const installed = existsSync(depPath);

await installPkg(
{
name,
Expand Down Expand Up @@ -265,8 +317,9 @@ export async function installPkg(
dev: true,
});

if (deps.length > 0)
mkdir(`${cacheFolder}/node_modules`, { recursive: true });
// Disable dir creation to test if it works :)
/* if (deps.length > 0)
mkdir(`${cacheFolder}/node_modules`, { recursive: true }); */

// Install production deps
const installed = await Promise.all(
Expand All @@ -283,13 +336,15 @@ export async function installPkg(
name: dep.name,
version: manifest.version,
tarball: manifest.dist.tarball,
spec: dep.version,
},
pkgProjectDir,
spinner
);
return {
name: dep.name,
version: manifest.version,
spec: dep.version,
tarball: manifest.dist.tarball,
path: path.join(userFnpmCache, dep.name, manifest.version),
};
Expand All @@ -304,6 +359,7 @@ export async function installPkg(
[dep.version]: {
path: dep.path,
tarball: dep.tarball,
spec: dep.spec,
},
};
});
Expand Down
Loading