0.0.4 • Published 5 years ago
@utft/tt v0.0.4
tt - text template
About
npm install @utft/tttt replace variables like $FOO ${FOO} and {{FOO}}
tt can be used pro by cli and programmatically
- If input is file tt place new file in output path
If input is dir tt replace variables recursive in files and subdirs and place them in output dir relatively by their path from input dir
cli tt has a format tt input output
Cli options
-e FOO=barand--env FOO=baradd variable--env-file /.envadd variables form file
Examples
Cli file
initial paths
/input/file/output/
file by path /input/file
Lorem $FOO ipsum ${FOO} dolor sit {{FOO}} ametcommand
npx @utft/tt -e FOO=bar /input/file /output/new/path/file
result paths
/input/file/output/new/path/file
file by path /output/new/path/file
Lorem bar ipsum bar dolor sit bar ametCli dir
initial paths
/input/file/input/subdir/file/output/
file by path /input/file
Lorem $FOO ipsum ${FOO} dolor sit {{FOO}} ametfile by path /input/subdir/file
Lorem $FOO ipsum ${FOO} dolor sit {{FOO}} ametcommand
npx @utft/tt -e FOO=bar /input /output
result paths
/input/file/input/subdir/file/output/file/output/subdir/file
file by path /output/file
Lorem bar ipsum bar dolor sit bar ametfile by path /output/subdir/file
Lorem bar ipsum bar dolor sit bar ametProgrammatically replace variables in files
import tt from '@utft/tt'
;(async () => {
const env = {FOO: 'bar'}
const replacedString = await tt.convertFiles('/path/to/input', 'path/to/output', env)
})()String prepare
tt can prepare string to insert variables, tt looks for variables only once when call tt.prepare(), further it only substitutes the variables
import tt from '@utft/tt'
import fs from 'fs'
const textToReplace = '$FOO 1 ${FOO} 2 {{FOO}}'
const preparedToInsert = tt.prepare(textToReplace)
preparedToInsert({FOO: 'bar'}) // 'bar 1 bar 2 bar'
preparedToInsert({FOO: 'ho'}) // 'ho 1 ho 2 ho'