Skip to content

Commit

Permalink
Merge pull request #485 from nobkd/update-create
Browse files Browse the repository at this point in the history
Collection of small create, cli, version, helper changes
  • Loading branch information
nobkd authored Feb 12, 2025
2 parents 62626d0 + 296e737 commit 4a81640
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
12 changes: 7 additions & 5 deletions packages/nuekit/src/cli-help.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { colors, openUrl, getVersion } from './util.js'
import { colors, version } from './util.js'

const HELP = `
Usage
Expand All @@ -11,6 +11,7 @@ Commands
build Build the site under <root_dir>
create Use a project starter template
init Re-generate /@nue system files
docs Open Nue's docs
Options
-r or --root Source directory. Default "." (current working dir)
Expand All @@ -19,7 +20,8 @@ Options
-n or --dry-run Show what would be built. Does not create outputs
-b or --esbuild Use esbuild as JS bundler. Please install it manually
-l or --lcss Use lightningcss as CSS bundler. Please install it manually
-P or --port Port to serve the site on
-P or --port Serves the site on the specified port
-o or --open Opens the local site in the browser
File matches
Only build files that match the rest of the arguments. For example:
Expand All @@ -39,15 +41,15 @@ Examples
nue build .md .css
# more examples
${openUrl} https://nuejs.org/docs/command-line-interface.html
Visit https://nuejs.org/docs/command-line-interface.html
┏━┓┏┓┏┳━━┓
┃┏┓┫┃┃┃┃━┫ ${await getVersion()}
┃┏┓┫┃┃┃┃━┫ ${version}
┃┃┃┃┗┛┃┃━┫ nuejs.org
┗┛┗┻━━┻━━┛
`

const commands = ['serve', 'build', 'init', 'create']
const commands = ['serve', 'build', 'init', 'create', 'docs']

function formatLine(line) {
const { gray, magenta, cyan, green } = colors
Expand Down
18 changes: 10 additions & 8 deletions packages/nuekit/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { sep } from 'node:path'

import esMain from 'es-main'

import { log, colors, getVersion, getEngine } from './util.js'
import { log, colors, version, getEngine, openUrl } from './util.js'


// [-npe] --> [-n, -p, -e]
Expand All @@ -22,7 +22,7 @@ export function expandArgs(args) {

// TODO: tests
export function getArgs(argv) {
const commands = ['serve', 'build', 'init', 'create']
const commands = ['serve', 'build', 'init', 'create', 'docs']
const args = { paths: [], root: null }
const checkExecutable = /[\\\/]nue(\.(cmd|ps1|bunx|exe))?$/
let opt
Expand Down Expand Up @@ -52,6 +52,7 @@ export function getArgs(argv) {
else if (['-l', '--lcss'].includes(arg)) args.lcss = true
else if (['-d', '--deploy'].includes(arg)) args.deploy = args.is_prod = true
else if (['-I', '--incremental'].includes(arg)) args.incremental = true
else if (['-o', '--open'].includes(arg)) args.open = true

// string values
else if (['-e', '--environment'].includes(arg)) opt = 'env'
Expand Down Expand Up @@ -83,30 +84,31 @@ async function printHelp() {
}

async function printVersion() {
const v = await getVersion()
log(`Nue ${v} ${colors.green('•')} ${getEngine()}`)
return v
log(`Nue ${version} ${colors.green('•')} ${getEngine()}`)
}

async function runCommand(args) {
if (args.cmd == 'docs') return openUrl('https://nuejs.org/docs/')

const { createKit } = await import('./nuekit.js')
const { cmd = 'serve', dryrun, deploy, root = null, port } = args
const init = cmd == 'init'

if (!root) args.root = '.' // ensure root is unset for create, if not set manually

console.info('')
await printVersion()
args.nuekit_version = version

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

args.nuekit_version = await printVersion()

const nue = await createKit(args)
if (!nue) return
if (args.open) openUrl(`http://localhost:${nue.port}/`)

// deployer (private repo)
const { deploy: deployer } = deploy ? await import('nue-deployer') : {}
Expand Down
27 changes: 17 additions & 10 deletions packages/nuekit/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,39 @@ import { join } from 'node:path'
import { openUrl } from './util.js'
import { createKit } from './nuekit.js'

const templates = {
'simple-blog': 'welcome/',
}


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

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

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

// debug mode with: `nue create test`
const debug = name == 'test'
if (debug) name = 'simple-blog'
args.debug = args.name == 'test'
if (args.debug) args.name = 'simple-blog'

const { debug, name, root } = args

// currently only simple-blog is available
if (name != 'simple-blog') return console.error(`Template "${name}" does not exist`)
if (!Object.keys(templates).includes(name)) return console.error(`Template "${name}" does not exist`)

if (existsSync(root)) {
// read files
const files = (await fs.readdir(root)).filter(f => !f.startsWith('.'))

// already created -> serve
if (files.includes('site.yaml')) return serve(root)
if (files.includes('site.yaml')) return serve(args)

// must be empty directory
if (files.length) return console.error('Please create the template to an empty directory')
Expand All @@ -54,5 +61,5 @@ export async function create({ root, name = 'simple-blog', port }) {
await fs.rm(archive_name)

// serve
return await serve(root, port, debug)
return await serve(args)
}
4 changes: 2 additions & 2 deletions packages/nuekit/src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { resolve } from 'import-meta-resolve'
import { compileFile as nueCompile } from 'nuejs-core'

import { buildJS } from './builder.js'
import { colors, srcdir } from './util.js'
import { version, colors, srcdir } from './util.js'


export async function initNueDir({ dist, is_dev, esbuild, force }) {
Expand All @@ -15,7 +15,7 @@ export async function initNueDir({ dist, is_dev, esbuild, force }) {
const outdir = join(cwd, dist, '@nue')

// has all latest?
const latest = join(outdir, '.rc-2')
const latest = join(outdir, `.v${version}`)

if (force || !existsSync(latest)) {
await fs.rm(outdir, { recursive: true, force: true })
Expand Down
10 changes: 7 additions & 3 deletions packages/nuekit/src/util.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
/* misc stuff. think shame.css */

import { execSync } from 'node:child_process'
import { promises as fs } from 'node:fs'
import { sep, parse, resolve, normalize, join, isAbsolute, dirname } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'


export const srcdir = dirname(fileURLToPath(import.meta.url))

export const openUrl = process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open'
export function openUrl(url) {
const open = process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open'
execSync(`${open} ${url}`)
}

// read from package.json
export async function getVersion() {
export const version = await async function() {
const path = join(srcdir, '../package.json')
const json = await fs.readFile(path, 'utf-8')
return JSON.parse(json).version
}
}()

export async function importFromCWD(path) {
const abs_path = resolve(process.cwd(), path)
Expand Down

0 comments on commit 4a81640

Please sign in to comment.