Skip to content
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
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"comment-parser": "^1.4.1",
"enquirer": "^2.4.1",
"es-toolkit": "^1.39.5",
"find-up": "^8.0.0",
"minimatch": "^10.0.3",
"tmp-promise": "^3.0.3",
"ts-morph": "^22.0.0",
Expand Down
9 changes: 6 additions & 3 deletions packages/cli/src/commands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import { isExportSourceFile } from "../../core/parser/source/is-export-source-fi
import { getExportedDeclarationsBySourceFile } from "../../core/parser/source/get-exported-declarations-by-sourcefile.js";
import { excludeBarrelReExports } from "../../core/parser/source/exclude-barrel-re-exports.js";
import { hasJSDocTag } from "../../core/parser/jsdoc/jsdoc-utils.js";
import { getWorkingDirectory } from "../../utils/get-working-directory.js";

export class BuildCommand extends Command {
static paths = [[`build`]];

async execute(): Promise<number> {
const {
projectConfig,
projectRoot,
buildConfig,
targetPackages,
parser,
Expand All @@ -46,7 +48,7 @@ export class BuildCommand extends Command {
console.log(`📝 ${pkg.name} processing...`);

try {
const tsConfigPath = getTsConfigPath(projectConfig.root, pkg.location);
const tsConfigPath = getTsConfigPath(projectRoot, pkg.location);
const project = getTsProject(tsConfigPath);
const projectSourceFiles = project.getSourceFiles();

Expand Down Expand Up @@ -103,7 +105,7 @@ export class BuildCommand extends Command {
}

async function loadContext() {
const root = process.cwd();
const root = getWorkingDirectory();
const config = await loadConfig(root);
const projectRoot = path.resolve(root, config.project.root);
const buildConfig = config.commands.build;
Expand Down Expand Up @@ -148,11 +150,12 @@ async function loadContext() {
pluginManager,
});

const outputDir = path.resolve(projectConfig.root, buildConfig.outputDir);
const outputDir = path.resolve(projectRoot, buildConfig.outputDir);

return {
buildConfig,
projectConfig,
projectRoot,
parser,
generator,
targetPackages,
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/src/commands/check/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import { excludeBarrelReExports } from "../../core/parser/source/exclude-barrel-
import { hasJSDocTag } from "../../core/parser/jsdoc/jsdoc-utils.js";
import { getPackageEntryPoints } from "../../core/entry-point.js";
import path from "path";
import { getWorkingDirectory } from "../../utils/get-working-directory.js";

export class CheckCommand extends Command {
static paths = [[`check`]];

async execute(): Promise<number> {
const { checkConfig, projectConfig, targetPackages } = await loadContext();
const { checkConfig, projectConfig, projectRoot, targetPackages } = await loadContext();

if (targetPackages.length === 0) {
printNoPackagesFound(
Expand All @@ -29,9 +30,9 @@ export class CheckCommand extends Command {
for (const pkg of targetPackages) {
console.log(`📝 ${pkg.name} processing...`);

const packagePath = path.resolve(projectConfig.root, pkg.location);
const packagePath = path.resolve(projectRoot, pkg.location);
try {
const tsConfigPath = getTsConfigPath(projectConfig.root, pkg.location);
const tsConfigPath = getTsConfigPath(projectRoot, pkg.location);
const project = getTsProject(tsConfigPath);

const entryPoints =
Expand Down Expand Up @@ -76,7 +77,7 @@ export class CheckCommand extends Command {
}

async function loadContext() {
const root = process.cwd();
const root = getWorkingDirectory();
const config = await loadConfig(root);
const projectRoot = path.resolve(root, config.project.root);
const projectConfig = config.project;
Expand All @@ -97,6 +98,7 @@ async function loadContext() {
return {
checkConfig,
projectConfig,
projectRoot,
targetPackages,
};
}
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/commands/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import { ExportDeclaration } from "../../core/types/parser.types.js";
import { Package } from "../../package-manager/types/package-manager.type.js";
import { Project } from "ts-morph";
import path from "path";
import { getWorkingDirectory } from "../../utils/get-working-directory.js";

export class GenerateCommand extends Command {
static paths = [[`generate`]];

async execute(): Promise<number> {
const { projectConfig, generateConfig, targetPackages } =
const { projectConfig, projectRoot, generateConfig, targetPackages } =
await loadContext();
if (!generateConfig) {
console.error("❌ not found generate config");
Expand All @@ -39,7 +40,7 @@ export class GenerateCommand extends Command {
console.log(`📝 ${pkg.name} processing...`);

try {
const tsConfigPath = getTsConfigPath(projectConfig.root, pkg.location);
const tsConfigPath = getTsConfigPath(projectRoot, pkg.location);
const project = getTsProject(tsConfigPath);
const projectSourceFiles = project.getSourceFiles();

Expand Down Expand Up @@ -124,7 +125,7 @@ export class GenerateCommand extends Command {
}

async function loadContext() {
const root = process.cwd();
const root = getWorkingDirectory();
const config = await loadConfig(root);
const projectRoot = path.resolve(root, config.project.root);
const generateConfig = config.commands?.generate;
Expand All @@ -150,6 +151,7 @@ async function loadContext() {

return {
projectConfig,
projectRoot,
generateConfig,
packages,
targetPackages,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { execSync } from "child_process";
import { z } from "zod";
import path from "path";
import { PackageManager, Package } from "../types/package-manager.type.js";

const npmSchema = z.object({
name: z.string(),
location: z.string(),
path: z.string(),
});

export class NpmPackageManager implements PackageManager {
constructor(private cwd: string) {}
constructor(private cwd: string) { }

getPackages(): Package[] {
try {
const raw = execSync("npm query .workspace --json", { encoding: "utf8", cwd: this.cwd });
Expand All @@ -18,7 +19,10 @@ export class NpmPackageManager implements PackageManager {

return parsed
.map((ws) => npmSchema.parse(ws))
.map(({ name, location }) => ({ name, location }));
.map(({ name, path: pkgPath }) => ({
name,
location: path.relative(this.cwd, pkgPath),
}));
} catch {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { execSync } from "child_process";
import { z } from "zod";
import { PackageManager, Package } from "../types/package-manager.type.js";
import path from "path";
import { z } from "zod";
import { Package, PackageManager } from "../types/package-manager.type.js";

const pnpmSchema = z.object({
name: z.string().optional(),
path: z.string(),
});

export class PnpmPackageManager implements PackageManager {
constructor(private cwd: string) {}
constructor(private cwd: string) { }

getPackages(): Package[] {
try {
const raw = execSync("pnpm list --recursive --json", { encoding: "utf8", cwd: this.cwd });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
import { execSync } from "child_process";
import { findUpSync } from "find-up";
import path from "path";
import { z } from "zod";
import { PackageManager, Package } from "../types/package-manager.type.js";
import { Package, PackageManager } from "../types/package-manager.type.js";

const workspaceIdentitySchema = z.object({
name: z.string().nullable(),
location: z.string(),
});

export class YarnPackageManager implements PackageManager {
constructor(private cwd: string) {}
constructor(private cwd: string) { }

getPackages(): Package[] {
try {
const output = execSync("yarn workspaces list --json", { encoding: "utf8", cwd: this.cwd });
const repoRootPath = this.getRepoRootPath();

return output
.split("\n")
.filter(Boolean)
.map((line) => workspaceIdentitySchema.parse(JSON.parse(line)))
.filter((ws) => ws.name !== null)
.map((ws) => ({ name: ws.name as string, location: ws.location }));
.map((ws) => {
const locationAbsolutePath = path.join(repoRootPath, ws.location);

return {
name: ws.name as string,
location: path.relative(this.cwd, locationAbsolutePath)
};
});
} catch {
return [];
}
}

private getRepoRootPath(): string {
const root = findUpSync("yarn.lock", { cwd: this.cwd });

if (root == null) {
throw new Error(`'yarn.lock' not found from ${this.cwd}`);
}

return path.dirname(root);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,19 @@ describe("PackageManager integration", () => {
expect.arrayContaining([
expect.objectContaining({
name: "@libs/core",
location: "packages/core",
}),
expect.objectContaining({
name: "@libs/math",
location: "packages/math",
}),
expect.objectContaining({
name: "@libs/types",
location: "packages/types",
}),
expect.objectContaining({
name: "@libs/utils",
location: "packages/utils",
}),
])
);
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/tests/utils/create-e2e-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
createTypesPackage,
} from "./package-creators.js";
import { createDocflowConfig } from "./docflow-config.js";
import { getWorkingDirectory } from "../../utils/get-working-directory.js";

export type E2EWorkspace = TestWorkspace;

Expand All @@ -16,7 +17,7 @@ export async function createE2EWorkspace(options?: {
packageManager?: "yarn" | "pnpm" | "npm";
}): Promise<TestWorkspace> {
const workspace = await createTestWorkspace();
const root = path.resolve(process.cwd());
const root = path.resolve(getWorkingDirectory());
const packageManager = options?.packageManager ?? "yarn";

await workspace.write("tsconfig.json", {
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/tests/utils/create-test-workspace.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { realpathSync } from "fs";
import fs from "fs/promises";
import path from "path";
import { dir as tmpDir } from "tmp-promise";
Expand All @@ -10,7 +11,8 @@ export interface TestWorkspace {
}

export async function createTestWorkspace(): Promise<TestWorkspace> {
const { path: root, cleanup } = await tmpDir({ unsafeCleanup: true });
const { path: tmpPath, cleanup } = await tmpDir({ unsafeCleanup: true });
const root = realpathSync(tmpPath);

const write = async (filePath: string, content: unknown): Promise<void> => {
const absPath = path.join(root, filePath);
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/utils/get-working-directory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function getWorkingDirectory(): string {
// This is more reliable than process.cwd() when running via npm, pnpm
if (process.env.INIT_CWD != null) {
return process.env.INIT_CWD;
}

return process.cwd();
}
Loading