Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/upstream/master' into global-pa…
Browse files Browse the repository at this point in the history
…ckages
  • Loading branch information
himynameisdave committed Aug 6, 2017
2 parents 0c8d253 + dac5e68 commit 6119391
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
*.bcup
*.DS_Store
/coverage/
*.git-label-maker-config
1 change: 1 addition & 0 deletions bin/constants.js
Expand Up @@ -17,6 +17,7 @@ const constants = {
'Welcome to git-labelmaker',
'Adding Custom Labels',
'Adding Labels From Package',
'Add Global Package',
'Creating Package From Labels',
'Removing Labels',
'Resetting Token',
Expand Down
23 changes: 23 additions & 0 deletions bin/index.js
Expand Up @@ -5,6 +5,7 @@
// EXTERNAL DEPENDENCIES
const fs = require('fs');
const prompt = require('inquirer').prompt;
const pathTool = require('path');
const gitLabel = require('git-label');
// UTILS ARE STANDALONE METHODS WITH NO DEPENDENCIES
const alertDeletes = require('./utils/alert-deletes.js');
Expand Down Expand Up @@ -111,6 +112,18 @@ const addFromPackage = (repo, token, path) => {
};
exports.addFromPackage = addFromPackage;

const setGlobalPackage = (path, token) => {
fs.writeFile('.git-labelmaker-config', pathTool.resolve(__dirname, '..', path), (err) => {
if (err) throw err;
console.log(`Saved ${path} as a global. When you specify no file when adding from a package, the global will be used instead.`);
gitLabelmaker(token);
});
};
// Not in use... yet :)
// const getGlobalPackage = () => {
// return fs.readFileSync('.git-labelmaker-config', 'utf-8');
// };

// removeLabels function
const removeLabels = (repo, token, answers) => {
// Tell the user what they're about to lose
Expand Down Expand Up @@ -143,6 +156,16 @@ const handleMainPrompts = (repo, token, ans) => {
case 'add custom labels':
return addCustom(repo, token);

case 'add global package':
return prompt([{
name: 'path',
type: 'input',
message: 'What is the pathname you want to use for your package? (Must be valid json)',
validate: validateAddPackages,
}])
.then((globalAns) => setGlobalPackage(globalAns.path, token))
.catch(console.warn);

case 'add labels from package':
banner.addFromPackage();
return prompt([{
Expand Down
36 changes: 36 additions & 0 deletions bin/modules/validateAddPackages.js
@@ -0,0 +1,36 @@
/**
* Ensures that the label package file actually exists
* @param {String} path - path to the json file label package
* @return {Bool || Error} this is a try-catch where the error
*/


const fs = require('fs');
const path = require('path');
const isJsonString = require('../utils/is-json-string.js');

// Not using arrows bc it will mess up "this" context
module.exports = function (jsonPath) { // eslint-disable-line func-names
// Declare function as asynchronous, and save the done callback
const done = this.async();
// this is clunky, but this way `we can just use a default
if (jsonPath === '') done(true);
try {
if (jsonPath.indexOf('.json') < 0) {
done('Not a JSON file');
return;
}
// Calculate the full path of the JSON file based upon the current working directory. Using
// path.resolve allows for absolute paths to be used also.
const fullPath = path.resolve(process.cwd(), jsonPath);
fs.readFile(fullPath, (err, data) => {
if (err) { done(err); return; }
if (isJsonString(data)) {
done(true);
}
done('Not a valid JSON file');
});
} catch (e) {
done(e);
}
};
13 changes: 13 additions & 0 deletions bin/prompts/mainMenu.js
@@ -0,0 +1,13 @@
/**
* The "Initial"/"Main" prompts
*/
const menuChoices = require('../constants.js').menuChoices;

module.exports = [
{
type: 'list',
name: 'main',
message: 'Welcome to git-labelmaker!\nWhat would you like to do?',
choices: menuChoices,
}
];
19 changes: 13 additions & 6 deletions bin/utils/__tests__/banners.spec.js
Expand Up @@ -42,43 +42,50 @@ test('it prints the addFromPackage banner to the console', () => {
expect(console.log.mock.calls[0][0]).toBe(expected);
});

test('it prints the createPkgFromLabels banner to the console', () => {
test('it prints the addGlobalPackage banner to the console', () => {
const expected = makeExpected(pad33(constants.banners[3]));
banners.addGlobalPackage();
expect(console.log.mock.calls.length).toBe(1);
expect(console.log.mock.calls[0][0]).toBe(expected);
});

test('it prints the createPkgFromLabels banner to the console', () => {
const expected = makeExpected(pad33(constants.banners[4]));
banners.createPkgFromLabels();
expect(console.log.mock.calls.length).toBe(1);
expect(console.log.mock.calls[0][0]).toBe(expected);
});

test('it prints the removeLabels banner to the console', () => {
const expected = makeExpected(pad33(constants.banners[4]));
const expected = makeExpected(pad33(constants.banners[5]));
banners.removeLabels();
expect(console.log.mock.calls.length).toBe(1);
expect(console.log.mock.calls[0][0]).toBe(expected);
});

test('it prints the resetToken banner to the console', () => {
const expected = makeExpected(pad33(constants.banners[5]));
const expected = makeExpected(pad33(constants.banners[6]));
banners.resetToken();
expect(console.log.mock.calls.length).toBe(1);
expect(console.log.mock.calls[0][0]).toBe(expected);
});

test('it prints the seeYa banner to the console', () => {
const expected = makeExpected(pad33(constants.banners[6]));
const expected = makeExpected(pad33(constants.banners[7]));
banners.seeYa();
expect(console.log.mock.calls.length).toBe(1);
expect(console.log.mock.calls[0][0]).toBe(expected);
});

test('it prints the wrongPassword banner to the console', () => {
const expected = makeExpected(pad33(constants.banners[7]));
const expected = makeExpected(pad33(constants.banners[8]));
banners.wrongPassword();
expect(console.log.mock.calls.length).toBe(1);
expect(console.log.mock.calls[0][0]).toBe(expected);
});

test('it prints the removeAllLabels banner to the console', () => {
const expected = makeExpected(pad33(constants.banners[8]));
const expected = makeExpected(pad33(constants.banners[9]));
banners.removeAllLabels();
expect(console.log.mock.calls.length).toBe(1);
expect(console.log.mock.calls[0][0]).toBe(expected);
Expand Down
2 changes: 1 addition & 1 deletion bin/utils/banners.js
Expand Up @@ -4,7 +4,6 @@

const pad33 = require('./pad-33.js');
const banners = require('../constants.js').banners;

const bar = '=======================================\n';
const block = ']|[';
const emptyRow = `${block} ${block}\n`;
Expand All @@ -15,6 +14,7 @@ const props = [
'welcome',
'addCustom',
'addFromPackage',
'addGlobalPackage',
'createPkgFromLabels',
'removeLabels',
'resetToken',
Expand Down

0 comments on commit 6119391

Please sign in to comment.