Skip to content

Commit

Permalink
v0.0.4-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenvachon committed Jul 14, 2017
1 parent b684329 commit cd74a8a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 110 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
language: node_js
services:
- postgresql
language: node_js
os:
- linux
- osx
node_js:
- 6
- 8
Expand Down
107 changes: 16 additions & 91 deletions es2017/createdb.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,38 @@
"use strict";
const editEnv = require("edit-dotenv");
const Enquirer = require("enquirer");
const dotenv = require("dotenv");
const dotenvPrompt = require("dotenv-prompt");
const isset = require("isset");
const knex = require("knex");
const {outputFile, readFile}= require("fs-extra");
const {parse:parseEnv} = require("dotenv");



// TODO :: try https://npmjs.com/pgtools ?

const createdb = async (envPath=".env", envSamplePath=".env.sample") =>
{
const envs = await Promise.all(
const promptVarnames =
[
readEnv(envPath),
readEnv(envSamplePath)
]);

const envString = envs[0] || envs[1];
const env = await parseEnv(envString);
const changes = await prompts(env);

if (Object.keys(changes).length > 0)
{
Object.assign(env, changes);

await outputFile(envPath, editEnv(envString, changes));
}
else if (envString === envs[1])
{
// Duplicate the sample
await outputFile(envPath, envString);
}
"POSTGRES_HOST",
"POSTGRES_NAME",
"POSTGRES_USER",
"POSTGRES_PASSWORD"
];

await db(env);
};
await dotenvPrompt(envPath, envSamplePath, promptVarnames);

// TODO :: also prompt for superuser name/password (https://github.com/brianc/node-postgres/wiki/Client#new-clientobject-config--client)

dotenv.config({ path:envPath });

