ADAMKDEAN

Add notices to auto-generated files with gulp

Published Tuesday, 25 August 2015

I wrote a little tool today to pop into my gulp workflow which writes some text to the top of files processed by gulp. The problem I find is that quite often you can be working on a project with source files and generated files (e.g. jsx -> js) and accidentally edit the wrong file, only for your changes to be overwritten.

This tool will put a notice at the top of auto-generated files to hopefully help put a stop to that. There are other packages that do this too, but so far in the past week there have been about 10 releases of gulp-header and quite a few of them were broken. Also they originated from godaddy so confidence in the package is pretty low!

Usage

You can use the default notice:

var gulp = require('gulp'),
    notice = require('gulp-notice');

gulp.task('default', function () {
    gulp.src('src/*.js')
        .pipe(notice())
        .pipe(gulp.dest('dist/'));
});

Which will prepend files with:

/* --------------------------------------------------------------------- *\
|  This code was auto-generated by a tool.                                |
|                                                                         |
|  Changes to this file may cause incorrect behavior and will be lost if  |
|  the code is regenerated.                                               |
\* --------------------------------------------------------------------- */

Or you can provide your own string as the first parameter:

var gulp = require('gulp'),
    notice = require('gulp-notice');

var text = '/* this file was auto-generated */';

gulp.task('default', function () {
    gulp.src('src/*.js')
        .pipe(notice(text))
        .pipe(gulp.dest('dist/'));
});

Which will prepend files with:

/* this file was auto-generated */

Working wth streams

If you're working with streams (e.g. vinyl-source-stream) then gulp-streamify will help you. Let's say you're using browserify with vinyl-source-stream, you can wrap gulp-notice with streamify and it'll work:

return b.bundle()
    .pipe(source(entry))
    .pipe(streamify(notice()))
    .pipe(gulp.dest('./public/js'));

Install it

npm install gulp-notice

More links

GitHub: https://github.com/adamkdean/gulp-notice
NPM: https://www.npmjs.com/package/gulp-notice

Koa middleware for serving static files

Published Wednesday, 12 August 2015

Quite often I find myself using the same snippet of code over and over in projects for serving up static files as part of projects that use Koa. Today I exceeded my limit for copy pasting, so I've bundled this together into a module called koa-serve.

It's probably a lot simpler than koa-static but it works well for what I want and need.

var koa = require('koa'),
    serve = require('koa-serve'),
    app = koa();

app.use(serve('assets'));
app.listen(8000);

You can also define where you root dir is, if isn't __dirname.

var koa = require('koa'),
    serve = require('koa-serve'),
    app = koa();

app.use(serve('assets', '/path/to/your/root'));
app.listen(8000);

Example if your client files are in the parent directory, and index.js is in server/ for example:

var koa = require('koa'),
    serve = require('koa-serve'),
    path = require('path'),
    app = koa();

app.use(serve('assets', path.join(__dirname, '..', 'client'));
app.listen(8000);

Install it

npm install koa-serve

More links

GitHub: https://github.com/adamkdean/koa-serve
NPM: https://www.npmjs.com/package/koa-serve

Node.js IsDirectory

Published Wednesday, 12 August 2015

Here's a useful snippet for checking if a path is a directory in Node.js, this obviously requires('fs').

fs.lstatSync(path_string).isDirectory()

Also available to you:

fs.lstatSync(path_string).isFile()
fs.lstatSync(path_string).isDirectory()
fs.lstatSync(path_string).isBlockDevice()
fs.lstatSync(path_string).isCharacterDevice()
fs.lstatSync(path_string).isSymbolicLink()
fs.lstatSync(path_string).isFIFO()
fs.lstatSync(path_string).isSocket()

Useful git aliases

Published Friday, 31 July 2015

I've been so busy recently that this blog has been neglected. I'm planning on fixing some design quirks and adding in some archiving, maybe even giving it a biy of a face lift so it's not so drab. Something I don't want to happen is to have empty months when it comes to my archive so today is the last day of July and I'll show you what aliases I have in my git config.

[alias]
    a = "!git add -A . && git add -u"
    ab = branch -avv
    dt = difftool
    s = status
    b = branch -a
    k = !gitk --all --select-commit=HEAD &
    r = remote -v
    rh = reset --hard
    pr = "!git add . && git add -u && git stash && git pull && git stash pop"
    logp = log -n10 --decorate --graph --pretty=tformat:\"%C(yellow)%h%Creset%C(cyan)%d%C(reset) %s %C(green)(%an, %ar)%C(reset)\" --relative-date
    diffp = diff --ignore-space-at-eol -b -w   
    llm = log -1
    unadd = reset --
    ua = reset --
    h = rev-list --max-count=1 HEAD
    list-merge-backups = !git status -su | grep -e'\\.orig$' | cut -f2 -d' '
    delete-merge-backups = !git list-merge-backups | xargs rm
    mergefrom = merge -s recursive -X theirs

Well, that was an easy blog post, but I can't use git without these, they save a lot of time!

Copying files from one container to another

Published Sunday, 7 June 2015

This only works on OS X as I'm using pbcopy and pbpaste. You may replace these as you see fit. Also I'm using superdocker here, you can swap that for docker if you like, it makes no difference.

What I needed to do was copy some SSL certificates from one container to another. I wanted the easiest way of doing this for the future. Thus I came up with this somewhat messy solution. I like it though. In this case the source container is called 'registry' and the destination container is a data volume container called 'publisherd-data'.

superdocker exec -ti $(superdocker ps | grep registry | awk '{print $1}') \
    cat /go/src/github.com/docker/distribution/certs/domain.crt | \
    pbcopy && \
    superdocker run --rm -ti \
        --volumes-from publisherd-data \
        ubuntu bash -c "echo '$(pbpaste)' > /etc/nginx/certs/registry.domain.com.crt"

superdocker exec -ti $(superdocker ps | grep registry | awk '{print $1}') \
    cat /go/src/github.com/docker/distribution/certs/domain.key | \
    pbcopy && \
    superdocker run --rm -ti \
        --volumes-from publisherd-data \
        ubuntu bash -c "echo '$(pbpaste)' > /etc/nginx/certs/registry.domain.com.key"

To break it down, we first grab the certificate by running docker exec on the source container, which we find by doing a quick docker ps | grep | awk.

superdocker exec -ti $(superdocker ps | grep registry | awk '{print $1}') \
    cat /go/src/github.com/docker/distribution/certs/domain.crt

Next we pipe that to pbcopy, the OS X clibpoard utility.

superdocker exec -ti $(superdocker ps | grep registry | awk '{print $1}') \
    cat /go/src/github.com/docker/distribution/certs/domain.crt | \
    pbcopy

Then we run an ethereal container which we connect to the destination data volume container. We then simply empty our clipboard into the destination file.

superdocker exec -ti $(superdocker ps | grep registry | awk '{print $1}') \
    cat /go/src/github.com/docker/distribution/certs/domain.crt | \
    pbcopy && \
    superdocker run --rm -ti \
        --volumes-from publisherd-data \
        ubuntu bash -c "echo '$(pbpaste)' > /etc/nginx/certs/registry.domain.com.crt"

It's a figurative mouthful, but it does the trick without saving your certs anywhere. You might also want to clear that clipboard afterwards though.

echo '' | pbcopy
 
Showing posts 1-5 of 169