Skip to content

Commit

Permalink
rework parts of create
Browse files Browse the repository at this point in the history
  • Loading branch information
nobkd committed Feb 11, 2025
1 parent af5acfe commit 14bf4f4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
6 changes: 3 additions & 3 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, openUrl, version } from './util.js'

const HELP = `
Usage
Expand Down Expand Up @@ -38,10 +38,10 @@ Examples
nue build .md .css
# more examples
${openUrl} https://nuejs.org/docs/command-line-interface.html
https://nuejs.org/docs/command-line-interface.html
┏━┓┏┓┏┳━━┓
┃┏┓┫┃┃┃┃━┫ ${await getVersion()}
┃┏┓┫┃┃┃┃━┫ ${version}
┃┃┃┃┗┛┃┃━┫ nuejs.org
┗┛┗┻━━┻━━┛
`
Expand Down
12 changes: 5 additions & 7 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 } from './util.js'


// [-npe] --> [-n, -p, -e]
Expand Down Expand Up @@ -82,9 +82,7 @@ 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) {
Expand All @@ -95,15 +93,15 @@ async function runCommand(args) {
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

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 14bf4f4

Please sign in to comment.