diff --git a/package-lock.json b/package-lock.json index 53b8350..6995cfa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@gcoredev/fastedge-sdk-js", - "version": "0.0.1", + "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@gcoredev/fastedge-sdk-js", - "version": "0.0.1", + "version": "1.0.1", "license": "Apache-2.0", "dependencies": { "@bytecodealliance/jco": "^1.2.4", diff --git a/src/componentize/index.js b/src/componentize/index.js index c318d37..abbb775 100644 --- a/src/componentize/index.js +++ b/src/componentize/index.js @@ -11,21 +11,28 @@ import { addWasmMetadata } from './add-wasm-metadata.js'; import { getJsInputContents } from './get-js-input.js'; import { precompile } from './precompile.js'; -import { getTmpDir, npxPackagePath, resolveTmpDir } from '~utils/file-system.js'; +import { + getTmpDir, + npxPackagePath, + resolveOsPath, + resolveTmpDir, + useUnixPath, +} from '~utils/file-system.js'; import { validateFilePaths } from '~utils/input-path-verification.js'; async function componentize(jsInput, output, opts = {}) { const { debug = false, - wasmEngine = await npxPackagePath('./lib/fastedge-runtime.wasm'), + wasmEngine = npxPackagePath('./lib/fastedge-runtime.wasm'), enableStdout = false, enablePBL = false, preBundleJSInput = true, } = opts; - const jsPath = fileURLToPath(new URL(resolve(process.cwd(), jsInput), import.meta.url)); + const jsPath = resolveOsPath(process.cwd(), jsInput); + + const wasmOutputDir = resolveOsPath(process.cwd(), output); - const wasmOutputDir = fileURLToPath(new URL(resolve(process.cwd(), output), import.meta.url)); await validateFilePaths(jsPath, wasmOutputDir, wasmEngine); const contents = await getJsInputContents(jsPath, preBundleJSInput); @@ -51,14 +58,14 @@ async function componentize(jsInput, output, opts = {}) { '--inherit-env=true', '--dir=.', // '--dir=../', // todo: Farq: NEED to iterate config file and add these paths for static building... - `--dir=${dirname(wizerInput)}`, + `--dir=${useUnixPath(dirname(wizerInput))}`, '-r _start=wizer.resume', - `-o=${wasmOutputDir}`, - wasmEngine, + `-o=${useUnixPath(wasmOutputDir)}`, + useUnixPath(wasmEngine), ], { stdio: [null, process.stdout, process.stderr], - input: wizerInput, + input: useUnixPath(wizerInput), shell: true, encoding: 'utf-8', env: { @@ -85,9 +92,7 @@ async function componentize(jsInput, output, opts = {}) { } const coreComponent = await readFile(output); - const adapter = fileURLToPath( - new URL(npxPackagePath('./lib/preview1-adapter.wasm'), import.meta.url), - ); + const adapter = npxPackagePath('./lib/preview1-adapter.wasm'); const generatedComponent = await componentNew(coreComponent, [ ['wasi_snapshot_preview1', await readFile(adapter)], diff --git a/src/fastedge-build/config-build/index.js b/src/fastedge-build/config-build/index.js index fb34cd6..be01452 100644 --- a/src/fastedge-build/config-build/index.js +++ b/src/fastedge-build/config-build/index.js @@ -1,10 +1,10 @@ -import path from 'node:path'; +import { pathToFileURL } from 'node:url'; import { createStaticManifest } from './build-manifest/create-static-manifest.js'; import { buildWasm } from './build-wasm.js'; import { normalizeBuildConfig } from '~utils/config-helpers.js'; -import { isFile } from '~utils/file-system.js'; +import { isFile, resolveOsPath } from '~utils/file-system.js'; import { colorLog } from '~utils/prompts.js'; async function buildFromConfig(config) { @@ -36,7 +36,8 @@ async function loadConfig(configPath) { try { const configFileExists = await isFile(configPath); if (configFileExists) { - const { config } = await import(/* webpackChunkName: "config" */ configPath); + const configUrlPath = pathToFileURL(configPath).href; + const { config } = await import(/* webpackChunkName: "config" */ configUrlPath); return normalizeBuildConfig(config); } colorLog('error', `Error: Config file not found at ${configPath}. Skipping build.`); @@ -48,7 +49,7 @@ async function loadConfig(configPath) { async function buildFromConfigFiles(configFilePaths = []) { for (const configFilePath of configFilePaths) { - const configPath = path.resolve(configFilePath); + const configPath = resolveOsPath(configFilePath); // Await in loop is correct, it must run sequentially - it overwrites files within each build cycle // eslint-disable-next-line no-await-in-loop await buildFromConfig(await loadConfig(configPath)); diff --git a/src/utils/file-system.js b/src/utils/file-system.js index e85ff21..636a712 100644 --- a/src/utils/file-system.js +++ b/src/utils/file-system.js @@ -1,18 +1,37 @@ import { readdirSync } from 'node:fs'; import { mkdir, mkdtemp, readdir, stat } from 'node:fs/promises'; import { tmpdir } from 'node:os'; -import path from 'node:path'; +import path, { resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { colorLog } from './prompts.js'; +/** + * + * Normalizes and resolves the path for unix/windows compatibility + * @param {Array} paths + * @returns {string} path + */ +const resolveOsPath = (...paths) => path.normalize(resolve(...paths)); + +/** + * + * Replaces backslashes with forward slashes - wizer requires unix paths + * @param {string} path + * @returns {string} path + */ +const useUnixPath = (path) => path.replace(/\\/gu, '/'); + /** * * @param {string} filePath * @returns {string} npxPackagePath */ const npxPackagePath = (filePath) => { - const __dirname = path.dirname(fileURLToPath(import.meta.url)).replace(/\/bin([^/]*)$/u, ''); + const __dirname = path + .dirname(fileURLToPath(import.meta.url)) + .replace(/[\\/]bin([\\/][^\\/]*)?$/u, ''); + try { return path.resolve(__dirname, filePath); } catch { @@ -120,5 +139,7 @@ export { isDirectory, isFile, npxPackagePath, + resolveOsPath, resolveTmpDir, + useUnixPath, };