import path from 'path';
import * as fspp from '../fs-promise-proxy';

export default function* addFile(data, cfg, plop) {
	// if not already an absolute path, make an absolute path from the basePath (plopfile location)
	const makeTmplPath = p => path.resolve(plop.getPlopfilePath(), p);
	const makeDestPath = p => path.resolve(plop.getDestBasePath(), p);

	var {template} = cfg;
	const fileDestPath = makeDestPath(plop.renderString(cfg.path || '', data));

	try {
		if (cfg.templateFile) {
			template = yield fspp.readFile(makeTmplPath(cfg.templateFile));
		}
		if (template == null) { template = ''; }

		// check path
		const pathExists = yield fspp.fileExists(fileDestPath);
		
		if(!plop.getForce() && pathExists){
			throw `File already exists\n -> ${fileDestPath}`;
		} else {
			yield fspp.makeDir(path.dirname(fileDestPath));
			yield fspp.writeFile(fileDestPath, plop.renderString(template, data));
		}

		// return the added file path (relative to the destination path)
		return fileDestPath.replace(path.resolve(plop.getDestBasePath()), '');

	} catch(err) {
		if (typeof err === 'string') {
			throw err;
		} else {
			throw err.message || JSON.stringify(err);
		}
	}
}

@@ -373,16 +373,42 @@
  = cfg;%0A
+%09const %7Boverwrite%7D = cfg;%0A
 %09const f
@@ -726,16 +726,30 @@
 rce() &&
+ !overwrite &&
  pathExi

Add overwrite property to Add and AddMany