Skip to content

Commit

Permalink
refacto promises into await just for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudlewis committed Nov 16, 2016
1 parent e7c6273 commit ac2bcd4
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 94 deletions.
52 changes: 24 additions & 28 deletions lib/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,43 @@ import Helpers from './helpers';

export default { connect };

function connect(base, args, noconfirm) {
async function connect(base, args, noconfirm) {
const email = args['--email'];
const password = args['--password'];
let cookiesPromise;


if (email && password) {
// The user included login/password, we need to log him with those
cookiesPromise = Login(base, email, password).then(function() {
return config.get('cookies');
});
await Login(base, email, password);
cookiesPromise = config.get('cookies');
} else {
cookiesPromise = config.get('cookies')
.then(function(cookies) {
if (cookies) {
// The user has cookies saved in his home directory, use this
return cookies;
} else {
if (noconfirm) {
// Can't proceed non-interactively if we can't login!
Helpers.UI.display('Error: to use noconfirm, login first or pass the email/password as options.');
return null;
}
// No login/pass, no cookie => need to signin or signup the user before we proceed
return signupOrLogin(base).then(function() {
return config.get('cookies');
});

const cookies = await config.get('cookies');
if (cookies) {
// The user has cookies saved in his home directory, use this
cookiesPromise = cookies;
} else {
if (noconfirm) {
// Can't proceed non-interactively if we can't login!
Helpers.UI.display('Error: to use noconfirm, login first or pass the email/password as options.');
cookiesPromise = null;
}
});
// No login/pass, no cookie => need to signin or signup the user before we proceed
await signupOrLogin(base);
cookiesPromise = config.get('cookies');
}
}
return cookiesPromise;
}

function signupOrLogin(base) {
return promptSignupOrLogin(base).then(function(answers) {
if (answers.login == 'login') {
return Login(base);
} else {
return Signup(base);
}
});
async function signupOrLogin(base) {
const answers = await promptSignupOrLogin(base);
if (answers.login == 'login') {
return Login(base);
} else {
return Signup(base);
}
}

function promptSignupOrLogin(base) {
Expand Down
54 changes: 28 additions & 26 deletions lib/prismic.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,47 +90,49 @@ async function theme(config, url, args) {
}

// For testing only
function heroku(config, args) {
return Helpers.Prismic.templates()
.then((templates) => Repository.heroku(templates, args['--template']))
.catch((err) => Helpers.UI.display(err));
async function heroku(config, args) {
const templates = Helpers.Prismic.templates();
try {
Repository.heroku(templates, args['--template']);
} catch(err) {
Helpers.UI.display(err);
}
}

function signup(config, args) {
async function signup(config, args) {
var base = config.base || Helpers.Domain.default;
Signup(base, args['--email'], args['--password'])
.then(() => Helpers.UI.display('Successfully created your account! You can now create repositories.'));
await Signup(base, args['--email'], args['--password']);
Helpers.UI.display('Successfully created your account! You can now create repositories.');
}

function login(config, args) {
async function login(config, args) {
var base = config.base || Helpers.Domain.default;
Login(base, args['--email'], args['--password'])
.then(() => Helpers.UI.display('Successfully logged in! You can now create repositories.'));
await Login(base, args['--email'], args['--password']);
Helpers.UI.display('Successfully logged in! You can now create repositories.');
}

function logout(config, args) {
configuration.set({cookies: ''}) // clear the cookies
.then(() => Helpers.UI.display('Successfully logged out !'))
.catch((err) => console.log(err));
async function logout(config, args) {
try {
await configuration.set({cookies: ''}); // clear the cookies
Helpers.UI.display('Successfully logged out !');
} catch(err) {
console.log(err);
}
}

function list() {
async function list() {
Helpers.UI.display('Available templates:');
return Helpers.Prismic.templates()
.then((templates) => {
Helpers.UI.display(Template.getDisplayed(templates).map(function(template) {
return `* ${template.name}`;
}));
});
const templates = await Helpers.Prismic.templates();
Helpers.UI.display(Template.getDisplayed(templates).map(function(template) {
return `* ${template.name}`;
}));
}

// Should only be used by staff, which is why it's not documented
// prismic base http://wroom.dev
function base(base) {
Base(base)
.then(function(newBase) {
Helpers.UI.display(`Successfully changed base ${newBase} !`);
});
async function base(base) {
const newBase = await Base(base);
Helpers.UI.display(`Successfully changed base ${newBase} !`);
}

// === Main function
Expand Down
74 changes: 35 additions & 39 deletions lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ async function create(templates, base, domain, args, theme) {

async function validateTheme(themeURL) {

function retry(url, message) {
async function retry(url, message) {
Helpers.UI.display(message);
return promptThemeURL(themeURL)
.then(answers => validateTheme(answers.url));
const answers = await promptThemeURL(themeURL);
return validateTheme(answers.url);
}

try {
Expand Down Expand Up @@ -107,17 +107,18 @@ async function chooseDomain(newRepository, base, domain, args, noconfirm) {
} else if (noconfirm) {
throw 'The noconfirm options requires the domain option to be set.';
} else {
return promptName().then((answers) => isDomainAvailableOrRetry(newRepository, base, answers.domain, args, noconfirm));
const answers = await promptName();
return isDomainAvailableOrRetry(newRepository, base, answers.domain, args, noconfirm);
}
}

async function chooseFolder(domain, args, noconfirm) {
function prompt(folder) {
return promptFolder(folder)
.then((answers) => answers.folder);
async function prompt(folder) {
const answers = await promptFolder(folder);
return answers.folder;
}

const folder = args['--folder'] || domain;
const folder = args['--folder'];
if(folder) {
if (shell.test('-e', folder)) {
Helpers.UI.display('Error: folder '+ folder + ' already exists.');
Expand All @@ -126,14 +127,14 @@ async function chooseFolder(domain, args, noconfirm) {
return folder;
}
} else {
return prompt();
return prompt(domain);
}
}

async function chooseTemplate(domain, templates, args, noconfirm) {
function prompt(template) {
return promptTemplate(templates, template)
.then((answers) => answers.template);
async function prompt(template) {
const answers = await promptTemplate(templates, template);
return answers.template;
}

const template = args['--template'];
Expand Down Expand Up @@ -222,12 +223,10 @@ async function isDomainAvailableOrRetry(newRepository, base, domain, args, nocon
}
}

function retry(err) {
async function retry(err) {
Helpers.UI.display(err);
return promptName()
.then((answers) => {
return exec(answers.domain);
});
const answers = await promptName();
return exec(answers.domain);
}
return exec(domain);
}
Expand Down Expand Up @@ -312,30 +311,27 @@ function promptTemplate(templates, templateName) {
}]);
}

function heroku(templates, templateName) {
async function heroku(templates, templateName) {
var template = Template.get(templates, templateName);
Helpers.UI.display('Initialize heroku project');
(template ? Promise.resolve({template: Template.get(templates, templateName)}) : promptTemplate(templateName))
.then(function(answers) {
if (!answers.template.url) {
throw new Error('Not implemented yet!');
}
Helpers.UI.display('Initialize local project...');
return Template.unzip(answers.template).then(function() {
Template.replace('.', answers.template, [{
pattern: /[\'\"]https:\/\/your-repo-name\.prismic\.io\/api[\'\"]/,
value: 'process.env.PRISMIC_ENDPOINT'
}]);
Helpers.UI.display('Running npm install...');
shell.exec(`npm install > ${isWin ? 'NUL' : '/dev/null'}`);
Helpers.UI.display([
'Your project in ready! Next steps:',
' => Open your writing room: \'heroku addons:docs prismic\'',
' => Create the custom types as described in the docs: \'heroku addons:docs prismic\'',
' => Run the project: \'heroku local\''
]);
});
});
const answers = template ? {template: Template.get(templates, templateName)} : await promptTemplate(templateName);
if (!answers.template.url) {
throw new Error('Not implemented yet!');
}
Helpers.UI.display('Initialize local project...');
await Template.unzip(answers.template);
Template.replace('.', answers.template, [{
pattern: /[\'\"]https:\/\/your-repo-name\.prismic\.io\/api[\'\"]/,
value: 'process.env.PRISMIC_ENDPOINT'
}]);
Helpers.UI.display('Running npm install...');
shell.exec(`npm install > ${isWin ? 'NUL' : '/dev/null'}`);
Helpers.UI.display([
'Your project in ready! Next steps:',
' => Open your writing room: \'heroku addons:docs prismic\'',
' => Create the custom types as described in the docs: \'heroku addons:docs prismic\'',
' => Run the project: \'heroku local\''
]);
}

export default { create, validateTheme, heroku };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"babel-runtime": "^6.18.0",
"command-line-commands": "1.0.3",
"command-line-usage": "3.0.2",
"inquirer": "1.0.3",
"inquirer": "1.2.3",
"lodash": "4.13.1",
"path": "^0.12.7",
"prismic.io": "^3.2.0",
Expand Down

0 comments on commit ac2bcd4

Please sign in to comment.