vuepress-plugin-netlify-functions
Version:
The Plugin for VuePress 2, Support Netlify Functions
42 lines (41 loc) • 1.24 kB
JavaScript
import { Buffer } from 'node:buffer';
import process from 'node:process';
import { fs, getDirname, path } from 'vuepress/utils';
import dotenv from 'dotenv';
import { execa } from 'execa';
import * as portFinder from 'portfinder';
const __dirname = getDirname(import.meta.url);
function loadEnvConfig() {
const configPath = path.resolve(process.cwd(), '.env');
if (!fs.existsSync(configPath))
return {};
try {
const content = fs.readFileSync(configPath, 'utf-8');
return dotenv.parse(Buffer.from(content));
}
catch {
return {};
}
}
export async function netlifyServe({ directory, }) {
const port = await portFinder.getPortPromise({ port: 9000 });
const argv = [
'functions:serve',
'--port',
`${port}`,
'--functions',
path.join('./', path.relative(process.cwd(), directory.temp)),
// '--debug',
];
const { stdout, kill } = execa(path.resolve(__dirname, '../../../node_modules/.bin/netlify'), argv, {
cwd: process.cwd(),
env: {
...loadEnvConfig(),
},
});
stdout?.pipe(process.stdout);
return {
host: `http://localhost:${port}`,
close: () => kill(),
};
}