<commit_before>/*
* Map Reduce procedure for the traffic collection hourly
*/

var map_rescode_hourly=function () {
  var date = new Date();
  date.setTime(this._id.sample_time);
  var sample_hour = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), 0, 0, 0);
  emit({
    "sample_time": sample_hour,
    "pubservhost": this._id.pubservhost,
    "zone": this._id.zone
  }, {
    "qps": {},
    "counters": this.qps,
    "count": 1,
    "created_time": this.created_time
  });
};

var reduce_hourly=function (key, values) {
  var r = {
    "qps": {},
    "counters": {},
    "count": 0,
    "created_time": 0
  };
  values.forEach(function(v) {
    r.counters = hash_add(v.counters, r.counters);
    r.count++;
    r.created_time = v.created_time;
  });
  return r;
};

var finalize_hourly=function (key, value) {
  // for hourly we divide the counters by 12 (5 minutes per hour)
  var r = {
    "qps": hash_divide(value.counters, 12),
    "counters": value.counters,
    "count": value.count,
    "created_time": value.created_time
  };
  return r;
};

// pull the last sample_time from the DB
var last_processed_cur = db.mr_rescode_traffic_hourly_log.find({}, {
  last_processed_time: 1
}).sort({
  last_processed_time: -1
}).limit(1);

// this is where we store the mapreduce output
var mr_output;
var last_processed_time;

// if we have a previous set processed
if (last_processed_cur.hasNext()) {
  var last_processed = last_processed_cur.next();
  last_processed_time = last_processed.last_processed_time;

  print("Running mapreduce with: gt: " + last_processed_time + "\n");

  // Run mapReduce with the previous value
  /* mr_output = db.traffic.mapReduce(map_rescode_hourly, reduce_hourly, {
    query: {
      created_time: {
        $gt: last_processed_time
      }
    },
    out: {
      reduce: "rescode_traffic_hourly"
    },
    finalize: finalize_hourly
  });
  */

  mr_output=db.runCommand( { "mapreduce":"traffic",
  			      "map": map_rescode_hourly,
  			      "reduce": reduce_hourly,
  			      "query":{ "created_time": { $gt: last_processed_time }},
  			      "out": { reduce: "rescode_traffic_hourly" },
  			      "finalize":finalize_hourly
  			    });

} else {
  print("Running mapreduce for the first time!\n");

  // This is the first time running
 /* mr_output = db.traffic.mapReduce(map_rescode_hourly, reduce_hourly, {
    out: {
      reduce: "rescode_traffic_hourly"
    },
    finalize: finalize_hourly
  });
*/

mr_output=db.runCommand( { "mapreduce":"traffic",
 			      "map": map_rescode_hourly,
 			      "reduce": reduce_hourly,
 			      "out": "rescode_traffic_hourly",
 			      "finalize":finalize_hourly
 			    });

  // Create index
  db.rescode_traffic_hourly.ensureIndex({
    "_id.sample_time": 1,
    "_id.pubservhost": 1,
    "_id.zone": 1
  });

  // Index the created time field
  db.rescode_traffic_hourly.ensureIndex({
    created_time: 1
  });
}

if (mr_output.ok) {
  var last_processed_cur=db.rescode_traffic_hourly.find({},{created_time:1}).sort({created_time:-1}).limit(1);

  if(last_processed_cur.hasNext()){
    var lp = last_processed_cur.next();
    db.mr_rescode_traffic_hourly_log.insert({
      "last_processed_time": lp.created_time,
      "result": mr_output
    });
  }
}

<commit_msg>fixed created_time dilemma<commit_after>/*
* Map Reduce procedure for the traffic collection hourly
*/

var map_rescode_hourly=function () {
  var date = new Date();
  date.setTime(this._id.sample_time);
  var sample_hour = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), 0, 0, 0);
  emit({
    "sample_time": sample_hour,
    "pubservhost": this._id.pubservhost,
    "zone": this._id.zone
  }, {
    "qps": {},
    "counters": this.qps,
    "count": 1,
    "created_time": this.created_time
  });
};

var reduce_hourly=function (key, values) {
  var r = {
    "qps": {},
    "counters": {},
    "count": 0,
    "created_time": 0
  };
  values.forEach(function(v) {
    r.counters = hash_add(v.counters, r.counters);
    r.count++;
    r.created_time = v.created_time;
  });
  return r;
};

var finalize_hourly=function (key, value) {
  // for hourly we divide the counters by 12 (5 minutes per hour)
  var r = {
    "qps": hash_divide(value.counters, 12),
    "counters": value.counters,
    "count": value.count,
    "created_time": value.created_time
  };
  return r;
};

// pull the last sample_time from the DB
var last_processed_cur = db.mr_rescode_traffic_hourly_log.find({}, {
  last_processed_time: 1
}).sort({
  last_processed_time: -1
}).limit(1);

// this is where we store the mapreduce output
var mr_output;
var last_processed_time;

// if we have a previous set processed
if (last_processed_cur.hasNext()) {
  var last_processed = last_processed_cur.next();
  last_processed_time = last_processed.last_processed_time;

  print("Running mapreduce with: gt: " + last_processed_time + "\n");

  // Run mapReduce with the previous value
  /* mr_output = db.traffic.mapReduce(map_rescode_hourly, reduce_hourly, {
    query: {
      created_time: {
        $gt: last_processed_time
      }
    },
    out: {
      reduce: "rescode_traffic_hourly"
    },
    finalize: finalize_hourly
  });
  */

  mr_output=db.runCommand( { "mapreduce":"traffic",
  			      "map": map_rescode_hourly,
  			      "reduce": reduce_hourly,
  			      "query":{ "created_time": { $gt: last_processed_time }},
  			      "out": { reduce: "rescode_traffic_hourly" },
  			      "finalize":finalize_hourly
  			    });

} else {
  print("Running mapreduce for the first time!\n");

  // This is the first time running
 /* mr_output = db.traffic.mapReduce(map_rescode_hourly, reduce_hourly, {
    out: {
      reduce: "rescode_traffic_hourly"
    },
    finalize: finalize_hourly
  });
*/

mr_output=db.runCommand( { "mapreduce":"traffic",
 			      "map": map_rescode_hourly,
 			      "reduce": reduce_hourly,
 			      "out": "rescode_traffic_hourly",
 			      "finalize":finalize_hourly
 			    });

  // Create index
  db.rescode_traffic_hourly.ensureIndex({
    "_id.sample_time": 1,
    "_id.pubservhost": 1,
    "_id.zone": 1
  });

  // Index the created time field
  db.rescode_traffic_hourly.ensureIndex({
    created_time: 1
  });
}

if (mr_output.ok) {
  var last_processed_cur=db.rescode_traffic_hourly.find({}).sort({created_time:-1}).limit(1);

  if(last_processed_cur.hasNext()){
    var lp = last_processed_cur.next();
    db.mr_rescode_traffic_hourly_log.insert({
      "last_processed_time": lp.value.created_time,
      "result": mr_output
    });
  }
}

<|endoftext|>


<commit_before>/**
 * @file index.js
 * XSD Schema to HTML5 Web Form
 * @author George Bouris <gb@eworx.gr>
 * @copyright Copyright (C) 2017 EEA, Eworx, George Bouris. All rights reserved.
 */

'use strict';

// import heapdump from 'heapdump';  //debug library
import fs from 'fs';
import path from 'path';
import ncp from 'ncp';
import rimraf from 'rimraf';
import openurl from 'openurl';
import express from 'express';
import uglify from "uglify-js";
import uglifycss from "uglifycss";
import webshot from "webshot";
import XSDWebFormParser from './lib/xsdwebform.parser.js'
import XSDWebFormParserError from './lib/xsdwebform.parser.error.js'
import XSDWebFormParserTest from './lib/xsdwebform.parser.test.js'

/**
 * Class XSDWebForm
 * XSD Schema to HTML5
 */
export default class XSDWebForm {

