Skip to content

Commit

Permalink
npm + working bin
Browse files Browse the repository at this point in the history
  • Loading branch information
abernier committed Aug 10, 2024
1 parent 5efb2d7 commit 9c62212
Show file tree
Hide file tree
Showing 8 changed files with 7,123 additions and 4,432 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
-e DIST_DIR="$MDX/out$BASE_PATH" \
-e MDX \
-e NEXT_PUBLIC_LIBNAME \
ghcr.io/pmndrs/docs:app-router yarn build
ghcr.io/pmndrs/docs:app-router npm run build
env:
BASE_PATH: ${{ steps.configurepages.outputs.base_path }}
MDX: ${{ inputs.mdx }}
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM node:20-alpine
RUN apk add --no-cache libc6-compat git && apk update
WORKDIR /app

COPY package.json yarn.lock ./
RUN yarn
COPY package.json package-lock.json ./
RUN npm ci

COPY . .
73 changes: 62 additions & 11 deletions bin/build.mjs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,90 @@

// $ node bin/build.mjs ~/code/pmndrs/react-three-fiber/docs

import { spawn } from 'node:child_process'
import { spawn, exec as execCb } from 'node:child_process'
import { promisify } from 'node:util'
import minimist from 'minimist'
import { resolve, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
const exec = promisify(execCb)

console.log('argv=', process.argv)
var argv = minimist(process.argv.slice(2))
console.log('argv=', argv)
console.log('argv2=', argv)

const help = argv.help || argv.h
const man = `
Usage: npm exec -y --package=@pmndrs/docs build -- MDX [ OUTDIR ] [ --libname=LIBNAME --basePath=BASE_PATH --help|-h ]
Generate static, pmndrs-standardized documentation website from *.mdx folder.
Example: npx @pmndrs/docs build ./docs
npx @pmndrs/docs build ~code/pmndrs/react-three-fiber/docs --libname="React Three Fiber" --basePath="/react-three-fiber" static-out
Arguments:
MDX: Path to the folder containing the MDX files (absolute or relative to process.cwd())
OUT_DIR: Path to the output directory (absolute or relative to process.cwd()), default: "out"
Options:
libname: Name of the library that documentation is for, eg: "React Three Fiber"
basePath: base path for the final URL, eg: "/react-three-fiber"
help, h: Show this help message
`
if (help) {
console.log(man)
process.exit(0)
}

const __filename = fileURLToPath(import.meta.url) // Converts the URL to a file path
const __dirname = dirname(__filename) // Gets the directory name

// Positional arguments
const mdx = argv._[0]
const outdir = argv._[1] || 'out'
if (!mdx) {
console.error('Please provide the mdx folder as the first argument.')
console.log(man)
process.exit(1)
}

// const __filename = fileURLToPath(import.meta.url); // Converts the URL to a file path
// const __dirname = dirname(__filename); // Gets the directory name
// const viteConfigPath = resolve(__dirname, "../src/vite.config.build.ts");
// // console.log("viteConfigPath=", viteConfigPath);
const MDX = resolve(process.cwd(), mdx)
const NEXT_PUBLIC_LIBNAME = argv.libname || process.env.LIBNAME
const BASE_PATH = argv.basePath || process.env.BASE_PATH || ''
const DIST_DIR = `out${BASE_PATH}`

const outHostDirAbsolute = resolve(process.cwd(), outdir)
const outLocalDirAbsolute = resolve(__dirname, '..', 'out')

const env = {
MDX,
NEXT_PUBLIC_LIBNAME,
BASE_PATH,
DIST_DIR,
}
console.log('env=', env)

const cmd = spawn('npx', ['next', 'build'], {
await exec(`rm -rf ${outLocalDirAbsolute}`)

const cmd = await spawn('npx', ['next', 'build'], {
stdio: 'inherit',
cwd: resolve(__dirname, '..'),
env: {
MDX: mdx,
NEXT_PUBLIC_LIBNAME: argv.libname,
...process.env,
...env,
},
})

cmd.on('exit', (code) => {
cmd.on('exit', async (code) => {
if (code !== 0) {
console.error('Build failed with error')
process.exit(1)
}

console.log('Build completed successfully.')

await exec(
`mkdir -p ${outHostDirAbsolute}; cp -rf ${outLocalDirAbsolute}/* ${outHostDirAbsolute}`
)
console.log(`Preview: \`npx -y serve ${outHostDirAbsolute}\``)
})
1 change: 1 addition & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ const nextConfig = {
// ]
// },
}
// console.log('nextConfig=', nextConfig)

export default nextConfig
Loading

0 comments on commit 9c62212

Please sign in to comment.