Skip to content
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

Add .relative-deps-ignore option, to solve double rebuild in TS / other compiled or built repos #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ You can run `relative-deps watch` and it'll run `relative-deps` command when one
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.

### 🔔 If you get multiple re-builds (due to your project being compiled, and then "new changes" found again): for the relative-dep, in it's folder, add a `.relative-deps-ignore` file with `!dist` or similar entries
```
!dist
!.output
!.built
```

# How

Roughly, it works like this (obviously this can get out of date quickly):
Expand Down
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,16 @@ async function watchRelativeDeps() {
async function libraryHasChanged(name, libDir, targetDir, hashStore) {
const hashFile = path.join(targetDir, "node_modules", name, ".relative-deps-hash")
const referenceContents = fs.existsSync(hashFile) ? fs.readFileSync(hashFile, "utf8") : ""
// compute the hahses
const libFiles = await findFiles(libDir, targetDir)
// glob pattern list, relative to libDir, made for adding "dist" and similar
const relativeDepsIgnoreFile = path.join(libDir, ".relative-deps-ignore")
const additionalIgnoredList = fs.existsSync(relativeDepsIgnoreFile)
? fs.readFileSync(relativeDepsIgnoreFile, "utf8")
.split("\n")
.filter(line => line.trim() && !line.trim().startsWith('#'))
.map(line => line.split('#')[0])
Comment on lines +87 to +90
Copy link
Collaborator

Choose a reason for hiding this comment

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

So in addition to checking that the file exists, there may be other causes of errors in reading the file contents (such as a lack of permissions, or the file may be huge and fail to be read into memory or a number of other issues).
To mitigate that, at the very least this block needs to be wrapped in try/catch. But instead of trying to mitigate all possible issues here, I'm sure there's a package which handles reading/parsing .gitignore format instead.

: undefined
// compute the hashes
const libFiles = await findFiles(libDir, targetDir, additionalIgnoredList)
const hashes = []
for (file of libFiles) hashes.push(await getFileHash(path.join(libDir, file)))
const contents = libFiles.map((file, index) => hashes[index] + " " + file).join("\n")
Expand All @@ -106,8 +114,8 @@ async function libraryHasChanged(name, libDir, targetDir, hashStore) {
return true
}

async function findFiles(libDir, targetDir) {
const ignore = ["**/*", "!node_modules", "!.git"]
async function findFiles(libDir, targetDir, additionalIgnoredList = []) {
const ignore = ["**/*", "!node_modules", "!.git", ...additionalIgnoredList]
// TODO: use resolved paths here
if (targetDir.indexOf(libDir) === 0) {
// The target dir is in the lib directory, make sure that path is excluded
Expand Down