Skip to content

Commit

Permalink
fix 🐛: (cli) Fix error when generating lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
nachoaldamav committed Dec 13, 2022
1 parent 915d1b7 commit 6ff1a55
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 57 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@swc/core": "1.3.6",
"@types/glob": "7.2.0",
"@types/mv": "2.1.2",
"@types/node": "^18.11.13",
"@types/pacote": "11.1.5",
"@types/prompts": "2.0.14",
"@types/semver": "7.3.12",
Expand Down
3 changes: 0 additions & 3 deletions packages/cli/src/utils/checkDist.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import os from "node:os";
import { GLIBC, MUSL } from "detect-libc";
import manifestFetcher from "./manifestFetcher.js";
import ora from "ora";
import chalk from "chalk";

const system = {
platform: os.platform(),
Expand Down
50 changes: 35 additions & 15 deletions packages/cli/src/utils/genLockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,51 @@ import { join } from "path";
import { readFileSync, writeFileSync } from "fs";
import os from "os";

type DEP = {
name: string;
version: string;
integrity: string;
tarball: string;
path: string;
cache: string;
};

export function genLock() {
// Get all ".ultra" files inside node_modules
const files = glob.sync("**/.ultra", {
cwd: join(process.cwd(), "node_modules"),
absolute: false,
});

const deps = files.map((file) => {
const data = readFileSync(join("node_modules", file), "utf-8");
const parsed = JSON.parse(data);
const { name, version, integrity, path, tarball } = parsed["ultra:self"];

return {
name: name,
version: version,
integrity: integrity,
tarball: tarball,
path: join("/node_modules", file.replace("/.ultra", "")),
cache: path.replace(join(os.homedir(), ".ultra-cache"), ""),
};
});
// @ts-ignore-next-line
const deps: DEP[] = files
.map((file) => {
const data = readFileSync(join("node_modules", file), "utf-8");
const parsed = JSON.parse(data);
try {
const { name, version, integrity, path, tarball } =
parsed["ultra:self"];

return {
name: name,
version: version,
integrity: integrity,
tarball: tarball,
path: join("/node_modules", file.replace("/.ultra", "")),
cache: path.replace(join(os.homedir(), ".ultra-cache"), ""),
};
} catch (e) {
return null;
}
})
.filter((dep) => {
return dep !== null;
});

const lock: ultra_lock = {};

deps.forEach((dep) => {
// Filter null values
deps.forEach((dep: DEP) => {
if (!lock[dep.name]) {
lock[dep.name] = {};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/utils/gitInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Ora } from "ora";
import { execa } from "execa";
import { join, dirname } from "path";
import { existsSync, mkdirSync, symlinkSync, writeFileSync } from "fs";
import { hardLinkSync } from "./hardLinkSync.js";
import readPackage from "./readPackage.js";
import { getDeps } from "./getDeps.js";
import { installPkg } from "./installPkg.js";
import binLinks from "bin-links";
import { linker } from "./linker.js";

export async function gitInstall(
manifest: any,
Expand Down Expand Up @@ -65,7 +65,7 @@ export async function gitInstall(
if (!justDownload) {
const nmPath = join(process.cwd(), "node_modules", manifest.name);

hardLinkSync(targetPath, nmPath);
await linker(targetPath, nmPath);

__DOWNLOADED.push({
name: manifest.name,
Expand Down
40 changes: 16 additions & 24 deletions packages/cli/src/utils/hardLinkSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import {
copyFileSync,
constants,
} from "node:fs";
import path from "path";
import os from "os";
import path from "node:path";
import ora from "ora";
import chalk from "chalk";

const isMac = os.platform() === "darwin";

export function hardLinkSync(dir: string, targetDir: string) {
try {
const files = readdirSync(dir);
Expand All @@ -26,26 +23,21 @@ export function hardLinkSync(dir: string, targetDir: string) {
} else {
// Create previous folders if they don't exist
mkdirSync(path.dirname(targetPath), { recursive: true });
if (!isMac) {
try {
linkSync(filePath, targetPath);
} catch (e: any) {
if (e.code === "EEXIST") return;
if (e.code === "EXDEV")
return copyFileSync(
filePath,
targetPath,
constants.COPYFILE_FICLONE
);
ora(
chalk.red(
`Error: ${e.message} (file: ${filePath}, target: ${targetPath})`
)
).fail();
}
} else {
// Use clonefile on mac
copyFileSync(filePath, targetPath, constants.COPYFILE_FICLONE);
try {
linkSync(filePath, targetPath);
} catch (e: any) {
if (e.code === "EEXIST") return;
if (e.code === "EXDEV")
return copyFileSync(
filePath,
targetPath,
constants.COPYFILE_FICLONE
);
ora(
chalk.red(
`Error: ${e.message} (file: ${filePath}, target: ${targetPath})`
)
).fail();
}
}
});
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/utils/installLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import parseTime from "../utils/parseTime.js";
import readPackage from "../utils/readPackage.js";
import basePostInstall from "../utils/basePostInstall.js";
import { __dirname } from "../utils/__dirname.js";
import { hardLinkSync } from "../utils/hardLinkSync.js";
import checkLock from "../utils/checkLock.js";
import { executePost } from "../utils/postInstall.js";
import { ultraExtract } from "./extract.js";
import { updateIndex } from "./updateIndex.js";
import { checkDist } from "./checkDist.js";
import { gitInstall } from "./gitInstaller.js";
import { linker } from "./linker.js";

export async function installLock(lock: any) {
const start = performance.now();
Expand Down Expand Up @@ -76,7 +76,7 @@ export async function installLock(lock: any) {
}

if (existsSync(cache)) {
hardLinkSync(cache, pathname);
await linker(cache, pathname);
__install.text = chalk.green(`${pkg}`);
__install.prefixText = "🔗";
} else {
Expand All @@ -92,7 +92,7 @@ export async function installLock(lock: any) {
}
updateIndex(pkg, version);
__install.prefixText = "🔗";
hardLinkSync(cache, pathname);
await linker(cache, pathname);
}

manifest = readPackage(path.join(cache, "package.json"));
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/utils/installPkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import semver from "semver";
import binLinks from "bin-links";
import { getDeps } from "./getDeps.js";
import manifestFetcher from "./manifestFetcher.js";
import { hardLinkSync } from "./hardLinkSync.js";
import { ultraExtract } from "./extract.js";
import { gitInstall } from "./gitInstaller.js";
import { getDir } from "./getInstallableDir.js";
import { sleep } from "./sleep.js";
import getVersions from "./getVersions.js";
import { checkDist } from "./checkDist.js";
import { updateIndex } from "./updateIndex.js";
import { linker } from "./linker.js";

type Return = {
name: string;
Expand Down Expand Up @@ -143,7 +143,7 @@ export async function installPkg(

// Create directory for package without the last folder
mkdirSync(path.dirname(pkgProjectDir), { recursive: true });
hardLinkSync(cacheFolder, pkgProjectDir);
await linker(cacheFolder, pkgProjectDir);

try {
const pkgJson = readPackage(path.join(cacheFolder, "package.json"));
Expand Down Expand Up @@ -345,7 +345,7 @@ export async function installPkg(

mkdirSync(path.dirname(pkgProjectDir), { recursive: true });

hardLinkSync(cacheFolder, pkgProjectDir);
await linker(cacheFolder, pkgProjectDir);

if (manifest.fromMonorepo !== undefined) {
try {
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/utils/linker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { platform } from "node:os";
import { hardLink } from "./hardLink.js";
import { hardLinkSync } from "./hardLinkSync.js";

export async function linker(src: string, dest: string) {
if (platform() === "darwin" || platform() === "win32") {
return hardLink(src, dest);
} else {
return hardLinkSync(src, dest);
}
}
20 changes: 13 additions & 7 deletions pnpm-lock.yaml

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

0 comments on commit 6ff1a55

Please sign in to comment.