|
|
|
@ -11,7 +11,7 @@ const { exec } = require("child_process") |
|
|
|
const sqrl = require("squirrelly") |
|
|
|
|
|
|
|
exports.fetch = async function(ctx) { |
|
|
|
const db = new CouchDB(ClientDb.name(env.CLIENT_ID)) |
|
|
|
const db = new CouchDB(ClientDb.name(getClientId(ctx))) |
|
|
|
const body = await db.query("client/by_type", { |
|
|
|
include_docs: true, |
|
|
|
key: ["app"], |
|
|
|
@ -21,16 +21,30 @@ exports.fetch = async function(ctx) { |
|
|
|
} |
|
|
|
|
|
|
|
exports.fetchAppPackage = async function(ctx) { |
|
|
|
const db = new CouchDB(ClientDb.name(env.CLIENT_ID)) |
|
|
|
const clientId = await lookupClientId(ctx.params.applicationId) |
|
|
|
const db = new CouchDB(ClientDb.name(clientId)) |
|
|
|
const application = await db.get(ctx.params.applicationId) |
|
|
|
ctx.body = await getPackageForBuilder(ctx.config, application) |
|
|
|
} |
|
|
|
|
|
|
|
exports.create = async function(ctx) { |
|
|
|
const db = new CouchDB(ClientDb.name(env.CLIENT_ID)) |
|
|
|
const clientId = |
|
|
|
(ctx.request.body && ctx.request.body.clientId) || env.CLIENT_ID |
|
|
|
|
|
|
|
if (!clientId) { |
|
|
|
ctx.throw(400, "ClientId not suplied") |
|
|
|
} |
|
|
|
const appId = newid() |
|
|
|
// insert an appId -> clientId lookup
|
|
|
|
const masterDb = new CouchDB("master") |
|
|
|
await masterDb.put({ |
|
|
|
_id: appId, |
|
|
|
clientId, |
|
|
|
}) |
|
|
|
const db = new CouchDB(ClientDb.name(clientId)) |
|
|
|
|
|
|
|
const newApplication = { |
|
|
|
_id: newid(), |
|
|
|
_id: appId, |
|
|
|
type: "app", |
|
|
|
instances: [], |
|
|
|
userInstanceMap: {}, |
|
|
|
@ -47,11 +61,10 @@ exports.create = async function(ctx) { |
|
|
|
|
|
|
|
const createInstCtx = { |
|
|
|
params: { |
|
|
|
clientId: env.CLIENT_ID, |
|
|
|
applicationId: newApplication._id, |
|
|
|
}, |
|
|
|
request: { |
|
|
|
body: { name: `dev-${env.CLIENT_ID}` }, |
|
|
|
body: { name: `dev-${clientId}` }, |
|
|
|
}, |
|
|
|
} |
|
|
|
await instanceController.create(createInstCtx) |
|
|
|
@ -61,6 +74,7 @@ exports.create = async function(ctx) { |
|
|
|
await runNpmInstall(newAppFolder) |
|
|
|
} |
|
|
|
|
|
|
|
ctx.status = 200 |
|
|
|
ctx.body = newApplication |
|
|
|
ctx.message = `Application ${ctx.request.body.name} created successfully` |
|
|
|
} |
|
|
|
@ -79,7 +93,6 @@ const createEmptyAppPackage = async (ctx, app) => { |
|
|
|
|
|
|
|
if (await exists(newAppFolder)) { |
|
|
|
ctx.throw(400, "App folder already exists for this application") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
await copy(templateFolder, newAppFolder) |
|
|
|
@ -99,6 +112,24 @@ const createEmptyAppPackage = async (ctx, app) => { |
|
|
|
return newAppFolder |
|
|
|
} |
|
|
|
|
|
|
|
const lookupClientId = async appId => { |
|
|
|
const masterDb = new CouchDB("master") |
|
|
|
const { clientId } = await masterDb.get(appId) |
|
|
|
return clientId |
|
|
|
} |
|
|
|
|
|
|
|
const getClientId = ctx => { |
|
|
|
const clientId = |
|
|
|
(ctx.request.body && ctx.request.body.clientId) || |
|
|
|
(ctx.query && ctx.query.clientId) || |
|
|
|
env.CLIENT_ID |
|
|
|
|
|
|
|
if (!clientId) { |
|
|
|
ctx.throw(400, "ClientId not suplied") |
|
|
|
} |
|
|
|
return clientId |
|
|
|
} |
|
|
|
|
|
|
|
const updateJsonFile = async (filePath, app) => { |
|
|
|
const json = await readFile(filePath, "utf8") |
|
|
|
const newJson = sqrl.Render(json, app) |
|
|
|
|