run-if-changed icon indicating copy to clipboard operation
run-if-changed copied to clipboard

Preserve order of execution

Open thelinuxlich opened this issue 9 months ago • 5 comments

I have a task triggering pnpm install whenever pnpm-lock.yaml changes and it should run before other tasks that might be dependent on it.

thelinuxlich avatar Feb 21 '25 14:02 thelinuxlich

@thelinuxlich can you provide your config and how do you imagine this working in terms of configuration API?

hkdobrev avatar Feb 21 '25 19:02 hkdobrev

Sure, I have something like this in my package.json:

"run-if-changed": {
    "pnpm-lock.yaml": "pnpm i",
    "prisma/rise/migrations/**/*.sql": "pnpm migrate-all",
    "prisma/rise_private/migrations/**/*.sql": "pnpm migrate-all",
    "prisma/rise_audit/migrations/**/*.sql": "pnpm migrate-all",
    "packages/db/src/seed.ts": "pnpm --filter db seed && pnpm codegen",
    "packages/db/src/helpers/contracts.ts": "pnpm --filter db seed && pnpm codegen",
    "packages/db/src/helpers/*.json": "pnpm --filter db seed && pnpm codegen"
  }

I'd expect nothing changes config-wise, just that run-if-changed respects the order defined here. (I need packages installed and migrations applied before running codegen)

thelinuxlich avatar Feb 21 '25 19:02 thelinuxlich

It might work if we reverse the order in which run-if-changed processes the state. Currently, it asks Git for the changed files and tries to find matching patterns for each of them in the config. If we instead read the config of patterns first and try to find matching files in the changed files from Git, it might work the way you describe.

Would try to give it a go in the following days.

In the meantime, you can repeat some commands so that it also installs packages and runs migration before running codegen.

hkdobrev avatar Feb 21 '25 20:02 hkdobrev

Hm, actually run-if-changed already does that in the order I described, but in the process of flattening the commands the order can get scrambled:

https://github.com/hkdobrev/run-if-changed/blob/d0b41c3859391658b880b67b32b92bb52edb32f4/src/resolveMatchingPatterns.js#L3-L11

Might need to play with that further, but I'm afraid it will be hard to make it work reliably without changing the config structure.

hkdobrev avatar Feb 21 '25 20:02 hkdobrev

@thelinuxlich OK, I tried this something very close to your config.

It does work like you would expect. It goes through the configured patterns in order. The alphabetic order doesn't matter, but the order you used in your configuration file.

If files found in multiple patterns are changed, then it will first run the commands attached to the earlier patterns in the config.

If you are not seeing that, could you show a reproducible demo?

Please check that multiple patterns are affected at the same time. It might be that only later commands are run when the lock file hasn't changed. If you want the installation or migration commands to run before codegen even when the respective files are unchanged, you would need to repeat these commands before the codegen command.

hkdobrev avatar Feb 21 '25 20:02 hkdobrev