1 | require('dotenv').config();
|
2 | const app = require('template')();
|
3 | const path = require('path');
|
4 | const engineLodash = require('engine-lodash');
|
5 | const {
|
6 | compose,
|
7 | snakeToCamel,
|
8 | getComponentName,
|
9 | toDashCase,
|
10 | } = require('../utils');
|
11 | const { copyFiles } = require('./files');
|
12 | const config = require('../lib/config');
|
13 |
|
14 | const PACKAGE_PATH = process.env.MODE === 'development' ? process.cwd() : path.dirname(require.main.filename);
|
15 | const PACKAGE_TMPL_PATH = `${PACKAGE_PATH}/tmpl`;
|
16 | const TMPL_PATH = config.get('tmplPath') ? `${process.cwd()}/${config.get('tmplPath')}` : PACKAGE_TMPL_PATH;
|
17 |
|
18 | const STYLES_FILE_NAMING_TYPE = config.get('cssFileName');
|
19 |
|
20 | app.engine('tmpl', engineLodash);
|
21 | app.create('pages');
|
22 |
|
23 | app.page('welcome.tmpl', { path: 'welcome.tmpl', content: 'Hello, <%= name %>!' });
|
24 |
|
25 | const 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 |
|
44 | const copyTemplatesToCustomFolder = async (folderName) => {
|
45 | const bla = await copyFiles(`${path.dirname(require.main.filename)}/tmpl`, `${process.cwd()}/${folderName}`);
|
46 | return bla;
|
47 | };
|
48 |
|
49 | module.exports = {
|
50 | getFileContent,
|
51 | copyTemplatesToCustomFolder,
|
52 | };
|