Skip to content

Commit f0f278c

Browse files
boutellETLaurentBoDonkey
authored
PRO-8856: fix it so testing against Pro modules is optional (but always happens in our CI) (#5251)
* WIP. I fixed it for import-export but there is also one test suite in apostrophe core that needs the attention apparently * did not need to touch this file * changeset, CI * env var in right place * guidance for contributors * not used * Update CONTRIBUTING.md Co-authored-by: E.T <8301962+ETLaurent@users.noreply.github.com> * redundant if * changesets * monorepo * link * Update MONOREPO.md Co-authored-by: Robert Means <robert@apostrophecms.com> * Update MONOREPO.md Co-authored-by: Robert Means <robert@apostrophecms.com> --------- Co-authored-by: E.T <8301962+ETLaurent@users.noreply.github.com> Co-authored-by: Robert Means <robert@apostrophecms.com>
1 parent 34a2428 commit f0f278c

File tree

8 files changed

+667
-446
lines changed

8 files changed

+667
-446
lines changed

.changeset/modern-radios-attack.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apostrophecms/import-export": patch
3+
---
4+
5+
Support for testing with or without pro modules available (without, by default)

.github/workflows/monorepo.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ jobs:
365365
run: pnpm run --filter "${{ matrix.package }}" --if-present test
366366
env:
367367
CI: true
368+
# Yes we want import-export to test with automatic-translation
369+
TEST_WITH_PRO: "1"
368370

369371
- name: Stop Redis
370372
if: always() && matrix.needsRedis

CONTRIBUTING.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,20 @@ were applicable. For Apostrophe core, that should be in the
9898
and for other modules, add it in their README files (unless the README directs
9999
you elsewhere).
100100

101+
### Working with the monorepo
102+
103+
All of our public npm modules are contained in this single monorepo. You can find the individual packages in the `packages/` subdirectory. This means you will not be using `npm link` in the way you may be used to.
104+
105+
See [MONOREPO.md](./MONOREPO.md) for more information about how to work with the monorepo.
106+
101107
### Contributing to Apostrophe Core
102108

103109
This is assuming you are interacting with the ApostropheCMS repositories on the GitHub website. If you are using GitHub Desktop you can read about how to fork a repository in the [GitHub docs.](https://docs.github.com/en/desktop/contributing-and-collaborating-using-github-desktop/adding-and-cloning-repositories/cloning-and-forking-repositories-from-github-desktop)
104110

105111
1. Fork the main Apostrophe repository to your own account by clicking on the fork button at the top of the [GitHub page.](https://github.com/apostrophecms/apostrophe) If you are contributing to the latest version of Apostrophe you can simply click on the "Create fork" button on the next screen. If you are contributing to Apostrophe 2, you will need to uncheck the "Copy the main branch only" selection before creating the fork.
106112
2. The forked version of the repository can be modified in any way you would like without impact on the original repository. As a best practice, we request that you create a branch with a short informative name for making code changes. This makes it easier for our team to track what you are contributing when you make your pull request (PR).
107-
3. Once you've completed your code changes (and updates to `CHANGELOG.md` if needed - see the notes above) you can push all of your changes to your repo. Navigating to your GitHub repository page you will see a banner for creating a PR. Click on the "Contribute" and then "Open a pull request" buttons.
113+
3. Once you've completed your code changes, `git add` and `git commit` them locally. Then run the `pnpm changeset` command as described in [MONOREPO.md](./MONOREPO.md) to generate "changesets," which will be merged into the changelog later. Please do not directly edit the `CHANGELOG.md` file.
114+
4. Push all of your changes to your repo. Navigating to your GitHub repository page you will see a banner for creating a PR. Click on the "Contribute" and then "Open a pull request" buttons.
108115
4. This will bring up a PR page. There are a number of sections to be filled out. Please read carefully and make selections where needed. Following this checklist is very helpful when our team reviews your request. If you need help, just ask!
109116
5. Finally, it is best to notify a team member on the [Discord channel](https://discord.com/channels/517772094482677790/701815369005924374) that you have submitted a PR. You can also find our contact addresses on our GitHub pages. We don't get automatic notifications from community-submitted PRs. We will see it in the queue eventually, notifying us will just speed up the process.
110117

MONOREPO.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# Work with the monorepo
2+
3+
This is a monorepo containing many packages. You will need `pnpm`, not `npm`.
4+
5+
## pnpm installation
6+
7+
[See official documentation](https://pnpm.io/installation). Cheat sheet: `npm install -g pnpm` works.
8+
9+
When pnpm is installed, you can clone the [apostrophe repository](https://github.com/apostrophecms/apostrophe) which is now a monorepo (or just fetch / pull main).
10+
11+
## Monorepo behavior
12+
13+
Inside this repository, there are a few things to see that relate to pnpm and the monorepo:
14+
15+
- `.npmrc` file. Here we ask pnpm when installing dependencies to put every package at the root of the node_modules folder.
16+
17+
```jsx
18+
public-hoist-pattern[]=*
19+
```
20+
21+
Otherwise, pnpm installs dependencies of each dependencies and allow multiple modules to have the same dependency with a different version.
22+
23+
We cannot do that because apostrophe expects to find everything at the root of `node_modules`.
24+
25+
That’s why we use this option. It still installs very fast because it uses symlinks.
26+
27+
(In local development, pnpm has a global store where it stores dependencies and symlinks them in projects, that’s why it’s very fast.)
28+
29+
- `pnpm-workspace.yaml` file. This file defines the structure of the monorepo to pnpm. It’s very simple for us, because we only have packages, but we could have `apps` too, for example, with starter kits.
30+
31+
```jsx
32+
packages:
33+
- 'packages/*'
34+
```
35+
36+
- In `package.json` file, we have recursive scripts, allowing to run a command that will be run in every package:
37+
38+
```jsx
39+
"scripts": {
40+
"lint": "pnpm --recursive run lint",
41+
"test": "pnpm --recursive run test",
42+
"eslint": "pnpm --recursive run eslint",
43+
"mocha": "pnpm --recursive run mocha",
44+
"clean": "pnpm -r exec rm -rf node_modules && rm -rf node_modules && rm pnpm-lock.yaml"
45+
},
46+
```
47+
48+
We also have `pnpm` configuration inside `package.json`. We have to tell pnpm which packages have the permission to run post install scripts. This reduces the risk of attacks like `sha1 hulud` which is important to keep us out of the headlines:
49+
50+
```jsx
51+
"pnpm": {
52+
"onlyBuiltDependencies": [
53+
"sharp",
54+
"vue-demi",
55+
"@parcel/watcher",
56+
"esbuild",
57+
"unrs-resolver"
58+
]
59+
},
60+
```
61+
62+
- Inside `package.json` you will also see new syntax for internal dependencies:
63+
64+
```jsx
65+
"devDependencies": {
66+
"eslint": "^9.39.1",
67+
"eslint-config-apostrophe": "workspace:^"
68+
}
69+
```
70+
71+
`workspace:` allows us to install a package of the monorepo as a dependency of another package of the monorepo. It’s automatically resolved to the right versionwhen running `pnpm publish`.
72+
73+
There is just one issue with this, in `testbed`, for example, we install dependencies this way, directly from the monorepo.
74+
75+
```jsx
76+
"apostrophe": "github:apostrophecms/apostrophe#main&path:packages/apostrophe"
77+
```
78+
79+
(Note the use of `path`, which allows us to pull in a module from a subdirectory of the repo. Regular npm does not support this so we only use it during testing.)
80+
81+
The problem is that in the monorepo, apostrophe has `workspace` dependencies. While it works perfectly inside the monorepo, when installing dependencies in testbed for example, which is external to the monorepo, `pnpm` needs to know how to rewrite them.
82+
83+
So we use a pnpm feature called `pnpmfile` ([see documentation](https://pnpm.io/pnpmfile)). Basically this allows us to hook into the installation process. We can then rewrite the `workspace:` dependencies. We also use this in pro modules that need apostrophe core for tests and are outside of the monorepo.
84+
85+
See the testbed’s `pnpmfile` or look in the pro modules where it enables testing against our latest `main` etc.
86+
87+
## How do I run just one test file?
88+
89+
Let’s say you are in the root of the monorepo and you only want to run mocha for the `test/styles.js` file from `apostrophe`:
90+
91+
```bash
92+
pnpm -C packages/apostrophe mocha test/styles
93+
```
94+
95+
To run **all** of the tests for one module, try:
96+
97+
```jsx
98+
pnpm -C packages/apostrophe test
99+
```
100+
101+
## Working locally
102+
103+
### Basic symlinks
104+
105+
To test a module that is still in development as part of a starter kit, you simply need to create a symlink to the monorepo package you need. (No, you can’t use `npm link`, but `npm link` has been quite broken for quite a while now and the team activately avoids it. So this is not really new.)
106+
107+
- Be sure your monorepo dependencies are installed:
108+
109+
```jsx
110+
cd apostrophe && pnpm install
111+
```
112+
113+
- go to your starter-kit and install dependencies here too (make sure you have a `.npmrc` with the hoisting option):
114+
115+
```jsx
116+
cd starter-kit-essentials && pnpm install
117+
```
118+
119+
- Then link the module you need, in this case `apostrophe`:
120+
121+
> Examples here assume you have the monorepo checked out in `~/apostrophecms/apostrophe`, mirroring the github org name and repo name.
122+
>
123+
124+
```jsx
125+
rm -rf ./node_modules/apostrophe
126+
ln -s ~/apostrophecms/apostrophe/packages/apostrophe ./node_modules/apostrophe
127+
```
128+
129+
## Internal dependencies
130+
131+
`pnpm` allows this syntax for internal dependencies.
132+
133+
```jsx
134+
{
135+
"dependencies": {
136+
"foo": "workspace:*",
137+
"bar": "workspace:~",
138+
"qar": "workspace:^",
139+
"zoo": "workspace:^1.5.0"
140+
}
141+
}
142+
```
143+
144+
We exclusively want to use this one:
145+
146+
```bash
147+
"qar": "workspace:^",
148+
```
149+
150+
This ensures that when we are ready to publish (see below), the module is published with an npm dependency like:
151+
152+
```bash
153+
"qar": "3.^",
154+
```
155+
156+
So that the customer can `npm update` and get the expected result and not a different major version.
157+
158+
## Changesets
159+
160+
[Changeset CLI documentation](github.com/changesets/changesets/blob/main/docs/command-line-options.md)
161+
162+
### Day to day work
163+
164+
For `CHANGELOG` generation, versioning and publishing we are now using [Changesets](https://github.com/changesets/changesets).
165+
166+
This is a powerful tool made for monorepos.
167+
168+
When you have finished to work on a feature, fix or whatever, just run:
169+
170+
```bash
171+
pnpm changeset
172+
```
173+
174+
It will ask you which packages have been updated by your work. Select each package of interest by pressing the spacebar.
175+
176+
It will then ask what kind of change you made for each package (major / minor / patch). Select "major" only for backwards compatibility breaks - such changes likely will not be accepted without serious consultation. Select "major" for any feature addition. "Patch" for fixes only.
177+
178+
Finally, it will ask for a summary (this will be the changelog entry).
179+
180+
It will create a unique file inside the `.changeset` folder, no more `CHANGELOG` conflicts 🥳.
181+
182+
Include this file in your PR.

packages/import-export/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"stylelint": "stylelint ui/**/*.{scss,vue}",
99
"lint": "npm run eslint && npm run stylelint",
1010
"mocha": "mocha",
11-
"test": "npm run lint && mocha --ignore=test/import-page.js && mocha test/import-page.js"
11+
"test": "(../../scripts/pro-if-needed && npm run lint && mocha --ignore=test/import-page.js && mocha test/import-page.js) ; ../../scripts/undo-pro-if-needed"
1212
},
1313
"repository": {
1414
"type": "git",
@@ -19,7 +19,6 @@
1919
"author": "Apostrophe Technologies",
2020
"license": "UNLICENSED",
2121
"devDependencies": {
22-
"@apostrophecms-pro/automatic-translation": "github:apostrophecms/automatic-translation",
2322
"apostrophe": "workspace:^",
2423
"eslint": "^9.39.1",
2524
"eslint-config-apostrophe": "workspace:^",

0 commit comments

Comments
 (0)