Skip to content

Commit

Permalink
Organize code in domains
Browse files Browse the repository at this point in the history
  • Loading branch information
0x80 committed Dec 15, 2023
1 parent 11c9b5d commit fe70847
Show file tree
Hide file tree
Showing 42 changed files with 215 additions and 204 deletions.
98 changes: 0 additions & 98 deletions src/helpers/detect-package-manager.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { isolate } from "./isolate";
export { isolate } from "./isolate";
export type { Logger } from "./utils/logger";
export type { Logger } from "./lib/logger";

export type IsolateExports = {
isolate: typeof isolate;
Expand Down
37 changes: 18 additions & 19 deletions src/isolate.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
import fs from "fs-extra";
import assert from "node:assert";
import path from "node:path";
import { adaptInternalPackageManifests } from "./helpers/adapt-internal-package-manifests";
import { adaptTargetPackageManifest } from "./helpers/adapt-target-package-manifest";
import type { IsolateConfig } from "./helpers/config";
import type { IsolateConfig } from "./lib/config";
import {
getUserDefinedConfig,
resolveConfig,
setUserConfig,
} from "./helpers/config";
} from "./lib/config";
import { processLockfile } from "./lib/lockfile";
import type { Logger } from "./lib/logger";
import { setLogLevel, setLogger, useLogger } from "./lib/logger";
import {
createPackagesRegistry,
type PackageManifest,
} from "./helpers/create-packages-registry";
import { detectPackageManager } from "./helpers/detect-package-manager";
import { getBuildOutputDir } from "./helpers/get-build-output-dir";
import { listInternalPackages } from "./helpers/list-internal-packages";
import { packDependencies } from "./helpers/pack-dependencies";
import { processBuildOutputFiles } from "./helpers/process-build-output-files";
import { processLockfile } from "./helpers/process-lockfile";
import { unpackDependencies } from "./helpers/unpack-dependencies";
import type { Logger } from "./utils";
adaptInternalPackageManifests,
adaptTargetPackageManifest,
} from "./lib/manifest";
import {
getBuildOutputDir,
packDependencies,
processBuildOutputFiles,
unpackDependencies,
} from "./lib/output";
import { detectPackageManager } from "./lib/package-manager";
import { createPackagesRegistry, listInternalPackages } from "./lib/registry";
import type { PackageManifest } from "./lib/types";
import {
getDirname,
getRootRelativePath,
isDefined,
readTypedJson,
setLogLevel,
setLogger,
useLogger,
} from "./utils";
} from "./lib/utils";

const __dirname = getDirname(import.meta.url);

Expand Down
8 changes: 2 additions & 6 deletions src/helpers/config.ts → src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import fs from "fs-extra";
import assert from "node:assert";
import path from "node:path";
import { isEmpty } from "ramda";
import {
inspectValue,
readTypedJsonSync,
setLogLevel,
useLogger,
} from "../utils";
import { setLogLevel, useLogger } from "./logger";
import { inspectValue, readTypedJsonSync } from "./utils";

