Skip to content

Commit

Permalink
Merge pull request #330 from nobkd/feat/port-cli-option
Browse files Browse the repository at this point in the history
feat: add port cli option
  • Loading branch information
tipiirai authored Aug 29, 2024
2 parents 4ffa6e0 + e716afb commit eb63db0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/nuejs.org/docs/command-line-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Options
-I or --init Force clear and initialize output directory
-n or --dry-run Show what would be built. Does not create outputs
-b or --esbuild Use esbuild as bundler. Please install it manually
-P or --port Port to serve the site on

File matches
Only build files that match the rest of the arguments. For example:
Expand Down
1 change: 1 addition & 0 deletions packages/nuekit/src/cli-help.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Options
-I or --init Force clear and initialize output directory
-n or --dry-run Show what would be built. Does not create outputs
-b or --esbuild Use esbuild as bundler. Please install it manually
-P or --port Port to serve the site on
File matches
Only build files that match the rest of the arguments. For example:
Expand Down
24 changes: 14 additions & 10 deletions packages/nuekit/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,23 @@ export function getArgs(argv) {
else if (['-v', '--verbose'].includes(arg)) args.verbose = true
else if (['-s', '--stats'].includes(arg)) args.stats = true
else if (['-b', '--esbuild'].includes(arg)) args.esbuild = true
else if (['-P', '--push'].includes(arg)) args.push = args.is_prod = true
else if (['-d', '--deploy'].includes(arg)) args.deploy = args.is_prod = true
else if (['-I', '--init'].includes(arg)) args.init = true

// string values
else if (['-e', '--environment'].includes(arg)) opt = 'env'
else if (['-r', '--root'].includes(arg)) opt = 'root'
else if (['-P', '--port'].includes(arg)) opt = 'port'

// bad options
else throw `Unknown option: "${arg}"`

} else if (arg && arg[0] != '-') {
if (opt) { args[opt] = arg; opt = null }
if (opt) {
args[opt] = opt == 'port' ? Number(arg) : arg
// Number(alphabetic characters) is falsy. Check if port is really set:
if (opt != 'port' || (opt == 'port' && args.port)) opt = null
}
else args.paths.push(arg)
} else if (opt) throw `"${opt}" option is not set`
})
Expand All @@ -81,32 +86,31 @@ async function printVersion() {

async function runCommand(args) {
const { createKit } = await import('./nuekit.js')
const { cmd='serve', dryrun, push, root=null } = args
if (!root) args.root = '.'
const { cmd='serve', dryrun, deploy, root=null, port } = args
if (!root) args.root = '.' // ensure root is unset for create, if not set manually

console.info('')

// create nue
if (cmd == 'create') {
const { create } = await import('./create.js')
return await create({ root, name: args.paths[0] })
return await create({ root, name: args.paths[0], port })
}

const nue = await createKit(args)
args.nuekit_version = await printVersion()


// stats
if (cmd == 'stats') await nue.stats(args)

// build
if (dryrun || push || args.paths[0] || cmd == 'build') {
if (dryrun || deploy || args.paths[0] || cmd == 'build') {
const paths = await nue.build(args.paths, dryrun)

// deploy (private repo ATM)
if (!dryrun && push) {
const { deploy } = await import('nue-deployer')
await deploy(paths, { root: nue.dist, init: args.init })
if (!dryrun && deploy) {
const { deploy: deployer } = await import('nue-deployer')
await deployer(paths, { root: nue.dist, init: args.init })
}

// serve
Expand Down
8 changes: 4 additions & 4 deletions packages/nuekit/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { join } from 'node:path'
import { openUrl } from './util.js'
import { createKit } from './nuekit.js'

async function serve(root, debug) {
const nue = await createKit({ root })
async function serve(root, port, debug) {
const nue = await createKit({ root, port })
const terminate = await nue.serve()

// open welcome page
if (!debug) execSync(`${openUrl} http://localhost:${nue.port}/welcome/`)
return terminate
}

export async function create({ root, name = 'simple-blog' }) {
export async function create({ root, name = 'simple-blog', port }) {
if (!root) root = name

// debug mode with: `nue create test`
Expand Down Expand Up @@ -49,5 +49,5 @@ export async function create({ root, name = 'simple-blog' }) {
await fs.rm(archive_name)

// serve
return await serve(root, debug)
return await serve(root, port, debug)
}
9 changes: 4 additions & 5 deletions packages/nuekit/src/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ export async function createSite(args) {
libs: site_data.libs || [],
}

const {
dist: rawDist,
port = is_prod ? 8081 : 8080
} = site_data
const port = args.port ? args.port :
site_data.port ? site_data.port :
is_prod ? 8081 : 8080

const dist = joinRootPath(root, rawDist || join('.dist', is_prod ? 'prod' : 'dev'))
const dist = joinRootPath(root, site_data.dist || join('.dist', is_prod ? 'prod' : 'dev'))

// flag if .dist is empty
try {
Expand Down

0 comments on commit eb63db0

Please sign in to comment.