var fs = require('fs'); var nodePath = require('path'); var cwd = process.cwd(); var resolveFrom = require('resolve-from'); // Try to use the Marko compiler installed with the project var markoCompilerPath; try { markoCompilerPath = resolveFrom(process.cwd(), 'marko/compiler'); } catch(e) {} var markoCompiler; if (markoCompilerPath) { markoCompiler = require(markoCompilerPath); } else { markoCompiler = require('../compiler'); } var Minimatch = require('minimatch').Minimatch; var appModulePath = require('app-module-path'); require('raptor-polyfill/string/startsWith'); require('raptor-polyfill/string/endsWith'); markoCompiler.defaultOptions.checkUpToDate = false; var mmOptions = { matchBase: true, dot: true, flipNegate: true }; function relPath(path) { if (path.startsWith(cwd)) { return path.substring(cwd.length+1); } } var args = require('raptor-args').createParser({ '--help': { type: 'boolean', description: 'Show this help message' }, '--files --file -f *': { type: 'string[]', description: 'A set of directories or files to compile' }, '--ignore -i': { type: 'string[]', description: 'An ignore rule (default: --ignore "/node_modules" ".*")' }, '--clean -c': { type: 'boolean', description: 'Clean all of the *.marko.js files' }, '--paths -p': { type: 'string[]', description: 'Additional directories to add to the Node.js module search path' } }) .usage('Usage: $0 [options]') .example('Compile a single template', '$0 template.marko') .example('Compile all templates in the current directory', '$0 .') .example('Compile multiple templates', '$0 template.marko src/ foo/') .example('Delete all *.marko.js files in the current directory', '$0 . --clean') .validate(function(result) { if (result.help) { this.printUsage(); process.exit(0); } if (!result.files || result.files.length === 0) { this.printUsage(); process.exit(1); } }) .onError(function(err) { this.printUsage(); if (err) { console.log(); console.log(err); } process.exit(1); }) .parse(); var paths = args.paths; if (paths && paths.length) { paths.forEach(function(path) { appModulePath.addPath(nodePath.resolve(cwd, path)); }); } var ignoreRules = args.ignore; if (!ignoreRules) { ignoreRules = ['/node_modules', '.*']; } ignoreRules = ignoreRules.filter(function (s) { s = s.trim(); return s && !s.match(/^#/); }); ignoreRules = ignoreRules.map(function (pattern) { return new Minimatch(pattern, mmOptions); }); function isIgnored(path, dir, stat) { if (path.startsWith(dir)) { path = path.substring(dir.length); } path = path.replace(/\\/g, '/'); var ignore = false; var ignoreRulesLength = ignoreRules.length; for (var i=0; i