Skip to content

Commit eef12d9

Browse files
authored
Enable the module to be linked in other project (#55)
1 parent a52890e commit eef12d9

File tree

4 files changed

+146
-7
lines changed

4 files changed

+146
-7
lines changed

CONTRIBUTING.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,29 @@ Thank You Very Much ❤️
3838

3939
PS: If you want to contribute to the Doc website. You can edit [the source Markdown](https://github.com/codegouvfr/react-dsfr/tree/v1_docs) or ask me for access to our GitBook. (We'll migrate to Docusaurus once we have the DSFR theme for it ready.)
4040

41-
## Linking a working version of `@gouvfr/dsfr`
41+
## Linking your local copy of `@codegouvfr/react-dsfr` in your project
42+
43+
This will enable you to see your react-dsfr changes in your main project.
44+
45+
```bash
46+
cd ~/github
47+
git clone https://github.com/ORG/YOUR-PROJECT-USING-REACT-DSFR
48+
cd YOUR-PROJECT-USING-REACT-DSFR
49+
yarn # or npm install or pnpm install depending of what you are using...
50+
51+
cd ~/github
52+
git clone https://github.com/codegouvfr/react-dsfr
53+
cd react-dsfr
54+
yarn
55+
yarn build
56+
yarn link-external YOUR-PROJECT-USING-REACT-DSFR
57+
npx tsc -w -p src # Leave this running if you want hot reload.
58+
```
59+
60+
## Linking a working version of `@gouvfr/dsfr` (For the SIG)
4261

4362
```bash
44-
cd ~/github/
63+
cd ~/github
4564
git clone http://github.com/gouvernementfr/dsfr
4665
cd dsfr
4766
# git checkout my-working-branch

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
},
99
"scripts": {
1010
"build": "ts-node -T src/scripts/build",
11-
"yarn-link": "ts-node -T src/scripts/yarn-link.ts",
12-
"start-cra": "yarn build && yarn yarn-link && ((tsc -w -p src) & (cd test/integration/cra && yarn start))",
13-
"start-vite": "yarn build && yarn yarn-link && ((tsc -w -p src) & (cd test/integration/vite && yarn dev))",
14-
"start-next-pagesdir": "yarn build && yarn yarn-link && ((tsc -w -p src) & (cd test/integration/next-pagesdir && yarn dev))",
15-
"start-next-appdir": "yarn build && yarn yarn-link && ((tsc -w -p src) & (cd test/integration/next-appdir && yarn dev))",
11+
"_link": "ts-node -T src/scripts/link-in-integration-apps.ts",
12+
"link-external": "ts-node -T src/scripts/link-in-external-project.ts",
13+
"start-cra": "yarn build && yarn _link && ((tsc -w -p src) & (cd test/integration/cra && yarn start))",
14+
"start-vite": "yarn build && yarn _link && ((tsc -w -p src) & (cd test/integration/vite && yarn dev))",
15+
"start-next-pagesdir": "yarn build && yarn _link && ((tsc -w -p src) & (cd test/integration/next-pagesdir && yarn dev))",
16+
"start-next-appdir": "yarn build && yarn _link && ((tsc -w -p src) & (cd test/integration/next-appdir && yarn dev))",
1617
"test": "vitest",
1718
"lint:check": "eslint . --ext .ts,.tsx",
1819
"lint": "yarn lint:check --fix",
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { execSync } from "child_process";
2+
import { join as pathJoin, relative as pathRelative } from "path";
3+
import { exclude } from "tsafe/exclude";
4+
import * as fs from "fs";
5+
6+
const rootDirPath = pathJoin(__dirname, "..", "..");
7+
8+
const commonThirdPartyDeps = (() => {
9+
const namespaceModuleNames = ["@emotion"];
10+
const standaloneModuleNames = ["react", "@types/react"];
11+
12+
return [
13+
...namespaceModuleNames
14+
.map(namespaceModuleName =>
15+
fs
16+
.readdirSync(pathJoin(rootDirPath, "node_modules", namespaceModuleName))
17+
.map(submoduleName => `${namespaceModuleName}/${submoduleName}`)
18+
)
19+
.reduce((prev, curr) => [...prev, ...curr], []),
20+
...standaloneModuleNames
21+
];
22+
})();
23+
24+
const yarnHomeDirPath = pathJoin(rootDirPath, ".yarn_home");
25+
26+
fs.rmSync(yarnHomeDirPath, { "recursive": true, "force": true });
27+
fs.mkdirSync(yarnHomeDirPath);
28+
29+
const execYarnLink = (params: { targetModuleName?: string; cwd: string }) => {
30+
const { targetModuleName, cwd } = params;
31+
32+
const cmd = [
33+
"yarn",
34+
"link",
35+
...(targetModuleName !== undefined ? [targetModuleName] : [])
36+
].join(" ");
37+
38+
console.log(`$ cd ${pathRelative(rootDirPath, cwd) || "."} && ${cmd}`);
39+
40+
execSync(cmd, {
41+
cwd,
42+
"env": {
43+
...process.env,
44+
"HOME": yarnHomeDirPath
45+
}
46+
});
47+
};
48+
49+
const testAppPaths = (() => {
50+
const [, , ...testAppNames] = process.argv;
51+
52+
return testAppNames
53+
.map(testAppName => {
54+
const testAppPath = pathJoin(rootDirPath, "..", testAppName);
55+
56+
if (fs.existsSync(testAppPath)) {
57+
return testAppPath;
58+
}
59+
60+
console.warn(`Skipping ${testAppName} since it cant be found here: ${testAppPath}`);
61+
62+
return undefined;
63+
})
64+
.filter(exclude(undefined));
65+
})();
66+
67+
if (testAppPaths.length === 0) {
68+
console.error("No test app to link into!");
69+
process.exit(-1);
70+
}
71+
72+
testAppPaths.forEach(testAppPath => execSync("yarn install", { "cwd": testAppPath }));
73+
74+
console.log("=== Linking common dependencies ===");
75+
76+
const total = commonThirdPartyDeps.length;
77+
let current = 0;
78+
79+
commonThirdPartyDeps.forEach(commonThirdPartyDep => {
80+
current++;
81+
82+
console.log(`${current}/${total} ${commonThirdPartyDep}`);
83+
84+
const localInstallPath = pathJoin(
85+
...[
86+
rootDirPath,
87+
"node_modules",
88+
...(commonThirdPartyDep.startsWith("@")
89+
? commonThirdPartyDep.split("/")
90+
: [commonThirdPartyDep])
91+
]
92+
);
93+
94+
execYarnLink({ "cwd": localInstallPath });
95+
});
96+
97+
commonThirdPartyDeps.forEach(commonThirdPartyDep =>
98+
testAppPaths.forEach(testAppPath =>
99+
execYarnLink({
100+
"cwd": testAppPath,
101+
"targetModuleName": commonThirdPartyDep
102+
})
103+
)
104+
);
105+
106+
console.log("=== Linking in house dependencies ===");
107+
108+
execYarnLink({ "cwd": pathJoin(rootDirPath, "dist") });
109+
110+
testAppPaths.forEach(testAppPath =>
111+
execYarnLink({
112+
"cwd": testAppPath,
113+
"targetModuleName": JSON.parse(
114+
fs.readFileSync(pathJoin(rootDirPath, "package.json")).toString("utf8")
115+
)["name"]
116+
})
117+
);
118+
119+
export {};
File renamed without changes.

0 commit comments

Comments
 (0)