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

v0.4.1 #53

Merged
merged 6 commits into from
Sep 24, 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 apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
"next": "12.2.3",
"react": "18.2.0",
"react-dom": "18.2.0",
"ui": "*"
"ui": "workspace:*"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@types/node": "^17.0.12",
"@types/react": "17.0.48",
"autoprefixer": "^10.4.8",
"eslint": "7.32.0",
"eslint-config-custom": "*",
"eslint-config-custom": "workspace:*",
"next-transpile-modules": "9.0.0",
"postcss": "^8.4.16",
"tailwindcss": "^3.1.8",
"tsconfig": "*",
"tsconfig": "workspace:*",
"typescript": "^4.5.3"
}
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fnpm",
"version": "0.4.0",
"version": "0.4.1",
"private": true,
"repository": {
"type": "git",
Expand All @@ -17,7 +17,8 @@
"format": "prettier --write \"**/*.{ts,tsx,md}\""
},
"devDependencies": {
"eslint-config-custom": "*",
"eslint-config-custom": "workspace:*",
"tsconfig": "workspace:*",
"ora": "6.1.2",
"prettier": "latest",
"turbo": "latest"
Expand Down
7 changes: 4 additions & 3 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.4.0",
"version": "0.4.1",
"description": "FNPM CLI Tool.",
"private": false,
"main": "build/bin/cli.js",
Expand All @@ -13,7 +13,8 @@
"scripts": {
"dev": "swc ./src -d ./build --watch",
"build": "swc ./src -d ./build",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"publish": "npm publish"
},
"files": [
"build"
Expand Down Expand Up @@ -45,4 +46,4 @@
"@swc/core": "1.2.242",
"@types/mv": "2.1.2"
}
}
}
20 changes: 19 additions & 1 deletion packages/cli/src/commands/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import { writeFile } from "fs/promises";
import { markdownTable } from "markdown-table";
import path from "path";
import { execa } from "execa";
import rpjf from "read-package-json-fast";
import { fileURLToPath } from "url";

const homeDir = os.homedir();

const __filename = fileURLToPath(import.meta.url);

const __dirname = path.dirname(__filename);

const tests = [
{
name: "NPM install (no cache / no lockfile)",
Expand Down Expand Up @@ -118,6 +124,8 @@ const tests = [
];

export async function benchmark(args: string[]) {
const pkg = await rpjf(path.join(__dirname, "..", "..", "package.json"));
const currentPkg = await rpjf(path.join(process.cwd(), "package.json"));
// If the user passed flag --only-fnpm, we only run the fnpm tests
const onlyfnpm = args.includes("--only-fnpm");
const ignoreBun = args.includes("--ignore-bun");
Expand All @@ -130,7 +138,7 @@ export async function benchmark(args: string[]) {

const testsToRun = !selectedGroup
? onlyfnpm
? tests.filter((test) => test.name.includes("fnpm"))
? tests.filter((test) => test.name.includes("FNPM"))
: tests
: tests.filter((test) => test.group === parseInt(selectedGroup));

Expand Down Expand Up @@ -252,6 +260,16 @@ export async function benchmark(args: string[]) {
};
});

// Print version info
console.log(
chalk.green(`
Node.js: ${process.version}
OS: ${process.platform}
FNPM version: ${pkg.version}
Current project: ${currentPkg.name} (${currentPkg.version || "no version"})
\n`)
);

// Print the results
console.table(fmt);

Expand Down
134 changes: 75 additions & 59 deletions packages/cli/src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { hardLink } from "../utils/hardLink.js";
import getParamsDeps from "../utils/parseDepsParams.js";
import readConfig from "../utils/readConfig.js";
import parseTime from "../utils/parseTime.js";
import { spinnerGradient } from "../utils/spinnerGradient.js";

type pkg = {
name: string;
Expand Down Expand Up @@ -97,7 +98,7 @@ export default async function install(opts: string[]) {
)
);

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

await Promise.all(
Expand All @@ -106,26 +107,15 @@ export default async function install(opts: string[]) {
})
);

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) => {
[...new Set(__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,
Expand All @@ -134,14 +124,23 @@ export default async function install(opts: string[]) {
tarball: manifest.dist.tarball,
},
undefined,
undefined
__install
);
}

return;
})
);

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

const __binaries = ora(chalk.blue("Installing binaries...")).start();
const __binaries_start = performance.now();
await installBins();
Expand Down Expand Up @@ -237,16 +236,24 @@ export async function installPkg(
}

// Check if package is already in root node_modules
const isSuitable = __INSTALLED.find((pkg) => pkg.name === manifest.name);
const isInRoot = existsSync(
path.join(process.cwd(), "node_modules", manifest.name)
);

// If package is already installed, but not in root node_modules then install it in root node_modules else install it in parent node_modules
const pkgProjectDir = !isSuitable
? path.join(process.cwd(), "node_modules", manifest.name)
: parent
? path.join(parent, "node_modules", manifest.name)
: path.join(process.cwd(), "node_modules", manifest.name);
const getDir = () => {
if (!isInRoot) {
return path.join(process.cwd(), "node_modules", manifest.name);
}
if (parent) {
return path.join(parent, "node_modules", manifest.name);
} else {
return path.join(process.cwd(), "node_modules", manifest.name);
}
};

