@harperdb/nextjs
Advanced tools
Comparing version 0.0.2 to 0.0.3
15
cli.js
@@ -8,3 +8,3 @@ #!/usr/bin/env node | ||
async function executeHarperDB (mode) { | ||
async function executeHarperDB(mode) { | ||
const p = child_process.spawn('harperdb', ['run', './'], { | ||
@@ -15,4 +15,5 @@ cwd, | ||
...process.env, | ||
HARPERDB_NEXTJS_MODE: mode | ||
} | ||
HARPERDB_NEXTJS_MODE: mode, | ||
THREADS_DEBUG: mode === 'dev' ? 'true' : 'false', | ||
}, | ||
}); | ||
@@ -31,5 +32,5 @@ const [exitCode] = await events.once(p, 'exit'); | ||
start - Start HarperDB and run Next.js in production mode | ||
` | ||
`; | ||
switch(command) { | ||
switch (command) { | ||
case 'build': | ||
@@ -47,4 +48,4 @@ await executeHarperDB('build'); | ||
case 'help': | ||
console.log('Usage: harperdb-nextjs build|dev|start|help'); | ||
console.log(HELP); | ||
break; | ||
} | ||
} |
@@ -8,3 +8,3 @@ import fs from 'node:fs'; | ||
import next from 'next'; | ||
// import next from 'next'; | ||
import semver from 'semver'; | ||
@@ -28,3 +28,3 @@ import shellQuote from 'shell-quote'; | ||
* Assert that a given option is a specific type | ||
* | ||
* | ||
* @param {string} name The name of the option | ||
@@ -37,3 +37,3 @@ * @param {any=} option The option value | ||
const found = typeof option; | ||
assert.strictEqual(found, expectedType, `${name} must be type ${expectedType}. Received: ${found}`) | ||
assert.strictEqual(found, expectedType, `${name} must be type ${expectedType}. Received: ${found}`); | ||
} | ||
@@ -47,3 +47,3 @@ } | ||
*/ | ||
function resolveConfig (options) { | ||
function resolveConfig(options) { | ||
if (CONFIG) return CONFIG; // return memoized config | ||
@@ -75,3 +75,3 @@ | ||
// Memoize config resolution | ||
return CONFIG = { | ||
return (CONFIG = { | ||
buildCommand: options.buildCommand ?? 'next build', | ||
@@ -83,3 +83,3 @@ buildOnly: options.buildOnly ?? false, | ||
prebuilt: options.prebuilt ?? false, | ||
}; | ||
}); | ||
} | ||
@@ -96,17 +96,19 @@ | ||
* with exit code 1. | ||
* | ||
* | ||
* Additionally, it memoizes previous verifications. | ||
* | ||
* | ||
* @param {string} componentPath | ||
* @returns void | ||
*/ | ||
function assertNextJSApp (componentPath) { | ||
function assertNextJSApp(componentPath) { | ||
try { | ||
if (nextJSAppCache[componentPath]) { return; } | ||
if (nextJSAppCache[componentPath]) { | ||
return; | ||
} | ||
if(!fs.existsSync(componentPath)) { | ||
if (!fs.existsSync(componentPath)) { | ||
throw new NextJSAppVerificationError(`The folder ${componentPath} does not exist`); | ||
} | ||
if(!fs.statSync(componentPath).isDirectory) { | ||
if (!fs.statSync(componentPath).isDirectory) { | ||
throw new NextJSAppVerificationError(`The path ${componentPath} is not a folder`); | ||
@@ -128,3 +130,5 @@ } | ||
// Check for Next.js Config | ||
const configExists = fs.existsSync(path.join(componentPath, 'next.config.js')) || fs.existsSync(path.join(componentPath, 'next.config.ts')); | ||
const configExists = | ||
fs.existsSync(path.join(componentPath, 'next.config.js')) || | ||
fs.existsSync(path.join(componentPath, 'next.config.ts')); | ||
@@ -137,3 +141,3 @@ // Check for dependency | ||
for (let dependencyList of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']) { | ||
let nextJSVersion = packageJSON[dependencyList]?.['next'] | ||
let nextJSVersion = packageJSON[dependencyList]?.['next']; | ||
if (nextJSVersion) { | ||
@@ -150,3 +154,5 @@ if (!semver.satisfies(semver.minVersion(nextJSVersion), '>=14.0.0')) { | ||
if (!configExists && !dependencyExists) { | ||
throw new NextJSAppVerificationError(`Could not determine if ${componentPath} is a Next.js project. It is missing both a Next.js config file and the "next" dependency in package.json`); | ||
throw new NextJSAppVerificationError( | ||
`Could not determine if ${componentPath} is a Next.js project. It is missing both a Next.js config file and the "next" dependency in package.json` | ||
); | ||
} | ||
@@ -168,3 +174,3 @@ | ||
* Execute a command as a promise and wait for it to exit before resolving. | ||
* | ||
* | ||
* Will automatically stream output to stdio when log level is set to debug. | ||
@@ -176,6 +182,6 @@ * @param {string} commandInput The command string to be parsed and executed | ||
async function executeCommand(commandInput, componentPath) { | ||
const [ command, ...args ] = shellQuote.parse(commandInput); | ||
const [command, ...args] = shellQuote.parse(commandInput); | ||
const cp = child_process.spawn(command, args, { | ||
cwd: componentPath, | ||
stdio: logger.log_level === 'debug' ? 'inherit' : 'ignore' | ||
stdio: logger.log_level === 'debug' ? 'inherit' : 'ignore', | ||
}); | ||
@@ -192,10 +198,10 @@ | ||
* on the main thread. | ||
* | ||
* | ||
* The Resource Extension is responsible for installing application component | ||
* dependencies and running the application build command. | ||
* | ||
* @param {ExtensionOptions} options | ||
* @returns | ||
* | ||
* @param {ExtensionOptions} options | ||
* @returns | ||
*/ | ||
export function startOnMainThread (options = {}) { | ||
export function startOnMainThread(options = {}) { | ||
const config = resolveConfig(options); | ||
@@ -207,3 +213,3 @@ | ||
async setupDirectory(_, componentPath) { | ||
logger.info(`Next.js Extension is setting up ${componentPath}`) | ||
logger.info(`Next.js Extension is setting up ${componentPath}`); | ||
@@ -213,7 +219,7 @@ assertNextJSApp(componentPath); | ||
if (!fs.existsSync(path.join(componentPath, 'node_modules'))) { | ||
await executeCommand(config.installCommand, componentPath) | ||
await executeCommand(config.installCommand, componentPath); | ||
} | ||
if (!config.prebuilt && !config.dev) { | ||
await executeCommand(config.buildCommand, componentPath) | ||
await executeCommand(config.buildCommand, componentPath); | ||
@@ -224,4 +230,4 @@ if (config.buildOnly) process.exit(0); | ||
return true; | ||
} | ||
} | ||
}, | ||
}; | ||
} | ||
@@ -233,10 +239,10 @@ | ||
* worker thread. | ||
* | ||
* | ||
* The Resource Extension is responsible for creating the Next.js server, and | ||
* hooking into the global HarperDB server. | ||
* | ||
* @param {ExtensionOptions} options | ||
* @returns | ||
* | ||
* @param {ExtensionOptions} options | ||
* @returns | ||
*/ | ||
export function start (options = {}) { | ||
export function start(options = {}) { | ||
const config = resolveConfig(options); | ||
@@ -246,3 +252,2 @@ | ||
async handleDirectory(_, componentPath) { | ||
logger.info(`Next.js Extension is creating Next.js Request Handlers for ${componentPath}`); | ||
@@ -252,2 +257,4 @@ | ||
const next = (await import(path.join(componentPath, 'node_modules/next/dist/server/next.js'))).default; | ||
const app = next({ dir: componentPath, dev: config.dev }); | ||
@@ -259,9 +266,8 @@ | ||
const servers = options.server.http((request) => { | ||
return requestHandler( | ||
request._nodeRequest, | ||
request._nodeResponse, | ||
url.parse(request._nodeRequest.url, true) | ||
); | ||
}, { port: config.port }); | ||
const servers = options.server.http( | ||
(request) => { | ||
return requestHandler(request._nodeRequest, request._nodeResponse, url.parse(request._nodeRequest.url, true)); | ||
}, | ||
{ port: config.port } | ||
); | ||
@@ -275,7 +281,7 @@ if (config.dev) { | ||
logger.info(`Next.js App available on localhost:${config.port}`) | ||
logger.info(`Next.js App available on localhost:${config.port}`); | ||
return true; | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
} |
{ | ||
"name": "@harperdb/nextjs", | ||
"description": "A HarperDB Component for running Next.js apps.", | ||
"keywords": [ | ||
"harperdb", | ||
"next.js", | ||
"distributed", | ||
"applications" | ||
], | ||
"version": "0.0.2", | ||
"homepage": "https://github.com/HarperDB-Add-Ons/nextjs", | ||
"bugs": { | ||
"url": "https://github.com/HarperDB-Add-Ons/nextjs/issues", | ||
"email": "support@harperdb.io" | ||
}, | ||
"author": { | ||
"name": "HarperDB", | ||
"email": "support@harperdb.io" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/HarperDB-Add-Ons/nextjs.git" | ||
}, | ||
"license": "MIT", | ||
"type": "module", | ||
"bin": { | ||
"harperdb-nextjs": "cli.js" | ||
}, | ||
"files": [ | ||
"config.yaml", | ||
"extension.js", | ||
"cli.js" | ||
], | ||
"dependencies": { | ||
"next": "^14.2.6", | ||
"semver": "^7.6.3", | ||
"shell-quote": "^1.8.1" | ||
} | ||
"name": "@harperdb/nextjs", | ||
"version": "0.0.3", | ||
"type": "module", | ||
"description": "A HarperDB Component for running Next.js apps.", | ||
"keywords": [ | ||
"harperdb", | ||
"next.js", | ||
"distributed", | ||
"applications" | ||
], | ||
"homepage": "https://github.com/HarperDB-Add-Ons/nextjs", | ||
"bugs": { | ||
"url": "https://github.com/HarperDB-Add-Ons/nextjs/issues", | ||
"email": "support@harperdb.io" | ||
}, | ||
"author": { | ||
"name": "HarperDB", | ||
"email": "support@harperdb.io" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/HarperDB-Add-Ons/nextjs.git" | ||
}, | ||
"license": "MIT", | ||
"bin": { | ||
"harperdb-nextjs": "cli.js" | ||
}, | ||
"files": [ | ||
"config.yaml", | ||
"extension.js", | ||
"cli.js" | ||
], | ||
"scripts": { | ||
"format": "prettier --write ." | ||
}, | ||
"dependencies": { | ||
"semver": "^7.6.3", | ||
"shell-quote": "^1.8.1" | ||
}, | ||
"devDependencies": { | ||
"@harperdb/code-guidelines": "^0.0.2", | ||
"prettier": "^3.3.3" | ||
}, | ||
"prettier": "@harperdb/code-guidelines/prettier" | ||
} |
@@ -10,6 +10,9 @@ # @harperdb/nextjs | ||
1. Install: | ||
```sh | ||
npm install @harperdb/nextjs | ||
``` | ||
2. Add to `config.yaml`: | ||
```yaml | ||
@@ -20,7 +23,11 @@ @harperdb/nextjs: | ||
``` | ||
3. Run your app with HarperDB: | ||
```sh | ||
harperdb run nextjs-app | ||
``` | ||
Alternatively, you can use the included `harperdb-nextjs` CLI: | ||
```sh | ||
@@ -27,0 +34,0 @@ harperdb-nextjs build | dev | start |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
13411
2
266
66
2
- Removednext@^14.2.6
- Removed@next/env@14.2.23(transitive)
- Removed@next/swc-darwin-arm64@14.2.23(transitive)
- Removed@next/swc-darwin-x64@14.2.23(transitive)
- Removed@next/swc-linux-arm64-gnu@14.2.23(transitive)
- Removed@next/swc-linux-arm64-musl@14.2.23(transitive)
- Removed@next/swc-linux-x64-gnu@14.2.23(transitive)
- Removed@next/swc-linux-x64-musl@14.2.23(transitive)
- Removed@next/swc-win32-arm64-msvc@14.2.23(transitive)
- Removed@next/swc-win32-ia32-msvc@14.2.23(transitive)
- Removed@next/swc-win32-x64-msvc@14.2.23(transitive)
- Removed@swc/counter@0.1.3(transitive)
- Removed@swc/helpers@0.5.5(transitive)
- Removedbusboy@1.6.0(transitive)
- Removedcaniuse-lite@1.0.30001695(transitive)
- Removedclient-only@0.0.1(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedjs-tokens@4.0.0(transitive)
- Removedloose-envify@1.4.0(transitive)
- Removednanoid@3.3.8(transitive)
- Removednext@14.2.23(transitive)
- Removedpicocolors@1.1.1(transitive)
- Removedpostcss@8.4.31(transitive)
- Removedreact@18.3.1(transitive)
- Removedreact-dom@18.3.1(transitive)
- Removedscheduler@0.23.2(transitive)
- Removedsource-map-js@1.2.1(transitive)
- Removedstreamsearch@1.1.0(transitive)
- Removedstyled-jsx@5.1.1(transitive)
- Removedtslib@2.8.1(transitive)