Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: drop bun chdir hack #281

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 20 additions & 20 deletions packages/nuekit/src/create.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@

import { createKit } from './nuekit.js'
import { finished } from 'node:stream/promises'
import { createWriteStream } from 'node:fs'
import { execSync } from 'node:child_process'
import { promises as fs } from 'node:fs'
import { Readable } from 'node:stream'

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

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

// open welcome page
execSync('open http://localhost:8083/welcome/')
try {
execSync(`${openUrl} http://localhost:${nue.port}/welcome/`)
} catch {}
return terminate
}

export async function create({ name='simple-blog' }) {
export async function create({ name = 'simple-blog' }) {

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

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

// currently only simple-blog is available
if (name != 'simple-blog') return console.error('Template does not exist', name)
if (name != 'simple-blog') return console.error('Template does not exist:', name)

// must be empty directory
if (files[1] || files[0] != '.DS_Store') {
return console.error('Please create the appplication to an empty directory')
}
if (files.length) return console.error('Please create the template to an empty directory')

// download zip
const zip = await fetch(`https://${ name }.nuejs.org/${name}.zip`)
await Bun.write('source.zip', zip)
// download archive
const archive_name = 'source.tar.gz'
const archive = await fetch(`https://${name}.nuejs.org/${name}.tar.gz`)
await fs.writeFile(archive_name, await archive.arrayBuffer())

// unzip
await execSync('unzip source.zip')
// unzip and remove archive
execSync(`tar -xf ${archive_name}`)
await fs.rm(archive_name)

// serve
await serve()
}
return await serve()
}
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)
}
2 changes: 1 addition & 1 deletion packages/nuekit/src/nuekit.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export async function createKit(args) {
gen, getPageData, renderMPA, renderSPA,

// public API
build, serve, stats, dist,
build, serve, stats, dist, port,
}

}
Expand Down
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
7 changes: 3 additions & 4 deletions packages/nuekit/test/misc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ test('path parts', () => {
})


test.only('create', async () => {
test('create', async () => {
await fs.mkdir('simple-blog', { recursive: true })
await process.chdir('simple-blog')

const body = await create({ name: 'simple-blog' })


const terminate = await create({ name: 'simple-blog' })
terminate()
})