Saturday, March 5, 2016

5 npm script secrets

No Secret

Last week I was hosting a workshop about Electron Shell, ES6 and Reactjs at ForwardJS in San Francisco. Preparing all the sample code, I was inspired by this blog post to avoid any build tool like grunt or gulp at all. In fact, I wanted to remove anything that could distract the attendees of the workshop by throwing another unknown toolset at them. Instead, leveraging the build in capabilities of npm.

The Idea

Why use grunt, gulp, etc. if you can leverage the power of npm scripts? Not having to maintain a build script is a good idea and helps to remove another piece of complexity from your project.

Secrets

Different types of scripts

npm supports custom scripts defined by scripts property of package.json. npm distinguishes between 2 different kind of scripts

  • lifecycle scripts

  • directly-run scripts

Lifecycle scripts tie into a specific phase of your project lifecycle, like install, publish, start, stop, etc. These scripts can be executed with npm lifecycle-script. Each lifecycle script can have a pre and post script for that phase, e.g. preinstall and postinstall.

The directly-run scripts need the extra run command after npm, e.g. npm run *my-script-name*. Your directly-run scripts can have a pre and post step with the same name as your script name, e.g. premy-script-name and *postmy-script-name. Always without space between the prefix and the script name.

You can read more about it in the official docs.

Tip Call your own scripts always with npm run <script-name>.

Platform differences

Sometimes you need to do things differenly when it comes to build your project on multiple platforms. I ran into the issue, where I needed to copy files. On OSX this is as simple as cp srcfile.txt destfile.txt. On Windows, this didn’t work, since cp is an unknown command, unless you have cygwin installed. Building your project in the default Windows command prompt fails with an error, once you want to copy those important files. On Windows you should use copy instead.

Tip Create an external copy.js file and do the OS specific tasks there. You can use process.platform === 'win32' to determine if you are running Windows.

DRY - refer to anything defined in your package.json

DRY (don’t repeat yourself) is a great principle when it comes to software development. You can even refer to anything defined in your package.json when you execute your script. Let’s assume, you want to create a zip for distribution.

"scripts": {
    "create-zip": "zip -r app-1.0.zip dist"
}

Now you can call npm run create-zip to create a zip containing all the files to distribute your app.

But why can’t we just refer to the name of the app and the version already defined in package.json? Well, we can. npm exposes all config keys from package.json in the form npm_package_configkey for you to use in your scripts.

"scripts": {
    "create-zip": "zip -r $npm_package_name-$npm_package_version.zip $npm_package_distdir"
}
Tip Stay DRY! Refer to information already available in package.json. You can define whatever config option in package.json and refer to it in the same way.

Call any script in another script

What if you want to run more than one script at the same time? You want for example watch *.js files and *.scss files and preprocess them? How can you do this with one npm script?

You can concatenate different scripts and run them all at the same time:

"scripts": {
    "sass-watch": "node-sass -watch src/style.scss dest/style.css",
    "babel-watch": "babel -w *.js dist",
    "watch": "npm run sass-watch & npm run babel-watch" // 1
}
1 npm run watch will call both custom scripts and run them at the same time. This will create two processes that run independently from each other.
Tip Concatenate multiple scripts with & to run them as one script.

Run any script in your node_modules/.bin directory

Installing any node module that adds it’s own executable script, will be available from projectDir/node_modules/.bin. Let’s say, you need babel for your project and you installed it with npm install babel-cli --save-dev, you can run babel from within your project directory by typing ./node_modules/.bin/babel. That is great, since you don’t have to rely on a global installation of babel.

If you want to run babel as part of a script, you would probably specify the whole path to the babel script:

"scripts": {
    "babel-watch": "./node_modules/.bin/babel -w *.js dist"
}

Since npm adds all executable script to the PATH for the script, you can instead write

"scripts": {
    "babel-watch": "babel -w *.js dist"
}
Tip Don’t specify the full path to a script in your projects node_modules/.bin folder. Use the command without the path.

Thank you very much for reading. If you have any comments or questions, please leave them in the comments below.

Sunday, November 1, 2015

Einmal Tennessee und zurück

"Put on my blue suede shoesAnd I boarded the planeTouched down in the land of the Delta BluesIn the middle of the pouring rain"

Chattanooga