	/**
	 * Class constructor
	 * Check for arguments
	 */
	constructor(args) {
		return new Promise ( (resolve, reject) => {

			//Version
			this.Vesrion="0.2";

			//logging
			this.showLog = true;
			this.verbose = true;
			this.showXSDLog = true;

			//Test Server
			this.serverPort =3001;

			//Build directory
			this.buildPath = "build/";
			// Input file variable
			var xsdFile = null;
			// HTML Input file variable
			var xmlHtmlFile = null;
			// Open the default browser when build is completed
			this.autoOpenOutput = false;

			// Check for [-f file] [-a | open browser after build] input
			args.forEach(
				(item, index) => {
					if (item === '-f') {
						xsdFile = args[index + 1];
						return;
					}
					if (item === '-ul') {
						this.showLog = false;
						this.verbose = false;
						return;
					}
					if (item === '-l') {
						this.showXSDLog = false;
						return;
					}
					if (item === '-a') {
						this.autoOpenOutput = true;
						return;
					}
				});

			// If not file input
			if (!xsdFile) {
				xsdFile = "./test/test";
			}

			// Lookup for base file name. Needed to check for (formname).form.xml file. Also, if file is named form.xsd then .js,.css filets etc are going to be named form.js form.css
			this.baseFileName = path.basename(xsdFile);
			this.basePath = path.dirname(xsdFile);
			xsdFile += ".xsd";

			xmlHtmlFile = this.baseFileName + ".form.xml";

			// XSDWebFormParser
			this.parser = new XSDWebFormParser(this.baseFileName, this.showLog, this.verbose, this.showXSDLog);

			// XSDWebFormTest
			this.tester = new XSDWebFormParserTest(this.serverPort, this.baseFileName, this.showLog, this.verbose);

			// Create (/clean) Build directory and move files
			this.prepareJSFiles().then((res) => {
				try {
					// After Build directory preperation, parse the document
					this.parseFiles(xsdFile, xmlHtmlFile, this.basePath).then ( (response) => {
						var app = express();
						var parent = this;
						var filePath = path.join( __dirname.substring(0, __dirname.length - 3), this.buildPath);

						// Start test web server in order to view the HTML5 file result.
						app.use(express.static(filePath))
							.listen(parent.serverPort, function () {
								if (parent.showLog) {
									console.log(`\x1b[1m\x1b[37mTest web server is listening on port ${parent.serverPort}\x1b[0m`);
									console.log(`http://localhost:${parent.serverPort}/${parent.baseFileName}.html\n\n`);
								}
								resolve(parent);
							});
						});
				} catch (err) {
					XSDWebFormParserError.reportError(err);
					reject(this);
				}
			});
		});
	};

	/**
	 * parseFiles - Parse XSD and XML files
	 * Open .xsd and .form.xml files and read the contents
	 * @param xsdFile
	 * @param xmlHtmlFile
	 */
	parseFiles(xsdFile, xmlHtmlFile, filePath) {
		return new Promise ( (resolve, reject) => {
			var xObject = {
				xfile: xsdFile,
				xdata: '',
				hfile: xmlHtmlFile,
				hdata: ''
			};

			this.getFile(xsdFile).then((res) => {
				xObject.xdata = res;
				this.getFile(filePath + "/" + xmlHtmlFile).then((res) => {
					xObject.hdata = res;
					// Parse file content
					this.parser.parse(xObject);

					// Create HTML file
					this.createFile(this.buildPath + this.baseFileName + ".html", this.getHeader() + this.parser.getHTMLOutput() + this.getFooter() ). then ( () => {
						this.getFile(__dirname + "/client/lng/ct-codelists-en.json").then((langs) => {
							langs = JSON.parse(langs);
							let langData = this.parser.getFullTextContent();
							langs.CTCodelists.Languages.item.forEach((item) => {
								this.createFile(this.buildPath + "lng/" + this.baseFileName + "." + item.code + ".lang.json", langData, false);
							});
							if (this.showLog) {
								this.createFile(this.buildPath + "log/" + this.baseFileName + ".log.html", this.parser.logger.getHtmlLog(), false);
								webshot(`http://localhost:${this.serverPort}/${this.baseFileName}.html`,  this.buildPath + "log/scrnsht.png", { shotSize : { width: 'all', height: 'all'} }, (res) => { return; });
							}
							this.tester.test().then ((res) => {
								let cres = "\x1b[32m ✓\x1b[1m ";
								let cresPlus = "";
								if (res.status !== 'PASS') {
									cres = "\x1b[31mx ";
									cresPlus += "\n\x1b[37m\x1b[1mErrors: \x1b[2m" + res.errors.map((item) => {
										return `\n\tLine: ${item.line}, Column: ${item.column}\n\t\x1b[31m\x1b[1m${item.message}\n\t\x1b[0m\x1b[37m${item.solution}\n\x1b[2m`
									}).join("");
									cresPlus += "\n\x1b[37m\x1b[1m\nPotential Problems: \x1b[2m" + res.potentialProblems.map((item) => {
										return `\n\tLine: ${item.line}, Column: ${item.column}\n\t\x1b[31m\x1b[1m${item.message}\n\t\x1b[0m\x1b[37m${item.source}\n\x1b[2m`
									}).join("");
								}
								console.log(`\x1b[1m\x1b[37mWCAG 2-AA Accessibility checking: \x1b[1m${cres}${res.status} ${cresPlus}\n\x1b[0m\x1b[37mcheck http://localhost:${this.serverPort}/${this.baseFileName}.html Form Testing Component for Onsite testing results\n\n`);
							});

							resolve();
						});
					});

					// Create XSLT output
					this.createFile(this.buildPath + "xslt/" + this.baseFileName + ".xslt", this.parser.getXSLTOutput());
					this.createFile(this.buildPath + "xslt/" + this.baseFileName + ".xml", this.parser.getXSLTXMLOutput(), false);

					// Open browser
					if (this.autoOpenOutput)
						openurl.open(`http://localhost:3001/${this.baseFileName}.html`);
				});
			});
		});
	}

	/**
	 * prepareJSFiles - Copy JS folder to build
	 */
	prepareJSFiles() {
		var parent = this;
		return new Promise((resolve, reject) => {
			rimraf(parent.buildPath, fs, function() {
				if (!fs.existsSync(parent.buildPath)) {
					fs.mkdirSync(parent.buildPath);
					fs.mkdirSync(parent.buildPath+"xslt");
					fs.mkdirSync(parent.buildPath+"log");
					ncp(__dirname + "/client/lng/", parent.buildPath + "lng", function(err) {
						if (err) {
							console.error(err);
							reject(err);
						}
					});
					ncp(__dirname + "/client/dev/", parent.buildPath + "dev", function(err) {
						parent.getFile(__dirname + "/client/dev/package.json").then ((data) => {
							parent.createFile(parent.buildPath  + "dev/package.json", data.replace(/\$FNM\$/g, parent.baseFileName) , false);
						});
						if (err) {
							console.error(err);
							reject(err);
						}
					});
					ncp(__dirname + "/client/assets/", parent.buildPath + "assets", function(err) {
						if (err) {
							console.error(err);
							reject(err);
						}
						ncp(__dirname + "/webform.js", parent.buildPath + parent.baseFileName + ".webform.js", function(err) {
							parent.getFile(__dirname + "/webform.js").then ((data) => {
								parent.createFile(parent.buildPath + parent.baseFileName + ".webform.min.js", uglify.minify(data).code, false);
							});
							if (err) {
								console.error(err);
								reject(err);
							}
							ncp(__dirname + "/webform.css", parent. buildPath + parent.baseFileName + ".webform.css", function(err) {
								let uglified = uglifycss.processFiles(
								[ __dirname + "/webform.css" ],
									{ maxLineLen: 500, expandVars: true }
								);
								parent.createFile(parent.buildPath + parent.baseFileName +  ".webform.min.css", uglified, false);
								if (err) {
									console.error(err);
									reject(err);
								}
							resolve();
							});
						});
					});
				}
			});
		});
	}

	/**
	 * setHeaer
	 * @param pageTitle
	 */

