Skip to content

feat: npm workspaces #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 30, 2024
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
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ updates:
- package-ecosystem: "npm"
directories:
- "/"
- "/spark-publish"
schedule:
interval: "daily"
time: "09:00"
timezone: "Europe/Berlin"
versioning-strategy: increase
commit-message:
prefix: "deps"
prefix-development: "deps(dev)"
Expand Down
23 changes: 17 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build-api:
Expand All @@ -25,13 +24,14 @@ jobs:
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
GLIF_TOKEN: ${{ secrets.GLIF_TOKEN }}
NPM_CONFIG_WORKSPACE: spark-api
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run test:api
- run: npm test

build-publish:
runs-on: ubuntu-latest
Expand All @@ -51,14 +51,25 @@ jobs:
--health-retries 5
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
NPM_CONFIG_WORKSPACE: spark-publish
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: node bin/migrate.js
- run: npm test

lint-all:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run migrate
- run: npm run test:publish
- run: npm run lint
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to run standard for all files in the monorepo, including those that are not in any sub-package, e.g. migrations/index.js

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we turn migrations into a sub-package too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we turn migrations into a sub-package too?

I would prefer indeed if both would work: linting from the parent and the subpackage. Could we create a standard config that re-exports the parent config?

I am not a fan of this. It adds a lot of boilerplate. We will need to add package.json to migrations, add scripts like lint, add standard as a dependency, and so on.

Having written that, I also want to get this monorepo setup finished ASAP and choose the setup that works for both of us, so I am going to take the direction you suggested.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we turn migrations into a sub-package too?

Hmm, we would have to turn bin and scripts into sub-packages too. I definitely don't like that.

I am going to explore a different setup, where the monorepo root is a package on its own, and then some of the subdirectories are packages (workspaces) too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in daa2650

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am keeping the lint-all step so that we can lint all workspaces in one go, and in parallel with running the tests.

The alternative is to lint spark-api and spark-publish as part of build-* workflows, and then add another workflow to run only files that are not part of any child workspace (bin, migrations, scripts). I am not sure how easy that would be to do, though, so I prefer the current proposal.


docker-build:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -94,7 +105,7 @@ jobs:

