typicode / hotel

🏩 A simple process manager for developers. Start apps from your browser and access them using local domains
MIT License
9.98k stars 424 forks source link

How to run a script when visiting a hotel site #245

Open mehrdad-shokri opened 6 years ago

mehrdad-shokri commented 6 years ago

I'm using json-server to serve json files.
by running yarn run json_server command json-server --watch data/db.json --port 3000 is run.
How can I bind this command to a domain in hotel so every time I visit fake.dev I get output of the json server.
I know I can do something like $ hotel add 'http://localhost:3000' --name fake --port 3000 --out hotel.log But I'm looking for binding a npm script to hotel.
BTW thanks for both of your amazing projects.

j-f1 commented 6 years ago

Try $ hotel add --name fake --out hotel.log 'json-server --watch data/db.json --port $PORT'. Note that:

  1. You have to use single quotes — that way, $PORT will reflect the port Hotel picks for the server.
  2. If your directory is called fake, you don’t have to provide the --name fake option.
  3. You can go to http://hotel.dev to view the logs in your browser instead of saving them to a file, if you’d like.
mehrdad-shokri commented 6 years ago

@j-f1 Ah, maybe my misinforming. I want to run a package.json script when visiting a hotel site.
My package.json scripts section is:

"scripts": {
    "json_server": "json-server --watch data/db.json  --port 3000 -q",
    "gulp": "gulp --silent",
    "start": "run-p --silent json_server gulp"
  }

So when visiting fake.dev I want to run yarn run start but apparently hotel runs that script from c.
Running start creates a json server also initiates a gulp task that watches for file changes and changes db.json which is used for json server.
Is it possible to serve a json server by running an npm script and seeing the server fake.dev?
If my question seems vague please ask to clarify.

j-f1 commented 6 years ago

Is it possible to serve a json server by running an npm script and seeing the server fake.dev?

Does hotel add --port 3000 --name fake --out hotel.log 'yarn run start' work for you? This command will run the yarn run start command and connect http://fake.dev to localhost:3000. Note that hotel won’t automatically start the server — you have to switch it on at http://hotel.dev.

mehrdad-shokri commented 6 years ago

@j-f1 it tries to run yarn run start from directory C.
package.json is located in D:\somedir\someotherdir.
How can I run it from that dir? I tried to pass --dir argument but apparently it didn't apply.

j-f1 commented 6 years ago

Is the cwd key correct in %HOME%\.hotel\servers\fake.json? If so, can you try running hotel add --port 3000 --name fake --out cd.log cd and tell me what it puts in cd.log?

mehrdad-shokri commented 6 years ago

The command I ran: $ hotel add --port 3000 --name fake --out hotel.log "yarn run start" --dir "D:\somedirectory/projectdir"
My fake.json file:

{
  "cwd": "D:\somedirectory/projectdir",
  "cmd": "yarn run start",
  "out": "hotel.log",
  "env": {
    "PATH": "C:\\Users\\Mehrdad\\bin;C:\\Program Files\\Git\\mingw64\\bin;C:\\Program Files\\Git\\usr\\local\\bin;C:\\Program Files\\Git\\usr\\bin;C:\\Program Files\\Git\\usr\\bin;C:\\Program Files\\Git\\mingw64\\bin;C:\\Program Files\\Git\\usr\\bin;C:\\Users\\Mehrdad\\bin;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0;C:\\Program Files\\Git\\cmd;C:\\wamp64\\bin\\php\\php7.1.11;C:\\ProgramData\\ComposerSetup\\bin;C:\\Program Files\\PuTTY;C:\\Program Files (x86)\\OpenVPN\\bin;C:\\Program Files (x86)\\HashiCorp\\Vagrant\\bin;C:\\Program Files (x86)\\ngrok\\ngrok.exe;C:\\Program Files (x86)\\Yarn\\bin;C:\\Program Files\\nodejs;C:\\Users\\Mehrdad\\AppData\\Local\\Microsoft\\WindowsApps;E:\\programming\\android\\sdk\\platform-tools;C:\\Program Files (x86)\\Java\\jdk1.8.0_121\\bin;C:\\Users\\Mehrdad\\AppData\\Roaming\\Composer\\vendor\\bin;C:\\Program Files (x86)\\GnuWin32\\bin;C:\\Program Files\\Microsoft VS Code\\bin;C:\\Program Files (x86)\\ngrok;C:\\Users\\Mehrdad\\AppData\\Local\\Yarn\\bin;C:\\Users\\Mehrdad\\AppData\\Roaming\\npm;C:\\Program Files\\Git\\usr\\bin\\vendor_perl;C:\\Program Files\\Git\\usr\\bin\\core_perl",
    "PORT": 3000
  }
}

