create-lupine 1.0.27 → 1.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,371 +0,0 @@
1
- // esbuild command tools
2
-
3
- const esbuild = require('esbuild');
4
- const path = require('path');
5
- const fs = require('fs/promises');
6
- const {
7
- runCmd,
8
- copyFolder,
9
- sendRequest,
10
- loadEnv,
11
- readJson,
12
- pathExists,
13
- cpIndexHtml,
14
- pluginIfelse,
15
- markdownProcessOnEnd,
16
- obfuscatePlugin,
17
- sha256,
18
- } = require('lupine.api/dev');
19
-
20
- const triggerHandle = {
21
- restart: null,
22
- refresh: null,
23
- };
24
-
25
- // restart server for server code changes
26
- const triggerReStartServer = async (isDev, npmCmd, httpPort) => {
27
- if (triggerHandle.restart) {
28
- clearTimeout(triggerHandle.restart);
29
- }
30
- triggerHandle.restart = setTimeout(async () => {
31
- const token = sha256(process.env['DEV_TOKEN'] || '');
32
- const url = `http://127.0.0.1:${httpPort}/debug/shutdown?t=${token}`;
33
- await sendRequest(url, isDev ? 2 : 0);
34
- console.log('[dev-server] ReStart Server: ', url);
35
- isDev && runCmd(npmCmd);
36
- }, 500);
37
- };
38
-
39
- // refresh server for client code changes
40
- const triggerRefreshServer = async (isDev, httpPort) => {
41
- if (triggerHandle.refresh) {
42
- clearTimeout(triggerHandle.refresh);
43
- }
44
- triggerHandle.refresh = setTimeout(async () => {
45
- const token = sha256(process.env['DEV_TOKEN'] || '');
46
- const url = `http://127.0.0.1:${httpPort}/debug/refresh?t=${token}`;
47
- await sendRequest(url, isDev ? 2 : 0);
48
- console.log('[dev-server] Refresh Server: ', url);
49
- }, 500);
50
- };
51
-
52
- // watch server code changes
53
- const watchServerPlugin = (isDev, npmCmd, httpPort) => {
54
- return {
55
- name: 'watchServerPlugin',
56
- setup(build) {
57
- build.onEnd(async (res) => {
58
- // console.log(`Build meta data: `, res);
59
- console.log(`[dev-server] Build finished`);
60
- if (isDev) {
61
- await triggerReStartServer(isDev, npmCmd, httpPort);
62
- }
63
- });
64
- },
65
- };
66
- };
67
-
68
- // watch server code changes
69
- const watchServerLoader = async (isDev, npmCmd, httpPort, serverRootPath) => {
70
- const cmd = isDev ? esbuild.context : esbuild.build;
71
- const ctx = await cmd({
72
- entryPoints: ['apps/server/src/server-loader.ts'],
73
- // outdir: path.join(serverRootPath, 'server'),
74
- outfile: path.join(serverRootPath, 'server', 'server-loader.js'),
75
- platform: 'node',
76
- sourcemap: isDev ? 'inline' : false, // inline
77
- format: 'cjs',
78
- bundle: true,
79
- treeShaking: true,
80
- metafile: true,
81
- minify: !isDev,
82
- plugins: [watchServerPlugin(isDev, npmCmd, httpPort)],
83
- });
84
-
85
- isDev && (await ctx.watch());
86
- isDev && console.log('[dev-server] Watching...');
87
- };
88
-
89
- // watch server code changes
90
- const watchServer = async (isDev, npmCmd, httpPort, serverRootPath) => {
91
- const cmd = isDev ? esbuild.context : esbuild.build;
92
- const ctx = await cmd({
93
- entryPoints: ['apps/server/src/index.ts'],
94
- outdir: path.join(serverRootPath, 'server'),
95
- platform: 'node',
96
- sourcemap: isDev ? 'inline' : false, // inline
97
- format: 'cjs',
98
- bundle: true,
99
- treeShaking: true,
100
- metafile: true,
101
- external: ['better-sqlite3', 'nodemailer', 'pdfkit', 'sharp'],
102
- loader: { '.svg': 'text', '.glsl': 'text', '.png': 'file', '.gif': 'file', '.html': 'text' },
103
- minify: !isDev,
104
- plugins: [watchServerPlugin(isDev, npmCmd, httpPort)],
105
- });
106
-
107
- isDev && (await ctx.watch());
108
- isDev && console.log('[dev-server] Watching...');
109
- };
110
-
111
- // copy files to output directory
112
- const copyCache = new Map();
113
- const clientProcessOnEnd = async (saved) => {
114
- saved.indexHtml &&
115
- cpIndexHtml(
116
- saved.indexHtml,
117
- path.join(saved.outdir, 'index.html'),
118
- saved.appName,
119
- saved.isDev,
120
- saved.isMobile,
121
- saved.defaultThemeName,
122
- saved.outdirData,
123
- saved.outdirSub
124
- );
125
-
126
- const assets = saved['copyFiles'];
127
- if (assets) {
128
- for (oneDir of assets) {
129
- await copyFolder(copyCache, oneDir.from, oneDir.to, saved.isDev);
130
- }
131
- }
132
-
133
- if (saved.isDev) {
134
- await triggerRefreshServer(saved.isDev, saved.httpPort);
135
- }
136
- };
137
-
138
- // watch client code changes
139
- const watchClientPlugin = (saved) => {
140
- return {
141
- name: 'watchClientPlugin',
142
- setup(build) {
143
- build.onEnd(async (res) => {
144
- // console.log(`Build meta data: `, res);
145
- console.log(`[dev-client:${saved.appName}] Build finished`);
146
- await clientProcessOnEnd(saved);
147
- });
148
- },
149
- };
150
- };
151
-
152
- // plugin for conditional code logic
153
- /*
154
- #if MOBILE && DEV
155
- console.log('this is mobile and dev');
156
- #endif
157
- */
158
- const ifPluginVars = {
159
- MOBILE: '',
160
- DEV: '',
161
- };
162
- const watchClient = async (saved, isDev, entryPoints, outbase) => {
163
- const cmd = isDev ? esbuild.context : esbuild.build;
164
- const ctx = await cmd({
165
- entryPoints,
166
- outdir: saved.outdir,
167
- outbase,
168
- // entryNames: '[name]-[hash]',
169
- platform: 'browser',
170
- sourcemap: isDev ? 'inline' : false, // inline
171
- format: 'iife',
172
- bundle: true,
173
- treeShaking: true,
174
- external: [],
175
- metafile: true,
176
- minify: !isDev,
177
- loader: { '.svg': 'text', '.glsl': 'text', '.png': 'file', '.gif': 'file', '.html': 'text' },
178
- assetNames: '/pub_assets/[name]-[hash]',
179
- publicPath: saved.outdirSub,
180
- jsxImportSource: 'lupine.web',
181
- jsx: 'automatic',
182
- target: ['chrome87'],
183
- plugins: [watchClientPlugin(saved), pluginIfelse(ifPluginVars), obfuscatePlugin(saved.isObfuscate, [])],
184
- });
185
-
186
- isDev && (await ctx.watch());
187
- isDev && console.log(`[dev-client:${saved.appName}] Watching...`);
188
- };
189
-
190
- const watchApiPlugin = (isDev, httpPort) => {
191
- return {
192
- name: 'watchApiPlugin',
193
- setup(build) {
194
- build.onEnd(async (res) => {
195
- console.log(`[dev-api] Build finished`);
196
-
197
- if (isDev) {
198
- await triggerRefreshServer(isDev, httpPort);
199
- }
200
- });
201
- },
202
- };
203
- };
204
-
205
- const watchApi = async (saved, isDev, entryPoints) => {
206
- const cmd = isDev ? esbuild.context : esbuild.build;
207
- const ctx = await cmd({
208
- entryPoints,
209
- outdir: saved.outdirApi,
210
- // outbase,
211
- platform: 'node',
212
- sourcemap: isDev ? 'inline' : false, // inline
213
- format: 'cjs', // iife, cjs
214
- bundle: true,
215
- treeShaking: true,
216
- metafile: true,
217
- external: ['better-sqlite3', 'nodemailer', 'pdfkit', 'sharp'],
218
- loader: { '.svg': 'text', '.glsl': 'text', '.png': 'file', '.gif': 'file', '.html': 'text' },
219
- minify: !isDev,
220
- plugins: [watchApiPlugin(isDev, saved.httpPort)],
221
- });
222
-
223
- isDev && (await ctx.watch());
224
- isDev && console.log(`[dev-api:${saved.appName}] Watching...`);
225
- };
226
-
227
- const watchMarkdownPlugin = (saved) => {
228
- return {
229
- name: 'watchMarkdownPlugin',
230
- setup(build) {
231
- build.onEnd(async (res) => {
232
- // console.log(`Build meta data: `, res);
233
- console.log(`[dev-markdown:${saved.appName}] Build finished`);
234
- await markdownProcessOnEnd(saved.markdownEntryPoints.indir, saved.markdownEntryPoints.outdir);
235
- });
236
- },
237
- };
238
- };
239
-
240
- const watchMarkdown = async (saved, entryPoints) => {
241
- const cmd = saved.isDev ? esbuild.context : esbuild.build;
242
- const ctx = await cmd({
243
- entryPoints: [`${entryPoints.indir}/**/*.md`],
244
- outdir: entryPoints.outdir,
245
- write: false,
246
- loader: { '.md': 'empty' },
247
- plugins: [watchMarkdownPlugin({ ...saved, markdownEntryPoints: entryPoints })],
248
- });
249
-
250
- saved.isDev && (await ctx.watch());
251
- };
252
-
253
- const watchAdditionalFiles = async (saved, entryPoints) => {
254
- const cmd = saved.isDev ? esbuild.context : esbuild.build;
255
- const ctx = await cmd({
256
- entryPoints: entryPoints,
257
- outdir: 'dist/tmp',
258
- write: false,
259
- loader: { '.html': 'empty', '.css': 'empty', '.js': 'empty', '.md': 'empty' },
260
- plugins: [watchClientPlugin(saved)],
261
- });
262
-
263
- saved.isDev && (await ctx.watch());
264
- };
265
-
266
- // Add Server, API, Client and additional files entry points to esbuild
267
- const start = async () => {
268
- // Any code accessing process.env should be after loadEnv
269
- // process env files, "#!import .env" in .env file can be used to include another env file
270
- let envFile = process.argv.find((i) => i.startsWith('--env='))?.substring(6);
271
- // load env file, but don't overwrite existing env variables
272
- await loadEnv(envFile);
273
-
274
- const serverRootPathEnv = process.env['SERVER_ROOT_PATH'];
275
- if (!serverRootPathEnv) {
276
- console.error('SERVER_ROOT_PATH is not set');
277
- return 0;
278
- }
279
- const serverRootPath = path.resolve(serverRootPathEnv);
280
- await fs.mkdir(serverRootPath, { recursive: true });
281
- if (!(await pathExists(serverRootPath))) {
282
- console.error(`Can't create server root path: ${serverRootPath}`);
283
- return 0;
284
- }
285
-
286
- // command to start the server when source files are changed
287
- const npmCmd = process.argv.find((i) => i.startsWith('--cmd='))?.substring(6);
288
- const isDev = process.argv.find((i) => i === '--dev=1');
289
- const isMobile = process.argv.find((i) => i === '--mobile=1'); // when this changed, need to rebuild index.html
290
- const isObfuscate = !isDev && process.argv.find((i) => i === '--obfuscate=1');
291
- // this is for esbuild conditional compile
292
- ifPluginVars.DEV = isDev ? '1' : '';
293
- ifPluginVars.MOBILE = isMobile ? '1' : '';
294
-
295
- // All apps should be defined in .env file like this:
296
- // APPS=domain1.com,domain2.com
297
- const apps = (process.env['APPS'] || '').split(',');
298
-
299
- const httpPort = process.env['HTTP_PORT'];
300
- for (const app of apps) {
301
- const appJson = `apps/${app}/lupine.json`;
302
- const additionalFiles = [];
303
- const appCfg = await readJson(appJson);
304
- const appName = appCfg['name'];
305
- const appDir = path.dirname(appJson);
306
- if (appCfg['watchFiles']) {
307
- appCfg['watchFiles'].forEach((item) => additionalFiles.push(`${appDir}/${item}`));
308
- }
309
-
310
- // create data folder
311
- await fs.mkdir(`${serverRootPath}/${appName}_data`, { recursive: true });
312
-
313
- const saved = {
314
- isDev,
315
- isMobile,
316
- isObfuscate,
317
- defaultThemeName: 'light',
318
- appName,
319
- httpPort,
320
- serverRoot: serverRootPath,
321
- outdirWeb: `${serverRootPath}/${appName}_web`,
322
- outdirApi: `${serverRootPath}/${appName}_api`,
323
- outdirData: `${serverRootPath}/${appName}_data`,
324
- copyFiles: appCfg['copyFiles'].map((item) => ({
325
- from: `${appDir}/${item.from}`,
326
- to:
327
- item.type == 'data'
328
- ? `${serverRootPath}/${appName}_data/${item.to}`
329
- : `${serverRootPath}/${appName}_web/${item.to}`,
330
- })),
331
- };
332
-
333
- appCfg['webEntryPoints'].forEach((item, index) => {
334
- const entryPoint = `${appDir}/${item.index}`;
335
- const indexHtml = `${appDir}/${item.html}`;
336
- watchClient(
337
- {
338
- ...saved,
339
- outdirSub: item.outdir,
340
- outdir: `${serverRootPath}/${appName}_web/` + item.outdir,
341
- indexHtml,
342
- copyFiles: [],
343
- },
344
- isDev,
345
- [entryPoint],
346
- path.dirname(entryPoint)
347
- );
348
- additionalFiles.push(indexHtml);
349
- });
350
-
351
- if (appCfg['apiEntryPoint']) {
352
- const entryPointApi = `${appDir}/${appCfg['apiEntryPoint']}`;
353
- watchApi(saved, isDev, [entryPointApi]);
354
- }
355
- if (appCfg['markdownEntryPoints']) {
356
- const markdownEntryPoints = {
357
- indir: `${appDir}/${appCfg['markdownEntryPoints'].indir}`,
358
- outdir: `${appDir}/${appCfg['markdownEntryPoints'].outdir}`,
359
- };
360
- watchMarkdown(saved, markdownEntryPoints);
361
- }
362
- if (additionalFiles.length > 0) {
363
- // when some resources are changed, need to run command or refresh the server
364
- watchAdditionalFiles(saved, additionalFiles);
365
- }
366
- }
367
-
368
- watchServer(isDev, npmCmd, httpPort, serverRootPath);
369
- watchServerLoader(isDev, npmCmd, httpPort, serverRootPath);
370
- };
371
- start();