feat: add plugin build command (#10935)
Fixes: FRMW-2863 Adds the `plugin:build` command that is used to compile the source code of a plugin for publishing it to a package registry. The command is similar to the `build` command, except it does not copy the `package.json` and the `lock` files to the build output
This commit is contained in:
7
.changeset/calm-cameras-unite.md
Normal file
7
.changeset/calm-cameras-unite.md
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
"@medusajs/framework": patch
|
||||
"@medusajs/cli": patch
|
||||
---
|
||||
|
||||
feat: add plugin build command
|
||||
@@ -254,10 +254,20 @@ function buildLocalCommands(cli, isLocalProject) {
|
||||
})
|
||||
),
|
||||
})
|
||||
.command({
|
||||
command: "plugin:build",
|
||||
desc: "Build plugin source for publishing to a package registry",
|
||||
handler: handlerP(
|
||||
getCommandHandler("plugin/build", (args, cmd) => {
|
||||
process.env.NODE_ENV = process.env.NODE_ENV || `development`
|
||||
cmd(args)
|
||||
return new Promise((resolve) => {})
|
||||
})
|
||||
),
|
||||
})
|
||||
.command({
|
||||
command: "plugin:develop",
|
||||
desc: "Start plugin development process in watch mode. Changes will be re-published to the local packages registry",
|
||||
builder: (builder) => {},
|
||||
handler: handlerP(
|
||||
getCommandHandler("plugin/develop", (args, cmd) => {
|
||||
process.env.NODE_ENV = process.env.NODE_ENV || `development`
|
||||
|
||||
@@ -382,8 +382,55 @@ export class Compiler {
|
||||
}
|
||||
}
|
||||
|
||||
// @todo
|
||||
buildPluginBackend() {}
|
||||
/**
|
||||
* Compiles the plugin source code to JavaScript using the
|
||||
* TypeScript's official compiler
|
||||
*/
|
||||
async buildPluginBackend(tsConfig: tsStatic.ParsedCommandLine) {
|
||||
const tracker = this.#trackDuration()
|
||||
const dist = ".medusa/server"
|
||||
this.#logger.info("Compiling plugin source...")
|
||||
|
||||
/**
|
||||
* Step 1: Cleanup existing build output
|
||||
*/
|
||||
this.#logger.info(
|
||||
`Removing existing "${path.relative(this.#projectRoot, dist)}" folder`
|
||||
)
|
||||
await this.#clean(dist)
|
||||
|
||||
/**
|
||||
* Step 2: Compile TypeScript source code
|
||||
*/
|
||||
const { emitResult, diagnostics } = await this.#emitBuildOutput(
|
||||
tsConfig,
|
||||
["integration-tests", "test", "unit-tests", "src/admin"],
|
||||
dist
|
||||
)
|
||||
|
||||
/**
|
||||
* Exit early if no output is written to the disk
|
||||
*/
|
||||
if (emitResult.emitSkipped) {
|
||||
this.#logger.warn("Plugin build completed without emitting any output")
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify about the state of build
|
||||
*/
|
||||
if (diagnostics.length) {
|
||||
this.#logger.warn(
|
||||
`Plugin build completed with errors (${tracker.getSeconds()}s)`
|
||||
)
|
||||
} else {
|
||||
this.#logger.info(
|
||||
`Plugin build completed successfully (${tracker.getSeconds()}s)`
|
||||
)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles the backend source code of a plugin project in watch
|
||||
|
||||
22
packages/medusa/src/commands/plugin/build.ts
Normal file
22
packages/medusa/src/commands/plugin/build.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { logger } from "@medusajs/framework/logger"
|
||||
import { Compiler } from "@medusajs/framework/build-tools"
|
||||
|
||||
export default async function build({
|
||||
directory,
|
||||
adminOnly,
|
||||
}: {
|
||||
directory: string
|
||||
adminOnly: boolean
|
||||
}): Promise<boolean> {
|
||||
logger.info("Starting build...")
|
||||
const compiler = new Compiler(directory, logger)
|
||||
|
||||
const tsConfig = await compiler.loadTSConfigFile()
|
||||
if (!tsConfig) {
|
||||
logger.error("Unable to compile plugin")
|
||||
return false
|
||||
}
|
||||
|
||||
await compiler.buildPluginBackend(tsConfig)
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user