Hops CLI
hops-cli provides a small set of commands to manage your hops project. Installing hops-cli is the minimum requirement for being able to start working with Hops.
Installation
Minimum installation for any project
We assume that you already created a folder for your project and initialized it with npm init -y
. Go to the root folder of your project and install hops-cli. This installs hops-config and hops-server as dependencies as well:
npm install --save hops-cli
That's it - at least this is sufficient to get you started with a very basic setup.
Additional setup for a React project
In case you are going to implement a project with React, you need to install further packages:
npm install --save react react-router-dom hops-react
Usage
Available commands
The following commands are provided by hops-cli:
hops build
- initiates a project buildhops develop
- starts a Webpack development serverhops serve
- initiates a project build, stores the build artifacts in the file system and starts a production (Express) serverhops start
- if NODE_ENV is set to production, this runs hops serve
. Otherwise, hops develop
gets executedhops start --production
- this overrides the NODE_ENV setting and runs hops serve
Configure package.json
To actually use the hops-cli commands, you have to add them to the scripts section of your project's package.json:
"scripts": {
"build": "hops build",
"develop": "hops develop",
"serve": "hops serve",
"start": "hops start"
}
However, for most projects, it should be sufficient to just add "start": "hops start"
here.
Use via npm
After that, you can execute them via npm like so:
npm run build
npm run develop
npm run serve
npm start
npm start --production
Very basic example app
To try out the minimum installation described above, create an index.js file in the root folder of your project and put the following code in there:
export default function (req, res, next) {
if (req) {
console.log('server');
if (req.path === '/') {
res.set('Content-Type', 'text/html');
res.send('<script src="/main.js"></script>');
} else {
next();
}
} else {
console.log('browser');
}
}
Don't forget to configure the scripts section of your package.json. And you're done!