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

Export FILENAME as environment variable #40

Open
thomasfl opened this issue Nov 17, 2016 · 6 comments
Open

Export FILENAME as environment variable #40

thomasfl opened this issue Nov 17, 2016 · 6 comments

Comments

@thomasfl
Copy link

I suggest making the updated FILENAME available as an environment variable from within the command. It makes is possible to run commands like this:

  $ chokidar "**/**.js" -c "jshint $FILENAME"

The very much similar filewatcher CLI, written in ruby, exports these environment variables:

FILENAME           Relative filename.
ABSOLUTE_FILENAME  Asolute filename.
RELATIVE_FILENAME  Same as FILENAME but starts with "./"
BASENAME           File basename.
EVENT              Event type. Is either 'changed', 'delete' or 'new'.
DIRNAME            Absolute directory name.

Source: https://github.com/thomasfl/filewatcher

BTW! Chokidar seems to be a very good piece of software.

@es128
Copy link
Collaborator

es128 commented Nov 17, 2016

From the readme:

Pass the path and event details in to your custom command

chokidar '**/*.less' -c 'if [ "{event}" = "change" ]; then npm run build-less -- {path}; fi;'

@thomasfl
Copy link
Author

thomasfl commented Nov 18, 2016

Thank's @es128. I didn't notice that feature in the readme.

I would however suggest that instead of replacing "{path}" in the command string like this:

opts.command
                    .replace(/\{path\}/ig, path)
                    .replace(/\{event\}/ig, event)

...make the path and the event available as environment variables in the process running the command string. It's done by passing environment variables to the spawn function like this:

child = childProcess.spawn(SHELL_PATH, [EXECUTE_OPTION, cmd], {
                cwd: opts.cwd,
                stdio: opts.pipe ? 'inherit' : null,
                env: {PATH: path, EVENT: event}
            });

I think this is a bit more unix like way of making the variables available to the command process. If there's any interest, I'll be happy to create a pull request.

@kimmobrunfeldt
Copy link
Collaborator

@thomasfl I agree, using a random string template format is worse than using environment variables.

I'll leave this open. I'm open to merge a PR which implements this.

@es128
Copy link
Collaborator

es128 commented Nov 19, 2016

If you're going to replace the original templating with this, be mindful to release with a major version bump.

@kimmobrunfeldt
Copy link
Collaborator

@es128 yeah good point. Will do

@thomasfl
Copy link
Author

This was way more complicated than expected to implement. The spawn function is wrapped in four layers of lodash throttle magic. I was simply not able to figure out how to pass another variable to utils.run() from the watcher-loop. I suggest the environment variables are named FILEPATH and EVENT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants