Skip to content

Commit

Permalink
Merge pull request #281 from nobkd/chore/drop-bun-abs-dir-fix
Browse files Browse the repository at this point in the history
chore: drop bun chdir hack
  • Loading branch information
tipiirai authored Aug 13, 2024
2 parents 823ced5 + 23ce0d5 commit 29968a9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 55 deletions.
13 changes: 4 additions & 9 deletions packages/nuekit/src/cli-help.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

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

const HELP = `
Usage
Expand Down Expand Up @@ -39,18 +38,17 @@ Examples
nue build .md .css
# more examples
open https://nuejs.org/docs/cli
${openUrl} https://nuejs.org/docs/cli
Less is more
┏━┓┏┓┏┳━━┓
┃┏┓┫┃┃┃┃━┫
┃┏┓┫┃┃┃┃━┫ v${await getVersion()}
┃┃┃┃┗┛┃┃━┫ nuejs.org
┗┛┗┻━━┻━━┛
`

const commands = ['serve', 'build', 'stats']
const commands = ['serve', 'build', 'stats', 'create']

function formatLine(line) {
const { gray, magenta, cyan, green } = colors
Expand All @@ -72,6 +70,3 @@ export function getHelp() {
return line[0] === ' ' ? formatLine(line) : line
}).join('\n')
}



16 changes: 1 addition & 15 deletions packages/nuekit/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bun

import { log, colors } from './util.js'
import { log, colors, getVersion, getEngine } from './util.js'
import esMain from 'es-main'
import { sep } from 'node:path'

Expand Down Expand Up @@ -68,20 +68,6 @@ export function getArgs(argv) {
return args
}

// read from package.json
async function getVersion() {
const { promises } = await import('fs')
const pathname = new URL('../package.json', import.meta.url).pathname
const path = process.platform === "win32" && pathname.startsWith('/') ? pathname.slice(1) : pathname
const json = await promises.readFile(path, 'utf-8')
return JSON.parse(json).version
}

function getEngine() {
const v = process.versions
return process.isBun ? 'Bun ' + v.bun : 'Node ' + v.node
}

async function printHelp() {
const { getHelp } = await import('./cli-help.js')
console.info(getHelp())
Expand Down
4 changes: 2 additions & 2 deletions packages/nuekit/src/create.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { execSync } from 'node:child_process'
import { promises as fs } from 'node:fs'

import { openUrl } from './util.js'
import { createKit } from './nuekit.js'

async function serve() {
const nue = await createKit({ root: '.' })
const terminate = await nue.serve()

// open welcome page
const open = process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open'
try {
execSync(`${open} http://localhost:${nue.port}/welcome/`)
execSync(`${openUrl} http://localhost:${nue.port}/welcome/`)
} catch {}
return terminate
}
Expand Down
38 changes: 10 additions & 28 deletions packages/nuekit/src/init.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@

import { compileFile as nueCompile} from 'nuejs-core'
import { join, basename } from 'node:path'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { promises as fs } from 'node:fs'
import { resolve } from 'import-meta-resolve'
import { buildJS } from './builder.js'
import { colors } from './util.js'
import { colors, srcdir } from './util.js'


export async function init({ dist, is_dev, esbuild, force }) {

// directories
const cwd = process.cwd()
const srcdir = getSourceDir()
const outdir = join(cwd, dist, '@nue')


// has all latest?
const latest = join(outdir, '.05')
try {
Expand All @@ -28,20 +27,10 @@ export async function init({ dist, is_dev, esbuild, force }) {
await fs.writeFile(latest, '')
}

try {
// chdir hack (Bun does not support absWorkingDir)
process.chdir(srcdir)
process.env.ACTUAL_CWD = cwd

await initUnderChdir({ dist, is_dev, esbuild, cwd, srcdir, outdir })
} finally {
// recover
process.env.ACTUAL_CWD = ''
process.chdir(cwd)
}
await initDir({ dist, is_dev, esbuild, cwd, srcdir, outdir })
}

async function initUnderChdir({ dist, is_dev, esbuild, cwd, srcdir, outdir }) {
async function initDir({ dist, is_dev, esbuild, cwd, srcdir, outdir }) {

const fromdir = join(srcdir, 'browser')
const minify = !is_dev
Expand All @@ -57,7 +46,7 @@ async function initUnderChdir({ dist, is_dev, esbuild, cwd, srcdir, outdir }) {

// copy from NPM path
async function copyAsset(npm_path, toname) {
const path = await resolvePath(npm_path)
const path = resolvePath(npm_path)
await fs.copyFile(path, join(outdir, toname))
dot()
}
Expand All @@ -72,7 +61,7 @@ async function initUnderChdir({ dist, is_dev, esbuild, cwd, srcdir, outdir }) {
async function buildPackage(npm_path, toname) {
await buildJS({
bundle: true, esbuild, minify, outdir, toname,
path: await resolvePath(npm_path),
path: resolvePath(npm_path),
})
dot()
}
Expand Down Expand Up @@ -106,15 +95,8 @@ async function initUnderChdir({ dist, is_dev, esbuild, cwd, srcdir, outdir }) {
}


async function resolvePath(npm_path) {
function resolvePath(npm_path) {
const [ npm_name, ...parts ] = npm_path.split('/')
let main = await resolve(npm_name, `file://${process.cwd()}/`)
main = main.replace(/^file:\/\//, '')
main = process.platform === 'win32' && main.startsWith('/') ? main.slice(1) : main
return main.replace('index.js', parts.join('/'))
}

function getSourceDir() {
const path = new URL('.', import.meta.url).pathname
return process.platform === "win32" && path.startsWith('/') ? path.slice(1) : path
const module_path = dirname(fileURLToPath(resolve(npm_name, `file://${process.cwd()}/`)))
return join(module_path, ...parts)
}
20 changes: 19 additions & 1 deletion packages/nuekit/src/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@

/* misc stuff. think shame.css */
import { sep, parse, normalize, join, isAbsolute } from 'node:path'
import { sep, parse, normalize, join, isAbsolute, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { promises as fs } from 'node:fs'

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

export const openUrl = process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open'

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

export function getEngine() {
const v = process.versions
return process.isBun ? 'Bun ' + v.bun : 'Node ' + v.node
}

export function log(msg, extra='') {
console.log(colors.green('✓'), msg, extra)
Expand Down

0 comments on commit 29968a9

Please sign in to comment.