Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Commit

Permalink
feat(setup): create first user
Browse files Browse the repository at this point in the history
issue #16
  • Loading branch information
ianpogi5 committed Oct 27, 2019
1 parent 1914a51 commit c9bdb09
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
18 changes: 15 additions & 3 deletions packages/kdc-cms-setup/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,31 @@ if (args[0]) {
stage = args[0];
}

const configFile = path.resolve(rootDir, `config.${stage}.yml`);
const userFile = path.resolve(rootDir, `user.${stage}.yml`);

// Get document, or throw exception on error
try {
const filename = path.resolve(rootDir, `config.${stage}.yml`);
const config = yaml.safeLoad(fs.readFileSync(filename, "utf8"));
const config = yaml.safeLoad(fs.readFileSync(configFile, "utf8"));
process.env.DDB_REGION = process.env.DDB_REGION || config.REGION;
process.env.JWT_SECRET = process.env.JWT_SECRET || config.JWT_SECRET;
process.env.DDB_TABLE = `kdc-cms-database-${stage}`;
if (stage !== "local") {
process.env.AWS_PROFILE = process.env.AWS_PROFILE || config.PROFILE;
process.env.IS_OFFLINE = false;
process.env.DDB_TABLE = `database-${stage}.${config.ROOT_DOMAIN}`;
} else {
process.env.IS_OFFLINE = true;
process.env.DDB_TABLE = "kdc-cms-database-local";
}

const user = yaml.safeLoad(fs.readFileSync(userFile, "utf8"));
process.env.KDC_CMS = JSON.stringify({ ...user });
} catch (e) {
console.log(e);
}

// cleanup on exit
process.on("exit", function() {
delete process.env.KDC_CMS;
fs.unlinkSync(userFile);
});
5 changes: 1 addition & 4 deletions packages/kdc-cms-setup/first-user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
require("./env");
const questions = require("./lib/questions-user");
const createUser = require("./lib/create-user");

questions()
.then(ctx => createUser(ctx))
.catch(e => console.log(e));
createUser().catch(e => console.log(e));
7 changes: 4 additions & 3 deletions packages/kdc-cms-setup/lib/create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ const create = async ({ username, name, password, ...attr }) => {
return DDB("put", params);
};

const createUser = async ctx => {
const { user } = ctx;
const createUser = async () => {
const { KDC_CMS } = process.env;
const user = JSON.parse(KDC_CMS);
if (user) {
// same code as kdc-cms-api/services/users/lib/create
return create(user)
Expand All @@ -45,7 +46,7 @@ const createUser = async ctx => {
.catch(e => console.log(e));
}

return Promise.resolve(ctx);
return Promise.resolve();
};

module.exports = createUser;
2 changes: 1 addition & 1 deletion resources/database/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ provider:
region: ${file(../../config.${self:provider.stage}.yml):REGION}
profile: ${file(../../config.${self:provider.stage}.yml):PROFILE}
environment:
DDB_TABLE: ${self:service}-${opt:stage, self:provider.stage}
DDB_TABLE: database-${self:provider.stage}.${file(../../config.${self:provider.stage}.yml):ROOT_DOMAIN}

resources:
Resources:
Expand Down

0 comments on commit c9bdb09

Please sign in to comment.