deploy-api:
if: github.ref == 'refs/heads/main'
needs: [build-api, build-publish, docker-build, docker-build]
needs: [build-api, build-publish, docker-build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -123,7 +134,7 @@ jobs:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
deploy-publish:
if: github.ref == 'refs/heads/main'
needs: [build-api, build-publish, docker-build, docker-build]
needs: [build-api, build-publish, docker-build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
15 changes: 0 additions & 15 deletions .mocharc.cjs

This file was deleted.

12 changes: 9 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ RUN apt-get update -qq && \
# to install all modules: "npm install --production=false".
# Ref: https://docs.npmjs.com/cli/v9/commands/npm-install#description
COPY --link package-lock.json package.json ./
RUN npm ci

# We cannot use a wildcard until `COPY --parents` is stabilised
# See https://docs.docker.com/reference/dockerfile/#copy---parents
COPY --link spark-api/package.json ./spark-api/
COPY --link spark-publish/package.json ./spark-publish/

RUN npm ci --workspaces

# Copy application code
COPY --link . .
Expand All @@ -44,6 +50,6 @@ ARG SERVICE

# ARGs are not preserved at runtime, we need to store the value
# as a default value of an ENV var
ENV SCRIPT="start:${SERVICE}"
ENV WORKSPACE="spark-${SERVICE}"

CMD npm run ${SCRIPT}
CMD npm start --workspace ${WORKSPACE}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ docker run -d --name spark-db \
Start the API service:

```bash
npm run start:api
npm start --workspace spark-api
```

Run tests and linters:

```bash
npm run test:api
npm test --workspace spark-api
npm run lint --workspace spark-api
```

## Deployment
Expand Down
91 changes: 62 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 8 additions & 34 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,46 +1,20 @@
{
"name": "@filecoin-station/spark-api",
"name": "@filecoin-station/spark-api-monorepo",
"private": true,
"version": "0.0.0",
"license": "MIT",
"repository": "filecoin-station/spark-api",
"type": "module",
"description": "API for SPARK",
"workspaces": [
"spark-api",
"spark-publish"
],
"scripts": {
"migrate": "node bin/migrate.js",
"start:api": "node spark-api/bin/spark.js",
"start:publish": "node spark-publish/bin/spark-publish.js",
"test:api": "mocha spark-api/test/**/*.js",
"test:publish": "mocha spark-publish/test/**/*.js",
"test": "standard && mocha"
"lint": "standard",
"test": "npm run lint && npm test --workspaces"
},
"devDependencies": {
"@flydotio/dockerfile": "^0.5.7",
"light-my-request": "^5.13.0",
"mocha": "^10.4.0",
"standard": "^17.1.0",
"varint": "^6.0.0"
},
"dependencies": {
"@filecoin-station/spark-impact-evaluator": "^1.0.0",
"@glif/filecoin-address": "^3.0.5",
"@influxdata/influxdb-client": "^1.33.2",
"@ipld/car": "^5.3.0",
"@sentry/node": "^8.5.0",
"@ucanto/core": "^10.0.1",
"@ucanto/principal": "^9.0.1",
"@web3-storage/access": "^18.3.2",
"@web3-storage/w3up-client": "^13.1.1",
"compare-versions": "^6.1.0",
"ethers": "^6.12.1",
"http-assert": "^1.5.0",
"http-responders": "^2.0.2",
"multiformats": "^13.1.0",
"p-retry": "^6.2.0",
"p-timeout": "^6.1.2",
"pg": "^8.11.5",
"postgrator": "^7.2.0",
"raw-body": "^2.5.2"
"standard": "^17.1.0"
},
"standard": {
"env": [
Expand Down
38 changes: 38 additions & 0 deletions spark-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@filecoin-station/spark-api",
"private": true,
"version": "0.0.0",
"license": "MIT",
"repository": "filecoin-station/spark-api",
"type": "module",
"description": "API for SPARK",
"scripts": {
"start": "node bin/spark.js",
"lint": "standard",
"test": "mocha"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the main branch, one could run (cd spark-publish && npm test) to execute both the linter and the tests for a sub-package.

Is this something you want to preserve in the new monorepo setup?

In my proposal, you always have to run the linter from the monorepo root, because that's the place where package.json tells standard about Mocha.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer indeed if both would work: linting from the parent and the subpackage. Could we create a standard config that re-exports the parent config?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in daa2650

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we create a standard config that re-exports the parent config?

I could not find any quick way how to do that, Standard seems to accept config only via CLI args or in the closest package.json file.

The alternative is to eject from standard to eslint, but that's much more work than I am willing to do now.

We could also remove the env config from package.json and modify every file to start with the following line:

/* eslint-env mocha */

It would be cleaner because non-test files won't have access to describe & it globals, but it also feels like too much boilerplate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up duplicating the "standard":{"env":...}} section in each package.json.

},
"devDependencies": {
"light-my-request": "^5.13.0",
"mocha": "^10.4.0",
"standard": "^17.1.0",
"varint": "^6.0.0"
},
"dependencies": {
"@filecoin-station/spark-impact-evaluator": "^1.0.0",
"@glif/filecoin-address": "^3.0.5",
"@sentry/node": "^8.5.0",
"compare-versions": "^6.1.0",
"ethers": "^6.12.1",
"http-assert": "^1.5.0",
"http-responders": "^2.0.2",
"multiformats": "^13.1.0",
"pg": "^8.11.5",
"postgrator": "^7.2.0",
"raw-body": "^2.5.2"
},
"standard": {
"env": [
"mocha"
]
}
}
35 changes: 35 additions & 0 deletions spark-publish/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"type": "module",
"scripts": {
"start": "node bin/spark-publish.js",
"lint": "standard",
"test": "mocha"
},
"dependencies": {
"@filecoin-station/spark-impact-evaluator": "^1.0.0",
"@glif/filecoin-address": "^3.0.5",
"@influxdata/influxdb-client": "^1.33.2",
"@ipld/car": "^5.3.0",
"@sentry/node": "^8.4.0",
"@ucanto/core": "^10.0.1",
"@ucanto/principal": "^9.0.1",
"@web3-storage/access": "^18.3.2",
"@web3-storage/w3up-client": "^13.1.1",
"ethers": "^6.12.1",
"p-retry": "^6.2.0",
"p-timeout": "^6.1.2",
"pg": "^8.11.5"
},
"devDependencies": {
"@flydotio/dockerfile": "^0.5.7",
"mocha": "^10.4.0",
"multiformats": "^13.1.0",
"postgrator": "^7.2.0",
"standard": "^17.1.0"
},
"standard": {
"env": [
"mocha"
]
}
}