Skip to content

Commit

Permalink
generate plugin using product template
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Jun 20, 2024
1 parent e85d154 commit 221f235
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
5 changes: 3 additions & 2 deletions server/datastores/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,11 @@ module.exports = class PgDatastore extends Datastore {
filename: metadata.name.replace(/ /g, '-'),
type: metadata.type,
pluginId: pluginId,
template: metadata.template
template: metadata.template,
productTemplate: metadata.productTemplate
}

console.log('generate pluginID', pluginId, newPlugin.pluginId)
console.log('generate pluginID', pluginId, newPlugin)

return this.pool.connect()
.then(client => {
Expand Down
2 changes: 2 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { ENV, AUTHENTICATION } = require('./configuration');

const pluginsRouter = require('./routers/plugins');
const templatesRouter = require('./routers/templates');
const productsTemplatesRouter = require('./routers/products_templates');
const publicRouter = require('./routers/public');
const wasmRouter = require('./routers/wasm');
const invitationRouter = require('./routers/invitation');
Expand Down Expand Up @@ -125,6 +126,7 @@ function createServer(appVersion) {
app.use(`${baseURL}/`, publicRouter);
app.use(`${baseURL}/api/plugins`, pluginsRouter);
app.use(`${baseURL}/api/templates`, templatesRouter);
app.use(`${baseURL}/api/products_templates`, productsTemplatesRouter);
app.use(`${baseURL}/api/wasm`, wasmRouter);
app.use(`${baseURL}/api/version`, (_, res) => res.json(appVersion));
app.use(`${baseURL}/api/development`, (_, res) => res.json(ENV.IS_DEV));
Expand Down
83 changes: 83 additions & 0 deletions server/routers/products_templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const fetch = require('node-fetch');
const express = require('express');
const path = require('path');
const { FileSystem } = require('../services/file-system');
const { ENV } = require('../configuration');

const LANGUAGES_INDEX = {
rust: 'lib.rs',
js: 'index.js',
ts: 'index.ts',
go: 'main.go'
};

const router = express.Router()

router.get('/', (req, res) => {
if (!req.query) {
res
.status(400)
.json({
error: 'Missing type of project'
})
} else {
const { type, productTemplate } = req.query;
// const template = !req.query.template || req.query.template === 'undefined' ? 'empty' : req.query.template;

if (['rust', 'js', 'go', 'ts'].includes(type)) {
const path = `templates/otoroshi/${productTemplate}/${LANGUAGES_INDEX[type]}`

getTemplates(path, res);
} else {
res
.status(404)
.json({
error: 'No template for this type of project'
})
}
}
});

function getTemplatesFromPath(type, template, res) {
if (template !== 'empty')
return res.sendFile(path.join(__dirname, '../templates', template, `${type}.zip`))
else
return res.sendFile(path.join(__dirname, '../templates', `${type}.zip`))
}

function getTemplates(path, res) {
const source = ENV.MANAGER_TEMPLATES;

if (!source) {
return getTemplatesFromPath(path, res);
} else if (source.startsWith('file://')) {
const paths = [path]

FileSystem.existsFile(...paths)
.then(() => {
res.download(FileSystem.pathsToPath(...paths), 'data')
})
.catch(err => {
res
.status(400)
.json({ error: err })
})
} else if (source.startsWith('http')) {
fetch(`${source}/${path}`, {
redirect: 'follow'
})
.then(r => r.json())
.then(r => {
fetch(r.download_url)
.then(raw => raw.body.pipe(res))
})
} else {
res
.status(404)
.json({
error: 'No template for this type of project'
})
}
}

module.exports = router
21 changes: 20 additions & 1 deletion ui/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class App extends React.Component {
})
}

initializeEmptyPlugin = () => {
initializeEmptyPlugin = async () => {
const { selectedPlugin } = this.state;

const INFORMATIONS_FILENAME = {
Expand All @@ -302,6 +302,20 @@ class App extends React.Component {
opa: "package.json"
};

const LANGUAGES_INDEX = {
rust: 'lib.rs',
js: 'index.js',
ts: 'index.ts',
go: 'main.go'
};

let indexFileContent = undefined

if (selectedPlugin.type !== "opa" && selectedPlugin.productTemplate) {
indexFileContent = await Service.getPluginProductTemplate(selectedPlugin.type, selectedPlugin.template || 'empty', selectedPlugin.productTemplate)
.then(res => res.text())
}

this.updateSelectedPlugin({
selectedPlugin: {
...selectedPlugin,
Expand All @@ -313,6 +327,11 @@ class App extends React.Component {
.replace('@@PLUGIN_NAME@@', selectedPlugin.filename)
.replace('@@PLUGIN_VERSION@@', '1.0.0')
}
} else if (indexFileContent && file.filename === LANGUAGES_INDEX[selectedPlugin.type]) {
return {
...file,
content: indexFileContent
}
}
return file;
})
Expand Down
2 changes: 2 additions & 0 deletions ui/src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export const getPluginConfig = plugin => jsonFetch(`/plugins/${plugin}/configura

export const getPluginTemplate = (type, template, productTemplate) => f(`/templates?type=${type}&template=${template}&productTemplate=${productTemplate}`);

export const getPluginProductTemplate = (type, template, productTemplate) => f(`/products_templates?type=${type}&template=${template}&productTemplate=${productTemplate}`);

const buildZip = plugin => {
const jsZip = new JSZip()

Expand Down

0 comments on commit 221f235

Please sign in to comment.