const db = async env =>
{
const host = env.POSTGRES_HOST;
const name = env.POSTGRES_NAME;
const password = env.POSTGRES_PASSWORD;
const user = env.POSTGRES_USER;
const host = process.env.POSTGRES_HOST;
const name = process.env.POSTGRES_NAME;
const password = process.env.POSTGRES_PASSWORD;
const user = process.env.POSTGRES_USER;

if (!isset(host) || !isset(name) || !isset(password) || !isset(user))
{
throw new Error("Environmental variable(s) not set");
}

// TODO :: try https://npmjs.com/pgtools ?
const psql = knex({ client:"pg", connection:{ host } });

try
Expand Down Expand Up @@ -78,61 +60,4 @@ const db = async env =>



const prompts = async env =>
{
const enquirer = new Enquirer();

const promptVars =
[
"POSTGRES_HOST",
"POSTGRES_NAME",
"POSTGRES_USER",
"POSTGRES_PASSWORD"
];

// TODO :: also prompt for SUPERUSER_NAME and SUPERUSER_PASSWORD (https://github.com/brianc/node-postgres/wiki/Client#new-clientobject-config--client)

const questions = promptVars.map(varname => enquirer.question(
{
name: varname,
message: `Value for ${varname}`,
default: isset(env[varname]) ? env[varname] : ""
}));

const answers = await enquirer.ask(questions);

return Object.keys(answers).reduce((result, varname) =>
{
if (varname in env && answers[varname]===env[varname])
{
delete answers[varname];
}

return result;
}, answers);
};



const readEnv = async path =>
{
try
{
return await readFile(path, "utf8");
}
catch (error)
{
if (error.code === "ENOENT")
{
return "";
}
else
{
throw error;
}
}
};



module.exports = createdb;
2 changes: 1 addition & 1 deletion es2017/dropdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const dropdb = async (envPath=".env") =>

if (confirmed)
{
// TODO :: prompt for SUPERUSER_NAME and SUPERUSER_PASSWORD (https://github.com/brianc/node-postgres/wiki/Client#new-clientobject-config--client)
// TODO :: prompt for superuser name/password (https://github.com/brianc/node-postgres/wiki/Client#new-clientobject-config--client)

const psql = knex({ client:"pg", connection:{ host } });

Expand Down
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
"babel-core": "^6.25.0",
"babel-polyfill": "^6.23.0",
"dotenv": "^4.0.0",
"edit-dotenv": "^1.0.4",
"enquirer": "^1.0.2",
"fs-extra": "^3.0.1",
"dotenv-prompt": "stevenvachon/dotenv-prompt",
"fs-extra": "^4.0.0",
"isset": "^1.0.3",
"knex": "~0.13.0",
"pg": "^6.4.0",
Expand All @@ -24,6 +23,7 @@
"chai": "^4.1.0",
"coveralls": "^2.13.1",
"escape-string-regexp": "^1.0.5",
"js-stringify": "^1.0.2",
"mocha": "^3.4.2",
"nyc": "^11.0.3",
"suppose": "~0.6.2"
Expand All @@ -40,17 +40,21 @@
"files": [
"createdb.js",
"dropdb.js",
"es2017",
"es2015/createdb.js",
"es2015/dropdb.js"
"es2015",
"es2017"
],
"keywords": [
"boilerplate",
"bootstrapper",
"createdb",
"dotenv",
"dropdb",
"env",
".env",
"init",
"knex",
"postgres",
"postgresql"
"postgresql",
"prompt"
]
}
20 changes: 10 additions & 10 deletions test/createdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ const {outputFile, readFile, remove} = require("fs-extra");
const {PassThrough} = require("stream");
const suppose = require("suppose");

const envVarsError = /^Error: Environmental variable\(s\) not set/;



// TODO :: https://github.com/jprichardson/node-suppose/pull/31
const clean = str =>
{
return new RegExp(`^\s*${escapeStringRegexp(str)}\s*`, "m");
return new RegExp(`${escapeStringRegexp(str)}[^\\S\\r\\n]*`);
};


Expand All @@ -21,7 +23,7 @@ const createdb = expects =>
return new Promise((resolve, reject) =>
{
const stream = new PassThrough()
//.on("data", chunk => console.log(chunk.toString());
//.on("data", chunk => console.log(chunk.toString()));

const supposing = suppose("node", ["test/helpers/createdb"], { debug:stream, stripAnsi:true });

Expand Down Expand Up @@ -49,7 +51,7 @@ const emptyPrompts = () =>
.catch(error => error)
.then(error =>
{
expect(error).to.be.an("error").with.property("message").that.matches(/^Error: Environmental variable\(s\) not set/);
expect(error).to.be.an("error").with.property("message").that.matches(envVarsError);
});
});

Expand All @@ -65,7 +67,7 @@ const emptyPrompts = () =>
.catch(error => error)
.then(error =>
{
expect(error).to.be.an("error").with.property("message").that.matches(/^Error: Environmental variable\(s\) not set/);
expect(error).to.be.an("error").with.property("message").that.matches(envVarsError);
});
});

Expand All @@ -81,7 +83,7 @@ const emptyPrompts = () =>
.catch(error => error)
.then(error =>
{
expect(error).to.be.an("error").with.property("message").that.matches(/^Error: Environmental variable\(s\) not set/);
expect(error).to.be.an("error").with.property("message").that.matches(envVarsError);
});
});

Expand All @@ -97,7 +99,7 @@ const emptyPrompts = () =>
.catch(error => error)
.then(error =>
{
expect(error).to.be.an("error").with.property("message").that.matches(/^Error: Environmental variable\(s\) not set/);
expect(error).to.be.an("error").with.property("message").that.matches(envVarsError);
});
});

Expand All @@ -113,7 +115,7 @@ const emptyPrompts = () =>
.catch(error => error)
.then(error =>
{
expect(error).to.be.an("error").with.property("message").that.matches(/^Error: Environmental variable\(s\) not set/);
expect(error).to.be.an("error").with.property("message").that.matches(envVarsError);
});
});
};
Expand All @@ -135,7 +137,7 @@ const promptsAndWrites = () =>
.then(error =>
{
// Vague npmjs.com/pg error stemming from fake credentials
expect(error).to.be.an("error").with.property("message").that.does.not.match(/^Error: Environmental variable\(s\) not set/);
expect(error).to.be.an("error").with.property("message").that.does.not.match(envVarsError);

return readFile(".env", "utf8");
})
Expand Down Expand Up @@ -182,8 +184,6 @@ describe("createdb", function()
return outputFile(".env.sample", contents);
});



promptsAndWrites();
emptyPrompts();
});
Expand Down

0 comments on commit cd74a8a

Please sign in to comment.