	getHeader() {

		return `
<!DOCTYPE html>

<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<meta name="Publisher" content="EEA, The European Environment Agency" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{('labels.pagetitle' || 'pagetitle') | translate}}</title>

<link rel="stylesheet" type="text/css" href="./assets/css/webform.all.min.css"/>
<link rel="stylesheet" type="text/css" href="./${this.baseFileName}.webform.min.css"/>
<link rel="stylesheet" type="text/css" href="http://www.eionet.europa.eu/styles/eionet2007/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="http://www.eionet.europa.eu/styles/eionet2007/handheld.css" media="handheld" />

<link rel="shortcut icon" type="image/x-icon" href="./assets/img/favicon.ico"/>

<eea-header></eea-header>

EEA

Web Form

	<eea-language scp="this" lang="{{selectedLanguage}}" langfile="${this.baseFileName}" chooselanguage="{{'chooselanguage' | translate}}"></eea-language>

#

{{'labels.formtitle' | translate}}

	<eea-form-testing scp="this"></eea-form-testing>
	`;
	}

	/**
	 * getFooter
	 */
	getFooter() {

		return `

<eea-toolbar scp="this" off="{{'off' | translate}}" on="{{'on' | translate}}" save="{{'save' | translate}}" printpreview="{{'printpreview' | translate}}" close="{{'close' | translate}}" validation="{{'validation' | translate}}"></eea-toolbar>

		<eea-form-build date="${new Date()}" user="${process.env.USER}"></eea-form-build>

`;
	}

	/**
	 * getFile
	 * @param file
	 */
	getFile(file) {
		return new Promise((resolve, reject) => {
			fs.readFile(file, 'utf8', (err, data) => {
				if (err) {
					XSDWebFormParserError.reportError(`No such file or directory ${err.path}\n`);
					reject(err);
				}
				resolve(data);
			});
		});
	}

	/**
	 * createFile - Save output file
	 * @param filename
	 * @param content
	 * @param log
	 */
	createFile(filename, content, log = true) {
		var parent = this;
		return new Promise((resolve, reject) => {
			fs.writeFile(filename, content, function(err) {
				if (err) {
					console.log(err);
					reject(err);
				} else {
					if (parent.showLog && log)
						console.log(`\x1b[2m\x1b[36mThe file ${filename} was saved\x1b[0m\n`);
					resolve();
				}
			});
		});
	}
}

var xsdWebForm = new XSDWebForm(process.argv);

module.exports.xsdWebForm = xsdWebForm;

// heapdump.writeSnapshot('./' + Date.now() + '.heapsnapshot');<commit_msg>Updated components<commit_after>/**
 * @file index.js
 * XSD Schema to HTML5 Web Form
 * @author George Bouris <gb@eworx.gr>
 * @copyright Copyright (C) 2017 EEA, Eworx, George Bouris. All rights reserved.
 */

'use strict';

// import heapdump from 'heapdump';  //debug library
import fs from 'fs';
import path from 'path';
import ncp from 'ncp';
import rimraf from 'rimraf';
import openurl from 'openurl';
import express from 'express';
import uglify from "uglify-js";
import uglifycss from "uglifycss";
import webshot from "webshot";
import XSDWebFormParser from './lib/xsdwebform.parser.js'
import XSDWebFormParserError from './lib/xsdwebform.parser.error.js'
import XSDWebFormParserTest from './lib/xsdwebform.parser.test.js'

/**
 * Class XSDWebForm
 * XSD Schema to HTML5
 */
export default class XSDWebForm {

	/**
	 * Class constructor
	 * Check for arguments
	 */
	constructor(args) {
		return new Promise ( (resolve, reject) => {

			//Version
			this.Vesrion="0.2";

			//logging
			this.showLog = true;
			this.verbose = true;
			this.showXSDLog = true;

			//Test Server
			this.serverPort =3001;

			//Build directory
			this.buildPath = "build/";
			// Input file variable
			var xsdFile = null;
			// HTML Input file variable
			var xmlHtmlFile = null;
			// Open the default browser when build is completed
			this.autoOpenOutput = false;

			// Check for [-f file] [-a | open browser after build] input
			args.forEach(
				(item, index) => {
					if (item === '-f') {
						xsdFile = args[index + 1];
						return;
					}
					if (item === '-ul') {
						this.showLog = false;
						this.verbose = false;
						return;
					}
					if (item === '-l') {
						this.showXSDLog = false;
						return;
					}
					if (item === '-a') {
						this.autoOpenOutput = true;
						return;
					}
				});

			// If not file input
			if (!xsdFile) {
				xsdFile = "./test/test";
			}

			// Lookup for base file name. Needed to check for (formname).form.xml file. Also, if file is named form.xsd then .js,.css filets etc are going to be named form.js form.css
			this.baseFileName = path.basename(xsdFile);
			this.basePath = path.dirname(xsdFile);
			xsdFile += ".xsd";

			xmlHtmlFile = this.baseFileName + ".form.xml";

			// XSDWebFormParser
			this.parser = new XSDWebFormParser(this.baseFileName, this.showLog, this.verbose, this.showXSDLog);

			// XSDWebFormTest
			this.tester = new XSDWebFormParserTest(this.serverPort, this.baseFileName, this.showLog, this.verbose);

			// Create (/clean) Build directory and move files
			this.prepareJSFiles().then((res) => {
				try {
					// After Build directory preperation, parse the document
					this.parseFiles(xsdFile, xmlHtmlFile, this.basePath).then ( (response) => {
						var app = express();
						var parent = this;
						var filePath = path.join( __dirname.substring(0, __dirname.length - 3), this.buildPath);

						// Start test web server in order to view the HTML5 file result.
						app.use(express.static(filePath))
							.listen(parent.serverPort, function () {
								if (parent.showLog) {
									console.log(`\x1b[1m\x1b[37mTest web server is listening on port ${parent.serverPort}\x1b[0m`);
									console.log(`http://localhost:${parent.serverPort}/${parent.baseFileName}.html\n\n`);
								}
								resolve(parent);
							});
						});
				} catch (err) {
					XSDWebFormParserError.reportError(err);
					reject(this);
				}
			});
		});
	};

	/**
	 * parseFiles - Parse XSD and XML files
	 * Open .xsd and .form.xml files and read the contents
	 * @param xsdFile
	 * @param xmlHtmlFile
	 */
	parseFiles(xsdFile, xmlHtmlFile, filePath) {
		return new Promise ( (resolve, reject) => {
			var xObject = {
				xfile: xsdFile,
				xdata: '',
				hfile: xmlHtmlFile,
				hdata: ''
			};

			this.getFile(xsdFile).then((res) => {
				xObject.xdata = res;
				this.getFile(filePath + "/" + xmlHtmlFile).then((res) => {
					xObject.hdata = res;
					// Parse file content
					this.parser.parse(xObject);

					// Create HTML file
					this.createFile(this.buildPath + this.baseFileName + ".html", this.getHeader() + this.parser.getHTMLOutput() + this.getFooter() ). then ( () => {
						this.getFile(__dirname + "/client/lng/ct-codelists-en.json").then((langs) => {
							langs = JSON.parse(langs);
							let langData = this.parser.getFullTextContent();
							langs.CTCodelists.Languages.item.forEach((item) => {
								this.createFile(this.buildPath + "lng/" + this.baseFileName + "." + item.code + ".lang.json", langData, false);
							});
							if (this.showLog) {
								this.createFile(this.buildPath + "log/" + this.baseFileName + ".log.html", this.parser.logger.getHtmlLog(), false);
								webshot(`http://localhost:${this.serverPort}/${this.baseFileName}.html`,  this.buildPath + "log/scrnsht.png", { shotSize : { width: 'all', height: 'all'} }, (res) => { return; });
							}
							this.tester.test().then ((res) => {
								let cres = "\x1b[32m ✓\x1b[1m ";
								let cresPlus = "";
								if (res.status !== 'PASS') {
									cres = "\x1b[31mx ";
									cresPlus += "\n\x1b[37m\x1b[1mErrors: \x1b[2m" + res.errors.map((item) => {
										return `\n\tLine: ${item.line}, Column: ${item.column}\n\t\x1b[31m\x1b[1m${item.message}\n\t\x1b[0m\x1b[37m${item.solution}\n\x1b[2m`
									}).join("");
									cresPlus += "\n\x1b[37m\x1b[1m\nPotential Problems: \x1b[2m" + res.potentialProblems.map((item) => {
										return `\n\tLine: ${item.line}, Column: ${item.column}\n\t\x1b[31m\x1b[1m${item.message}\n\t\x1b[0m\x1b[37m${item.source}\n\x1b[2m`
									}).join("");
								}
								console.log(`\x1b[1m\x1b[37mWCAG 2-AA Accessibility checking: \x1b[1m${cres}${res.status} ${cresPlus}\n\x1b[0m\x1b[37mcheck http://localhost:${this.serverPort}/${this.baseFileName}.html Form Testing Component for Onsite testing results\n\n`);
							});

							resolve();
						});
					});

					// Create XSLT output
					this.createFile(this.buildPath + "xslt/" + this.baseFileName + ".xslt", this.parser.getXSLTOutput());
					this.createFile(this.buildPath + "xslt/" + this.baseFileName + ".xml", this.parser.getXSLTXMLOutput(), false);

					// Open browser
					if (this.autoOpenOutput)
						openurl.open(`http://localhost:3001/${this.baseFileName}.html`);
				});
			});
		});
	}

