Created
November 17, 2019 18:53
-
-
Save diaraujo13/af7c0ccf717fdb1bcaad05b282133a32 to your computer and use it in GitHub Desktop.
TODO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var cli = require('cli').enable('glob', 'version'), | |
watcher = require('chokidar'), | |
dust = require('..'), | |
fs = cli.native.fs, | |
path = cli.native.path, | |
eol = cli.native.os.EOL; | |
dust.config.whitespace = true; | |
dust.filters.toLowerCase = value => value.toLowerCase(); | |
dust.filters.capitalize = c => c.toLowerCase() && c.replace(c.charAt(0), c.charAt(0).toUpperCase()); | |
cli.setApp('dustc', require('../package.json').version); | |
cli.setUsage(cli.app + ' [options] [path1 [path2 path3...]]\n\n' + | |
' Compile all .dust files in a directory tree:\n ' + | |
cli.app + ' --output=compiled.js tmpl/**/*.dust\n' + | |
' Compile some files to AMD modules:\n ' + | |
cli.app + ' -as one.dust two.dust'); | |
cli.parse({ | |
name: ['n', 'name to use when rendering the template (multiple inputs automatically use their paths)', 'string'], | |
output: ['o', 'concatenate all output to this file', 'path'], | |
split: ['s', 'create one output file per input file instead of concatenating them to --output' ], | |
pwd: [false, 'generate template names starting from this directory', 'string'], | |
whitespace: ['w', 'preserve whitespace in templates', ], | |
amd: ['a', 'register templates as AMD modules' ], | |
cjs: [false, 'register templates as CommonJS modules (automatically turns on --split)' ], | |
watch: [false, 'watch files for changes and recompile' ] | |
}); | |
/** | |
* Handle input piped via stdin | |
*/ | |
if (!cli.argc) { | |
if (!cli.options.name) { | |
cli.info('No template name provided. The compiled template will be anonymous.'); | |
} | |
cli.info('No files to compile. Write template code and use ^D to exit'); | |
cli.withStdin(function(data) { | |
output(compile(data, cli.options.name), cli.options.output, cli.options.name); | |
}); | |
} | |
var paths = glob(cli.args), | |
streams; | |
/** | |
* Handle a list of paths passed as args | |
* Each path gets globbed in case the OS doesn't support it | |
*/ | |
function handle() { | |
streams = {}; | |
paths.forEach(function(inputFile, index, filesToProcess) { | |
read(inputFile, function(err, data) { | |
if (err) { | |
cli.info('Couldn\'t open ' + inputFile + ' for reading'); | |
return; | |
} | |
var outputFile = cli.options.output, | |
templateName = path.join(path.dirname(inputFile), | |
path.basename(inputFile, path.extname(inputFile))), | |
compiledData; | |
// If CommonJS is turned on, you have to compile one per file. | |
if (cli.options.cjs && filesToProcess.length > 1) { | |
if(!cli.options.split) { | |
cli.info('CommonJS modules are limited to one per file. Enabling --split'); | |
} | |
cli.options.split = true; | |
} | |
// Use the template's path as the output path if split-files is turned on | |
if (cli.options.split) { | |
outputFile = templateName + '.js'; | |
} | |
// Allow override of template name as long as there's only one template | |
if (cli.options.name && filesToProcess.length === 1) { | |
templateName = cli.options.name; | |
} | |
// Optionally strip leading directories from a template name | |
// For example, if --pwd=tmpl, `tmpl/foo/a` becomes `foo/a` | |
if (cli.options.pwd) { | |
templateName = path.relative(cli.options.pwd, templateName); | |
} | |
// Windows creates template names with \ so normalize to / to allow AMD loaders | |
// to correctly load templates, and so you don't have to write templates differently | |
// depending on what OS you use | |
templateName = templateName.replace(/\\/g, '/'); | |
compiledData = compile(data, templateName); | |
if (compiledData) { | |
output(compiledData, outputFile, templateName); | |
} | |
}); | |
}); | |
} | |
/** | |
* Turn on watching if --watch is enabled | |
* Watching runs the compilation again if any file changes or is deleted. | |
* However, adding new files means, for now, that you need to restart the watch. | |
*/ | |
if(cli.options.watch) { | |
cli.info('Watching for changes... ^C to quit'); | |
watcher = watcher | |
.watch(paths, { ignoreInitial: true }) | |
.on('unlink', function(file) { | |
paths.splice(paths.indexOf(file), 1); | |
watcher.unwatch(file); | |
}) | |
.on('all', function(method, file) { | |
cli.info(method + ' ' + file); | |
handle(); | |
}); | |
} else { | |
handle(); | |
} | |
/*** helper functions ***/ | |
function glob(globPaths) { | |
return globPaths.map(function(arg) { return cli.glob.sync(arg); }) | |
.reduce(function(a, b) { return a.concat(b); }, []); | |
} | |
function read(filename, cb) { | |
var data = '', | |
file = fs.createReadStream(filename); | |
file.on('error', cb); | |
file.on('data', function(chunk) { | |
data += chunk; | |
}); | |
file.on('end', function() { | |
cb(null, data); | |
}); | |
} | |
function compile(data, name) { | |
var compiled; | |
dust.config.whitespace = (cli.options.whitespace === true); | |
dust.config.amd = (cli.options.amd === true); | |
dust.config.cjs = (cli.options.cjs === true); | |
try { | |
compiled = dust.compile(data, name); | |
} catch(e) { | |
if (cli.options.watch) { | |
return cli.error('[' + name + '] ' + e); | |
} | |
return cli.fatal('[' + name + '] ' + e); | |
} | |
return compiled; | |
} | |
console.log('ok') | |
function output(data, file, templateName) { | |
var output_stream; | |
console.log('ok') | |
// Render compiled file | |
var props = { schemaName: 'tarefa', | |
schemaDisplayName: 'Tarefa', | |
schemaProps: | |
[ | |
{ name: 'title', | |
displayName: 'Título', | |
required: true, | |
typeName: 'string' }, | |
{ name: 'status', | |
displayName: 'Status', | |
default: '', | |
required: true, | |
typeName: 'picker', | |
typeValidOptions: ['Aberto','Processando','Finalizado','Abortado'] }, | |
{ name: 'prazo', | |
displayName: 'Prazo', | |
default: '', | |
required: false, | |
typeName: 'date' }, | |
{ name: 'descr', | |
displayName: 'Descrição', | |
default: '', | |
required: false, | |
typeName: 'textarea' } ] }; | |
dust.loadSource(data); | |
dust.render(templateName, props, function(err, out) { | |
try { | |
// `out` contains the rendered output. | |
if (file) { | |
output_stream = streams[file] ? streams[file] | |
: streams[file] = cli.native.fs.createWriteStream(file); | |
} else { | |
output_stream = process.stdout; | |
} | |
output_stream.write(out + eol); | |
} catch (e) { | |
cli.fatal('Could not write to output stream'); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment