Skip to content

Commit

Permalink
change package name and distribute as module
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-schubert-sbb committed Feb 8, 2024
1 parent e0ae1d3 commit 6380f66
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 189 deletions.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MIT License

Copyright (c) 2019 Michel Weststrate
Copyright (c) 2024 Stefan Schubert

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
69 changes: 32 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# relative-deps

[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/michelweststrate)
<a href="https://www.buymeacoffee.com/mweststrate" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 22px !important;width: auto !important;" ></a>
# link-deps

_Installs dependencies from a local checkout, and keeps them in sync, without the limitations of `link`_

---

# Summary

Relative deps introduces an additional dependency section in `package.json`, called `relativeDependencies`.
Based on a fork of [relative-deps](https://github.com/mweststrate/relative-deps) by Michel Weststrate.

`link-deps` introduces an additional dependency section in `package.json`, called `linkDependencies`.
This section contains paths to the local sources of any dependency, that will be built and installed over the publicly available versions, when needed.

Example `package.json`:
Expand All @@ -20,14 +17,14 @@ Example `package.json`:
"dependencies": {
"my-cool-library": "0.1.0"
},
"relativeDependencies": {
"linkDependencies": {
"my-cool-library": "../../packages/my-cool-library"
},
"scripts": {
"prepare": "relative-deps"
"prepare": "link-deps"
},
"devDependencies": {
"relative-deps": "^1.0.0"
"link-deps": "^1.0.0"
}
}
```
Expand All @@ -53,9 +50,9 @@ There are a few existing solutions, but they have their own limitations:
- `yarn link` / `npm link`. These work only if there are no peer / shared dependencies involved. If there are shared dependencies, the linked library will resolve those in their _own_ `node_modules`, instead of the `node_modules` of the hosting project, where it would normally be looked up. This results in peer dependencies ending up "twice" in the dependency tree, which often causes confusing behavior.
- `yarn workspaces`. Those solve the above issue by putting all dependencies in one large root level `node_modules`. However, this setup is in practice quite obtrusive to the whole development setup.

### How is relative deps different?
### How is `link-deps` different?

Relative deps doesn't fight the problem but tries to emulate a "normal" install. It builds the "linked" library on `prepare` (that is, after installing all deps), packs it, and unpacks it in the `node_modules` of the hosting project. Since there is no linking, or shared `node_modules` involved, the folder structure ends up to be exactly the same as if the thing was installed directly from `yarn` / `npm`. Which avoids a plethora of problems.
`link-deps` doesn't fight the problem but tries to emulate a "normal" install. It builds the "linked" library on `prepare` (that is, after installing all deps), packs it, and unpacks it in the `node_modules` of the hosting project. Since there is no linking, or shared `node_modules` involved, the folder structure ends up to be exactly the same as if the thing was installed directly from `yarn` / `npm`. Which avoids a plethora of problems.

Since building a linked package every time `yarn install` is run is expensive, this tool will take a hash of the directory contents of the library first, and only build and install if something changed.

Expand All @@ -64,26 +61,26 @@ Since building a linked package every time `yarn install` is run is expensive, t
## Installation

```bash
npx relative-deps init
npx link-deps init
```

Options:

- `--script`

Alias `-S`. Default: `prepare`. Script name which is using for running `relative-deps`.
Alias `-S`. Default: `prepare`. Script name which is using for running `link-deps`.

Running this script will install `relative-deps`, add script and initialize empty `relativeDependencies` section.
Running this script will install `link-deps`, add script and initialize empty `linkDependencies` section.

```json
{
"name": "my-project",
"devDependencies": {
"relative-deps": "^1.0.0"
"link-deps": "^1.0.0"
},
"relativeDependencies": {},
"linkDependencies": {},
"scripts": {
"prepare": "relative-deps"
"prepare": "link-deps"
}
}
```
Expand All @@ -94,22 +91,22 @@ Optionally, you can add this step also for more scripts, for example before star
{
"name": "my-project",
"scripts": {
"prepare": "relative-deps",
"prestart": "relative-deps",
"prebuild": "relative-deps",
"pretest": "relative-deps"
"prepare": "link-deps",
"prestart": "link-deps",
"prebuild": "link-deps",
"pretest": "link-deps"
}
}
```

In general, this doesn't add to much overhead, since usually relative-deps is able to determine rather quickly (~0.5 sec) that there are no changes.
In general, this doesn't add to much overhead, since usually `link-deps` is able to determine rather quickly (~0.5 sec) that there are no changes.

## Adding a relative dependency

Running following script will initialize `relative-deps` if not initialized yet, find the package at the provided path, install it as normal dependency and pack relative-dependency.
Running following script will initialize `link-deps` if not initialized yet, find the package at the provided path, install it as normal dependency and pack relative dependency.

```bash
npx relative-deps add ../../packages/my-cool-library
npx link-deps add ../../packages/my-cool-library
```

Options:
Expand All @@ -124,42 +121,40 @@ Alias `-D`. Installs relative dependency in `devDependencies` section.
"dependencies": {
"my-cool-library": "0.1.0"
},
"relativeDependencies": {
"linkDependencies": {
"my-cool-library": "../../packages/my-cool-library"
},
"scripts": {
"prepare": "relative-deps"
"prepare": "link-deps"
},
"devDependencies": {
"relative-deps": "^1.0.0"
"link-deps": "^1.0.0"
}
}
```

Example of a [repository migration to relative-deps](https://github.com/mobxjs/mst-gql/pull/40/commits/4d2c0858f8c44a562c0244466b56f79b0ed7591b)

## Run `npx relative-deps` when devving!
## Run `npx link-deps` when devving!

The relative deps will automatically be checked for changes, based on the hooks you've set up during [installation](#installation).
The relative dependency will automatically be checked for changes, based on the hooks you've set up during [installation](#installation).

However, you can always trigger a manual check-and-build-if-needed by running `npx relative-deps` (or just `yarn`). If you are working on a project that supports
However, you can always trigger a manual check-and-build-if-needed by running `npx link-deps` (or just `yarn`). If you are working on a project that supports
hot reloading, this will makes sure the changes in the relative dependency will automatically show up in your project!

## Watch mode

You can run `relative-deps watch` and it'll run `relative-deps` command when one of the relative dependecies changed, debounced with 500ms.
You can run `link-deps watch` and it'll run `link-deps` command when one of the relative dependencies changed, debounced with 500ms.
This can go along with config of your project to watch over the relevant packages and it will automate the process completely,
allowing you to change a library code and to enjoy the befefit of hot-reload.
allowing you to change a library code and to enjoy the benefit of hot-reload.

# How

Roughly, it works like this (obviously this can get out of date quickly):

```
- pre: yarn.lock exists or die
- read relativeDeps from nearest package.json
- read linkDependencies from nearest package.json
- doesn't exist? warn & exit
- for each relativeDep:
- for each relative dependency:
- check if target path exists
- if not, do we have the module from normal install?
- yes: warn
Expand Down
24 changes: 12 additions & 12 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#!/usr/bin/env node
const yargs = require("yargs");
const relativeDeps = require("./index");
import yargs from "yargs/yargs";
import { initLinkDeps, installLinkDeps, watchLinkDeps, addLinkDeps } from "./index.js";

yargs
yargs(process.argv.slice(2))
.usage("Usage: $0 <command> [options]")
.command({
command: "*",
describe: "Install relative deps",
handler: relativeDeps.installRelativeDeps
describe: "Install linked deps",
handler: installLinkDeps
})
.command({
command: "watch",
describe: "Watch relative deps and install on change",
handler: relativeDeps.watchRelativeDeps
describe: "Watch linked deps and install on change",
handler: watchLinkDeps
})
.command({
command: "init",
describe: "Initialize relative-deps",
handler: relativeDeps.initRelativeDeps
describe: "Initialize link-deps",
handler: initLinkDeps
})
.command({
command: "add [paths...]",
describe: "Add path as relative dependencies",
handler: relativeDeps.addRelativeDeps
describe: "Add path as linked dependencies",
handler: addLinkDeps
})
.option("D", {
alias: ["dev", "save-dev"],
Expand All @@ -32,7 +32,7 @@ yargs
})
.option("S", {
alias: ["script"],
description: "Script for relative-deps",
description: "Script for link-deps",
default: "prepare",
type: "string"
}).argv;
Loading

0 comments on commit 6380f66

Please sign in to comment.