Skip to content

Commit

Permalink
testing config
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpagan committed Jun 12, 2023
1 parent 129adf3 commit a584c73
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 67 deletions.
24 changes: 6 additions & 18 deletions CoCreate.config.js.example
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
module.exports = {
"organization": {
"_id": "<If Known or leave empty>",
"name": "<organization, company or project name>",
"host": [
"<CoCreate subdomain or custom domain pointing to CoCreateWS server>",
]
},
"user": {
"_id": "<If Known or leave empty>",
"name": "admin",
"email": "<email to use for loggin>",
"password": "<password to use for loggin>"
},
"config": {
"key": "<CoCreate API Key>",
"organization_id": "<CoCreate Organization ID>",
"host": "<CoCreate subdomain or custom domain pointing to CoCreateWS server>"
},
"user_id": "<If Known or leave empty>",
"email": "<email to use for loggin>",
"password": "<password to use for loggin>",
"organization_id": "<CoCreate Organization ID>",
"key": "<CoCreate API Key>",
"host": "<CoCreate subdomain or custom domain pointing to CoCreateWS server>"
"storage": {
"mongodb": {
"provider": "mongodb",
Expand Down
63 changes: 16 additions & 47 deletions installation/createDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,47 @@ const fs = require('fs');
const path = require("path")
let config = await cli.config([
{
key: 'organization_id',
prompt: 'Enter your organization_id: '
}, {
key: 'host',
prompt: 'Enter the host: '
}, {
prompt: 'Choose an authentication option: \n1.key\n2.Sign In\n',
choices: {
'1': {
key: 'key',
prompt: 'Enter your key: '
},
'2': [
{
key: 'email',
prompt: 'Enter your email: '
},
{
key: 'password',
prompt: 'Enter your password: '
}
]
}
key: 'storage',
prompt: 'Enter a JSON.stringify storage object'
}
])

// TODO: add config propmts
if (typeof config.storage === 'string')
config.storage = JSON.parse(config.storage)
let databases = Object.keys(config.storage)
let dbUrl = databases[0].url[0]
if (dbUrl)
update(dbUrl)
else
else {
console.log('Could not find a url in your storage object')
process.exit()
}


async function update(dbUrl) {
const dbClient = await MongoClient.connect(dbUrl, { useNewUrlParser: true, useUnifiedTopology: true });
const organization_id = config.organization._id || config.config.organization_id || `${ObjectId()}`
const key = config.organization.key || config.config.key || uuid.generate(32);
const user_id = config.user._id || `${ObjectId()}`;
const organization_id = config.organization_id || `${ObjectId()}`
const key = config.key || uuid.generate(32);
const user_id = config.user_id || `${ObjectId()}`;

console.log(organization_id, key, user_id)

try {
// Create organization
const organizations = dbClient.db(organization_id).collection('organizations');

let organization = config.organization;
let organization = {};
organization._id = ObjectId(organization_id);
organization.host = config.organization.host
organization.host = config.host
organization.storage = config.storage
organization.apis = {
stripe: {
environment: "test",
test: "",
production: ""
}
}
organization.organization_id = organization_id;
await organizations.insertOne(organization);

// Create user
const users = dbClient.db(organization_id).collection('users');
let user = config.user;
let user = {};
user['_id'] = ObjectId(user_id);
user['firstname'] = 'Admin'
user['organization_id'] = organization_id;
await users.insertOne(user);

Expand Down Expand Up @@ -124,15 +100,8 @@ function updateConfig(organization_id, key) {
return console.error('path does not exist:', configfile)

let object = require(configfile)
if (!object.config) {
let obj = {}
obj['config'] = {}
object = { ...obj, ...object }
}
Object.assign(object.config, { organization_id })
Object.assign(object.config, { key })
delete object.organization
delete object.user
object.organization_id = organization_id
object.key = key

let config = `module.exports = ${JSON.stringify(object, null, 4)}`

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"homepage": "https://cocreate.app/docs/CoCreateWS",
"main": "./src/server.js",
"scripts": {
"start": "PORT=3000 node src/server.js",
"dev": "PORT=3000 nodemon src/server.js",
"start": "node src/server.js",
"dev": "nodemon src/server.js",
"test": "echo \"Error: no test specified\" && exit 1",
"postinstall": "node -e \"const { execSync } = require('child_process'); try { execSync('coc --version', { stdio: 'ignore' }); } catch (error) { try { execSync('npm install -g @cocreate/cli', { stdio: 'inherit' }); console.log('Installed \"@cocreate/cli\" globally.'); } catch (error) { console.error('Failed to install \"@cocreate/cli\" globally:', error); } }\"",
"createdb": "node ./installation/createDB.js"
Expand Down
10 changes: 10 additions & 0 deletions src/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ const authorize = require("@cocreate/authorize");

module.exports.init = async function (server) {
try {
let config = await cli.config([
{
key: 'organization_id',
prompt: 'Enter your organization_id or continue and one will created: '
}
])

if (!config.organization_id)
require('@cocreate/createDB');

const databases = {
mongodb: require('@cocreate/mongodb')
}
Expand Down

0 comments on commit a584c73

Please sign in to comment.