A seed project for custom Four51 Solutions built on AngularJS
Node.js is required for the following node package manager (npm) tasks. If you don't have node.js installed, you can download it here.
$ npm -g install karma bower
$ npm -g install "gulpjs/gulp-cli#4.0"
$ npm install
$ bower install
$ gulp buildYou should now have a few more directories in your project.
OrderCloud/
|- build/
|- node_modules/
|- vendor/
WebStorm is our chosen development IDE. It provides an interface for the capabilities of the seed projects configuration.
Once you've installed the prerequisites and run your gulp build you can setup and run your Karma tests.
Create a Run configuration using the Karma plugin with the following settings:
Node interpreter: C:\Program Files (x86)\nodejs\node.exe
Karma package: C:\Four51\WebFiles\SPASites\defaults\OrderCloud\node_modules\karma
Configuration file: C:\Four51\WebFiles\SPASites\defaults\OrderCloud\build\karma-unit.js
At a high level, the structure looks roughly like this:
OrderCloud/
|- Gulp/
|- |- assetTasks.js
|- |- generalTasks.js
|- |- scriptTasks.js
|- |- testTasks.js
|- |- watchTasks.js
|- karma/
|- node_modules/
|- src/
| |- app/
| | |- <application code>
| |- assets/
| | |- <static files>
| |- index.html
|- vendor/
| |- <bower components>
|- .bowerrc
|- bower.json
|- gulpConfig.js
|- Gulpfile.js
|- karma.conf.js
|- module.prefix
|- module.suffix
|- package.json
This section provides a little more detailed understanding of what goes into
getting OrderCloud up and running. Though OrderCloud is really simple
to use, it might help to have an understanding of the tools involved here, like
Node.js and Gulp and Bower. If you're completely new to highly organized,
modern JavaScript development, take a few short minutes to read this overview
of the tools before continuing with this section.
Here it is:
OrderCloud uses Gulp as its build system, so
Node.js is required. Also, we are using Gulp 4.0 prior to
its official release date. You can install Gulp 4.0 on your machine globally by running
the following command:
$ npm -g install "gulpjs/gulp-cli#4.0" karma bowerAnd then install the remaining build dependencies locally:
$ npm installThis will read the dependencies (empty by default) and the devDependencies
(which contains our build requirements) from package.json and install
everything needed into a folder called node_modules/.
There are many Bower packages used by OrderCloud, like AngularJS and the
OrderCloud-Angular-SDK, which are listed in bower.js. To install them into the
vendor/ directory, simply run:
$ bower installIn the future, should you want to add a new Bower package to your app, run the
install command and add --save to save the dependency in your bower.json file:
$ bower install packagename --saveThe --save flag tells Bower to add the package at its current version to
our project's bower.js file so should another developer download our
application (or we download it from a different computer), we can simply run the
bower install command as above and all our dependencies will be installed for
us. Neat!
Technically, OrderCloud is now ready to go.
To ensure your setup works, build your application and then run it with the following commands:
$ gulp build
$ gulp watchThe built files are placed in the build/ directory by default. And you application
should automatically open in the browser window on a localhost!
watch actually starts a few other processes in the background to help you develop your
application. Using browser-sync and some built in gulp functions the app is now watching
for changes in your source directory. Should you make any changes to your html or js files
the app should automatically reload your application with the appropriate changes. Also
if you make any changes to your style sheets (less or css) the app will rebuild those changes
and inject them directly into the application, without reloading the entire page!
When you're ready to push your app into production, just run the compile
command:
$ gulp compileThis will concatenate and minify your sources and place them by default into the
compile/ directory. There will only be three files (excluding assets): index.html,
OrderCloud.js, and OrderCloud.css. All of the vendor dependencies like
AngularJS styles and the OrderCloud-SDK itself have been added to them for super-easy
deploying. If you use any assets (src/assets/) then they will be copied to
compile/ as is.
Lastly, a complete build is always available by simply running the default
task, which runs build and then compile:
$ gulpThe best way to learn about the build system is by familiarizing yourself with
Gulp and then looking through the code, Gulpfile.js and the Gulp folder.
But you don't need to do that to be very productive with
OrderCloud. What follows in this section is a quick introduction to the
tasks provided and should be plenty to get you started.
Below is a description of the gulp tasks in the project, sorted by their general purpose and location within the Gulp directory.
####Asset Tasks
b_m:less- Compiles all of the app and bower less files into css and moves the compiled file into the temp folderb_m:sass- Compiles all of the app and bower sass files into css and moves the compiled file into the temp folderb_m:css- Compiles all of the bower css files (with the exception of the font awesome file as it was already compiled inb_m:less) into one file and autoprefixes the css to run for the last to versions of all the major browsersb_m:appCss- Compiles all of the app css files into one file and autoprefixes the css to run for the last to versions of all the major browsersb_m:styles- Compiles all of the css files in the temp directory into one file, replaces the path to fonts to be correct for the build directory and renames the file with the version of the app. Places the file in the build folder and injects the change into browser-sync if it is currently runningb_c:styles- Deletes all of the assets that have been moved over to the build folder and all of the files in the temp folderb_m:assets- Moves over all asset files (fonts and images) saved in the src/assets directory to the build/assets directoryb_m:fonts- Moves over all fonts saved within the bower dependencies to the build/assets directoryb_c:assets- Deletes all assets files from the build directoryc_m:css- Minifies all css files in the build directory and moves them to the compile folderc_c:css- Deletes all css files from the compile directoryc_m:assets- Moves over all asset files (fonts and images) from the build folder to the compile folderc_c:assets- Delete all asset and css files from the compile directorybuild:styles- Runsb_c:styles,b_m:less,b_m:sass,b_m:css,b_m:appCss,b_m:stylesin ordercompile:css- Runsc_c:css,build:styles,c_m:cssin orderbuild:assets- Runsb_c:assets,b_m:assets,b_m:fontsin ordercompile:assets- Runsc_c:assets,build:assets,c_m:assetsin order
####General Tasks
compile:inject- Injects the app dependencies into the index.html file for the compiled build Note: Theere should only be two:OrderCloud-[Version#].cssandapp.jsbuild:inject- Injects the app dependencies into the index.html file for the unminified buildmasterClean- Deletes the build, compile, and temp directories. Basically anything built by the gulp tasks will be removed by this taskbuild- First runsmaster clean, then runsbuild:js_bower,build:js,build:templateCache,build:styles,build:assets, and lastly runsbuild:injectto create an unminified build of the projectcompile- First runsbuild, then runscompile:js,compile:assets,compile:cssand lastly runscompile:injectto create a minified build of the projectdefault- Runs thecompiletask when onlygulpis typed into the command prompt
####Script Tasks
b_m:js_bower- Moves the bower dependencies over to the build folderb_c:js_bower- Deletes bower dependencies form the build folderb_m:js- Moves over all of the app js files need to run the application (not the ones for unit testing) and annotates them and wraps each file in an IIFE call.b_c:js- Deletes all of the javascript files moved over to the build folder except for the templateCache fileb_m:templateCache- Creates an angular templateCache of all the html files (except the index) that allows for the application to run faster.b_c:templateCache- Cleans out the compiled template file generated byb_m:templateCachec_c:js- Deletes all js files from the compile directorybuild:js- Runsb_c:js, andb_m:jsin orderbuild:js_bower- Runsb_c:js_bower, andb_m:js_bowerin orderbuild:templateCache- Runsb_c:templateCache, andb_m:templateCachein ordercompile:js- Runsc_c:js,build:js_bower,build:js, andbuild:templateCacheat the same time, then runsc_m:js
####Test Tasks
testServe- Starts the browser-sync server on localhost:12000 Note: this task does not build the application first so you must rungulp buildfirst for it to work. This task differs fromdevin that it opens a tunnel that allows the app to be viewed on other computers not on the same network.
####Watch Tasks
dev- Starts the browser-sync server on localhost:8000karma:unit- Starts the karma unit testswatch:js- Starts a process that watches for changes in the javascript files in the src directorywatch:assets- Starts a process that watches for changes in the style sheet files (less and css)watch:other- Starts a process the watches for changes in the html fileswatch- Starts all of the previously mentioned tasks in parallel
As covered in the previous section, gulp build and gulp watch will execute a full build
up-front and then run any of the aforementioned watch tasks as needed to
ensure the fastest possible build. So whenever you're working on your project,
start with:
$ gulp buildthen:
$ gulp watchAnd everything will be done automatically!
To make the build even faster, tasks are placed into two categories: build and compile. The build tasks (like those we've been discussing) are the minimal tasks required to run your app during development.
Compile tasks, however, get your app ready for production. The compile tasks include concatenation, minification, compression, etc. These tasks take a little bit longer to run and are not at all necessary for development so are not called automatically during build or watch.
To initiate a full compile, you simply run the default task:
$ gulpThis will perform a build and then a compile. The compiled site is located in compile/.
To test that your full site works as expected, open the compile/index.html file in your browser. Voila!