src/qgen.js
function qgen(options) {
	const defaultOptions = {
		dest: DEFAULT_DESTINATION,
		cwd: process.cwd(),
		directory: 'qgen-templates',
		config: './qgen.json',
		helpers: undefined,
		force: false,
		preview: false
	};

	const configfilePath = createConfigFilePath(defaultOptions, options);
	const configfileOptions = loadConfig(configfilePath);
	const config = Object.assign(defaultOptions, configfileOptions, options);

	/** Throw error if qgen template directory is missing */
	if (isFileOrDir(config.directory) !== 'directory') {
		throw new QGenError(`qgen templates directory '${config.directory}' not found.`);
	}

	/**
	 * Lists the available template names
	 *
	 * @return {Array} available template names
	 */
	const templates = () => {
		return globby.sync(['*'], {
			cwd: path.join(config.cwd, config.directory),
			expandDirectories: false,
			onlyFiles: false
		});
	};

	/**
	 * Render the template file and save to the destination path
	 * @param  {String} template The name of the template
	 * @param  {String} destination Destination path
	 */
	const render = async (template, destination) => {
		const templatePath = path.join(config.directory, template);
		const templateType = isFileOrDir(path.join(config.cwd, templatePath));
		const templateConfig = createTemplateConfig(config, template, DEFAULT_DESTINATION);
		const filepathRenderer = templateRenderer({
			helpers: config.helpers,
			cwd: config.cwd
		});

		// Override config dest with passed destination
		if (destination) {
			templateConfig.dest = destination;
		}

		let fileObjects;

		if (templateType === 'directory') {
			const files = globby.sync(['**/*'], {
				cwd: path.join(config.cwd, templatePath),
				nodir: true
			});

			fileObjects = files.map(filePath => {
				return {
					src: path.join(templatePath, filePath),
					dest: path.join(templateConfig.cwd, templateConfig.dest, filepathRenderer.render(filePath, config))
				};
			});
		} else if (templateType === 'file') {
			fileObjects = [{
				src: templatePath,
				dest: path.join(templateConfig.cwd, templateConfig.dest, template)
			}];
		} else {
			throw new QGenError(`Template '${templatePath}' not found.`);
		}

		const filesForRender = config.preview ? fileObjects : await enquireToOverwrite(fileObjects, config.force);

		if (!filesForRender.some(f => f.abort)) {
			renderFiles(filesForRender, templateConfig, config.preview);
		}
	};

	return Object.freeze({
		templates,
		render
	});
}
function qgen(options) {
	const defaultOptions = {
		dest: DEFAULT_DESTINATION,
		cwd: process.cwd(),
		directory: 'qgen-templates',
		config: './qgen.json',
		helpers: undefined,
		force: false,
		preview: false
	};

	const configfilePath = createConfigFilePath(defaultOptions, options);
	const configfileOptions = loadConfig(configfilePath);
	const config = Object.assign(defaultOptions, configfileOptions, options);

	/** Throw error if qgen template directory is missing */
	if (isFileOrDir(config.directory) !== 'directory') {
		throw new QGenError(`qgen templates directory '${config.directory}' not found.`);
	}

	/**
	 * Lists the available template names
	 *
	 * @return {Array} available template names
	 */
	const templates = () => {
		return globby.sync(['*'], {
			cwd: path.join(config.cwd, config.directory),
			expandDirectories: false,
			onlyFiles: false
		});
	};

	/**
	 * Render the template file and save to the destination path
	 * @param  {String} template The name of the template
	 * @param  {String} destination Destination path
	 */
	const render = async (template, destination) => {
		const templatePath = path.join(config.directory, template);
		const templateType = isFileOrDir(path.join(config.cwd, templatePath));
		const templateConfig = createTemplateConfig(config, template, DEFAULT_DESTINATION);
		const filepathRenderer = templateRenderer({
			helpers: config.helpers,
			cwd: config.cwd
		});

		// Override config dest with passed destination
		if (destination) {
			templateConfig.dest = destination;
		}

		let fileObjects;

		if (templateType === 'directory') {
			const files = globby.sync(['**/*'], {
				cwd: path.join(config.cwd, templatePath),
				nodir: true
			});

			fileObjects = files.map(filePath => {
				return {
					src: path.join(templatePath, filePath),
					dest: path.join(templateConfig.cwd, templateConfig.dest, filepathRenderer.render(filePath, config))
				};
			});
		} else if (templateType === 'file') {
			fileObjects = [{
				src: templatePath,
				dest: path.join(templateConfig.cwd, templateConfig.dest, template)
			}];
		} else {
			throw new QGenError(`Template '${templatePath}' not found.`);
		}

		const filesForRender = config.preview ? fileObjects : await enquireToOverwrite(fileObjects, config.force);

		if (!filesForRender.some(f => f.abort)) {
			renderFiles(filesForRender, templateConfig, config.preview);
		}
	};

	return Object.freeze({
		templates,
		render
	});
}
function qgen ( options ) { const defaultOptions = { dest : DEFAULT_DESTINATION , cwd : process . cwd ( ) , directory : 'qgen-templates' , config : './qgen.json' , helpers : undefined , force : false , preview : false } ; const configfilePath = createConfigFilePath ( defaultOptions , options ) ; const configfileOptions = loadConfig ( configfilePath ) ; const config = Object . assign ( defaultOptions , configfileOptions , options ) ; /** Throw error if qgen template directory is missing */ if ( isFileOrDir ( config . directory ) !== 'directory' ) { throw new QGenError ( ` ${ config . directory } ` ) ; } /**
	 * Lists the available template names
	 *
	 * @return {Array} available template names
	 */ const templates = ( ) => { return globby . sync ( [ '*' ] , { cwd : path . join ( config . cwd , config . directory ) , expandDirectories : false , onlyFiles : false } ) ; } ; /**
	 * Render the template file and save to the destination path
	 * @param  {String} template The name of the template
	 * @param  {String} destination Destination path
	 */ const render = async ( template , destination ) => { const templatePath = path . join ( config . directory , template ) ; const templateType = isFileOrDir ( path . join ( config . cwd , templatePath ) ) ; const templateConfig = createTemplateConfig ( config , template , DEFAULT_DESTINATION ) ; const filepathRenderer = templateRenderer ( { helpers : config . helpers , cwd : config . cwd } ) ; // Override config dest with passed destination if ( destination ) { templateConfig . dest = destination ; } let fileObjects ; if ( templateType === 'directory' ) { const files = globby . sync ( [ '**/*' ] , { cwd : path . join ( config . cwd , templatePath ) , nodir : true } ) ; fileObjects = files . map ( filePath => { return { src : path . join ( templatePath , filePath ) , dest : path . join ( templateConfig . cwd , templateConfig . dest , filepathRenderer . render ( filePath , config ) ) } ; } ) ; } else if ( templateType === 'file' ) { fileObjects = [ { src : templatePath , dest : path . join ( templateConfig . cwd , templateConfig . dest , template ) } ] ; } else { throw new QGenError ( ` ${ templatePath } ` ) ; } const filesForRender = config . preview ? fileObjects : await enquireToOverwrite ( fileObjects , config . force ) ; if ( ! filesForRender . some ( f => f . abort ) ) { renderFiles ( filesForRender , templateConfig , config . preview ) ; } } ; return Object . freeze ( { templates , render } ) ; }
Creates new qgen object
@param {Object} options - Options such as dest, config file path etc.
@returns {qgen} qgen object
Creates new qgen object