file-factory

Generate Files From A JSON File And A Basic Template.

Usage no npm install needed!

<script type="module">
  import fileFactory from 'https://cdn.skypack.dev/file-factory';
</script>

README

file-factory

Generate Files From A JSON File And A Basic Template.

Usage

Assume you wanna generate files reference to a specify template:
_layout

<!DOCTYPE html>
<html>
<head>
    <title>%%title</title>
    <meta charset="utf-8" />
    <meta name="description" content="%%description">
    <meta name="keywords" content="%%keywords">
</head>
<body>
    <h1>%%heading</h1>
    <div id="view"></div>
</body>
</html>

And you have a JSON file list all of the files information:
meta.json

{
    "index.html": {
        "title": "GULP FILE FACTORY",
        "description": "index.html did not sepcify `dest` in this file.",
        "keywords": "so it will store in the directory of `www`",
        "heading": "The Page Index"
    },
    "foo.html": {
        "title": "FOO",
        "description": "foo.html sepcify `dest` in this file.",
        "keywords": "so it will store in the directory of `./page/foo`",
        "heading": "The Page Foo",
        "dest": "./page/foo"
    },
    "bar.html": {
        "title": "BAR",
        "description": "bar.html sepcify `dest` in this file.",
        "keywords": "so it will store in the directory of `../../page/bar`",
        "heading": "The Page Bar",
        "dest": "../../page/bar"
    }
}

Rusult:
www/index.html

<!DOCTYPE html>
<html>
<head>
    <title>GULP FILE FACTORY</title>
    <meta charset="utf-8" />
    <meta name="description" content="index.html did not sepcify `dest` in this file.">
    <meta name="keywords" content="so it will store in the directory of `www`">
</head>
<body>
    <h1>The Page Index</h1>
    <div id="view"></div>
</body>
</html>

page/foo/foo.html

<!DOCTYPE html>
<html>
<head>
    <title>FOO</title>
    <meta charset="utf-8" />
    <meta name="description" content="foo.html sepcify `dest` in this file.">
    <meta name="keywords" content="so it will store in the directory of `./page/foo`">
</head>
<body>
    <h1>The Page Foo</h1>
    <div id="view"></div>
</body>
</html>

page/bar/bar.html
(this file will be created to the upper directories relative to your current work directory)

<!DOCTYPE html>
<html>
<head>
    <title>BAR</title>
    <meta charset="utf-8" />
    <meta name="description" content="bar.html sepcify `dest` in this file.">
    <meta name="keywords" content="so it will store in the directory of `../../page/bar`">
</head>
<body>
    <h1>The Page Bar</h1>
    <div id="view"></div>
</body>
</html>

How To Use
First, install file-factory as a development dependency:

npm install --save-dev file-factory

Use with nodejs
file-factory.js

var fileFactory = require('file-factory');

fileFactory({
    layout: './_layout',    // The template for all of generated files
    meta: './meta.json',    // The data of the specify file
    dest: './www',          // The directory where the generated files store,
                            // this field will be cover by the field meta defined in the meta.json
    identify: '%%'          // The prefix of the search string
});

run:

node file-factory

Running in gulp task
gulpfile.js

var gulp        = require('gulp');
var fileFactory = require('file-factory');

gulp.task('file-factory', function() {
    return fileFactory({
        layout: './_layout',
        meta: './meta.json',
        dest: './www',
        identify: '%%'
    });
})

run:

gulp file-factory

API

fileFactory(options)

options

Type: Object

options.layout

Type: String
The template for all of generated files.

options.meta

Type: String
The data of the specify files.

options.dest

Type: String
Default: www
The directory where the generated files store,
this field will be cover by the field dest which defined in the file where options.meta specify.

options.identify

Type: String
Default: %%
The prefix of the search string.