npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-writefile

v0.1.4

Published

Writes static files using handlebars templates.

Downloads

2,530

Readme

grunt-writefile

Writes static files using handlebars templates.

Getting Started

This plugin requires Grunt ~0.4.2

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-writefile --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-writefile');

The "writefile" Task

The writefile task is a simple task to create all kind of static files from handlebars templates. You might find this helpful if you want to export files for different environments (e.g. development and production) and need a basic and quick solution.

Options

| Property | Type | Description |:--------------------|:---------------|:------------- | data | Object/String | The data object passed to the handlebars template. If a string is given, it is interpreted as a path to a JSON file (defaults to undefined). | paths | Object | Creates an array of paths for each given file pattern and adds (or overrides) a paths property to the template data. Each file pattern can either be a string or an object for building paths dynamically (defaults to undefined). | helpers | Object | Custom handlebars helpers, where the key is the helper name and the value is the helper function (defaults to undefined). | preserveExtension | Boolean | This option is only relevant for expanded paths. Will strip the file extension from the destination path when set to false or keep it unchanged when set to true (defaults to false). | encoding | String | The file encoding to write files with (defaults to grunt.file.defaultEncoding). | mode | Boolean/Number | Whether to copy or set the existing file permissions. Set to true to copy the existing file permissions. Or set to the mode (i.e. 0644) that copied files will be set to (defaults to false).

Examples

Basic Options

Basic configuration reading/writing a single file.

grunt.initConfig({
  writefile: {
    options: {
        data: {
            foo: 1,
            bar: 2
        }
    },
    main: {
        src: 'path/to/template.hbs',
        dest: 'path/to/target.txt'
    }
  }
});

Advanced Options

Scans for *.html.hbs files nested inside a template directory and writes the structure to a public directory. As preserveExtension is set to false, the file extension will be dropped when writing. Thereby index.html.hbs will become index.html.

grunt.initConfig({
  writefile: {
    options: {
        preserveExtension: false,
        data: 'path/to/data.json',  // read template data from JSON file
        helpers: {                  // provide handlebars helper functions
            someHelper: function (value) {
                return '<strong>' + value + '</strong>';
            }
        },
        paths: {                    // provide directory contents to template
            someFiles: '/path/to/**/some/files.*',
            otherFiles: {
                cwd: 'path/base/',
                src: '**/other/files.*'
            }
        }
    },
    main: {
        files: [{
            expand: true,
            cwd: 'path/base/',
            src: 'templates/**/*.html.hbs',
            dest: 'public/',
        }]
    }
  }
});

Real-World Example

This example illustrates how to use the plugin for writing files for different environments.

If you use the paths object, you probably want to run the writefile task after all other file manipulating tasks (like less or uglify) to make sure you get the right directory contents.

grunt.initConfig({
    less: {
        dev: { /* ... */ },
        prod: { /* ... */ }
    },
    uglify: {
        dev: { /* ... */ },
        prod: { /* ... */ }
    },
    writefile: {
        options: {
            data: {
                title: 'My Page Title'
            },
            paths: {
                stylesheets: 'assets/styles/*.css',
                scripts: 'assets/scripts/*.js'
            }
        }
        index: {
            src: 'templates/index.html.hbs',
            dest: 'public/index.html'
        }
    }
});

// ...

grunt.registerTask('dev', ['less:dev', 'uglify:dev', 'writefile:index']);
grunt.registerTask('prod', ['less:prod', 'uglify:prod', 'writefile:index']);

The index.html.hbs template file could look like this:

<!DOCTYPE html>
<html>
    <head>
        <title>{{title}}</title>

        {{#each paths.stylesheets}}
        <link href="{{this}}" rel="stylesheet" type="text/css">
        {{/each}}
    </head>
    <body>
        <!-- content -->

        {{#each paths.scripts}}
        <script src="{{this}}"></script>
        {{/each}}
    </body>
</html>

Contributing

Thanks to the grunt project, the handlebars template engine and all people that are somehow involved in all that.

Release History

| Release | Description |:--------|:------------ | 0.1.0 | Initial release. | 0.1.1 | Fixed bug related to preserveExtension option. | 0.1.2 | Updated library versions. | 0.1.3 | Updated peer dependencies. | 0.1.4 | Updated library versions.