Skip to content

Commit

Permalink
build: patch workspace modules links
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Apr 21, 2022
1 parent c2bedc6 commit 4a70273
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -56,6 +56,7 @@
"clean:ts": "tsc --build tsconfig.build.json --clean",
"format": "prettier -w .",
"format:check": "prettier -c .",
"postinstall": "pnpm scripts postinstall",
"lint": "eslint --ignore-path .gitignore .",
"prepare": "husky install",
"release": "changeset publish",
Expand Down
40 changes: 40 additions & 0 deletions scripts/actions/patchWsModulesLinks.ts
@@ -0,0 +1,40 @@
import { readdir, symlink, unlink } from "fs/promises";
import { join } from "path";
import { safeReadLink, safeStat } from "~/util/fs";
import { parsePackageFile } from "~/util/package";
import { getPackageNames, packagesPath } from "~/util/workspace";

async function patch(packagePath: string) {
const wsPath = `${packagePath}/node_modules/@suid`;
if (!(await safeStat(wsPath))) return;
const dependencyNames = await readdir(wsPath);
for (const dependencyName of dependencyNames) {
const dependencyPath = join(wsPath, dependencyName);
const link = await safeReadLink(dependencyPath);
if (!link) continue;
const pkg = await parsePackageFile(join(dependencyPath, "package.json"));
if (pkg.publishConfig?.directory) {
await unlink(dependencyPath);
await symlink(
join(link, pkg.publishConfig.directory),
dependencyPath,
"junction"
);
}
}
}

/**
* @link https://github.com/pnpm/pnpm/issues/3901
*/
async function patchWsModulesLinks() {
const { name } = patchWsModulesLinks;
const packageNames = await getPackageNames();
for (const packageName of packageNames) {
console.log(`[${name}] ${packageName}`);
const packagePath = `${packagesPath}/${packageName}`;
await patch(packagePath);
}
}

export default patchWsModulesLinks;
7 changes: 7 additions & 0 deletions scripts/actions/postinstall.ts
@@ -0,0 +1,7 @@
import patchWsModulesLinks from "~/actions/patchWsModulesLinks";

async function postinstall() {
await patchWsModulesLinks();
}

export default postinstall;
8 changes: 7 additions & 1 deletion scripts/index.ts
Expand Up @@ -9,7 +9,9 @@ import genRes from "~/actions/genRes";
import genRoadmap from "~/actions/genRoadmap";
import patchLibModulesLinks from "~/actions/patchLibModulesLinks";
import patchTsConfigs from "~/actions/patchTsConfigs";
import patchWsModulesLinks from "~/actions/patchWsModulesLinks";
import postbuild from "~/actions/postbuild";
import postinstall from "~/actions/postinstall";
import prebuild from "~/actions/prebuild";
import syncIconsMaterial from "~/actions/syncIconsMaterial";

Expand Down Expand Up @@ -56,8 +58,12 @@ program

program.command(snakeCase(patchTsConfigs.name)).action(() => patchTsConfigs());

program.command(snakeCase(postbuild.name)).action(() => postbuild());
program
.command(snakeCase(patchWsModulesLinks.name))
.action(() => patchWsModulesLinks());

program.command(snakeCase(postbuild.name)).action(() => postbuild());
program.command(snakeCase(postinstall.name)).action(() => postinstall());
program.command(snakeCase(prebuild.name)).action(() => prebuild());

program
Expand Down
10 changes: 9 additions & 1 deletion scripts/util/fs.ts
@@ -1,4 +1,4 @@
import { readFile, stat } from "fs/promises";
import { readFile, readlink, stat } from "fs/promises";
import { parse } from "json5";

export async function parseJSONFile<T = any>(path: string) {
Expand All @@ -13,3 +13,11 @@ export async function safeStat(path: string) {
return;
}
}

export async function safeReadLink(path: string) {
try {
return await readlink(path);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
}
}

0 comments on commit 4a70273

Please sign in to comment.