2016年12月22日木曜日

開発環境

Automate with Grunt (Brian P. Hogan(著)、Pragmatic Bookshel)の Chapter 2(Manage Files)の 「What's Next?」の部分を取り組んでみる。

コード(Emacs)

package.json

{
  "name": "deploying",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "kamimura",
  "license": "MIT",
  "devDependencies": {
    "grunt": "^1.0.1"
  }
}

Gruntfile.js

module.exports = (grunt) => {
    grunt.config.init({
        pkg: grunt.file.readJSON('package.json'),
        copyFiles: {
            options: {
                workingDirectory: 'working',
                manifest: [
                    'index.html',
                    'css/',
                    'js/'
                ]
            },
        },
    });
    grunt.registerTask(
        'createFolder',
        'Create the working folder',
        () => {
            grunt.config.requires('copyFiles.options.workingDirectory');
            grunt.file.mkdir(grunt.config.get(
                'copyFiles.options.workingDirectory'));
        });
    grunt.registerTask(
        'clean',
        'Deletes the working folder and its contents',
        () => {
            grunt.config.requires('copyFiles.options.workingDirectory');
            grunt.file.delete(
                grunt.config.get('copyFiles.options.workingDirectory'));
        });
    grunt.registerTask(
        'copyFiles',
        function () {
            this.requiresConfig(`${this.name}.options.manifest`);
            this.requiresConfig(`${this.name}.options.workingDirectory`);

            let files = this.options().manifest;
            let workingDirectory = this.options().workingDirectory;

            let recursiveCopy = (src, dest) => {
                if (grunt.file.isDir(src)) {
                    grunt.file.recurse(src, (file) => {
                        recursiveCopy(file, dest);
                    });
                } else {
                    grunt.log.writeln(`Copying ${src} to ${dest}`);
                    grunt.file.copy(src, `${dest}/${src}`);
                }
            };
            files.forEach((item) => {
                recursiveCopy(item, workingDirectory);
            });

        });
    grunt.registerTask(
        'version',
        () => {            
            let content = '<%=pkg.name %> version <%= pkg.version %>';

            content = grunt.template.process(content);

            grunt.config.requires('copyFiles.options.workingDirectory');
            
            let dir = grunt.config.get('copyFiles.options.workingDirectory');
            
            grunt.file.write(`${dir}/version.txt`, content);
        });
    grunt.registerTask(
        'deploy',
        ['clean', 'createFolder', 'copyFiles', 'version']);
};

入出力結果(Terminal)

$ grunt deploy
Running "clean" task
>> Cannot delete nonexistent file.

Running "createFolder" task

Running "copyFiles" task
Copying index.html to working
Copying css/layout.css to working
Copying css/style.css to working
Copying js/app.js to working

Running "version" task

Done.
$

0 コメント:

コメントを投稿