Diese Zeilen aus dem Song "Walking in Memphis" von Marc Cohn, hatte ich im Kopf, als ich letzten Donnerstag mit dem Flugzeug in Chattanooga gelandet bin. Wow, wo bin ich hier gelandet? Alles irgendwie anders, langsamer, aber spannend und interessant. Chattanooga ist eine Kleinstadt, die wirtschaftlich stark gelitten hat, als die großen Industriebetriebe ihre Türen geschlossen haben. Viele gutbezahlte Arbeitskräfte haben ihren Job verloren und zu einer hohen Arbeitslosenquote geführt. Mehr als 20 Jahre hat die Stadt gebraucht, um sich von diesem wirtschaftlichen Untergang zu erholen. Heute hat Chattanooga den Tourismus für sich entdeckt. Ansonsten finden eine ganze Menge Sportveranstaltungen in Chattanooga statt. Die größte Veranstaltung ist ein Ironman.

Chattanooga Airport
Die orangenen Laufschuhe
Statt Lederschuhen hatte ich meine Laufschuhe an.
Downtown Chattanooga

Coco Cola in Chattanooga

Ein paar weise Worte.

Market Street Bridge über den Tennessee River

Sonnenuntergang am Tennessee River

Leider auch hier: Bausünden

"Kleine Kuchenauswahl" in einem lokalem Cafe
Ich war hier angekommen um an einer, ich möchte mal vorsichtig sagen, verrückten Veranstaltung teilzunehmen: ein Staffellauf von Chattanooga nach Nashville. Distanz ca. 200 Meilen (ungefähr 320 km). Teams haben entweder 12 oder 6 Läufer. Gelaufen wird die ganze Zeit. Egal wie spät es ist. Ankunft aller Läufer in Nashville ca. 24-30 Stunden später.
Mein Team bestand aus 12 Läufern und jeder von uns ist 3 mal in dieser Zeit gelaufen. Die zu laufende Distanz pro Läufer variierte zwischen 3 und 10 Meilen.

Jedes Team hat 2 Vans mit jeweils 6 Läufern. Ich war in Van 1 unseres Teams und der letzte Läufer der 6er Gruppe.
Ich bin das erste mal am Freitag um 14:30h unterwegs gewesen. 3.1 Meilen in der prallen Mittagssonne. Nicht meine Lieblingsbedingungen zum Laufen. Aber was solls. Immerhin hatte ich eine relativ flache und kurze Strecke zu absolvieren.

Mein nächster Einsatz war dann erst Nachts um 23:30h. Kurze und flache Strecke mit 2.7 Meilen Länge. Der letzte Lauf war dann am nächsten Tag um 9:00h. Diesmal 4.5 Meilen und mit ziemlich vielen Anstiegen. Immerhin war die Temperatur angenehm zum laufen.
Danach war ich aber auch froh, dass ich nicht nochmal Laufen musste.
Nach dem letzten Lauf. Fertig, aber glücklich.

Jo, genau das richtige nach dem Laufen: Zucker, Mehl und Fett in Kreisform. Auch als Donut bekannt.

Zieleinlauf in Nashville bei bedecktem Himmel

Lea und ich warten mit den anderen auf die Ankunft unserer 6 Läufer aus Van Nr. 2

Sehr coole Idee: alle unsere Medaillen ergeben zusammen eine grosse Medaille

Abendessen mit dem gesamten Team nach fast 30 Stunden

Die ganze Veranstaltung war sehr gut organisiert. Viele Helfer bei den Übergabestellen. Für die längeren Pausen wurden immer Schulen hergerichtet und die Turnhallen zu Schlafplätzen umfunktioniert. Auch in den Schulen waren sehr viele Helfer unterwegs. Sei es nun, um das Chaos auf den Parkplätzen zu koordinieren oder aber um Essen und Getränke in der Schulkantine zu verkaufen.

Drei Dinge sind mir aufgefallen, als ich in Tennessee war:

  1. Hier läuft die Zeit langsamer! Im Vergleich zur Bay Area, wo sich alles schnell bewegt, ist in Tennessee alles ein paar Takte langsamer. Aber das war sehr entspannend, obwohl die Wartezeit in Restaurants manchmal schon als grenzwertig lang zu bezeichnen war.
  2. Die Menschen sind hier sehr freundlich. Jemand hat mir gesagt, dass das die Southern Hospitality ist. Aufgesetzt und hinter der Fassade sieht es anders aus. Ich hab mich dort aber sehr wohlgefühlt und die Interaktion mit den Menschen war ausgesprochen nett.
  3. Es mangelt hier nicht an Kirchen!!!

Nashville ist cool!

