diff --git a/packages/cli/lang/en.json b/packages/cli/lang/en.json index 79633ce515..2281a0c78f 100644 --- a/packages/cli/lang/en.json +++ b/packages/cli/lang/en.json @@ -24,7 +24,7 @@ "commands_build_options_s": "Strategy to use for building the wrapper", "commands_build_options_s_strategy": "strategy", "commands_build_options_l": "Log file to save console output to", - "commands_build_error_codegen_failed": "Code generation failed!", + "commands_build_error_codegen_failed": "Code generation failed", "commands_infra_description": "Modular Infrastructure-As-Code Orchestrator", "commands_infra_actions_subtitle": "Infra allows you to execute the following commands:", "commands_infra_options_options": "options", @@ -90,7 +90,7 @@ "commands_codegen_options_o": "Output directory for custom generated types (default: 'types/')", "commands_codegen_options_o_path": "path", "commands_codegen_success": "Types were generated successfully", - "commands_codegen_project_load_error": "Failed to load project! Please make sure {manifestPath} is a valid Project manifest!", + "commands_codegen_project_load_error": "Failed to load project, please make sure {manifestPath} is a valid Project manifest", "commands_codegen_options_publish": "Output path for the built schema and manifest (default: {default})", "commands_docgen_description": "Generate wrapper documentation", "commands_docgen_success": "Docs were generated successfully", @@ -216,7 +216,8 @@ "lib_compiler_noNodeModules": "could not locate {folder} in parent directories of polywrap manifest", "lib_compiler_noInvoke": "WASM module is missing the _wrap_invoke export. This should never happen...", "lib_compiler_invalid_module": "Invalid Wasm module found. {modulePath} is invalid. Error: {error}", - "lib_compiler_cannotBuildInterfaceModules": "Cannot build modules for an Interface Polywrap", + "lib_compiler_cannotBuildModule": "Cannot build the module for a project of type {project}", + "lib_compiler_missingBuildStrategy": "The compiler is missing a build strategy", "lib_compiler_copyResourcesFolderText": "Copying resources folder: {folder}", "lib_compiler_copyResourcesFolderError": "Failed to copy resources folder: {folder}", "lib_compiler_copyResourcesFolderWarning": "Warnings copying resources folder: {folder}", @@ -255,6 +256,7 @@ "lib_helpers_docker_projectFolderMissing": "Build image `{image}` missing /project folder", "lib_helpers_docker_projectBuildFolderMissing": "Build image `{image}` missing build artifact `{artifact}` in folder /project/build", "lib_helpers_parameters_unexpectedValue": "Unexpected value provided for one or more of {options}. See --help for more information.", + "lib_helpers_wrap_unsupportedType": "Unsupported WRAP type {type}", "lib_language_unsupportedManifestLanguage": "The manifest's language {language} is not supported. Please use one of the supported lanauges: {supported}", "lib_language_noInterfaceCodegen": "Code generation cannot be run for Polywrap Interface projects", "lib_system_env_var_not_found": "Environment variable not found: `{variableName}`", diff --git a/packages/cli/lang/es.json b/packages/cli/lang/es.json index 79633ce515..2281a0c78f 100644 --- a/packages/cli/lang/es.json +++ b/packages/cli/lang/es.json @@ -24,7 +24,7 @@ "commands_build_options_s": "Strategy to use for building the wrapper", "commands_build_options_s_strategy": "strategy", "commands_build_options_l": "Log file to save console output to", - "commands_build_error_codegen_failed": "Code generation failed!", + "commands_build_error_codegen_failed": "Code generation failed", "commands_infra_description": "Modular Infrastructure-As-Code Orchestrator", "commands_infra_actions_subtitle": "Infra allows you to execute the following commands:", "commands_infra_options_options": "options", @@ -90,7 +90,7 @@ "commands_codegen_options_o": "Output directory for custom generated types (default: 'types/')", "commands_codegen_options_o_path": "path", "commands_codegen_success": "Types were generated successfully", - "commands_codegen_project_load_error": "Failed to load project! Please make sure {manifestPath} is a valid Project manifest!", + "commands_codegen_project_load_error": "Failed to load project, please make sure {manifestPath} is a valid Project manifest", "commands_codegen_options_publish": "Output path for the built schema and manifest (default: {default})", "commands_docgen_description": "Generate wrapper documentation", "commands_docgen_success": "Docs were generated successfully", @@ -216,7 +216,8 @@ "lib_compiler_noNodeModules": "could not locate {folder} in parent directories of polywrap manifest", "lib_compiler_noInvoke": "WASM module is missing the _wrap_invoke export. This should never happen...", "lib_compiler_invalid_module": "Invalid Wasm module found. {modulePath} is invalid. Error: {error}", - "lib_compiler_cannotBuildInterfaceModules": "Cannot build modules for an Interface Polywrap", + "lib_compiler_cannotBuildModule": "Cannot build the module for a project of type {project}", + "lib_compiler_missingBuildStrategy": "The compiler is missing a build strategy", "lib_compiler_copyResourcesFolderText": "Copying resources folder: {folder}", "lib_compiler_copyResourcesFolderError": "Failed to copy resources folder: {folder}", "lib_compiler_copyResourcesFolderWarning": "Warnings copying resources folder: {folder}", @@ -255,6 +256,7 @@ "lib_helpers_docker_projectFolderMissing": "Build image `{image}` missing /project folder", "lib_helpers_docker_projectBuildFolderMissing": "Build image `{image}` missing build artifact `{artifact}` in folder /project/build", "lib_helpers_parameters_unexpectedValue": "Unexpected value provided for one or more of {options}. See --help for more information.", + "lib_helpers_wrap_unsupportedType": "Unsupported WRAP type {type}", "lib_language_unsupportedManifestLanguage": "The manifest's language {language} is not supported. Please use one of the supported lanauges: {supported}", "lib_language_noInterfaceCodegen": "Code generation cannot be run for Polywrap Interface projects", "lib_system_env_var_not_found": "Environment variable not found: `{variableName}`", diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/build.ts index a2d47e316b..f77fc33b75 100644 --- a/packages/cli/src/commands/build.ts +++ b/packages/cli/src/commands/build.ts @@ -15,8 +15,6 @@ import { parseLogFileOption, getProjectFromManifest, isPolywrapManifestLanguage, - isPluginManifestLanguage, - generateWrapFile, polywrapManifestLanguages, pluginManifestLanguages, parseWrapperEnvsOption, @@ -31,8 +29,6 @@ import { } from "../lib/build-strategies"; import { defaultCodegenDir } from "../lib/defaults/defaultCodegenDir"; -import fs from "fs"; -import path from "path"; import readline from "readline"; import { Env, PolywrapClient } from "@polywrap/client-js"; import { PolywrapManifest } from "@polywrap/polywrap-manifest-types-js"; @@ -197,18 +193,29 @@ async function run(options: BuildCommandOptions) { const manifest = await project.getManifest(); const language = manifest.project.type; - let execute: () => Promise; + if (supportedProjectTypes.indexOf(language) === -1) { + logger.error( + intlMsg.commands_build_error_unsupportedProjectType({ + supportedTypes: supportedProjectTypes.join(", "), + }) + ); + process.exit(1); + } + + let buildStrategy: BuildStrategy; if (isPolywrapManifestLanguage(language)) { await validateManifestModules(manifest as PolywrapManifest); - const buildStrategy = createBuildStrategy( + buildStrategy = createBuildStrategy( strategy, outputDir, project as PolywrapProject ); + } - execute = async (): Promise => { + const execute = async (): Promise => { + try { const schemaComposer = new SchemaComposer({ project, client, @@ -235,65 +242,12 @@ async function run(options: BuildCommandOptions) { buildStrategy, }); - const result = await compiler.compile(); - - if (!result) { - return result; - } - - return true; - }; - } else if (isPluginManifestLanguage(language)) { - execute = async (): Promise => { - const schemaComposer = new SchemaComposer({ - project, - client, - }); - - // Output the built manifest - const manifestPath = path.join(outputDir, "wrap.info"); - - try { - if (codegen) { - const codeGenerator = new CodeGenerator({ - project, - schemaComposer, - codegenDirAbs: codegenDir, - }); - const codegenSuccess = await codeGenerator.generate(); - - if (!codegenSuccess) { - logger.error(intlMsg.commands_build_error_codegen_failed()); - return false; - } - } - - if (!fs.existsSync(outputDir)) { - fs.mkdirSync(outputDir); - } - - await generateWrapFile( - await schemaComposer.getComposedAbis(), - await project.getName(), - "plugin", - manifestPath, - logger - ); - } catch (err) { - logger.error(err.message); - return false; - } - - return true; - }; - } else { - logger.error( - intlMsg.commands_build_error_unsupportedProjectType({ - supportedTypes: supportedProjectTypes.join(", "), - }) - ); - return; - } + return await compiler.compile(); + } catch (err) { + logger.error(err.message); + return false; + } + }; if (!watch) { const result = await execute(); diff --git a/packages/cli/src/lib/Compiler.ts b/packages/cli/src/lib/Compiler.ts index 639a9132d9..463d303e67 100644 --- a/packages/cli/src/lib/Compiler.ts +++ b/packages/cli/src/lib/Compiler.ts @@ -6,6 +6,7 @@ import { generateWrapFile, intlMsg, PolywrapProject, + PluginProject, resetDir, SchemaComposer, logActivity, @@ -21,8 +22,8 @@ import path from "path"; export interface CompilerConfig { outputDir: string; - project: PolywrapProject; - buildStrategy: BuildStrategy; + project: PolywrapProject | PluginProject; + buildStrategy?: BuildStrategy; schemaComposer: SchemaComposer; } @@ -39,13 +40,13 @@ export class Compiler { // Output: wrap.info await this._outputWrapManifest(); - if (!(await this._isInterface())) { - // Compile the Wrapper + if (await this._isWasm()) { + // Build & Output: wasm.wrap await this._buildModules(); - } - // Copy: Resources folder - await this._copyResourcesFolder(); + // Copy: Resources folder + await this._copyResourcesFolder(); + } }; try { @@ -64,17 +65,26 @@ export class Compiler { } } - private async _isInterface(): Promise { + private async _isWasm(): Promise { const { project } = this._config; const manifest = await project.getManifest(); - return manifest.project.type === "interface"; + return manifest.project.type.startsWith("wasm/"); } private async _buildModules(): Promise { - const { outputDir } = this._config; + const { outputDir, project } = this._config; + + if (!this._config.buildStrategy) { + throw Error(intlMsg.lib_compiler_missingBuildStrategy()); + } - if (await this._isInterface()) { - throw Error(intlMsg.lib_compiler_cannotBuildInterfaceModules()); + if (!(await this._isWasm())) { + const manifest = await project.getManifest(); + throw Error( + intlMsg.lib_compiler_cannotBuildModule({ + project: manifest.project.type, + }) + ); } // Build the sources @@ -90,7 +100,7 @@ export class Compiler { const run = async () => { const manifest = await project.getManifest(); - const type = (await this._isInterface()) ? "interface" : "wasm"; + const type = manifest.project.type.split("/")[0]; const abi = await schemaComposer.getComposedAbis(); await generateWrapFile( abi, @@ -122,7 +132,7 @@ export class Compiler { private async _copyResourcesFolder(): Promise { const { outputDir, project } = this._config; - const projectManifest = await project.getManifest(); + const projectManifest = await (project as PolywrapProject).getManifest(); if (!projectManifest || !projectManifest.resources) { return Promise.resolve(); diff --git a/packages/cli/src/lib/helpers/wrap.ts b/packages/cli/src/lib/helpers/wrap.ts index 8522f869d2..5af8df3d07 100644 --- a/packages/cli/src/lib/helpers/wrap.ts +++ b/packages/cli/src/lib/helpers/wrap.ts @@ -10,10 +10,16 @@ import { } from "@polywrap/wrap-manifest-types-js"; import { normalizePath, writeFileSync } from "@polywrap/os-js"; +export const supportedWrapTypes: WrapManifest["type"][] = [ + "interface", + "wasm", + "plugin", +]; + const run = async ( abi: WrapAbi, name: string, - type: "interface" | "wasm" | "plugin", + type: WrapManifest["type"], path: string ): Promise => { const manifest: WrapManifest = { @@ -31,10 +37,16 @@ const run = async ( export const generateWrapFile = async ( abi: WrapAbi, name: string, - type: "interface" | "wasm" | "plugin", + type: string, path: string, logger: Logger ): Promise => { + if (!supportedWrapTypes.includes(type as WrapManifest["type"])) { + throw Error(intlMsg.lib_helpers_wrap_unsupportedType({ type })); + } + + const wrapType = type as WrapManifest["type"]; + const relativePath = displayPath(path); return await logActivity( logger, @@ -48,7 +60,7 @@ export const generateWrapFile = async ( path: normalizePath(relativePath), }), async (): Promise => { - await run(abi, name, type, path); + await run(abi, name, wrapType, path); } ); };