makepack 1.6.8 → 1.6.10
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.
- package/package.json +6 -7
- package/src/actions/build/index.js +14 -14
- package/src/actions/create/index.js +3 -11
- package/src/actions/start/index.js +150 -96
- package/src/actions/start/vite.js +1 -0
- package/src/helpers.js +46 -11
- package/src/index.js +0 -0
- package/src/actions/start/express.js +0 -43
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "makepack",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A CLI tool to create, build, and manage JavaScript, TypeScript, React, and React-TypeScript libraries for npm projects.",
|
|
6
6
|
"files": [
|
|
@@ -33,16 +33,14 @@
|
|
|
33
33
|
"chalk": "^5.4.1",
|
|
34
34
|
"chokidar": "^4.0.3",
|
|
35
35
|
"commander": "^12.1.0",
|
|
36
|
-
"
|
|
37
|
-
"esbuild": "^0.25.0",
|
|
36
|
+
"esbuild": "^0.25.5",
|
|
38
37
|
"express": "^4.21.1",
|
|
39
|
-
"figlet": "^1.8.0",
|
|
40
|
-
"figures": "^6.1.0",
|
|
41
38
|
"fs-extra": "^11.2.0",
|
|
42
39
|
"glob": "^11.0.0",
|
|
43
40
|
"inquirer": "^12.1.0",
|
|
41
|
+
"lodash.debounce": "^4.0.8",
|
|
42
|
+
"madge": "^8.0.0",
|
|
44
43
|
"ora": "^8.1.1",
|
|
45
|
-
"typescript": "^5.7.2",
|
|
46
44
|
"vite": "^6.0.2"
|
|
47
45
|
},
|
|
48
46
|
"keywords": [
|
|
@@ -57,6 +55,7 @@
|
|
|
57
55
|
"devDependencies": {
|
|
58
56
|
"@types/fs-extra": "^11.0.4",
|
|
59
57
|
"react": "^19.0.0",
|
|
60
|
-
"react-dom": "^19.0.0"
|
|
58
|
+
"react-dom": "^19.0.0",
|
|
59
|
+
"typescript": "^5.7.2"
|
|
61
60
|
}
|
|
62
61
|
}
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import esbuild from 'esbuild'
|
|
2
2
|
import fs from 'fs-extra'
|
|
3
3
|
import path from 'path'
|
|
4
|
-
import chalk from 'chalk'
|
|
5
4
|
import ora from 'ora'
|
|
6
5
|
import { glob } from 'glob'
|
|
7
6
|
import ts from 'typescript'
|
|
7
|
+
import { concolor, logger } from '../../helpers.js'
|
|
8
8
|
|
|
9
9
|
const eBuild = async (conf) => {
|
|
10
10
|
|
|
11
11
|
await esbuild.build({
|
|
12
12
|
jsx: 'automatic',
|
|
13
13
|
...conf,
|
|
14
|
-
// outExtension: { '.js': ebconfig.format === 'esm' ? '.mjs' : '.cjs' },
|
|
15
14
|
loader: {
|
|
16
15
|
'.ts': 'ts',
|
|
17
16
|
'.tsx': 'tsx'
|
|
@@ -70,12 +69,12 @@ const build = async (args) => {
|
|
|
70
69
|
}
|
|
71
70
|
if (args.format === 'default') {
|
|
72
71
|
await eBuild({ ...config, format: "esm" });
|
|
73
|
-
|
|
72
|
+
logger.success('ESM build generated successfully!');
|
|
74
73
|
await eBuild({ ...config, format: "cjs" });
|
|
75
|
-
|
|
74
|
+
logger.success('CJS build generated successfully!');
|
|
76
75
|
} else {
|
|
77
76
|
await eBuild({ ...config, format: args.format });
|
|
78
|
-
|
|
77
|
+
logger.success(`${args.format} build generated successfully!`);
|
|
79
78
|
}
|
|
80
79
|
}
|
|
81
80
|
|
|
@@ -91,7 +90,7 @@ const build = async (args) => {
|
|
|
91
90
|
);
|
|
92
91
|
|
|
93
92
|
if (!parsedConfig) {
|
|
94
|
-
|
|
93
|
+
logger.error("Error parsing tsconfig.json");
|
|
95
94
|
process.exit(1);
|
|
96
95
|
} else {
|
|
97
96
|
tsconfig = parsedConfig.options;
|
|
@@ -120,13 +119,13 @@ const build = async (args) => {
|
|
|
120
119
|
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
|
|
121
120
|
if (diagnostic.file) {
|
|
122
121
|
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
|
123
|
-
|
|
122
|
+
logger.error(`Error at ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
|
|
124
123
|
} else {
|
|
125
|
-
|
|
124
|
+
logger.error(`${message}`);
|
|
126
125
|
}
|
|
127
126
|
});
|
|
128
127
|
} else {
|
|
129
|
-
|
|
128
|
+
logger.success("TypeScript declaration files generated successfully!")
|
|
130
129
|
}
|
|
131
130
|
}
|
|
132
131
|
spinner.text = "Copying package.json and readme.md files..."
|
|
@@ -139,15 +138,16 @@ const build = async (args) => {
|
|
|
139
138
|
delete pkgjson.type
|
|
140
139
|
fs.writeFileSync(path.join(outdir, 'package.json'), JSON.stringify(pkgjson, null, 2));
|
|
141
140
|
} else {
|
|
142
|
-
|
|
141
|
+
logger.error("package.json not found!");
|
|
143
142
|
return;
|
|
144
143
|
}
|
|
145
144
|
|
|
146
145
|
fs.copyFileSync(path.join(process.cwd(), '/readme.md'), path.join(outdir, `/readme.md`))
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
146
|
+
logger.success("package.json and readme.md files copied successfully!")
|
|
147
|
+
logger.success(`completed successfully!`, 'BUILD:', true);
|
|
148
|
+
logger.info(`To publish your package to npm:`, 'INFO:', true);
|
|
149
|
+
logger.info(`${concolor.yellow(`npm run release`)} or navigate to the ${concolor.yellow(`.mpack`)} directory and run ${concolor.yellow(`npm publish`)}`, 'RELEASE:', true)
|
|
150
|
+
spinner.stop();
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
export default build
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { execSync, logger } from "../../helpers.js"
|
|
1
|
+
import { execSync, concolor, logger } from "../../helpers.js"
|
|
2
2
|
import makeFiles from "./makeFiles.js"
|
|
3
3
|
import path from 'path'
|
|
4
4
|
import inquirer from 'inquirer'
|
|
5
|
-
import figlet from 'figlet'
|
|
6
5
|
import fs from "fs-extra"
|
|
7
|
-
import chalk from 'chalk';
|
|
8
6
|
const cwd = process.cwd()
|
|
9
7
|
const cwdFolder = cwd.split(path.sep).pop()
|
|
10
8
|
|
|
@@ -89,16 +87,10 @@ const create = async () => {
|
|
|
89
87
|
|
|
90
88
|
logger.success("Project setup complete!", "")
|
|
91
89
|
if (isCurrentDir) {
|
|
92
|
-
|
|
90
|
+
logger.info(`Run the development server: ${concolor.yellow("npm start")}\nEnjoy your new project! 😊`);
|
|
93
91
|
} else {
|
|
94
|
-
|
|
92
|
+
logger.info(`Navigate to your project directory:\n${concolor.yellow("cd " + info.pdir, false)} and Run the development server: ${concolor.yellow("npm start")}\nEnjoy your new project! 😊`);
|
|
95
93
|
}
|
|
96
|
-
|
|
97
|
-
figlet("Makepack CLI", function (err, data) {
|
|
98
|
-
if (!err) {
|
|
99
|
-
console.log(data);
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
94
|
}
|
|
103
95
|
|
|
104
96
|
export default create
|
|
@@ -1,118 +1,172 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { pathToFileURL } from 'url';
|
|
5
|
+
import { createRequire } from 'module';
|
|
6
6
|
import chokidar from 'chokidar';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
7
|
+
import madge from 'madge';
|
|
8
|
+
import viteSetup from './vite.js';
|
|
9
|
+
import * as esbuild from 'esbuild';
|
|
10
|
+
import { randomUUID } from 'crypto';
|
|
11
|
+
import debounce from 'lodash.debounce';
|
|
12
|
+
import { logger, concolor } from '../../helpers.js';
|
|
13
|
+
|
|
14
|
+
const projectRoot = process.cwd();
|
|
15
|
+
const requireFn = createRequire(import.meta.url);
|
|
16
|
+
|
|
17
|
+
let server = null;
|
|
18
|
+
let app = null;
|
|
19
|
+
let viteServer = null;
|
|
20
|
+
|
|
21
|
+
const pkg = path.join(process.cwd(), 'package.json');
|
|
22
|
+
const pkgjson = JSON.parse(fs.readFileSync(pkg, 'utf-8'));
|
|
23
|
+
let isEsmProject = pkgjson.type === 'module';
|
|
24
|
+
|
|
25
|
+
const uxpfileJS = path.resolve(projectRoot, 'express.js');
|
|
26
|
+
const uxpfileTS = path.resolve(projectRoot, 'express.ts');
|
|
27
|
+
const expExists = fs.existsSync(uxpfileJS) || fs.existsSync(uxpfileTS);
|
|
28
|
+
let uxpfile = expExists ? (fs.existsSync(uxpfileJS) ? uxpfileJS : uxpfileTS) : null;
|
|
29
|
+
|
|
30
|
+
const connections = new Set();
|
|
31
|
+
|
|
32
|
+
function trackConnections(srv) {
|
|
33
|
+
srv.on('connection', (conn) => {
|
|
34
|
+
connections.add(conn);
|
|
35
|
+
conn.on('close', () => connections.delete(conn));
|
|
27
36
|
});
|
|
28
|
-
return server;
|
|
29
37
|
}
|
|
30
38
|
|
|
31
|
-
|
|
32
|
-
let
|
|
39
|
+
async function bootServer(args) {
|
|
40
|
+
let wasServer = !!server;
|
|
41
|
+
if (server) {
|
|
42
|
+
for (const conn of connections) {
|
|
43
|
+
conn.destroy();
|
|
44
|
+
}
|
|
45
|
+
await new Promise((resolve, reject) => {
|
|
46
|
+
server.close(err => {
|
|
47
|
+
if (err) {
|
|
48
|
+
logger.error(`while closing server: ${err.message || err}`);
|
|
49
|
+
reject(err);
|
|
50
|
+
} else {
|
|
51
|
+
resolve();
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
server = null;
|
|
56
|
+
}
|
|
33
57
|
|
|
34
|
-
if (
|
|
35
|
-
|
|
58
|
+
if (viteServer) {
|
|
59
|
+
await viteServer.close();
|
|
36
60
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
61
|
+
|
|
62
|
+
app = express();
|
|
63
|
+
try {
|
|
64
|
+
const middleware = await loadExp();
|
|
65
|
+
if (typeof middleware === 'function') {
|
|
66
|
+
middleware(app);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
viteServer = await viteSetup(app);
|
|
70
|
+
const port = args.port || 4000;
|
|
71
|
+
server = app.listen(port, () => {
|
|
72
|
+
if (!wasServer) {
|
|
73
|
+
logger.success(`Server running on: ${concolor.green(concolor.bold(`http://localhost:${port}`))}`, '')
|
|
74
|
+
}
|
|
75
|
+
trackConnections(server);
|
|
76
|
+
})
|
|
77
|
+
} catch (err) {
|
|
78
|
+
logger.error(`Failed to start server: ${err.message || err}`);
|
|
41
79
|
}
|
|
42
|
-
|
|
80
|
+
}
|
|
43
81
|
|
|
44
82
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
83
|
+
let esbuildCtx = null;
|
|
84
|
+
const mpack = path.join(projectRoot, '.mpack');
|
|
85
|
+
if (fs.existsSync(mpack)) {
|
|
86
|
+
fs.rmSync(mpack, { recursive: true, force: true });
|
|
87
|
+
}
|
|
88
|
+
fs.mkdirSync(mpack, { recursive: true });
|
|
89
|
+
const buildFile = path.join(mpack, `${randomUUID().substring(0, 15)}.js`);
|
|
90
|
+
|
|
91
|
+
async function loadExp() {
|
|
92
|
+
if (!expExists) return null
|
|
93
|
+
|
|
94
|
+
const cacheKeys = Object.keys(requireFn.cache || {});
|
|
95
|
+
if (!isEsmProject) {
|
|
96
|
+
for (const key of cacheKeys) {
|
|
97
|
+
if (key.startsWith(process.cwd())) {
|
|
98
|
+
delete requireFn.cache[key];
|
|
99
|
+
}
|
|
53
100
|
}
|
|
54
101
|
}
|
|
55
102
|
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
if (
|
|
60
|
-
|
|
61
|
-
|
|
103
|
+
const ext = path.extname(uxpfile);
|
|
104
|
+
const isTs = ext === '.ts';
|
|
105
|
+
|
|
106
|
+
if (isTs) {
|
|
107
|
+
if (esbuildCtx) {
|
|
108
|
+
await esbuildCtx.rebuild();
|
|
109
|
+
} else {
|
|
110
|
+
esbuildCtx = await esbuild.context({
|
|
111
|
+
entryPoints: [uxpfile],
|
|
112
|
+
outfile: buildFile,
|
|
113
|
+
format: isEsmProject ? 'esm' : 'cjs',
|
|
114
|
+
platform: 'node',
|
|
115
|
+
sourcemap: 'inline',
|
|
116
|
+
bundle: true,
|
|
117
|
+
packages: 'external',
|
|
118
|
+
});
|
|
119
|
+
await esbuildCtx.rebuild();
|
|
120
|
+
}
|
|
62
121
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
packages: 'external',
|
|
70
|
-
})
|
|
71
|
-
ctx.watch()
|
|
122
|
+
if (isEsmProject) {
|
|
123
|
+
const mod = await import(pathToFileURL(buildFile).href + `?update=${Date.now()}`);
|
|
124
|
+
return mod.default || mod;
|
|
125
|
+
} else {
|
|
126
|
+
return requireFn(buildFile);
|
|
127
|
+
}
|
|
72
128
|
}
|
|
73
129
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
define: {
|
|
82
|
-
'process.env.PORT': JSON.stringify(port),
|
|
83
|
-
},
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
let server = startServer();
|
|
87
|
-
|
|
88
|
-
const uxp = path.join(mpack, 'uxp.js')
|
|
89
|
-
|
|
90
|
-
if (fs.existsSync(uxp)) {
|
|
91
|
-
const watcher = chokidar.watch(uxp, {
|
|
92
|
-
persistent: true,
|
|
93
|
-
ignoreInitial: true,
|
|
94
|
-
});
|
|
130
|
+
if (isEsmProject) {
|
|
131
|
+
const mod = await import(pathToFileURL(uxpfile).href + `?update=${Date.now()}`);
|
|
132
|
+
return mod.default || mod;
|
|
133
|
+
} else {
|
|
134
|
+
return requireFn(uxpfile);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
95
137
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
138
|
+
async function getAllDependencies() {
|
|
139
|
+
try {
|
|
140
|
+
const result = await madge(uxpfile, { fileExtensions: ['ts', 'js'] });
|
|
141
|
+
const deps = Object.keys(result.obj());
|
|
142
|
+
const circular = await result.circular();
|
|
143
|
+
if (circular.length) {
|
|
144
|
+
logger.warn(`Circular dependencies detected: ${circular.map(c => c.join(' -> ')).join(', ')}`);
|
|
145
|
+
}
|
|
146
|
+
return deps.map(dep => path.resolve(path.dirname(uxpfile), dep));
|
|
147
|
+
} catch (err) {
|
|
148
|
+
logger.error(`Failed to analyze dependencies with madge: ${err.message || err}`);
|
|
149
|
+
return [uxpfile];
|
|
101
150
|
}
|
|
151
|
+
}
|
|
102
152
|
|
|
103
|
-
process.on('SIGINT', () => {
|
|
104
|
-
console.log('Received SIGINT, killing server...');
|
|
105
|
-
server.kill('SIGINT');
|
|
106
|
-
process.exit(0);
|
|
107
|
-
});
|
|
108
153
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
154
|
+
let watcher;
|
|
155
|
+
async function startDevServer(args) {
|
|
156
|
+
await bootServer(args);
|
|
157
|
+
const filesToWatch = await getAllDependencies();
|
|
158
|
+
if (watcher) watcher.close();
|
|
159
|
+
watcher = chokidar.watch(filesToWatch, { ignoreInitial: true });
|
|
114
160
|
|
|
115
|
-
|
|
161
|
+
const reload = debounce(async (f) => {
|
|
162
|
+
await bootServer(args);
|
|
163
|
+
const prettyPath = concolor.dim(path.relative(process.cwd(), f));
|
|
164
|
+
logger.info(`${concolor.green('server reload')} ${prettyPath}`);
|
|
165
|
+
}, 100);
|
|
116
166
|
|
|
167
|
+
watcher.on('change', reload);
|
|
168
|
+
watcher.on('add', reload);
|
|
169
|
+
watcher.on('unlink', reload);
|
|
170
|
+
}
|
|
117
171
|
|
|
118
|
-
export default
|
|
172
|
+
export default startDevServer
|
package/src/helpers.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
import child_process from 'child_process'
|
|
2
|
-
import chalk from 'chalk';
|
|
3
|
-
import figures from 'figures';
|
|
4
|
-
|
|
5
2
|
export const execSync = (command, option = {}) => {
|
|
6
3
|
try {
|
|
7
4
|
const result = child_process.execSync(command, {
|
|
@@ -17,21 +14,59 @@ export const execSync = (command, option = {}) => {
|
|
|
17
14
|
};
|
|
18
15
|
|
|
19
16
|
|
|
17
|
+
export const conicon = {
|
|
18
|
+
info: 'ℹ',
|
|
19
|
+
success: '✔',
|
|
20
|
+
warning: '⚠',
|
|
21
|
+
error: '✖',
|
|
22
|
+
dot: '․',
|
|
23
|
+
pointer: '❯',
|
|
24
|
+
arrowRight: '→',
|
|
25
|
+
arrowDown: '↓',
|
|
26
|
+
arrowUp: '↑',
|
|
27
|
+
star: '★',
|
|
28
|
+
check: '✅',
|
|
29
|
+
cross: '❌',
|
|
30
|
+
question: '?',
|
|
31
|
+
ellipsis: '…',
|
|
32
|
+
clock: '⏱',
|
|
33
|
+
hourglass: '⏳',
|
|
34
|
+
rocket: '🚀',
|
|
35
|
+
bug: '🐞',
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const concolor = {
|
|
39
|
+
reset: (str) => `\x1b[0m${str}\x1b[0m`,
|
|
40
|
+
red: (str) => `\x1b[31m${str}\x1b[0m`,
|
|
41
|
+
green: (str) => `\x1b[32m${str}\x1b[0m`,
|
|
42
|
+
yellow: (str) => `\x1b[33m${str}\x1b[0m`,
|
|
43
|
+
blue: (str) => `\x1b[34m${str}\x1b[0m`,
|
|
44
|
+
magenta: (str) => `\x1b[35m${str}\x1b[0m`,
|
|
45
|
+
cyan: (str) => `\x1b[36m${str}\x1b[0m`,
|
|
46
|
+
white: (str) => `\x1b[37m${str}\x1b[0m`,
|
|
47
|
+
bold: (str) => `\x1b[1m${str}\x1b[0m`,
|
|
48
|
+
dim: (str) => `\x1b[2m${str}\x1b[0m`,
|
|
49
|
+
underline: (str) => `\x1b[4m${str}\x1b[0m`,
|
|
50
|
+
};
|
|
51
|
+
|
|
20
52
|
|
|
21
53
|
export const logger = {
|
|
54
|
+
log: (message, prefix, icon, color) => {
|
|
55
|
+
let _color = concolor[color] || concolor.reset;
|
|
56
|
+
let _icon = conicon[icon] || '';
|
|
57
|
+
prefix = prefix ? _color(concolor.bold(prefix)) : "";
|
|
58
|
+
console.log(`${_icon ? _color(_icon) + " " : ""}${prefix} ${message}`);
|
|
59
|
+
},
|
|
22
60
|
info: (message, prefix = 'INFO', icon = true) => {
|
|
23
|
-
|
|
61
|
+
logger.log(message, prefix, icon ? 'info' : '', 'blue');
|
|
24
62
|
},
|
|
25
63
|
success: (message, prefix = 'SUCCESS:', icon = true) => {
|
|
26
|
-
|
|
64
|
+
logger.log(message, prefix, icon ? 'success' : '', 'green');
|
|
27
65
|
},
|
|
28
66
|
warning: (message, prefix = 'WARNING:', icon = true) => {
|
|
29
|
-
|
|
67
|
+
logger.log(message, prefix, icon ? 'warning' : '', 'yellow');
|
|
30
68
|
},
|
|
31
69
|
error: (message, prefix = 'ERROR:', icon = true) => {
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
custom: (icon, color, label, message) => {
|
|
35
|
-
console.log(`${chalk[color](icon)} ${chalk[color].bold(`${label}:`)} ${message}`);
|
|
36
|
-
},
|
|
70
|
+
logger.log(message, prefix, icon ? 'error' : '', 'red');
|
|
71
|
+
}
|
|
37
72
|
};
|
package/src/index.js
CHANGED
|
File without changes
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import express from 'express';
|
|
2
|
-
import { logger } from '../../helpers.js'
|
|
3
|
-
import chalk from 'chalk';
|
|
4
|
-
import path from 'path'
|
|
5
|
-
import fs from 'fs'
|
|
6
|
-
import viteSetup from './vite.js';
|
|
7
|
-
const port = process.env.PORT || 3000;
|
|
8
|
-
const app = express();
|
|
9
|
-
const server = async () => {
|
|
10
|
-
|
|
11
|
-
// get type from package.json
|
|
12
|
-
const pkg = path.join(process.cwd(), 'package.json');
|
|
13
|
-
let type = 'module';
|
|
14
|
-
if (fs.existsSync(pkg)) {
|
|
15
|
-
const pkgjson = JSON.parse(fs.readFileSync(pkg, 'utf-8'));
|
|
16
|
-
type = pkgjson.type || 'module';
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const mpack = path.join(process.cwd(), '.mpack');
|
|
20
|
-
const uxp = path.join(mpack, 'uxp.js')
|
|
21
|
-
if (fs.existsSync(uxp)) {
|
|
22
|
-
// load user-express.js based on type
|
|
23
|
-
if (type === 'module') {
|
|
24
|
-
const { default: userExpress } = await import(uxp);
|
|
25
|
-
userExpress(app);
|
|
26
|
-
} else {
|
|
27
|
-
const userExpress = require(uxp).default;
|
|
28
|
-
userExpress(app);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
await viteSetup(app)
|
|
34
|
-
app.use((_req, res) => {
|
|
35
|
-
res.status(500).send('Internal Server Error');
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
app.listen(port, () => {
|
|
39
|
-
logger.success(`Server is running on ${chalk.blue(chalk.underline(`http://localhost:${port}`))}`);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
server()
|