export type IsolateConfigResolved = {
buildDirName?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Arborist from "@npmcli/arborist";
import fs from "node:fs/promises";
import path from "node:path";
import { useLogger } from "../utils";
import { useLogger } from "src/lib/logger";

/**
* Generate an isolated lockfile, based on the contents of node_modules in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
import assert from "node:assert";
import path from "node:path";
import { pick } from "ramda";
import { useLogger } from "../utils";
import { useConfig } from "./config";
import type { PackagesRegistry } from "./create-packages-registry";
import { pnpmMapImporter } from "./process-lockfile";
import { useConfig } from "src/lib/config";
import { useLogger } from "src/lib/logger";
import type { PackagesRegistry } from "src/lib/types";
import { pnpmMapImporter } from "../process-lockfile";

export async function generatePnpmLockfile({
workspaceRootDir,
Expand Down
1 change: 1 addition & 0 deletions src/lib/lockfile/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./process-lockfile";
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,11 @@ import type {
import fs from "fs-extra";
import path from "node:path";
import { mapObjIndexed } from "ramda";
import { useLogger } from "../utils";
import type { PackagesRegistry } from "./create-packages-registry";
import type { PackageManagerName } from "./detect-package-manager";
import { usePackageManager } from "./detect-package-manager";
import { generateNpmLockfile } from "./generate-npm-lockfile";
import { generatePnpmLockfile } from "./generate-pnpm-lockfile";

export function getLockfileFileName(name: PackageManagerName) {
switch (name) {
case "pnpm":
return "pnpm-lock.yaml";
case "yarn":
return "yarn.lock";
case "npm":
return "package-lock.json";
}
}
import { useLogger } from "../logger";
import { getLockfileFileName, usePackageManager } from "../package-manager";
import type { PackagesRegistry } from "../types";
import { generateNpmLockfile } from "./helpers/generate-npm-lockfile";
import { generatePnpmLockfile } from "./helpers/generate-pnpm-lockfile";

/** Convert dependency links */
export function pnpmMapImporter(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/logger.ts → src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from "chalk";
import type { IsolateConfigResolved, LogLevel } from "../helpers/config";
import type { IsolateConfigResolved, LogLevel } from "./config";
/**
* The Logger defines an interface that can be used to pass in a different
* logger object in order to intercept all the logging output. We keep the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import fs from "fs-extra";
import path from "node:path";
import { omit } from "ramda";
import { adaptManifestInternalDeps } from "./adapt-manifest-internal-deps";
import { useConfig } from "./config";
import type {
PackageManifest,
PackagesRegistry,
} from "./create-packages-registry";
import { usePackageManager } from "./detect-package-manager";
import { useConfig } from "../config";
import { usePackageManager } from "../package-manager";
import type { PackageManifest, PackagesRegistry } from "../types";
import { adaptManifestInternalDeps } from "./helpers";

/**
* Change the target package manifest file, so that:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fs from "fs-extra";
import path from "node:path";
import { omit } from "ramda";
import type { PackagesRegistry } from "../helpers/create-packages-registry";
import { useConfig } from "src/lib/config";
import { usePackageManager } from "src/lib/package-manager";
import type { PackagesRegistry } from "src/lib/types";
import { adaptManifestInternalDeps } from "./adapt-manifest-internal-deps";
import { useConfig } from "./config";
import { usePackageManager } from "./detect-package-manager";

/**
* Adapt the manifest files of all the isolated internal packages (excluding the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { omit } from "ramda";
import { filterObjectUndefined } from "../utils";
import type {
PackageManifest,
PackagesRegistry,
} from "./create-packages-registry";
import type { PackageManifest, PackagesRegistry } from "src/lib/types";
import { filterObjectUndefined } from "src/lib/utils";
import { patchInternalEntries } from "./patch-internal-entries";

/**
Expand Down
3 changes: 3 additions & 0 deletions src/lib/manifest/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./adapt-internal-package-manifests";
export * from "./adapt-manifest-internal-deps";
export * from "./patch-internal-entries";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "node:path";
import { useLogger } from "../utils";
import type { PackagesRegistry } from "./create-packages-registry";
import { useLogger } from "../../logger";
import type { PackagesRegistry } from "../../types";

export function patchInternalEntries(
dependencies: Record<string, string>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "node:path";
import type { PackageManifest } from "../types";
import { readTypedJson } from "../utils";
import type { PackageManifest } from "./create-packages-registry";

export async function importManifest(packageDir: string) {
return readTypedJson<PackageManifest>(path.join(packageDir, "package.json"));
Expand Down
3 changes: 3 additions & 0 deletions src/lib/manifest/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./adapt-target-package-manifest";
export * from "./helpers";
export * from "./import-manifest";
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import fs from "fs-extra";
import path from "node:path";
import outdent from "outdent";
import { readTypedJson, useLogger } from "../utils";
import { useConfig } from "./config";
import { useConfig } from "../config";
import { useLogger } from "../logger";
import { readTypedJson } from "../utils";

export async function getBuildOutputDir(targetPackageDir: string) {
const config = useConfig();
Expand Down
4 changes: 4 additions & 0 deletions src/lib/output/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./get-build-output-dir";
export * from "./pack-dependencies";
export * from "./process-build-output-files";
export * from "./unpack-dependencies";
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import assert from "node:assert";
import { pack, useLogger } from "../utils";
import { useConfig } from "./config";
import type { PackagesRegistry } from "./create-packages-registry";
import { usePackageManager } from "./detect-package-manager";
import { useConfig } from "../config";
import { useLogger } from "../logger";
import { usePackageManager } from "../package-manager";
import type { PackagesRegistry } from "../types";
import { pack } from "../utils";

/**
* Pack dependencies so that we extract only the files that are supposed to be
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "fs-extra";
import path from "node:path";
import { pack, unpack, useLogger } from "../utils";
import { useLogger } from "../logger";
import { pack, unpack } from "../utils";

const TIMEOUT_MS = 5000;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from "fs-extra";
import path, { join } from "node:path";
import { getIsolateRelativePath, useLogger } from "../utils";
import { unpack } from "../utils/unpack";
import type { PackagesRegistry } from "./create-packages-registry";
import { useLogger } from "../logger";
import type { PackagesRegistry } from "../types";
import { getIsolateRelativePath, unpack } from "../utils";

export async function unpackDependencies(
packedFilesByName: Record<string, string>,
Expand Down
26 changes: 26 additions & 0 deletions src/lib/package-manager/helpers/infer-from-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from "fs-extra";
import { execSync } from "node:child_process";
import path from "node:path";
import type { PackageManager, PackageManagerName } from "../names";
import { getLockfileFileName, supportedPackageManagerNames } from "../names";

export function inferFromFiles(workspaceRoot: string): PackageManager {
for (const name of supportedPackageManagerNames) {
const lockfileName = getLockfileFileName(name);

if (fs.existsSync(path.join(workspaceRoot, lockfileName))) {
return { name, version: getVersion(name) };
}
}

/** If no lockfile was found, it could be that there is an npm shrinkwrap file. */
if (fs.existsSync(path.join(workspaceRoot, "npm-shrinkwrap.json"))) {
return { name: "npm", version: getVersion("npm") };
}

throw new Error(`Failed to detect package manager`);
}
function getVersion(packageManagerName: PackageManagerName): string {
const buffer = execSync(`${packageManagerName} --version`);
return buffer.toString().trim();
}
Loading

0 comments on commit fe70847

Please sign in to comment.