0.0.1 • Published 5 years ago

insert-file-content v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

insert-file-content

insert-file-content (ifc) replace custom tokens and regular expression lines by file content.

Installation

As dependency

npm install insert-file-content

As dev dependency

npm install insert-file-content -D

Use from cmd line or install globally

npm install insert-file-content -g

Configuration

configuration can be set via config file, cmd line paramaters or ifc.ifcSync function parameters or combination of any.

Configuration options

srcDir : Source directory path for files to be replaced. If relative path (starts with ./ or ../), its always relative to where ifc process is started. Defaults to "./src".

outDir : Output directory path for files to be output after replac. If relative path (starts with ./ or ../), its always relative to where ifc process is started. Defaults to "./out".

tokens: Tokens to be replaced with file content. All files inside srcDir are searched for tokens and replaced with respective file content. Result files are placed under outDir. Tokens are provided as name:value format as shown below.

  tokens: {
    'token': 'file path'
  }

Example: 

  tokens: {
    '<!-- header here -->': './templates/header.html',
    'version-number': './templates/version.txt',
    '<!-- body here -->': './body.html',
    '<!-- inner header here -->': '../others/inner-header.html',
    '<!-- footer here -->': '/usr/templates/footer.html'
  },

Each token is replaced with respective file content provided in path. If relative path (starts with ./ or ../), its always relative to where ifc process is started. If template file contains matched tokens, template content is replaced with file content recursively. Defaults to {}.

lineRegExp : If source file content line matches regular expression, line is replaced with file content matching regular expression. This helps to provide both token and file path inside source file. Defaults to "".

Example: lineRegExp: 'filename=(.*.html)'

In Below html source file, lines 7 and 10 matches above regular expression and gets replaced with file path returned by regular expression (.*.html). In below case, line 7 gets replaced with file contents "./templates/body.html" and line 10 gets replaced with contents of "./templates/others/footer.html"

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<!-- filename=./templates/body.html -->
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<!-- filename=./templates/others/footer.html -->
</body>
</html>

config : Path to config file. If you wish to set configuration options from file, path to config file to read from. Config file should set all configuration options in json format. Defaults to "./ifc.json".

fileEncoding : File encoding format to use when reading files. Defaults to "utf8".

cleanOutDir : Delete outDir if exist on start. If true, outDir is deleted on start and re-created to output replaced files. See outDir configuration options for more details. Defaults to false

Example configuration file.

{
  cleanOutDir: false,
  config: "./ifc.json",
  fileEncoding: "utf8",
  lineRegExp: "filename=(.*.html)",
  outDir: "./out",
  srcDir: "./src",
  tokens: {
    '<!-- header here -->': './templates/header.html',
    'version-number': './templates/version.txt',
    '<!-- body here -->': './body.html',
    '<!-- inner header here -->': '../others/inner-header.html',
    '<!-- footer here -->': '/usr/templates/footer.html'
  },
}

USAGE:

As node source

const ifc = require('insert-file-content');
ifc.ifcSync({
  srcDir: './input',
  outDir: './output',
  tokens: {
    '<!-- header here -->': './templates/header.html',
    '<!-- body here -->': './templates/body.html',
    'version-number': './templates/version.txt',
    '<!-- inner header here -->': './templates/others/inner-header.html',
    '<!-- footer here -->': './templates/footer.html'
  },
  lineRegExp: 'filename=(.*.html)',
  cleanOutDir: true,
  config: './config.json'
});

From command line

ifc -h
Usage: ifc [options]

Options:
  -v, --version              output the version number
  -c, --config <config>      Config file path
  -e, --encoding <encoding>  File encoding when reading files to replace
  -l, --line <line>          Line regular expression
  -o, --out <out>            Output directory where files are saved after insert
  -s, --src <src>            Source directory where files to be inserted
  -t, --tokens <tokens>      Tokens to replace
  -C, --clean [clean]        Delete out directory if already exist before inserting file content (default: false)
  -h, --help                 output usage information

Cmd Example:

ifc -c ./config.json -e utf-8 --line 'filename=(.*.html)' -o ./outdir -s ./srcdir -t '<!-- header here -->:./templates/header.html,<!-- body here -->:./templates/body.html'