Skip to content

Commit c94a3db

Browse files
authored
Merge pull request #3 from constructive-io/feat/find-workspace-root
fix: resolve LICENSE and FOOTER.md from workspace root instead of hardcoded ../../
2 parents e9a6b8d + 1d99d4c commit c94a3db

4 files changed

Lines changed: 88 additions & 18 deletions

File tree

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1+
import fs from 'node:fs/promises';
12
import path from 'node:path';
23
import { runCopy } from './copy';
34
import { runReadmeFooter } from './readmeFooter';
5+
import { findWorkspaceRoot } from './workspace';
6+
7+
async function resolveRootFile(root: string | null, filename: string): Promise<string | null> {
8+
if (!root) return null;
9+
const filePath = path.join(root, filename);
10+
try {
11+
await fs.access(filePath);
12+
return filePath;
13+
} catch {
14+
return null;
15+
}
16+
}
417

518
export async function runAssets(_args: string[]) {
6-
// assumes we are in the package dir, LICENSE lives two levels up
7-
await runCopy(['../../LICENSE', 'package.json', 'dist', '--flat']);
8-
9-
// README + FOOTER -> dist/README.md
10-
await runReadmeFooter([
11-
'--source', 'README.md',
12-
'--footer', '../../FOOTER.md',
13-
'--dest', path.join('dist', 'README.md')
14-
]);
19+
const root = await findWorkspaceRoot(process.cwd());
20+
const licensePath = await resolveRootFile(root, 'LICENSE');
21+
const footerPath = await resolveRootFile(root, 'FOOTER.md');
22+
23+
if (licensePath) {
24+
await runCopy([licensePath, 'package.json', 'dist', '--flat']);
25+
} else {
26+
await runCopy(['package.json', 'dist', '--flat']);
27+
console.log('[makage] no LICENSE found at workspace root, skipping');
28+
}
29+
30+
if (footerPath) {
31+
await runReadmeFooter([
32+
'--source', 'README.md',
33+
'--footer', footerPath,
34+
'--dest', path.join('dist', 'README.md')
35+
]);
36+
} else {
37+
await runCopy(['README.md', 'dist', '--flat']);
38+
}
1539
}

packages/makage/src/commands/copy.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'node:fs/promises';
22
import path from 'node:path';
3+
import { findRootFile } from './workspace';
34

45
interface CopyOptions {
56
flat: boolean;
@@ -152,20 +153,21 @@ function matchPattern(name: string, pattern: string): boolean {
152153
async function copyReadmeWithFooter(readmePath: string, target: string) {
153154
const readme = await fs.readFile(readmePath, 'utf8');
154155

155-
// Look for FOOTER.md in the monorepo root (../../FOOTER.md relative to current dir)
156-
const footerPath = path.join('../../FOOTER.md');
156+
const footerPath = await findRootFile('FOOTER.md', process.cwd());
157157

158158
let footer: string | null = null;
159-
try {
160-
footer = await fs.readFile(footerPath, 'utf8');
161-
} catch (err: any) {
162-
if (err.code === 'ENOENT') {
163-
console.log(`[makage] warning: ${footerPath} not found, copying README without footer`);
164-
} else {
165-
throw err;
159+
if (footerPath) {
160+
try {
161+
footer = await fs.readFile(footerPath, 'utf8');
162+
} catch (err: any) {
163+
if (err.code !== 'ENOENT') throw err;
166164
}
167165
}
168166

167+
if (!footer) {
168+
console.log('[makage] no FOOTER.md found at workspace root, copying README without footer');
169+
}
170+
169171
const combined = footer
170172
? `${readme.trimEnd()}\n\n---\n\n${footer.trim()}\n`
171173
: readme;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import fs from 'node:fs/promises';
2+
import path from 'node:path';
3+
4+
const WORKSPACE_MARKERS = ['pnpm-workspace.yaml', 'lerna.json'];
5+
6+
export async function findWorkspaceRoot(from: string): Promise<string | null> {
7+
let dir = path.resolve(from);
8+
9+
while (true) {
10+
for (const marker of WORKSPACE_MARKERS) {
11+
try {
12+
await fs.access(path.join(dir, marker));
13+
return dir;
14+
} catch {}
15+
}
16+
17+
try {
18+
const raw = await fs.readFile(path.join(dir, 'package.json'), 'utf8');
19+
const pkg = JSON.parse(raw);
20+
if (pkg.workspaces) return dir;
21+
} catch {}
22+
23+
const parent = path.dirname(dir);
24+
if (parent === dir) return null;
25+
dir = parent;
26+
}
27+
}
28+
29+
export async function findRootFile(
30+
filename: string,
31+
from: string
32+
): Promise<string | null> {
33+
const root = await findWorkspaceRoot(from);
34+
if (!root) return null;
35+
36+
const filePath = path.join(root, filename);
37+
try {
38+
await fs.access(filePath);
39+
return filePath;
40+
} catch {
41+
return null;
42+
}
43+
}

packages/makage/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export { runAssets } from './commands/assets';
55
export { runBuild } from './commands/build';
66
export { runBuildTs } from './commands/buildTs';
77
export { runUpdateWorkspace } from './commands/updateWorkspace';
8+
export { findWorkspaceRoot, findRootFile } from './commands/workspace';

0 commit comments

Comments
 (0)