miolo 3.0.0-beta.2 → 3.0.0-beta.20

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.
@@ -3,6 +3,7 @@ import path from 'node:path'
3
3
  import { fileURLToPath } from 'node:url'
4
4
  import { fork } from 'node:child_process'
5
5
 
6
+
6
7
  const __filename = fileURLToPath(import.meta.url)
7
8
  const __dirname = path.dirname(__filename)
8
9
 
@@ -19,7 +20,7 @@ function _isProcessRunning(pid) {
19
20
  }
20
21
  }
21
22
 
22
- async function startDevServerProcess({ appName }) {
23
+ async function startDevServerProcess({ appName }) {
23
24
  const serverPath = path.join(__dirname, './dev_start.mjs')
24
25
  serverProcess = fork(serverPath)
25
26
 
@@ -65,7 +66,12 @@ async function startDevServerProcess({ appName }) {
65
66
  }
66
67
 
67
68
 
68
- export default async function(appName) {
69
+ export default async function(appName= undefined) {
70
+
71
+
72
+ // Based on command line params or .env
73
+ appName = appName || process.env.MIOLO_NAME
74
+
69
75
  console.log(`[${appName}][dev] Running DEV server`)
70
76
  await startDevServerProcess({ appName })
71
77
  }
@@ -1,8 +1,8 @@
1
1
  import path from 'node:path'
2
- import {miolo_dev} from '../src/server-dev.mjs'
2
+ import {miolo_dev} from '../../src/server-dev.mjs'
3
3
 
4
4
  async function _miolo_dev_start_server() {
5
- const config = await import(path.join(process.cwd(), process.env.MIOLO_CONFIG))
5
+ const config = await import(path.join(process.cwd(), process.env.MIOLO_DEV_CONFIG_ENTRY))
6
6
  const app = await miolo_dev(config.default)
7
7
  await app.start()
8
8
  return app
package/bin/index.mjs CHANGED
@@ -1,66 +1,75 @@
1
1
  #!/usr/bin/env node
2
+ import path from 'node:path'
2
3
  import yargs from 'yargs-parser'
3
- import { init_env_config } from './env.mjs'
4
+ import { init_env_config } from '../src/config/env.mjs'
5
+
4
6
 
5
7
  async function main() {
6
8
  const args = yargs(process.argv.slice(2))
7
9
  const command = args._[0]
8
10
 
11
+ // Init env
12
+ init_env_config()
13
+
14
+ // Init vars
15
+ const appName = args['app-name'] || process.env.MIOLO_NAME
16
+ const serverExt = 'node.bundle.mjs'
17
+
9
18
  try {
10
- init_env_config()
11
- const appName = process.env.MIOLO_NAME
12
- const serverName = args['server-name'] || 'miolo_server'
13
19
 
14
20
  switch (command) {
15
21
  case 'dev':
16
22
  process.env.NODE_ENV = 'development'
17
- const devHandler = (await import ('./dev.mjs')).default
23
+ const devHandler = (await import ('./dev/dev.mjs')).default
18
24
  await devHandler(appName)
19
25
  break
20
26
 
21
27
  case 'build-client':
22
- process.env.NODE_ENV = 'production'
23
- const buildClientHandler = (await import ('./build-client.mjs')).default
24
- const clientEntry = args.entry || process.env.MIOLO_BUILD_CLIENT_ENTRY
28
+ const entry = args.entry || process.env.MIOLO_BUILD_CLIENT_ENTRY
25
29
  const htmlFile = args['html-file'] || process.env.MIOLO_BUILD_HTML_FILE
26
- const clientDest = args.dest || process.env.MIOLO_BUILD_CLIENT_DEST
27
- await buildClientHandler(appName, /*entry*/ clientEntry, htmlFile, /*dest*/ clientDest)
30
+ const cliDest = args.dest || process.env.MIOLO_BUILD_CLIENT_DEST
31
+
32
+ const buildClientHandler = (await import ('./prod-build/build-client.mjs')).default
33
+ await buildClientHandler(appName, entry, htmlFile, cliDest)
28
34
  break
29
35
 
30
36
  case 'build-server':
31
- process.env.NODE_ENV = 'production'
32
- const buildServerHandler = (await import ('./build-server.mjs')).default
33
- const ssrEntry = args['ssr-entry'] || process.env.MIOLO_BUILD_SERVER_SSR_ENTRY
34
- const ssrDest = args['ssr-dest'] || process.env.MIOLO_BUILD_SERVER_DEST
35
- const serverDest = args.dest || process.env.MIOLO_BUILD_SERVER_DEST
36
- await buildServerHandler(appName, ssrEntry, ssrDest, /*dest*/ serverDest)
37
+ // Based on command line params or .env
38
+ const srvEntry= args['entry'] || process.env.MIOLO_BUILD_SERVER_ENTRY
39
+ const ssrEntry= args['ssr-entry'] || process.env.MIOLO_BUILD_SERVER_SSR_ENTRY
40
+ const ssrDest= args['ssr-dest'] || process.env.MIOLO_BUILD_SERVER_DEST
41
+ const srvDest= args.dest || process.env.MIOLO_BUILD_SERVER_DEST
42
+
43
+ const buildServerHandler = (await import ('./prod-build/build-server.mjs')).default
44
+ await buildServerHandler(appName, ssrEntry, ssrDest, srvEntry, srvDest)
37
45
  break
38
46
 
39
47
  case 'start':
40
- process.env.NODE_ENV = 'production'
41
- const startHandler = (await import ('./start.mjs')).default
42
- const startDest = args.dest || process.env.MIOLO_BUILD_SERVER_DEST
43
- await startHandler(appName, /*dest*/ startDest, serverName)
48
+
49
+ const destFile = args.dest || path.join(process.cwd(), `${process.env.MIOLO_BUILD_SERVER_DEST}/${appName}.${serverExt}`)
50
+
51
+ const startHandler = (await import ('./prod-run/start.mjs')).default
52
+ await startHandler(appName, destFile)
44
53
  break
45
54
 
46
55
  case 'stop':
47
- process.env.NODE_ENV = 'production'
48
- const stopHandler = (await import ('./stop.mjs')).default
56
+ const stopHandler = (await import ('./prod-run/stop.mjs')).default
49
57
  await stopHandler(appName)
50
58
  break
51
59
 
52
60
  case 'restart':
53
- process.env.NODE_ENV = 'production'
54
- const restartHandler = (await import ('./restart.mjs')).default
55
- const restartDest = args.dest || process.env.MIOLO_BUILD_SERVER_DEST
56
- await restartHandler({appName, /*dest*/ restartDest, serverName})
61
+ const restartDestFile = args.dest || path.join(process.cwd(), `${process.env.MIOLO_BUILD_SERVER_DEST}/${appName}.${serverExt}`)
62
+
63
+ const restartHandler = (await import ('./prod-run/restart.mjs')).default
64
+ await restartHandler(appName, restartDestFile)
57
65
  break
58
66
 
59
67
  case 'create-bin':
60
- process.env.NODE_ENV = 'production'
61
- const createHandler = (await import ('./create-bin.mjs')).default
62
- const createDest = args.dest || process.env.MIOLO_BUILD_SERVER_DEST
63
- await createHandler(appName, /*dest*/ createDest, serverName)
68
+ const dest = args.dest || process.env.MIOLO_BUILD_SERVER_DEST
69
+ const cbinDestFile = `./${appName}.${serverExt}`
70
+
71
+ const createHandler = (await import ('./prod-bin/create-bin.mjs')).default
72
+ await createHandler(appName, dest, cbinDestFile)
64
73
  break
65
74
 
66
75
  default:
@@ -0,0 +1,31 @@
1
+ import {readFileSync, writeFileSync} from 'node:fs'
2
+ import path from 'path'
3
+ import { fileURLToPath } from 'url'
4
+ import { copyFileSync } from '../util.mjs'
5
+
6
+ const __my_filename = fileURLToPath(import.meta.url)
7
+ const __my_dirname = path.dirname(__my_filename)
8
+
9
+ export default async function(appName, dest, destFile) {
10
+ console.log(`[${appName}][prod][create-bin] Creating bin files...`)
11
+
12
+ const readSource = (f) => readFileSync(path.resolve(__my_dirname, '../prod-run', f), {encoding:'utf8'})
13
+ const writeDest = (f, content) => writeFileSync(path.join(process.cwd(), dest, f), content, {encoding:'utf8',flag:'w'})
14
+
15
+ const pidContent = readSource('./pid.mjs')
16
+ writeDest('pid.mjs', pidContent)
17
+
18
+ let startContent = readSource('./start.mjs')
19
+ startContent = startContent.replace('(appName, destFile) {', `(appName= '${appName}', destFile= '${destFile}') {`)
20
+ writeDest('start.mjs', startContent)
21
+
22
+ let stopContent = readSource('./stop.mjs')
23
+ stopContent = stopContent.replace('(appName) {', `(appName= '${appName}') {`)
24
+ writeDest('stop.mjs', stopContent)
25
+
26
+ let restartContent = readSource('./restart.mjs')
27
+ restartContent = restartContent.replace('(appName, destFile) {', `(appName= '${appName}', destFile= '${destFile}') {`)
28
+ writeDest('restart.mjs', restartContent)
29
+
30
+ copyFileSync(path.join(__my_dirname, './run.mjs'), path.join(process.cwd(), dest, 'run.mjs'))
31
+ }
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env node
2
+ async function miolo_run() {
3
+ const args = process.argv.slice(2)
4
+ const command = args[0]
5
+
6
+ try {
7
+ switch (command) {
8
+ case 'start':
9
+ const startHandler = (await import ('./start.mjs')).default
10
+ await startHandler()
11
+ break
12
+
13
+ case 'stop':
14
+ const stopHandler = (await import ('./stop.mjs')).default
15
+ await stopHandler()
16
+ break
17
+
18
+ case 'restart':
19
+ const restartHandler = (await import ('./restart.mjs')).default
20
+ await restartHandler()
21
+ break
22
+
23
+ default:
24
+ console.error(`[miolo] Unknown command: ${command}`)
25
+ console.log('[miolo] Available commands: start, stop, restart')
26
+ process.exit(1)
27
+ }
28
+ } catch (error) {
29
+ console.error('[miolo] Error during command execution:', error)
30
+ process.exit(1)
31
+ }
32
+ }
33
+
34
+ miolo_run()
@@ -1,9 +1,17 @@
1
1
 
2
+ import path from 'node:path'
2
3
  import {xeiraBundle} from 'xeira'
3
- import { cleanFolder, copyFileSync, isFileExistingSync } from './util.mjs'
4
+ import { cleanFolder, copyFileSync, isFileExistingSync } from '../util.mjs'
4
5
 
5
6
  function _addCssLinkToHead(appName, htmlString, dest) {
6
- const linkTag = ` <link href="${dest}/${appName}.${process.env.MIOLO_BUNDLE_SUFFIX}.css" rel="stylesheet" media="all">`
7
+
8
+ const webDest = dest.startsWith('./')
9
+ ? dest.replace('./', '/')
10
+ : dest.startsWith('/')
11
+ ? dest
12
+ : `/${dest}`
13
+
14
+ const linkTag = ` <link href="${webDest}/${appName}.${process.env.MIOLO_BUILD_CLIENT_SUFFIX}.css" rel="stylesheet" media="all">`
7
15
 
8
16
  // Expresión regular para encontrar la etiqueta </head> de cierre.
9
17
  // Usamos un grupo de captura para mantener el contenido antes de </head>.
@@ -28,25 +36,28 @@ function _addCssLinkToHead(appName, htmlString, dest) {
28
36
  export default async function(appName, entry, htmlFile, dest) {
29
37
  console.log(`[${appName}][prod] Building client from entry ${entry}`)
30
38
 
39
+ // Clean dest folder
31
40
  cleanFolder(dest)
32
41
 
42
+ // Build client
33
43
  await xeiraBundle({
34
44
  source_index: entry,
35
45
  target: 'browser',
36
46
  bundle_folder: dest,
37
47
  bundle_name: appName,
38
- bundle_extension: process.env.MIOLO_BUNDLE_SUFFIX,
48
+ bundle_extension: process.env.MIOLO_BUILD_CLIENT_SUFFIX,
39
49
  bundler: 'rollup',
40
50
  bundle_node_polyfill: true
41
51
  })
42
52
 
43
- const destCssFile = `${dest}/${appName}.${process.env.MIOLO_BUNDLE_SUFFIX}.css`
53
+ // Fix css file
54
+ const destCssFile = `${dest}/${appName}.${process.env.MIOLO_BUILD_CLIENT_SUFFIX}.css`
44
55
  const destCssFileExists = isFileExistingSync(destCssFile)
45
56
 
46
57
  console.log(`[${appName}][prod] destCssFile ${destCssFile} exists?: ${destCssFileExists}`)
47
58
 
48
59
  console.log(`[${appName}][prod] Copying HTML file ${htmlFile} to ${dest}/index.html`)
49
- copyFileSync(htmlFile, `${dest}/index.html`, (content) => {
60
+ copyFileSync(path.join(process.cwd(), htmlFile), path.join(process.cwd(), `${dest}/index.html`), (content) => {
50
61
  if (destCssFileExists) {
51
62
  content = _addCssLinkToHead(appName, content, dest)
52
63
  }
@@ -3,8 +3,7 @@ import path from 'node:path'
3
3
  import { readFile, writeFile} from 'node:fs/promises'
4
4
  import { build } from 'vite'
5
5
  import {xeiraBundle} from 'xeira'
6
- import { cleanFolder } from './util.mjs'
7
-
6
+ import { cleanFolder } from '../util.mjs'
8
7
 
9
8
 
10
9
  export async function _fixProdBuild(appName, filePath) {
@@ -21,10 +20,13 @@ export async function _fixProdBuild(appName, filePath) {
21
20
  }
22
21
  }
23
22
 
24
- export default async function(appName, ssrEntry, ssrDest, dest) {
25
- cleanFolder(dest)
26
-
23
+ export default async function(appName, ssrEntry, ssrDest, srvEntry, srvDest) {
27
24
  console.log(`[${appName}][prod] Building first the SSR entry ${ssrEntry}`)
25
+
26
+ // clean dest folder
27
+ cleanFolder(srvDest)
28
+
29
+ // Build SSR entry
28
30
  await build({
29
31
  build: {
30
32
  outDir: path.resolve(process.cwd(), ssrDest),
@@ -35,15 +37,15 @@ export default async function(appName, ssrEntry, ssrDest, dest) {
35
37
  ssr: {
36
38
  noExternal: true
37
39
  }
38
- })
40
+ })
39
41
 
42
+ console.log(`[${appName}][prod] Building server entry for ${srvEntry}`)
40
43
 
41
44
  const serverExt = 'node.bundle.mjs'
42
45
  await xeiraBundle({
43
- //source_index: 'node_modules/miolo/packages/miolo/bin/prod_start.mjs',
44
- source_index: '../miolo/bin/prod_start.mjs',
46
+ source_index: srvEntry,
45
47
  target: 'node',
46
- bundle_folder: dest,
48
+ bundle_folder: srvDest,
47
49
  bundle_name: appName,
48
50
  bundle_extension: serverExt,
49
51
  bundler: 'rollup',
@@ -51,6 +53,6 @@ export default async function(appName, ssrEntry, ssrDest, dest) {
51
53
  })
52
54
 
53
55
  console.log(`[${appName}][prod] Fixing server build (prod)...`)
54
- const destFile = path.join(process.cwd(), `${dest}/${appName}.${serverExt}`)
56
+ const destFile = path.join(process.cwd(), `${srvDest}/${appName}.${serverExt}`)
55
57
  await _fixProdBuild(appName, destFile)
56
58
  }
@@ -0,0 +1,13 @@
1
+ import {writeFileSync, readFileSync} from 'node:fs'
2
+
3
+ export function pidFileCreate(appName) {
4
+ const pidFilePath = `/tmp/${appName}.pid`
5
+ writeFileSync(pidFilePath, `${process.pid}`, {encoding:'utf8',flag:'w'})
6
+ return process.pid
7
+ }
8
+
9
+ export function pidFileRead(appName) {
10
+ const pidFilePath = `/tmp/${appName}.pid`
11
+ const pid = readFileSync(pidFilePath, {encoding:'utf8'})
12
+ return pid
13
+ }
@@ -1,10 +1,9 @@
1
1
  import start from "./start.mjs"
2
2
  import stop from "./stop.mjs"
3
3
 
4
- export default async function(appName, dest, serverName) {
4
+ export default async function restart(appName, destFile) {
5
5
  console.log(`[${appName}][prod][restart] Restarting server...`)
6
6
 
7
7
  await stop(appName)
8
- await start(appName, dest, serverName)
9
-
10
- }
8
+ await start(appName, destFile)
9
+ }
@@ -0,0 +1,15 @@
1
+ import { pidFileCreate } from "./pid.mjs"
2
+
3
+ export default async function start(appName, destFile) {
4
+
5
+ // Start server
6
+ console.log(`[${appName}][prod][start] Starting server from ${destFile}...`)
7
+
8
+ const srv_module = await import(destFile)
9
+ const server = srv_module.default
10
+
11
+ const pid = pidFileCreate(appName)
12
+ console.log(`[${appName}][prod][start] Starting server. PID is ${pid}...`)
13
+
14
+ await server()
15
+ }
@@ -1,6 +1,7 @@
1
- import { pidFileRead } from "./util.mjs"
1
+ import { pidFileRead } from "./pid.mjs"
2
2
 
3
- export default async function(appName) {
3
+ export default async function stop(appName) {
4
+ // stop server by killin gprocess
4
5
  console.log(`[${appName}][prod][stop] Stopping server...`)
5
6
 
6
7
  try {
@@ -16,4 +17,4 @@ export default async function(appName) {
16
17
 
17
18
  return 0
18
19
  }
19
- }
20
+ }
package/bin/util.mjs CHANGED
@@ -2,40 +2,13 @@ import {readdirSync, rmSync, writeFileSync, readFileSync} from 'node:fs'
2
2
  import path from 'node:path'
3
3
 
4
4
 
5
- // export function getAppName() {
6
- // try {
7
- // const packageJsonPath = path.join(process.cwd(), 'package.json')
8
- // const content = readFileSync(packageJsonPath, 'utf8')
9
- // const packageData = JSON.parse(content)
10
- // return packageData.name
11
- // } catch (error) {
12
- // console.error('[miolo] Error reading package.json:', error)
13
- // return 'miolo'
14
- // }
15
- // }
16
-
17
5
  export function cleanFolder(folder) {
18
6
  readdirSync(folder).forEach(file => {
19
7
  rmSync(`${folder}/${file}`, { recursive: true, force: true });
20
8
  })
21
9
  }
22
10
 
23
-
24
- export function pidFileCreate(appName) {
25
- const pidFilePath = `/tmp/${appName}.pid`
26
- writeFileSync(pidFilePath, `${process.pid}`, {encoding:'utf8',flag:'w'})
27
- return process.pid
28
- }
29
-
30
- export function pidFileRead(appName) {
31
- const pidFilePath = `/tmp/${appName}.pid`
32
- const pid = readFileSync(pidFilePath, {encoding:'utf8'})
33
- return pid
34
- }
35
-
36
- export function copyFileSync(src, dest, modifier= undefined) {
37
- const srcPath = path.join(process.cwd(), src)
38
- const destPath = path.join(process.cwd(), dest)
11
+ export function copyFileSync(srcPath, destPath, modifier= undefined) {
39
12
  try {
40
13
  let content = readFileSync(srcPath, 'utf8')
41
14
  if (modifier) {
@@ -56,4 +29,4 @@ export function isFileExistingSync(filePath) {
56
29
  console.error(`[miolo] Error checking if file exists: ${filePath}`, error)
57
30
  return false
58
31
  }
59
- }
32
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "miolo",
3
- "version": "3.0.0-beta.2",
3
+ "version": "3.0.0-beta.20",
4
4
  "description": "all-in-one koa-based server",
5
5
  "author": "Donato Lorenzo <donato@afialapis.com>",
6
6
  "contributors": [
@@ -20,14 +20,9 @@
20
20
  },
21
21
  "type": "module",
22
22
  "exports": {
23
- "./server": {
24
- "node": "./src/index.mjs",
25
- "import": "./src/index.mjs",
26
- "default": "./dist/miolo.node.mjs"
27
- },
28
- "./server-dev": {
29
- "default": "./src/server-dev.mjs"
30
- }
23
+ "node": "./src/index.mjs",
24
+ "import": "./src/index.mjs",
25
+ "default": "./dist/miolo.node.mjs"
31
26
  },
32
27
  "files": [
33
28
  "bin",
@@ -38,8 +33,7 @@
38
33
  "clean": "rm -fr ./dist/*",
39
34
  "bundle": "npx xeira bundle --target=node --source_index=./src/index.mjs --bundle_folder=./dist --bundle_name=miolo --bundle_extension=node.mjs",
40
35
  "dist": "npm run clean && npm run bundle",
41
- "test": "npx xeira test --files=./test/index.mjs",
42
- "publish": "npm publish"
36
+ "test": "npx xeira test --files=./test/index.mjs"
43
37
  },
44
38
  "dependencies": {
45
39
  "@babel/plugin-proposal-decorators": "^7.28.0",
@@ -2,7 +2,7 @@
2
2
  # General
3
3
  #
4
4
  MIOLO_NAME=miolo
5
- MIOLO_CONFIG=./src/server/config.mjs
5
+ MIOLO_INTRE_LOCALE=es
6
6
 
7
7
  #
8
8
  # HTTP
@@ -11,7 +11,7 @@ MIOLO_PORT=8001
11
11
  MIOLO_HOSTNAME=localhost
12
12
 
13
13
  MIOLO_HTTP_CORS=simple # true | false | simple. For {options}, use JS
14
- MIOLO_HTTP_PROXY=true # true | false. For {options}, use JS
14
+ MIOLO_HTTP_PROXY=false # true | false. For {options}, use JS
15
15
 
16
16
  MIOLO_RATELIMIT_MAX=1000
17
17
  MIOLO_RATELIMIT_DURATION=60 * 1000
@@ -57,7 +57,7 @@ MIOLO_LOG_LEVEL=verbose
57
57
  MIOLO_LOG_CONSOLE_ENABLED=true
58
58
  #MIOLO_LOG_CONSOLE_LEVEL=verbose
59
59
 
60
- MIOLO_LOG_FILE_ENABLED=true
60
+ MIOLO_LOG_FILE_ENABLED=false
61
61
  #MIOLO_LOG_FILE_LEVEL=verbose
62
62
 
63
63
  MIOLO_LOG_FILE_PATH=/var/log/afialapis/%MIOLO%.log
@@ -100,8 +100,10 @@ MIOLO_DOTENVX_DEBUG=false
100
100
  MIOLO_BUILD_HTML_FILE=./src/cli/index.html
101
101
  MIOLO_BUILD_CLIENT_ENTRY=./src/cli/entry-cli.jsx
102
102
  MIOLO_BUILD_CLIENT_DEST=./dist/cli
103
+ MIOLO_BUILD_CLIENT_SUFFIX=iife.bundle.min
103
104
  MIOLO_BUILD_SERVER_SSR_ENTRY=./src/server/ssr/entry-server.jsx
105
+ MIOLO_BUILD_SERVER_ENTRY=./src/server/server.mjs
104
106
  MIOLO_BUILD_SERVER_DEST=./dist/server
105
- MIOLO_BUNDLE_SUFFIX=iife.bundle.min
107
+ MIOLO_DEV_CONFIG_ENTRY=./src/server/config.mjs
106
108
  MIOLO_DEV_WATCH_ENABLED=true
107
109
  MIOLO_DEV_WATCH_DIRS=./src/server