UNPKG

mist

Version:
232 lines (191 loc) 4.83 kB
var MistParser, MistResolver, Mistfile, fs, os, path; (require('./utils')).install(); os = require('os'); fs = require('fs'); path = require('path'); MistResolver = require('./mist-resolver'); MistParser = require('./parser/mist-parser'); module.exports = Mistfile = (function() { function Mistfile(vars1) { this.vars = vars1 != null ? vars1 : Mistfile.defaultVars(); this.rules = []; } /* * Sets a parse variable * * name: * The name of the variable to set * val: * The new value */ Mistfile.prototype.set = function(name, val) {}; /* * Gets a parse variable's value * * name: * The name of the variable to get */ Mistfile.prototype.get = function(name) { throw 'not ready yet'; }; /* * Unsets a variable * * name: * The name of the variable to unset */ Mistfile.prototype.unset = function(name) {}; /* * Expands a string using the currently configured parse variables */ Mistfile.prototype.expand = function(str) { return str; }; /* * Adds a rule given inputs and outputs and a command * * rule: Object * command: * The command to run for the inputs/outputs * inputs: * The inputs to process * [{type:String, value:String}] * dependencies: * The dependencies to add * [{type:String, value:String}] * orderDependencies: * The order dependencies to add * [{type:String, value:String}] * outputs: * The outputs to add * auxOutputs: * The auxiliary outputs to add * groups: * The groups to feed to * foreach: * Whether or not to split each resolved input into its own target */ Mistfile.prototype.addRule = function(rule) { var ref; if (rule == null) { rule = {}; } if (rule.command == null) { throw 'rules must have a command'; } this.rules.push(ref = { src: {} }); ref.src.command = rule.command; ref.src.inputs = (rule.inputs || []).slice(0); ref.src.dependencies = (rule.dependencies || []).slice(0); ref.src.orderDependencies = (rule.orderDependencies || []).slice(0); ref.src.outputs = (rule.outputs || []).slice(0); ref.src.auxOutputs = (rule.auxOutputs || []).slice(0); ref.src.groups = (rule.groups || []).slice(0); return ref.src.foreach = !!rule.foreach; }; /* * Creates a new resolver for this mistfile. * * Should only be called on the root-most Mistfile. * * root: * Absolute path for this mistfile (the folder to resolve against) * resolver: * The resolver to use */ Mistfile.prototype.resolve = function(root, resolver) { if (resolver == null) { resolver = MistResolver; } return new resolver(root, this); }; return Mistfile; })(); /* * Returns the default variables for parsing * (i.e. OS_ and ENV_ vars) */ Mistfile.defaultVars = function() { var k, ref1, v, vars; vars = { OS_PLATFORM: os.platform(), OS_TYPE: os.type(), OS_ENDIANNESS: os.endianness(), OS_HOSTNAME: os.hostname(), OS_ARCH: os.arch(), OS_RELEASE: os.release(), OS_UPTIME: os.uptime(), OS_LOADAVG: os.loadavg(), OS_TMPDIR: os.tmpdir(), OS_TOTALMEM: os.totalmem(), OS_FREEMEM: os.freemem(), OS_CPUS: os.cpus(), OS_EOL: os.EOL }; ref1 = process.env; for (k in ref1) { v = ref1[k]; vars["ENV_" + k] = v; } return vars; }; /* * Looks for a mist file in the given directory or its parents * * from: * The path from which to look for a Mistfile (traverses upward) */ Mistfile.find = function(from) { var lastPath, mistPath, stat; if (from == null) { from = process.cwd(); } lastPath = ''; while (true) { lastPath = from; mistPath = path.join(from, 'Mistfile'); try { stat = fs.statSync(mistPath); if (stat.isFile()) { return mistPath; } } catch (_error) {} if ((from = path.dirname(from)) === lastPath) { break; } } return null; }; /* * Reads a file in and parses it as Mistfile syntax, returning a * new Mistfile object * * filename: * The path of the file to read * vars: * Initial parse variables to use when parsing */ Mistfile.fromFile = function(filename, vars) { return Mistfile.fromString(fs.readFileSync(filename), vars); }; /* * Parses a string as Mistfile syntax, returning a new Mistfile object * * str: * The string contents to parse * vars: * Initial parse variables to use when parsing */ Mistfile.fromString = function(str, vars) { var mist, options; mist = new Mistfile(vars); options = { mist: mist, vars: mist.vars }; MistParser(str.toString(), options); return options.mist; }; //# sourceMappingURL=mistfile.js.map