-
Notifications
You must be signed in to change notification settings - Fork 264
/
package-bundle-utils.ts
146 lines (141 loc) · 4.49 KB
/
package-bundle-utils.ts
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
import { dirname, basename, join, sep } from "path";
import { existsSync, mkdirSync, copySync, readdirSync, rmSync } from "fs-extra";
import Plugin from "@saltcorn/data/models/plugin";
import File from "@saltcorn/data/models/file";
import { spawnSync } from "child_process";
const { getState } = require("@saltcorn/data/db/state");
/**
*
* @param buildDir directory where the app will be build
* @param plugins saltcorn plugins to install
*/
export async function bundlePackagesAndPlugins(
buildDir: string,
plugins: Plugin[]
) {
const result = spawnSync(
"npm",
[
"run",
"build",
"--",
"--env",
`plugins=${JSON.stringify(plugins)}`,
"--env",
`output=${buildDir}/www/js/`,
],
{
cwd: join(__dirname, "../../"),
}
);
console.log(result.output.toString());
rmSync(join(__dirname, "../..", "plugin_packages"), {
force: true,
recursive: true,
});
return result.status;
}
async function copyHeaderToApp(
pluginLocation: string,
header: string,
wwwDir: string
) {
const pathArr = header.split(sep);
if (pathArr.length > 4) {
const pluginSubDir = pathArr.slice(4, pathArr.length - 1).join(sep);
const dstPublicDir = join(wwwDir, dirname(header));
if (!existsSync(dstPublicDir)) {
mkdirSync(dstPublicDir, { recursive: true });
}
const headerFile = basename(header);
try {
copySync(
join(pluginLocation, "public", pluginSubDir, headerFile),
join(dstPublicDir, headerFile)
);
} catch (e) {
console.log(`Error copying header ${header} to ${dstPublicDir}`);
console.log(e);
}
} else {
console.log(`skipping header '${header}'`);
}
}
function copyAllPublicFiles(location: string, dstPublicDir: string) {
const srcPublicDir = join(location, "public");
if (existsSync(srcPublicDir)) {
if (!existsSync(dstPublicDir)) {
mkdirSync(dstPublicDir, { recursive: true });
}
for (const dirEntry of readdirSync(srcPublicDir)) {
copySync(join(srcPublicDir, dirEntry), join(dstPublicDir, dirEntry));
}
}
}
/**
* Copy files from the plugin 'public' directories that are needed as headers into the app.
* For themes, everything from 'public' will be copied.
* @param buildDir directory where the app will be build
*/
export async function copyPublicDirs(buildDir: string) {
const state = getState();
const wwwDir = join(buildDir, "www");
const pluginCfgs = state.plugin_cfgs || {};
for (const [k, v] of <[string, any]>Object.entries(state.plugins)) {
const location = state.plugin_locations[k];
if (location) {
for (const { script, css } of state.headers[k] || []) {
if (script) copyHeaderToApp(location, script, wwwDir);
if (css) copyHeaderToApp(location, css, wwwDir);
}
if (k !== "sbadmin2")
copyAllPublicFiles(location, join(wwwDir, "plugins", "public", k));
}
if (pluginCfgs[k] && pluginCfgs[k].alt_css_file) {
const altCssFile = await File.findOne(pluginCfgs[k].alt_css_file);
if (altCssFile)
copySync(
altCssFile.location,
join(wwwDir, "plugins", "public", k, pluginCfgs[k].alt_css_file),
{ recursive: true }
);
}
}
}
/**
*
* @param buildDir directory where the app will be build
* @param manager live-plugin-manager to load a npm pacakage (change to dependency??)
*/
export async function installNpmPackages(buildDir: string, manager: any) {
const npmTargetDir = join(buildDir, "www", "npm_packages");
if (!existsSync(npmTargetDir)) mkdirSync(npmTargetDir, { recursive: true });
const jwtInfo = await manager.install("jwt-decode", "3.1.2");
copySync(
join(jwtInfo.location, "build/jwt-decode.js"),
join(npmTargetDir, "jwt-decode.js")
);
const routerInfo = await manager.install("universal-router", "9.1.0");
copySync(
join(routerInfo.location, "universal-router.min.js"),
join(npmTargetDir, "universal-router.min.js")
);
const axiosInfo = await manager.install("axios", "0.27.2");
copySync(
join(axiosInfo.location, "dist", "axios.min.js"),
join(npmTargetDir, "axios.min.js")
);
const i18nNextInfo = await manager.install("i18next", "21.8.16");
copySync(
join(i18nNextInfo.location, "i18next.min.js"),
join(npmTargetDir, "i18next.min.js")
);
const postProcInfo = await manager.install(
"i18next-sprintf-postprocessor",
"0.2.2"
);
copySync(
join(postProcInfo.location, "i18nextSprintfPostProcessor.min.js"),
join(npmTargetDir, "i18nextSprintfPostProcessor.min.js")
);
}