Wirklich? Ja, ich finde schon. Die Stadt hat ein paar schöne Ecken. Für Unterhaltung ist auf dem Broadway gesorgt. Bars und Restaurants mit Live Musik. Country Musik ist nicht jedermanns Sache, aber es passt alles zusammen hier. Die Menschen haben Spass und die Stimmung war überall toll. Es gibt noch ein Football und ein Eishockey Team. Beide Teams haben ihre Stadien in Laufweite vom Broadway. Rundherum um den Broadway gibt es eine ganze Menge Geschäfte zum Einkaufen (Cowboy Hut und Stiefel gefällig?). Des Weiteren sind noch eine Menge Museen vorhanden. Allen voran die Country Music Hall of Fame. Leider war die Hall of Fame am Sonntag geschlossen und wir sind ins Johnny Cash Museum gegangen. Sehr interessant und in einer Stunde gut zu schaffen.
Der hippste und teuerste Coffeeshop in Nashville
Noch eine kurze Anekdote zum oben abgebildeten Coffeeshop: ich wollte für mein Team Kaffee besorgen, da wir vor dem Restaurant auf unser Frühstück etwas länger warten mussten (Ansage was 30 Minuten). Ich bin also schnell um die Ecke gegangen, hab dort ein paar Kaffee geordert und wollte dann schnell wieder zurück sein, damit die anderen ihren Kaffee trinken konnten solange sie in der Schlange draussen warten mussten. Nach 20 Minuten bekam ich eine Nachricht von meinem Team. Die Frage nach dem Kaffee musste ich verneinen und meine Bestellung hab ich per SMS übermittelt. Leider hatte ich nach 20 Minuten immer noch keinen Kaffee in der Hand. Kurz darauf hab ich den Kaffee bekommen und bin schnell wieder zurück zum Frühstückslokal. Schmunzelnde Gesichter bei meiner Ankunft. Der Kaffee war gut und teuer (Cappucino $5,50, Ice Cafe Latte $7,50) und hat ewig gedauert. Wie ich eingangs schon mal erwähnte: alles läuft hier langsamer als anderswo. Lektion gelernt.


Und zurück in die Bay Area

Die Kolumne in der New York Times heißt  "36 Stunden in xxx" (xxx=nimm irgendeine Stadt auf der Welt). Leider hatte ich weniger als 36 Stunden in Nashville. Ich hab ein bisschen was gesehen von der Stadt. Aber es gibt noch soviel andere Dinge zu entdecken, die ich mir für einen zukünftigen Besuch gemerkt habe. Also, bis bald mal wieder in Nashville. Dann mit etwas mehr Zeit.


Thursday, October 29, 2015

Install ownCloud on Synology NAS DS413

Here is a list of steps required to install ownCloud on a Synology NAS. I have a DS413 and I was able to install ownCloud on it. I don't know about the other Synology models out there. Let me know, if you succeeded to install ownCloud on any other model.

ownCloud is not part of the official Synology packet repository. You need to add the community repository to install packets from there.


  1. Log into the Synology admin site
  2. Open Package Center and open the Settings. Under Package Sources, add http://packages.synocommunity.com/ as a source (see screenshot). Click Ok to confirm the new repository.
  3. In Package Center, refresh the list of available packets by pressing the Refresh button at the top of the window
  4. Now search for MariaDB and install it. This is required to install Owncloud. Otherwise, the installation of ownCloud will abort with an error. MariaDB is a fork of MySQL and is one of two databases recommended by ownCloud. The other one is PostgreSQL.
  5. Next enable Web Station in the Control Panel. ownCloud is a webapp and needs the http server to properly work.
  6. Before we continue with the ownCloud installation, you need to create a new shared folder. The installation of ownCloud failed in the beginning, because this folder is required, but could not be created.
  7. Open the Control Panel and open Shared Folder. Create a new shared folder and give it the name ownCloud (this is the default during the ownCloud installation).
  8. Check off Hide this shared folder in "My Network Places". Nobody should modify any content in this directory. This belongs to the ownCloud installation!
  9. Change the permissions of the newly created shared folder. In order to grant ownCloud read/write permissions, select http from the Local Groups (in the Dropdown list on the left hand side above the table) and check the box in the Read/Write column. Press the OK button when done.
  10. Now search for ownCloud and install it.
  11. During the installation of ownCloud, you will be asked to enter the password for the root user. Leave this field empty. By default there root password is not set (this is not secure at all, but for demo purposes, this should be alright)
  12. After ownCloud was installed properly, click on the URL in the ownCloud overview page in the Package Center. In my case it's http://192.168.1.22/owncloud.
  13. Now you will be greeted with ownCloud's login page. Type in your admin password and log into your ownCloud installation. The administration is part of another blog post.

Summary

This is a list of steps to install ownCloud on a Synology NAS DS413. The steps cover the installation of dependencies required to run ownCloud. The administration of ownCloud is not part of this post.
I hope this will help anyone to successfully install ownCloud. Let me know if you have any questions or other constructive feedback.