UNPKG

1.58 kBJavaScriptView Raw
1require('dotenv').config();
2const app = require('template')();
3const path = require('path');
4const engineLodash = require('engine-lodash');
5const {
6 compose,
7 snakeToCamel,
8 getComponentName,
9 toDashCase,
10} = require('../utils');
11const { copyFiles } = require('./files');
12const config = require('../lib/config');
13
14const PACKAGE_PATH = process.env.MODE === 'development' ? process.cwd() : path.dirname(require.main.filename);
15const PACKAGE_TMPL_PATH = `${PACKAGE_PATH}/tmpl`;
16const TMPL_PATH = config.get('tmplPath') ? `${process.cwd()}/${config.get('tmplPath')}` : PACKAGE_TMPL_PATH;
17
18const STYLES_FILE_NAMING_TYPE = config.get('cssFileName');
19
20app.engine('tmpl', engineLodash);
21app.create('pages');
22
23app.page('welcome.tmpl', { path: 'welcome.tmpl', content: 'Hello, <%= name %>!' });
24
25const getFileContent = async ({ name, tmplFileName }) => {
26 const tmplPath = `${TMPL_PATH}/${tmplFileName}`;
27 const props = {
28 componentName: compose(getComponentName, snakeToCamel)(name),
29 stylesName: STYLES_FILE_NAMING_TYPE === 'component' ? `${toDashCase(name)}.css` : 'styles.css',
30 };
31
32 app.page(tmplPath, { path: tmplPath });
33
34 const page = app.pages.get(tmplPath);
35
36 const { content } = await page.render(props, (err, res) => {
37 if (err) return err;
38 return res.content;
39 });
40
41 return content;
42};
43
44const copyTemplatesToCustomFolder = async (folderName) => {
45 const bla = await copyFiles(`${path.dirname(require.main.filename)}/tmpl`, `${process.cwd()}/${folderName}`);
46 return bla;
47};
48
49module.exports = {
50 getFileContent,
51 copyTemplatesToCustomFolder,
52};