	/**
	 * prepareJSFiles - Copy JS folder to build
	 */
	prepareJSFiles() {
		var parent = this;
		return new Promise((resolve, reject) => {
			rimraf(parent.buildPath, fs, function() {
				if (!fs.existsSync(parent.buildPath)) {
					fs.mkdirSync(parent.buildPath);
					fs.mkdirSync(parent.buildPath+"xslt");
					fs.mkdirSync(parent.buildPath+"log");
					ncp(__dirname + "/client/lng/", parent.buildPath + "lng", function(err) {
						if (err) {
							console.error(err);
							reject(err);
						}
					});
					ncp(__dirname + "/client/dev/", parent.buildPath + "dev", function(err) {
						parent.getFile(__dirname + "/client/dev/package.json").then ((data) => {
							parent.createFile(parent.buildPath  + "dev/package.json", data.replace(/\$FNM\$/g, parent.baseFileName) , false);
						});
						if (err) {
							console.error(err);
							reject(err);
						}
					});
					ncp(__dirname + "/client/assets/", parent.buildPath + "assets", function(err) {
						if (err) {
							console.error(err);
							reject(err);
						}
						ncp(__dirname + "/webform.js", parent.buildPath + parent.baseFileName + ".webform.js", function(err) {
							parent.getFile(__dirname + "/webform.js").then ((data) => {
								parent.createFile(parent.buildPath + parent.baseFileName + ".webform.min.js", uglify.minify(data).code, false);
							});
							if (err) {
								console.error(err);
								reject(err);
							}
							ncp(__dirname + "/webform.css", parent. buildPath + parent.baseFileName + ".webform.css", function(err) {
								let uglified = uglifycss.processFiles(
								[ __dirname + "/webform.css" ],
									{ maxLineLen: 500, expandVars: true }
								);
								parent.createFile(parent.buildPath + parent.baseFileName +  ".webform.min.css", uglified, false);
								if (err) {
									console.error(err);
									reject(err);
								}
							resolve();
							});
						});
					});
				}
			});
		});
	}

	/**
	 * setHeaer
	 * @param pageTitle
	 */

	getHeader() {

		return `
<!DOCTYPE html>

<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<meta name="Publisher" content="EEA, The European Environment Agency" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{('labels.pagetitle' || 'pagetitle') | translate}}</title>

<link rel="stylesheet" type="text/css" href="./assets/css/webform.all.min.css"/>
<link rel="stylesheet" type="text/css" href="./${this.baseFileName}.webform.min.css"/>
<link rel="stylesheet" type="text/css" href="http://www.eionet.europa.eu/styles/eionet2007/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="http://www.eionet.europa.eu/styles/eionet2007/handheld.css" media="handheld" />

<link rel="shortcut icon" type="image/x-icon" href="./assets/img/favicon.ico"/>

<eea-header></eea-header>

EEA

Web Form

	<eea-language scp="this" lang="{{selectedLanguage}}" langfile="${this.baseFileName}" chooselanguage="{{'chooselanguage' | translate}}"></eea-language>

#

{{'labels.formtitle' | translate}}

	<eea-form-testing scp="this"></eea-form-testing>
	`;
	}

	/**
	 * getFooter
	 */
	getFooter() {

		return `

<eea-toolbar scp="this" off="{{'off' | translate}}" on="{{'on' | translate}}" save="{{'save' | translate}}" printpreview="{{'printpreview' | translate}}" close="{{'close' | translate}}" validation="{{'validation' | translate}}"></eea-toolbar>

		<eea-form-build date="${new Date()}" user="${process.env.USER}"></eea-form-build>

`;
	}

	/**
	 * getFile
	 * @param file
	 */
	getFile(file) {
		return new Promise((resolve, reject) => {
			fs.readFile(file, 'utf8', (err, data) => {
				if (err) {
					XSDWebFormParserError.reportError(`No such file or directory ${err.path}\n`);
					reject(err);
				}
				resolve(data);
			});
		});
	}

	/**
	 * createFile - Save output file
	 * @param filename
	 * @param content
	 * @param log
	 */
	createFile(filename, content, log = true) {
		var parent = this;
		return new Promise((resolve, reject) => {
			fs.writeFile(filename, content, function(err) {
				if (err) {
					console.log(err);
					reject(err);
				} else {
					if (parent.showLog && log)
						console.log(`\x1b[2m\x1b[36mThe file ${filename} was saved\x1b[0m\n`);
					resolve();
				}
			});
		});
	}
}

var xsdWebForm = new XSDWebForm(process.argv);

module.exports.xsdWebForm = xsdWebForm;

// heapdump.writeSnapshot('./' + Date.now() + '.heapsnapshot');<|endoftext|>


<commit_before>'use strict'

const num_instances = process.argv[2] || 42;
const instances = [];
const events = [];
const instances_to_connect = {};
const domain = '<http://doma.in/_i/';
const iri_end = '>';
const onEnd = domain + 'INSTANCE/onEnd>';
const onError = domain + 'INSTANCE/onError>';

let instance;
let instance_iri;
let step;
let num_events;
let event;
let event_iri;
let current_handler;
let eStep
let num_handler;
let next_handler;
let handler_type;
let target;
let hStep;
let target_instance;
let dependency_target;
let dependency_instance;

// TODO create ids, then the triples
// TODO select onError and onEnd from cached events

// module descriptor
process.stdout.write(
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#data> <http://schema.org/name> "\\"data\\"" .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#data> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/DataHandler> .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#stream> <http://schema.org/name> "\\"stream\\"" .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#stream> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/StreamHandler> .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://dbpedia.org/property/exports> <https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#data> .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://dbpedia.org/property/exports> <https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#stream> .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://schema.org/author> "\\"Adrian Ottiker <adrian@ottiker.com>\\"" .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://schema.org/codeRepository> <git+https://github.com/adioo/flow-dummy.git> .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://schema.org/description> "\\"Flow dummy module and data generator.\\"" .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://schema.org/name> "\\"flow-dummy\\"" .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://schema.org/softwareVersion> "\\"0.0.1\\"" .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/SoftwareSourceCode> .\n'
);

for (step = 0; num_instances > step; ++step) {

    // instance
    instance = UID(8);
    instance_iri = domain + instance + iri_end;
    instances.push(instance_iri);
    process.stdout.write(createInstance(instance_iri, instance));
    process.stdout.write(createArguments(instance_iri, '_:' + UID(8)));

    instances_to_connect[instance_iri] = {};

    // event
    num_events = random(1, 23);
    for (eStep = 0; num_events > eStep; ++eStep) {
        event = UID(8);
        event_iri = domain + instance + '/' + event + iri_end;
        events.push(event_iri);
        current_handler = '_:' + UID(8);
        process.stdout.write(createEvent(instance_iri, event_iri, event, current_handler));

        // handler
        num_handler = random(1, 23);
        for (hStep = 0; num_handler > hStep; ++hStep) {

            next_handler = hStep === (num_handler - 1) ? null : '_:' + UID(8);
            handler_type = random(0, 2);

            // select random event
            if (handler_type > 1) {
                target = events[random(0, events.length - 1)];

                target_instance = target.substring(0, target.lastIndexOf('/')) + '>';
                if (target_instance !== instance_iri) {
                    instances_to_connect[instance_iri][target_instance] = 1;
                }

                process.stdout.write(createEventHandler(target, current_handler, next_handler, event_iri));

            // select random instance
            } else {
                target = instances[random(0, instances.length - 1)];

                if (target !== instance_iri) {
                    instances_to_connect[instance_iri][target] = 1;
                }

                if (handler_type === 0) {
                    process.stdout.write(createDataHandler(target, current_handler, next_handler, event_iri));
                } else {
                    process.stdout.write(createStreamHandler(target, current_handler, next_handler, event_iri));
                }
            }

            // generate handler arguments
            process.stdout.write(createArguments(current_handler, '_:' + UID(8)));

            current_handler = next_handler;
        }
    }
}