And this is the log:
download 1

I see that hotel doesn't cd do the dir before running specified command. but I don't know why.
I'm on Windows.

UPDATE:

For sure running yarn from command line works properly. So I know that this is a hotel configuration problem.

j-f1 commented 6 years ago

"D:\somedirectory/projectdir"

Are there two \ after the D: in the actual string? If there aren’t, that’s a really weird bug. Also, the fact that there’s a / in the path indicates that something is improperly joining the paths together.

Can you try making hotel run cd (the Windows equivalent of pwd) and tell me what it outputs?

mehrdad-shokri commented 6 years ago

Are there two \ after the D: in the actual string?

Yep, there are 2 backslashes, This is also true for path property on env.

Can you try making hotel run cd (the Windows equivalent of pwd) and tell me what it outputs?

executing hotel run cd returns:

Create ~\.hotel\servers\fake.json
D:\web_development\work\app\fake
(node:6532) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'status' of undefined
(node:6532) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In
the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
mehrdad-shokri commented 6 years ago

@j-f1 sorry, I misunderstood you. Now I tried $hotel run "yarn start" --port 3000 (port 300 js json server's port) And it worked.
Don't know why running hotel add gives error.

mehrdad-shokri commented 6 years ago

Again to clarify when trying to visit fake.dev I get error:
'run-p' is not recognized as an internal or external command, run-p is installed locally. Really weird problem.

j-f1 commented 6 years ago

What does hotel add --name fake cd output?

mehrdad-shokri commented 6 years ago
$ hotel add --name fake cd
Create ~\.hotel\servers\fake.json
Output No log file specified (use '-o app.log')
Port Random port (use '-p 1337' to set a fixed port)

Visiting fake.dev:

mehrdad-shokri commented 6 years ago

I'm curious why $ hotel run 'yarn run start' --port 3000 works. But hotel add --name fake 'yarn start' --port 3000 doesn't

mehrdad-shokri commented 6 years ago

@j-f1 found sth, changing my script in package.json to:

 "scripts": {
    "start": "./node_modules/.bin/run-p --silent json_server gulp"
  }

Fixes the problem! (Note "./node_modules/.bin/ before command name).

Apparently, hotel tries to execute yarn from a directory other than the project root.
Am I right?

j-f1 commented 6 years ago

It looks like hotel is changing to the appropriate directory, so the problem might lie elsewhere.

The reason why hotel run works is because it actually runs the server in your terminal, whereas hotel add runs the server as a child of the daemon.

mehrdad-shokri commented 6 years ago

Why changing script to ./node_modules/.bin/run-p --silent json_server gulp works then?
Can provide another soloution?

j-f1 commented 6 years ago

This might be a bug with how yarn/Windows resolves executables. yarn should add node_modules/.bin to the lookup path, but it seems like it’s not doing that for some reason, preventing Windows from finding run-p.

mehrdad-shokri commented 6 years ago

I've tested it with npm. same result.

kuncevic commented 6 years ago

I have this script "start": "node ./node_modules/json-server/bin/index.js --watch --port=3000 db.json" if I run that like npm start - all looking good however if I do hotel add 'npm start' in my project folder and then trying to launch the thing from the hotel I am getting error ''npm' is not recognized as an internal or external command, operable program or batch file. Thoughts? UPDATE: just checked the path .hotel\servers\server.json and discovered that weirdness: "cmd": "'npm", so had to take the single quote out and actually ended up changing that to "cmd": "npm start", but still not sure if I actually was added the hotel ting right.

mehrdad-shokri commented 6 years ago

how does hotel know which dir to run npm start?
Also does npm start calls another script itself?

kuncevic commented 6 years ago

@mehrdaad I am running the thing in my project folder so as far as I can tell hotel applying the current dir if you not specifying the dir.

That is what I am doing now hotel add --port 3000 --name fake-api --out hotel.log "npm run start" which is working well

mehrdad-shokri commented 6 years ago

@kuncevic I do believe hotel runs the command from root dir. I don't know maybe @j-f1 can help.

j-f1 commented 6 years ago

@mehrdaad hotel saves the directory you’re in when you run hotel add, then switches back to that directory before running your script.