const pkgProjectDir = getDir();

if (!isSuitable) {
if (!isInRoot) {
__INSTALLED.push({
name: manifest.name,
version: manifest.version,
Expand All @@ -268,11 +275,13 @@ export async function installPkg(
spinner.text = chalk.green(
`Installing ${manifest.name}... ${chalk.grey("(cached)")}`
);

// Create directory for package without the last folder
const dirs = pkgProjectDir.split("/");
dirs.pop();
await mkdir(dirs.join("/"), { recursive: true });
await hardLink(cacheFolder, pkgProjectDir).catch((e) => {});

// Get deps from file
const cachedDeps = JSON.parse(
readFileSync(`${cacheFolder}/${downloadFile}`, "utf-8")
Expand All @@ -281,16 +290,16 @@ export async function installPkg(
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 { tarball, spec } = cachedDeps[dep][version];

await installPkg(
{
name,
version,
tarball,
pathname,
spec,
},
path.join(process.cwd(), "node_modules", manifest.name),
pkgProjectDir,
spinner
);
}
Expand All @@ -300,9 +309,7 @@ export async function installPkg(
`Installing ${manifest.name}... ${chalk.grey("(cache miss)")}`
);
await extract(cacheFolder, manifest.tarball);
if (existsSync(pkgProjectDir)) {
await rm(pkgProjectDir, { recursive: true });
}

// Create directory for package without the last folder
const dirs = pkgProjectDir.split("/");
dirs.pop();
Expand All @@ -317,10 +324,6 @@ export async function installPkg(
dev: 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(
deps.map(async (dep) => {
Expand All @@ -331,6 +334,14 @@ export async function installPkg(
}
);

if (manifest.deprecated) {
ora(
`[DEPR] ${chalk.bgYellowBright.black(
manifest.name + "@" + manifest.version
)} - ${manifest.deprecated}`
).warn();
}

await installPkg(
{
name: dep.name,
Expand All @@ -341,6 +352,7 @@ export async function installPkg(
pkgProjectDir,
spinner
);

return {
name: dep.name,
version: manifest.version,
Expand Down Expand Up @@ -369,28 +381,41 @@ export async function installPkg(
JSON.stringify(object, null, 2),
"utf-8"
);
}

// Execute postinstall script if exists
const postinstall = pkg.scripts.postinstall;
if (postinstall) {
const postinstallPath = path.join(cacheFolder, "node_modules", ".");
const postinstallScript = path.join(postinstallPath, postinstall);
// Execute postinstall script if exists
const postinstall = pkg.scripts.postinstall;
if (postinstall) {
const postinstallPath = path.join(cacheFolder, "node_modules", ".");
const postinstallScript = path.join(postinstallPath, postinstall);

if (existsSync(postinstallScript)) {
exec(`${postinstallScript}`, {
cwd: postinstallPath,
});
}
}

if (existsSync(postinstallScript)) {
exec(`${postinstallScript}`, {
cwd: postinstallPath,
});
__DOWNLOADED.push(`${manifest.name}@${manifest.version}`);
return;
} else {
// Execute postinstall script if exists
const postinstall = pkg.scripts.postinstall;
if (postinstall) {
const postinstallPath = path.join(cacheFolder, "node_modules", ".");
const postinstallScript = path.join(postinstallPath, postinstall);

if (existsSync(postinstallScript)) {
exec(`${postinstallScript}`, {
cwd: postinstallPath,
});
}
}
}

__DOWNLOADED.push(`${manifest.name}@${manifest.version}`);
return;
} catch (error: any) {
// Check if error is ENOENT
if (error.code === "ENOENT") {
return await extract(cacheFolder, manifest.tarball);
__DOWNLOADED.push(`${manifest.name}@${manifest.version}`);
return;
}
} catch (error: any) {
return;
}
}
}
Expand Down Expand Up @@ -429,17 +454,8 @@ async function extract(cacheFolder: string, tarball: string): Promise<any> {
return await extract(cacheFolder, tarball);
}

await writeFile(path.join(cacheFolder, downloadFile), JSON.stringify([]));
await writeFile(path.join(cacheFolder, downloadFile), JSON.stringify({}));
__DOWNLOADING.splice(__DOWNLOADING.indexOf(tarball), 1);

return { res, error };
}

type cachedDep = {
[key: string]: {
[key: string]: {
path: string;
tarball: string;
};
};
};
2 changes: 2 additions & 0 deletions packages/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import chalk from "chalk";
import { spawn } from "child_process";
import { readdirSync } from "fs";
import { execa } from "execa";
import checkNodeVersion from "../utils/checkNodeVersion.js";

export default async function run(args: Array<string>) {
const pkg = await rpjf(path.join(process.cwd(), "package.json"));
await checkNodeVersion(pkg.engines);
const { scripts } = pkg;
const script = scripts[args[0]];

Expand Down
Loading