for (dependency_instance in instances_to_connect) {
    for (dependency_target in instances_to_connect[dependency_instance]) {
        process.stdout.write(dependency_instance + ' <http://schema.jillix.net/vocab/dependency> ' + dependency_target + ' .\n');
    }
}

function UID (len) {
    len = len || 23;
    for (var i = 0, random = ''; i < len; ++i) {
        random += '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'[0 | Math.random() * 62];
    }
    return random;
};

function random (min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// create basic instance triples
function createInstance (iri, name) {
    let s = iri + ' <http://schema.org/name> "\\"' + name + '\\"" .\n' +
            iri + ' <http://schema.jillix.net/vocab/roles> "\\"*\\"" .\n' +
            iri + ' <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/ModuleInstanceConfig> .\n' +
            iri + ' <http://schema.jillix.net/vocab/module> <https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> .\n';

    return s;
};

function createEvent (inst, iri, name, next_handler) {
    let s = inst + ' <http://schema.jillix.net/vocab/event> ' + iri + ' .\n' +
            iri + ' <http://schema.jillix.net/vocab/onError> ' + onError + ' .\n' +
            iri + ' <http://schema.jillix.net/vocab/onEnd> ' + onEnd + ' .\n' +
            iri + ' <http://schema.org/name> "\\"' + name + '\\"" .\n' +
            iri + ' <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/FlowEvent> .\n' +
            iri + ' <http://schema.jillix.net/vocab/sequence> ' + next_handler + ' .\n';

    return s;
};

function createEventHandler(event, handler, next_handler, event_ref) {
    let s = handler + ' <http://schema.jillix.net/vocab/emit> ' + event + ' .\n' +
            handler + ' <http://schema.jillix.net/vocab/event> ' + event_ref + ' .\n' +
            handler + ' <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/EventEmit> .\n';

    if (next_handler) {
        s += handler + ' <http://schema.jillix.net/vocab/sequence> ' + next_handler + ' .\n';
    }

    return s;
};

function createDataHandler(instance, handler, next_handler, event_ref) {
    let s = handler + ' <http://schema.jillix.net/vocab/instance> ' + instance + ' .\n' +
            handler + ' <http://schema.jillix.net/vocab/event> ' + event_ref + ' .\n' +
            handler + ' <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/DataHandler> .\n' +
            handler + ' <http://schema.jillix.net/vocab/dataHandler> ' +
            '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#data> .\n';

    if (next_handler) {
        s += handler + ' <http://schema.jillix.net/vocab/sequence> ' + next_handler + ' .\n';
    }

    return s;
};

function createStreamHandler(instance, handler, next_handler, event_ref) {
    let s = handler + ' <http://schema.jillix.net/vocab/instance> ' + instance + ' .\n' +
            handler + ' <http://schema.jillix.net/vocab/event> ' + event_ref + ' .\n' +
            handler + ' <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/StreamHandler> .\n' +
            handler + ' <http://schema.jillix.net/vocab/streamHandler> ' +
            '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#stream> .\n';

    if (next_handler) {
        s += handler + ' <http://schema.jillix.net/vocab/sequence> ' + next_handler + ' .\n';
    }

    return s;
};

function createArguments (subject, args) {
    let s = subject + ' <http://schema.jillix.net/vocab/args> ' + args + ' .\n' +
            args + ' <http://schema.org/name> "\\"Name\\"" .\n';

    return s;
};
<commit_msg>make instance, event and handler count configurable with cli args<commit_after>'use strict'

const settings = {
    instances: process.argv[2] || 42,
    events: [process.argv[3] || 1, process.argv[4] || 5],
    handlers: [process.argv[5] || 1, process.argv[6] || 5]
};
const instances = [];
const events = [];
const instances_to_connect = {};
const domain = '<http://doma.in/_i/';
const iri_end = '>';
const onEnd = domain + 'INSTANCE/onEnd>';
const onError = domain + 'INSTANCE/onError>';

let instance;
let instance_iri;
let step;
let num_events;
let event;
let event_iri;
let current_handler;
let eStep
let num_handler;
let next_handler;
let handler_type;
let target;
let hStep;
let target_instance;
let dependency_target;
let dependency_instance;

// TODO create ids, then the triples
// TODO select onError and onEnd from cached events

// module descriptor
process.stdout.write(
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#data> <http://schema.org/name> "\\"data\\"" .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#data> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/DataHandler> .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#stream> <http://schema.org/name> "\\"stream\\"" .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#stream> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/StreamHandler> .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://dbpedia.org/property/exports> <https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#data> .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://dbpedia.org/property/exports> <https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#stream> .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://schema.org/author> "\\"Adrian Ottiker <adrian@ottiker.com>\\"" .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://schema.org/codeRepository> <git+https://github.com/adioo/flow-dummy.git> .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://schema.org/description> "\\"Flow dummy module and data generator.\\"" .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://schema.org/name> "\\"flow-dummy\\"" .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://schema.org/softwareVersion> "\\"0.0.1\\"" .\n' +
    '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/SoftwareSourceCode> .\n'
);

for (step = 0; settings.instances > step; ++step) {

    // instance
    instance = UID(8);
    instance_iri = domain + instance + iri_end;
    instances.push(instance_iri);
    process.stdout.write(createInstance(instance_iri, instance));
    process.stdout.write(createArguments(instance_iri, '_:' + UID(8)));

    instances_to_connect[instance_iri] = {};

    // event
    num_events = random(settings.events[0], settings.events[1]);
    for (eStep = 0; num_events > eStep; ++eStep) {
        event = UID(8);
        event_iri = domain + instance + '/' + event + iri_end;
        events.push(event_iri);
        current_handler = '_:' + UID(8);
        process.stdout.write(createEvent(instance_iri, event_iri, event, current_handler));

        // handler
        num_handler = random(settings.handlers[0], settings.handlers[1]);
        for (hStep = 0; num_handler > hStep; ++hStep) {

            next_handler = hStep === (num_handler - 1) ? null : '_:' + UID(8);
            handler_type = random(0, 2);

            // select random event
            if (handler_type > 1) {
                target = events[random(0, events.length - 1)];

                target_instance = target.substring(0, target.lastIndexOf('/')) + '>';
                if (target_instance !== instance_iri) {
                    instances_to_connect[instance_iri][target_instance] = 1;
                }

                process.stdout.write(createEventHandler(target, current_handler, next_handler, event_iri));

            // select random instance
            } else {
                target = instances[random(0, instances.length - 1)];

                if (target !== instance_iri) {
                    instances_to_connect[instance_iri][target] = 1;
                }

                if (handler_type === 0) {
                    process.stdout.write(createDataHandler(target, current_handler, next_handler, event_iri));
                } else {
                    process.stdout.write(createStreamHandler(target, current_handler, next_handler, event_iri));
                }
            }

            // generate handler arguments
            process.stdout.write(createArguments(current_handler, '_:' + UID(8)));

            current_handler = next_handler;
        }
    }
}

for (dependency_instance in instances_to_connect) {
    for (dependency_target in instances_to_connect[dependency_instance]) {
        process.stdout.write(dependency_instance + ' <http://schema.jillix.net/vocab/dependency> ' + dependency_target + ' .\n');
    }
}

function UID (len) {
    len = len || 23;
    for (var i = 0, random = ''; i < len; ++i) {
        random += '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'[0 | Math.random() * 62];
    }
    return random;
};

function random (min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// create basic instance triples
function createInstance (iri, name) {
    let s = iri + ' <http://schema.org/name> "\\"' + name + '\\"" .\n' +
            iri + ' <http://schema.jillix.net/vocab/roles> "\\"*\\"" .\n' +
            iri + ' <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/ModuleInstanceConfig> .\n' +
            iri + ' <http://schema.jillix.net/vocab/module> <https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json> .\n';

    return s;
};

function createEvent (inst, iri, name, next_handler) {
    let s = inst + ' <http://schema.jillix.net/vocab/event> ' + iri + ' .\n' +
            iri + ' <http://schema.jillix.net/vocab/onError> ' + onError + ' .\n' +
            iri + ' <http://schema.jillix.net/vocab/onEnd> ' + onEnd + ' .\n' +
            iri + ' <http://schema.org/name> "\\"' + name + '\\"" .\n' +
            iri + ' <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/FlowEvent> .\n' +
            iri + ' <http://schema.jillix.net/vocab/sequence> ' + next_handler + ' .\n';

    return s;
};

function createEventHandler(event, handler, next_handler, event_ref) {
    let s = handler + ' <http://schema.jillix.net/vocab/emit> ' + event + ' .\n' +
            handler + ' <http://schema.jillix.net/vocab/event> ' + event_ref + ' .\n' +
            handler + ' <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/EventEmit> .\n';

    if (next_handler) {
        s += handler + ' <http://schema.jillix.net/vocab/sequence> ' + next_handler + ' .\n';
    }

    return s;
};

function createDataHandler(instance, handler, next_handler, event_ref) {
    let s = handler + ' <http://schema.jillix.net/vocab/instance> ' + instance + ' .\n' +
            handler + ' <http://schema.jillix.net/vocab/event> ' + event_ref + ' .\n' +
            handler + ' <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/DataHandler> .\n' +
            handler + ' <http://schema.jillix.net/vocab/dataHandler> ' +
            '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#data> .\n';

    if (next_handler) {
        s += handler + ' <http://schema.jillix.net/vocab/sequence> ' + next_handler + ' .\n';
    }

    return s;
};

function createStreamHandler(instance, handler, next_handler, event_ref) {
    let s = handler + ' <http://schema.jillix.net/vocab/instance> ' + instance + ' .\n' +
            handler + ' <http://schema.jillix.net/vocab/event> ' + event_ref + ' .\n' +
            handler + ' <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.jillix.net/vocab/StreamHandler> .\n' +
            handler + ' <http://schema.jillix.net/vocab/streamHandler> ' +
            '<https://raw.githubusercontent.com/adioo/flow-dummy/master/module.json#stream> .\n';

    if (next_handler) {
        s += handler + ' <http://schema.jillix.net/vocab/sequence> ' + next_handler + ' .\n';
    }

    return s;
};

function createArguments (subject, args) {
    let s = subject + ' <http://schema.jillix.net/vocab/args> ' + args + ' .\n' +
            args + ' <http://schema.org/name> "\\"Name\\"" .\n';

    return s;
};
<|endoftext|>


<commit_before>import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import dive from 'diveSync';
import mkdirp from 'mkdirp';
import trash from 'trash';
import { isDir, read, write } from '../../utils';
import template from 'lodash-node/modern/string/template';

export function generate(options) {
  let templateFiles = path.join(options.src, 'files');
  let blueprint = blueprintFor(options.src);
  let data = blueprint.locals(options.args);

  dive(templateFiles, { all: true }, (err, absolutepath) => {
    console.log(absolutepath);
    if (isDir(absolutepath)) { return; }
    let relativepath = path.relative(templateFiles, absolutepath);

    let filenameTemplate = template(relativepath, { interpolate: /__([\S]+)__/g });
    let destRelativepath = filenameTemplate(data).replace('gitkeep', '.gitkeep');
    let destAbsolutepath = path.join(options.dest, destRelativepath);

    if (fs.existsSync(destAbsolutepath)) {
      console.log(`  ${ chalk.green('already exists') } ${ destRelativepath}`);
      return;
    }

    let contents = read(absolutepath);
    let contentsTemplate = template(contents);
    mkdirp.sync(path.dirname(destAbsolutepath));
    write(destAbsolutepath, contentsTemplate(data));
    console.log(`  ${ chalk.green('create') } ${ destRelativepath}`);
  });

  runWithCwd(options.dest, blueprint.postInstall, data, options);
}

export function destroy(options) {
  let templateFiles = path.join(options.src, 'files');
  let blueprint = blueprintFor(options.src);
  let data = blueprint.locals(options.args);

  let filesToDelete = [];
  dive(templateFiles, { all: true }, (err, absolutepath) => filesToDelete.push(absolutepath));

  // Get the absolute paths for the template source file and the dest file
  filesToDelete = filesToDelete.map((relativepath) => {
    let absolutepath = path.join(templateFiles, relativepath);
    let filenameTemplate = template(relativepath, { interpolate: /__([\S]+)__/g });
    let destRelativepath = filenameTemplate(data);
    let destAbsolutepath = path.join(options.dest, destRelativepath);
    return { destAbsolutepath, destRelativepath, absolutepath };

  // Ensure that the dest file actually exists
  }).filter(({ destAbsolutepath, destRelativepath, absolutepath }) => {
    if (isDir(absolutepath)) { return false; }
    let fileExists = fs.existsSync(destAbsolutepath);
    if (!fileExists) {
      console.log(`  ${ chalk.grey('missing') } ${ destRelativepath }`);
    }
    return fileExists;

  // And either hasn't been altered, or the force option is being used, to
  // ensure we don't destroy code
  }).filter(({ destAbsolutepath, absolutepath, destRelativepath }) => {
    let templateSrc = read(absolutepath);
    let compiled = template(templateSrc);
    let destFileIsNotDirty = read(destAbsolutepath) === compiled(data);

    if (destFileIsNotDirty) {
      console.log(`  ${ chalk.red('destroy') } ${ destRelativepath }`);
    } else {
      console.log(`  ${ chalk.blue('skipped') } ${ destRelativepath }`);
    }

    return destFileIsNotDirty;
  }).map(({ destAbsolutepath }) => {
    return destAbsolutepath;
  });

  trash(filesToDelete, () => {
    runWithCwd(options.dest, blueprint.postUninstall, data, options);
  });
}

function blueprintFor(srcpath) {
  let blueprint = require(path.join(srcpath, 'index.js'));
  blueprint.locals = blueprint.locals || function() { return {}; };
  blueprint.postInstall = blueprint.postInstall || function() {};
  blueprint.postUninstall = blueprint.postInstall || function() {};
  return blueprint;
}

function runWithCwd(cwd, fn, ...args) {
  let originalCwd = process.cwd();
  process.chdir(cwd);
  fn(...args);
  process.chdir(originalCwd);
}
<commit_msg>fix a few missed refactor items in blueprint task<commit_after>import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import dive from 'diveSync';
import mkdirp from 'mkdirp';
import trash from 'trash';
import { isDir, read, write } from '../../utils';
import template from 'lodash-node/modern/string/template';

export function generate(options) {
  let templateFiles = path.join(options.src, 'files');
  let blueprint = blueprintFor(options.src);
  let data = blueprint.locals(options.args);

  dive(templateFiles, { all: true }, (err, absolutepath) => {
    if (isDir(absolutepath)) { return; }
    let relativepath = path.relative(templateFiles, absolutepath);

    let filenameTemplate = template(relativepath, { interpolate: /__([\S]+)__/g });
    let destRelativepath = filenameTemplate(data);
    let destAbsolutepath = path.join(options.dest, destRelativepath);

    if (fs.existsSync(destAbsolutepath)) {
      console.log(`  ${ chalk.green('already exists') } ${ destRelativepath}`);
      return;
    }

    let contents = read(absolutepath);
    let contentsTemplate = template(contents);
    mkdirp.sync(path.dirname(destAbsolutepath));
    write(destAbsolutepath, contentsTemplate(data));
    console.log(`  ${ chalk.green('create') } ${ destRelativepath}`);
  });

  runWithCwd(options.dest, blueprint.postInstall, data, options);
}

export function destroy(options) {
  let templateFiles = path.join(options.src, 'files');
  let blueprint = blueprintFor(options.src);
  let data = blueprint.locals(options.args);

  let filesToDelete = [];
  dive(templateFiles, { all: true }, (err, absolutepath) => filesToDelete.push(absolutepath));

  // Get the absolute paths for the template source file and the dest file
  filesToDelete = filesToDelete.map((absolutepath) => {
    let relativepath = path.relative(templateFiles, absolutepath);
    let filenameTemplate = template(relativepath, { interpolate: /__([\S]+)__/g });
    let destRelativepath = filenameTemplate(data);
    let destAbsolutepath = path.join(options.dest, destRelativepath);
    return { destAbsolutepath, destRelativepath, absolutepath };

  // Ensure that the dest file actually exists
  }).filter(({ destAbsolutepath, destRelativepath, absolutepath }) => {
    if (isDir(absolutepath)) { return false; }
    let fileExists = fs.existsSync(destAbsolutepath);
    if (!fileExists) {
      console.log(`  ${ chalk.grey('missing') } ${ destRelativepath }`);
    }
    return fileExists;

  // And either hasn't been altered, or the force option is being used, to
  // ensure we don't destroy code
  }).filter(({ destAbsolutepath, absolutepath, destRelativepath }) => {
    let templateSrc = read(absolutepath);
    let compiled = template(templateSrc);
    let destFileIsNotDirty = read(destAbsolutepath) === compiled(data);

    if (destFileIsNotDirty) {
      console.log(`  ${ chalk.red('destroy') } ${ destRelativepath }`);
    } else {
      console.log(`  ${ chalk.blue('skipped') } ${ destRelativepath }`);
    }

    return destFileIsNotDirty;
  }).map(({ destAbsolutepath }) => {
    return destAbsolutepath;
  });

  trash(filesToDelete, () => {
    runWithCwd(options.dest, blueprint.postUninstall, data, options);
  });
}

function blueprintFor(srcpath) {
  let blueprint = require(path.join(srcpath, 'index.js'));
  blueprint.locals = blueprint.locals || function() { return {}; };
  blueprint.postInstall = blueprint.postInstall || function() {};
  blueprint.postUninstall = blueprint.postInstall || function() {};
  return blueprint;
}

function runWithCwd(cwd, fn, ...args) {
  let originalCwd = process.cwd();
  process.chdir(cwd);
  fn(...args);
  process.chdir(originalCwd);
}
<|endoftext|>


<commit_before>/*
 * Copyright (c) 2012 Peter Kuma

 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

var FileShack = new Class({
    Implements: Options,

    options: {
        updateInterval: 2000
    },

    initialize: function(options) {
        var this_ = this;

        this.setOptions(options);
        this.lastUpdate = undefined;

        this.items = new Collection();
        this.items.addEvent('add', function(view) {
            $('list').insertBefore(view.el, $('list').firstChild);
        });

        // Bootstrap items from JSON embedded in index.html.
        this.bootstrap($('bootstrap').get('text'))

        var dropbox = $('dropbox');
        var dropboxInput = $('dropbox-input');
        var iframe = $('iframe');

        // Drag & Drop.
        if (typeof dropbox.addEventListener != 'undefined' &&
            typeof dropbox.ondrop != 'undefined' &&
            typeof File != 'undefined')
        {
            // Register Drag & Drop events.
            dropbox.addEventListener("dragenter", function(e) { e.preventDefault(); }, false);
            dropbox.addEventListener("dragover", function(e) { e.preventDefault(); }, false);
            dropbox.addEventListener("drop", function(e) {
                e.preventDefault();
                if (e.dataTransfer.files)
                    Array.each(e.dataTransfer.files, function(f) { this_.upload(f); });
            }, false);
        } else {
            // Switch dropbox text, drag & drop is not supported.
            $('dropbox-text').setStyle('display', 'none');
            $('dropbox-text-nodragndrop').setStyle('display', 'block');
        }

        dropboxInput.onclick = function(e) {
            if (typeof e.stopPropagation != 'undefined')
                e.stopPropagation();
        };

        if (Browser.ie && Browser.version <= 7) {
            // Show the file upload input form.
            $('dropbox-text').setStyle('display', 'none');
            $('dropbox-text-nodragndrop').setStyle('display', 'none');
            $('dropbox-file').setStyle('visibility', 'visible');
            dropboxInput.addEvent('change', function() { dropbox.submit(); });
        } else {
            // Delegate click on dropbox to the hidden input file element.
            dropbox.addEvent('click', function(e) {
                if (typeof File != 'undefined') {
                    dropboxInput.click();
                } else { // Fallback to upload by the iframe hack (IE).
                    if (typeof iframe.contentDocument != 'undefined')
                        var form = iframe.contentDocument.forms[0];
                    else
                        var form = iframe.getDocument().forms[0];
                    form.file.onchange = function() {
                        var item = this_.upload(form);
                        iframe.onload = function() {
                            item.model.del();
                            this_.update();
                        };
                    };
                    form.file.click();
                }
            });
        }

        dropbox.addEvent('change', function(e) {
            if (e.target.files && (typeof FileReader != 'undefined' || typeof FormData == 'undefined')) {
                Array.each(e.target.files, function(file) {
                    this_.upload(file);
                });
            } else {
                this_.upload(dropbox);
            }
            dropbox.reset();
        });

        //window.setInterval(function() { this_.update() }, this.options.updateInterval);
        window.uuid = uuid();
        //this.update();
    },

    bootstrap: function(json) {
        var this_ = this;
        items = JSON.decode(json);
        Array.each(items, function(item) {
            var view = new ItemView(new Item());
            view.model.update(item);
            this_.items.add(view);
        });
    },

    update: function() {
        var xhr = new XMLHttpRequest();

        if (this.lastUpdate) xhr.open('GET', 'update/' + this.lastUpdate + '/');
        else xhr.open('GET', 'update/');

        var this_ = this;
        xhr.onreadystatechange = function(e) {
            if (this.readyState == 4) {
                json = JSON.decode(this.responseText);
                this_.removeStaleItems(json.item_ids);
                Array.each(json.items, function(item) {
                    if (this_.items.get(item.id)) { // Existing item.
                        // Do not update pending items.
                        if (this_.items.get(item.id).model.type != 'pending')
                            this_.items.get(item.id).model.update(item);
                    } else { // New item.
                        var view = new ItemView(new Item());
                        view.model.update(item);
                        this_.items.add(view);
                    }
                });
                this.lastUpdate = json.time;
            }
        };
        xhr.send();
    },

    upload: function(data) {
        // Determine name and size of the file.
        if (typeof File != 'undefined' && data instanceof File) {
            // Older browsers.
            if (data.fileName) var name = data.fileName;
            if (data.fileSize) var size = data.fileSize;
            // Newer browsers.
            if (data.name) var name = data.name;
            if (data.size) var size = data.size;
        } else if (data.file) {
            var name = basename(data.file.value);
        } else {
            var name = '';
            var size = 0;
        }

        // Is there a stale item with the same size and name?
        var i = this.items.find(function(i) {
            if (!(i.model.type == 'stale' || i.model.type == 'pending' && i.isError()))
                return false;
            if (i.model.size_total != file.size) return false;
            if (i.model.name == name) return true;
            var n = i.model.name;
            // Django appends _#no to duplicate file names, account for that.
            if (n.contains('_')) {
                var index = n.lastIndexOf('_');
                var first = n.substring(0, index);
                var second = n.substring(index);
                n = first + second.substring(second.indexOf('.'));
            }
            if (n == name) return true;
            return false;
        });

        // If this is a File and FileReader is supported, ask the user about resume.
        if (i && typeof FileReader != 'undefined' && data instanceof File) {
            var c = confirm('A stale file with the same name and size has been found.\nDo you want to resume uploading?');
            if (c) {
                item = i;
                item.clearError();
            }
        }

        if (!item) {
            var item = new ItemView(new Item({
                type: 'pending',
                'name': name,
                size: 0,
                size_total: size,
                status: 'UPLOADING'
            }));
            this.items.add(item);
        }

        if (ITEM_SIZE_LIMIT > 0 && size > ITEM_SIZE_LIMIT) {
            item.onError({
                label: ITEM_SIZE_LIMIT_ERROR_LABEL,
                message: ITEM_SIZE_LIMIT_ERROR_MESSAGE
	    });
            return null;
        }

        if (typeof File != 'undefined' && data instanceof File && typeof FileReader != 'undefined') {
            var reader = new FileReader()
            reader.onload = function(e) { item.model.upload(e.target.result); };
            reader.readAsBinaryString(data);
        } else if (data instanceof HTMLFormElement && typeof FormData != 'undefined') {
            item.model.upload(new FormData(data));
        } else if (typeof File != 'undefined' && data instanceof File && typeof FormData != 'undefined') {
            var formData = new FormData();
            formData.append('file', data);
            item.model.upload(formData);
        } else if (typeof File != 'undefined' && data instanceof File) {
            item.model.upload(data);
        } else {
            data.submit();
        }

        return item;
    },

    removeStaleItems: function(validIds) {
        Object.each(this.items.all(), function(item) {
            if (item.model.type != 'pending' && !validIds.contains(item.model.id))
                item.model.remove();
        });
    }
});

document.addEvent('domready', function() {
    var fs = new FileShack();
});

<commit_msg>Fix issues with Firefox 3.6.<commit_after>/*
 * Copyright (c) 2012 Peter Kuma

 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

var FileShack = new Class({
    Implements: Options,

    options: {
        updateInterval: 2000
    },

    initialize: function(options) {
        var this_ = this;

        this.setOptions(options);
        this.lastUpdate = undefined;

        this.items = new Collection();
        this.items.addEvent('add', function(view) {
            $('list').insertBefore(view.el, $('list').firstChild);
        });

        // Bootstrap items from JSON embedded in index.html.
        this.bootstrap($('bootstrap').get('text'))

        var dropbox = $('dropbox');
        var dropboxInput = $('dropbox-input');
        var iframe = $('iframe');

        // Drag & Drop.
        if (typeof dropbox.addEventListener != 'undefined' &&
            typeof dropbox.ondrop != 'undefined' &&
            typeof File != 'undefined')
        {
            // Register Drag & Drop events.
            dropbox.addEventListener("dragenter", function(e) { e.preventDefault(); }, false);
            dropbox.addEventListener("dragover", function(e) { e.preventDefault(); }, false);
            dropbox.addEventListener("drop", function(e) {
                e.preventDefault();
                if (e.dataTransfer.files)
                    Array.each(e.dataTransfer.files, function(f) { this_.upload(f); });
            }, false);
        } else {
            // Switch dropbox text, drag & drop is not supported.
            $('dropbox-text').setStyle('display', 'none');
            $('dropbox-text-nodragndrop').setStyle('display', 'block');
        }

        var clickDelegated = false;
        dropboxInput.onclick = function(e) {
            clickDelegated = true;
            if (typeof e.stopPropagation != 'undefined')
                e.stopPropagation();
        };

        if (Browser.ie && Browser.version <= 7) {
            this.fallback();
        } else {
            // Delegate click on dropbox to the hidden input file element.
            dropbox.addEvent('click', function() {
                if (typeof File != 'undefined') {
                    dropboxInput.click();
                    window.setTimeout(function() { if (!clickDelegated) this_.fallback(); }, 100);
                } else { // Fallback to upload by the iframe hack (IE).
                    if (typeof iframe.contentDocument != 'undefined')
                        var form = iframe.contentDocument.forms[0];
                    else
                        var form = iframe.getDocument().forms[0];
                    form.file.onchange = function() {
                        var item = this_.upload(form);
                        iframe.onload = function() {
                            item.model.del();
                            this_.update();
                        };
                    };
                    form.file.click();
                }
            });
        }

        dropbox.addEvent('change', function(e) {
            if (e.target.files && (typeof FileReader != 'undefined' || typeof FormData == 'undefined')) {
                Array.each(e.target.files, function(file) {
                    this_.upload(file);
                });
            } else {
                this_.upload(dropbox);
            }
            dropbox.reset();
        });

        //window.setInterval(function() { this_.update() }, this.options.updateInterval);
        window.uuid = uuid();
        //this.update();
    },

    bootstrap: function(json) {
        var this_ = this;
        items = JSON.decode(json);
        Array.each(items, function(item) {
            var view = new ItemView(new Item());
            view.model.update(item);
            this_.items.add(view);
        });
    },

    fallback: function() {
        var this_ = this;
        // Show the file upload input form.
        $('dropbox-text').setStyle('display', 'none');
        $('dropbox-text-nodragndrop').setStyle('display', 'none');
        $('dropbox-file').setStyle('visibility', 'visible');
        $('dropbox-input').addEvent('change', function() { this_.upload($('dropbox')); });
    },

    update: function() {
        var xhr = new XMLHttpRequest();

        if (this.lastUpdate) xhr.open('GET', 'update/' + this.lastUpdate + '/');
        else xhr.open('GET', 'update/');

        var this_ = this;
        xhr.onreadystatechange = function(e) {
            if (this.readyState == 4) {
                json = JSON.decode(this.responseText);
                this_.removeStaleItems(json.item_ids);
                Array.each(json.items, function(item) {
                    if (this_.items.get(item.id)) { // Existing item.
                        // Do not update pending items.
                        if (this_.items.get(item.id).model.type != 'pending')
                            this_.items.get(item.id).model.update(item);
                    } else { // New item.
                        var view = new ItemView(new Item());
                        view.model.update(item);
                        this_.items.add(view);
                    }
                });
                this.lastUpdate = json.time;
            }
        };
        xhr.send();
    },

    upload: function(data) {
        // Determine name and size of the file.
        if (typeof File != 'undefined' && data instanceof File) {
            // Older browsers.
            if (data.fileName) var name = data.fileName;
            if (data.fileSize) var size = data.fileSize;
            // Newer browsers.
            if (data.name) var name = data.name;
            if (data.size) var size = data.size;
        } else if (data.file) {
            var name = basename(data.file.value);
        } else {
            var name = '';
            var size = 0;
        }

        // Is there a stale item with the same size and name?
        var i = this.items.find(function(i) {
            if (!(i.model.type == 'stale' || i.model.type == 'pending' && i.isError()))
                return false;
            if (i.model.size_total != size) return false;
            if (i.model.name == name) return true;
            var n = i.model.name;
            // Django appends _#no to duplicate file names, account for that.
            if (n.contains('_')) {
                var index = n.lastIndexOf('_');
                var first = n.substring(0, index);
                var second = n.substring(index);
                n = first + second.substring(second.indexOf('.'));
            }
            if (n == name) return true;
            return false;
        });

        // If this is a File, ask the user about resume.
        if (i && data instanceof File) {
            var c = confirm('A stale file with the same name and size has been found.\nDo you want to resume uploading?');
            if (c) {
                item = i;
                item.clearError();
            }
        }

        if (!item) {
            var item = new ItemView(new Item({
                type: 'pending',
                'name': name,
                size: 0,
                size_total: size,
                status: 'UPLOADING'
            }));
            this.items.add(item);
        }

        if (ITEM_SIZE_LIMIT > 0 && size > ITEM_SIZE_LIMIT) {
            item.onError({
                label: ITEM_SIZE_LIMIT_ERROR_LABEL,
                message: ITEM_SIZE_LIMIT_ERROR_MESSAGE
	    });
            return null;
        }

        if (typeof File != 'undefined' && data instanceof File && typeof FileReader != 'undefined') {
            var reader = new FileReader()
            reader.onload = function(e) { item.model.upload(e.target.result); };
            reader.readAsBinaryString(data);
        } else if (data instanceof HTMLFormElement && typeof FormData != 'undefined') {
            item.model.upload(new FormData(data));
        } else if (typeof File != 'undefined' && data instanceof File && typeof FormData != 'undefined') {
            var formData = new FormData();
            formData.append('file', data);
            item.model.upload(formData);
        } else if (typeof File != 'undefined' && data instanceof File) {
            item.model.upload(data.getAsBinary());
        } else {
            data.submit();
        }

        return item;
    },

    removeStaleItems: function(validIds) {
        Object.each(this.items.all(), function(item) {
            if (item.model.type != 'pending' && !validIds.contains(item.model.id))
                item.model.remove();
        });
    }
});

document.addEvent('domready', function() {
    var fs = new FileShack();
});

<|endoftext|>