-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add component @cocreate/config, improves onboarding process usi…
…ng a combination of local configs, global configs and prompts
- Loading branch information
1 parent
a584c73
commit 35579f6
Showing
4 changed files
with
157 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,7 @@ | ||
module.exports = { | ||
"organization_id": "5ff747727005da1c272740ab", | ||
"organization_id": "", | ||
"key": "", | ||
"host": "", | ||
"storage": { | ||
"mongodb": { | ||
"provider": "mongodb", | ||
"url": [ | ||
"mongodb+srv://cocreate-app:[email protected]/?retryWrites=true&w=majority" | ||
] | ||
} | ||
}, | ||
"directories": [ | ||
{ | ||
"entry": "./superadmin", | ||
|
@@ -104,6 +96,10 @@ module.exports = { | |
'path': '../CoCreate-components/CoCreate-cli', | ||
'repo': 'github.com/CoCreate-app/CoCreate-cli.git' | ||
}, | ||
{ | ||
'path': '../CoCreate-components/CoCreate-config', | ||
'repo': 'github.com/CoCreate-app/CoCreate-config.git' | ||
}, | ||
{ | ||
'path': '../CoCreate-components/CoCreate-docs', | ||
'repo': 'github.com/CoCreate-app/CoCreate-docs.git' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,143 @@ | ||
// TODO: replace mongodb with @cocreate/crud to support multiple databases | ||
const { MongoClient, ObjectId } = require('mongodb'); | ||
const cli = require('@cocreate/cli'); | ||
const Config = require('@cocreate/config') | ||
const uuid = require('@cocreate/uuid'); | ||
const crudServer = require('@cocreate/crud-server') | ||
|
||
const fs = require('fs'); | ||
const path = require("path") | ||
let config = await cli.config([ | ||
{ | ||
key: 'storage', | ||
prompt: 'Enter a JSON.stringify storage object' | ||
|
||
module.exports = async function () { | ||
|
||
let config = await Config({ | ||
'name': { | ||
prompt: 'Enter a friendly name for the new storage: ', | ||
variable: true | ||
}, | ||
'storage.{{name}}.provider': { | ||
prompt: 'Enter the storage provider, ex mongodb: ' | ||
}, | ||
'storage.{{name}}.url': { | ||
prompt: 'Enter the storage providers url: ' | ||
} | ||
}, false, false) | ||
|
||
const storage = {} | ||
if (!config.name && !config.provider && !config.url) { | ||
console.log('Could not find a url in your storage object') | ||
process.exit() | ||
} | ||
]) | ||
|
||
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 { | ||
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 || `${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 = {}; | ||
organization._id = ObjectId(organization_id); | ||
organization.host = config.host | ||
organization.storage = config.storage | ||
organization.organization_id = organization_id; | ||
await organizations.insertOne(organization); | ||
|
||
// Create user | ||
const users = dbClient.db(organization_id).collection('users'); | ||
let user = {}; | ||
user['_id'] = ObjectId(user_id); | ||
user['firstname'] = 'Admin' | ||
user['organization_id'] = organization_id; | ||
await users.insertOne(user); | ||
|
||
// Create default key | ||
const permissions = dbClient.db(organization_id).collection('keys'); | ||
let data = { | ||
type: "key", | ||
key: key, | ||
actions: { | ||
"signIn": "", | ||
"signUp": "" | ||
}, | ||
default: true, | ||
organization_id | ||
storage[config.name] = { provider: config.provider, url: config.url } | ||
update() | ||
|
||
async function update() { | ||
const organization_id = config.organization_id || `${crudServer.ObjectId()}` | ||
const key = config.key || uuid.generate(32); | ||
const user_id = config.user_id || `${crudServer.ObjectId()}`; | ||
|
||
console.log(organization_id, key, user_id) | ||
|
||
try { | ||
// Create organization | ||
await crudServer.createDocument({ | ||
database: organization_id, | ||
collection: 'organizations', | ||
document: { | ||
_id: ObjectId(organization_id), | ||
host: config.host, | ||
storage: config.storage, | ||
organization_id | ||
}, | ||
organization_id | ||
}) | ||
|
||
// Create user | ||
await crudServer.createDocument({ | ||
database: organization_id, | ||
collection: 'users', | ||
document: { | ||
_id: ObjectId(user_id), | ||
firstname: 'Admin', | ||
organization_id | ||
}, | ||
organization_id | ||
}) | ||
|
||
|
||
// Create default key | ||
await crudServer.createDocument({ | ||
database: organization_id, | ||
collection: 'keys', | ||
document: { | ||
type: "key", | ||
key: key, | ||
actions: { | ||
"signIn": "", | ||
"signUp": "" | ||
}, | ||
default: true, | ||
organization_id | ||
}, | ||
organization_id | ||
}) | ||
|
||
// Create admin role | ||
let role_id = crudServer.ObjectId(); | ||
await crudServer.createDocument({ | ||
database: organization_id, | ||
collection: 'keys', | ||
document: { | ||
_id: role_id, | ||
type: "role", | ||
name: "admin", | ||
admin: "true", | ||
organization_id | ||
}, | ||
organization_id | ||
}) | ||
|
||
|
||
// Create user key | ||
await crudServer.createDocument({ | ||
database: organization_id, | ||
collection: 'keys', | ||
document: { | ||
type: "user", | ||
key: user_id, | ||
roles: [role_id], | ||
organization_id | ||
}, | ||
organization_id | ||
}) | ||
|
||
if (organization_id && key) { | ||
updateConfig(organization_id, key) | ||
return organization_id | ||
} | ||
|
||
} catch (error) { | ||
console.log(error) | ||
} | ||
await permissions.insertOne(data); | ||
|
||
// Create admin role | ||
let role = { | ||
_id: ObjectId(), | ||
type: "role", | ||
name: "admin", | ||
admin: "true", | ||
organization_id: organization_id | ||
}; | ||
await permissions.insertOne(role); | ||
|
||
// Create user key | ||
let userKey = { | ||
type: "user", | ||
key: user_id, | ||
roles: [role._id], | ||
organization_id: organization_id | ||
}; | ||
await permissions.insertOne(userKey); | ||
|
||
if (organization_id && key) | ||
updateConfig(organization_id, key) | ||
|
||
|
||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
|
||
function updateConfig(organization_id, key) { | ||
const ppath = './' | ||
let configfile = path.resolve(ppath, 'CoCreate.config.js'); | ||
if (!fs.existsSync(configfile)) | ||
return console.error('path does not exist:', configfile) | ||
function updateConfig(organization_id, key) { | ||
const ppath = './' | ||
let configfile = path.resolve(ppath, 'CoCreate.config.js'); | ||
if (!fs.existsSync(configfile)) | ||
return console.error('path does not exist:', configfile) | ||
|
||
let object = require(configfile) | ||
object.organization_id = organization_id | ||
object.key = key | ||
let object = require(configfile) | ||
object.organization_id = organization_id | ||
object.key = key | ||
|
||
let config = `module.exports = ${JSON.stringify(object, null, 4)}` | ||
let config = `module.exports = ${JSON.stringify(object, null, 4)}` | ||
|
||
if (fs.existsSync(configfile)) | ||
fs.unlinkSync(configfile) | ||
fs.writeFileSync(configfile, config) | ||
if (fs.existsSync(configfile)) | ||
fs.unlinkSync(configfile) | ||
fs.writeFileSync(configfile, config) | ||
|
||
console.log('updated: ', configfile) | ||
process.exit() | ||
console.log('updated: ', configfile) | ||
process.exit() | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters