-
Notifications
You must be signed in to change notification settings - Fork 2
/
.zuix.js
171 lines (161 loc) · 5.34 KB
/
.zuix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright 2020-2022 G-Labs. All Rights Reserved.
* https://zuixjs.github.io/zuix
*
* Licensed under the MIT license. See LICENSE file.
*
*/
/*
*
* This file is part of
* zUIx, Javascript library for component-based development.
* https://zuixjs.github.io/zuix
*
* @author Generoso Martello - https://github.com/genemars
* @version 1.0
*
*/
const fs = require('fs');
const path = require('path');
const {classNameFromHyphens} = require('zuix/common/utils');
const mkdirp = require("mkdirp");
const chalk = require('chalk');
const nunjucks = require('nunjucks');
const config = require('config');
const moment = require('moment');
const yesno = require('yesno');
const child_process = require('child_process');
const zuixConfig = config.get('zuix');
const sourceFolder = zuixConfig.get('build.input');
const buildFolder = zuixConfig.get('build.output');
const contentFolder = zuixConfig.get('build.contentFolder', 'content');
const contentSourceFolder = path.join(sourceFolder, contentFolder);
const contentBuildFolder = path.join(buildFolder, contentFolder)
function addPage(args) {
const pageTemplatesPath = path.join('./templates', contentFolder, '/');
const template = args.layout;
let outputFile = args.name.toLowerCase();
const pageName = path.basename(outputFile);
const pageTitle = classNameFromHyphens(pageName);
console.log(
chalk.cyanBright('*') + ' Generating',
chalk.yellow.bold(template),
'→',
outputFile
);
let extension = '.liquid';
let componentTemplate = `${pageTemplatesPath}${template}${extension}`;
if (!fs.existsSync(componentTemplate)) {
extension = '.md'
componentTemplate = `${pageTemplatesPath}${template}${extension}`;
}
if (fs.existsSync(componentTemplate)) {
let sectionFolder = path.join(sourceFolder, contentFolder);
if (args.section) {
args.section = args.section.toLowerCase();
sectionFolder = path.join(sectionFolder, args.section);
}
const frontMatter = args.frontMatter && '\n' + args.frontMatter.join('\n') + '\n';
const date = new moment().format('yyyy-MM-DD hh:mm:ss');
let pageTemplate = fs.readFileSync(componentTemplate).toString('utf8');
nunjucks.configure({
tags: {
blockStart: '<%',
blockEnd: '%>',
variableStart: '<$',
variableEnd: '$>',
commentStart: '<#',
commentEnd: '#>'
}
});
pageTemplate = nunjucks.renderString(pageTemplate, {
title: pageTitle,
name: pageName,
section: args.section,
frontMatter,
date
});
const outputPath = path.join(sectionFolder, outputFile);
outputFile = path.join(outputPath, 'index' + extension);
if (!fs.existsSync(outputFile)) {
mkdirp.sync(outputPath);
fs.writeFileSync(outputFile, pageTemplate);
console.log(chalk.cyanBright('*') + ' NEW page:', chalk.green.bold(outputFile));
// Create section if it does not exist
if (args.section) {
const sectionFile = path.join(sectionFolder, 'index.liquid');
if (!fs.existsSync(sectionFile)) {
addPage({layout: 'section', name: args.section, frontMatter: [
`group: ${args.section}`,
`title: ${classNameFromHyphens(args.section)}`,
]});
} else {
touch(sectionFile);
}
}
} else {
console.error(
chalk.red.bold('A file with that name already exists.')
);
}
} else {
console.error(
chalk.red.bold('Invalid page template:', componentTemplate)
);
}
}
async function wipeContent() {
const confirm = await yesno({
question: `All content in "${contentSourceFolder}" will be deleted.\nThis action cannot be undone!\nAre you sure to proceed?`
});
if (confirm) {
if (fs.existsSync(contentSourceFolder)) {
console.log(chalk.cyanBright('*') + ' Removing', chalk.green.bold(contentSourceFolder));
fs.rmSync(contentSourceFolder, {recursive: true});
}
if (fs.existsSync(contentBuildFolder)) {
console.log(chalk.cyanBright('*') + ' Removing', chalk.green.bold(contentBuildFolder));
fs.rmSync(contentBuildFolder, {recursive: true});
}
// "touch" index file to force reload
const filename = path.join(sourceFolder, 'index.liquid');
touch(filename);
}
}
function touch(filename) {
try {
const touchStream = fs.createWriteStream(filename, {flags: 'a'});
touchStream.close();
} catch (err) {
// TODO: log error
}
}
function collect(value, previous) {
return previous.concat([value]);
}
module.exports = (program) => {
program
.command('build')
.alias('b')
.description('Build web application')
.action(() => {
// todo: should check if it's a zuix.js project
child_process.execSync('npm run build',{
stdio:[0, 1, 2]
});
});
program
.command('add')
.alias('a')
.description('Add new page')
.requiredOption('-s, --section <section_name>', 'Page section')
.requiredOption('-n, --name <page_name>', 'Page name')
.option('-l, --layout <layout_template>', 'Layout template name', 'article')
.option('-fm, --front-matter "<field>: <value>"', 'Set a front matter field value', collect, [])
.action(addPage);
program
.command('wipe-content')
.alias('wc')
.description(`Delete all content in "${contentSourceFolder}" and "${contentBuildFolder}" folders.`)
.action(wipeContent);
};