This repository was archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththemeInit.js
More file actions
80 lines (66 loc) · 2.59 KB
/
themeInit.js
File metadata and controls
80 lines (66 loc) · 2.59 KB
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
const path = require('path');
const fs = require('fs');
const next = require('next').default;
const { execCmd, decodeBase64ToObject } = require('aql-utils');
const serverUtils = require('../../utils/server');
const dev = serverUtils.dev;
const themeName = path.basename(__dirname);
global.aquila = decodeBase64ToObject(global.aquila);
const pathToTheme = path.join(global.aquila.appRoot, 'themes', themeName, '/');
const start = async () => {
const app = next({ dev, dir: pathToTheme });
let handler = app.getRequestHandler();
createDotEnvIfNotExists();
createCustomCSSIfNotExists();
createListModulesIfNotExists();
console.log('next build start...');
await app.prepare();
console.log('next build finish');
return handler;
};
const build = async () => {
createDotEnvIfNotExists();
createCustomCSSIfNotExists();
createListModulesIfNotExists();
await execCmd('npx next build', pathToTheme);
};
const createCustomCSSIfNotExists = () => {
console.log('createCustomCSSIfNotExists');
// Create file if not exists
const customCssPath = path.join(pathToTheme, 'styles', 'custom.css');
if (!fs.existsSync(customCssPath)) {
fs.writeFileSync(customCssPath, '');
}
};
const createDotEnvIfNotExists = () => {
console.log('createDotEnvIfNotExists()...');
const dotEnvPath = path.join(pathToTheme, '.env');
if (!fs.existsSync(dotEnvPath)) {
let appUrl = '';
if (!global?.aquila?.envConfig?.environment?.appUrl) {
throw new Error('"appUrl" is not defined in your configuration.');
}
appUrl = global.aquila.envConfig.environment.appUrl.slice(0, -1);
const nextApiValue = `${appUrl}/api`;
process.env.NEXT_PUBLIC_API_URL = nextApiValue;
const data = `NEXT_PUBLIC_API_URL=${nextApiValue}`;
fs.writeFileSync(dotEnvPath, data);
}
};
const createListModulesIfNotExists = async () => {
console.log('createListModulesIfNotExists');
// Create folder "modules" if not exists
const modulesPath = path.join(pathToTheme, 'modules');
if (!fs.existsSync(modulesPath)) {
fs.mkdirSync(modulesPath);
}
// Create file if not exists
const listModulePath = path.join(modulesPath, 'list_modules.js');
if (!fs.existsSync(listModulePath)) {
fs.writeFileSync(listModulePath, 'export default [];');
}
};
module.exports = {
start,
build
};