Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"indent": [2, 4, { "SwitchCase": 1, "VariableDeclarator": 1 }],
"max-len": [0],
"no-console": [0],
"no-multi-spaces": [0],
"no-plusplus": [0],
"no-underscore-dangle": [0],
"global-require": [0],
Expand All @@ -25,6 +24,7 @@
"key-spacing": [0],
"radix": [0],
"strict": [0, "never"],
"import/extensions": [0]
"import/extensions": [0],
"import/newline-after-import": [0]
}
}
5 changes: 5 additions & 0 deletions bin/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
const constants = {
buttercup: {
group: 'git-labelmaker',
entry: 'githubtoken',
property: 'password',
},
menuChoices: [
'Add Custom Labels',
'Add Labels From Package',
Expand Down
239 changes: 119 additions & 120 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,65 @@
'use strict';

// EXTERNAL DEPENDENCIES
const fs = require('fs');
const iq = require('inquirer');
const gitLabel = require('git-label');
const fs = require('fs');
const prompt = require('inquirer').prompt;
const gitLabel = require('git-label');
// UTILS ARE STANDALONE METHODS WITH NO DEPENDENCIES
const alertDeletes = require('./utils/alert-deletes.js');
const banner = require('./utils/banners.js');
const configGitLabel = require('./utils/config-git-label.js');
const filterRemovalLabels = require('./utils/filter-removal-labels.js');
const removeAllFromStr = require('./utils/remove-all-from-str.js');
const validateRemovals = require('./utils/validate-removals.js');
const alertDeletes = require('./utils/alert-deletes.js');
const banner = require('./utils/banners.js');
const configGitLabel = require('./utils/config-git-label.js');
const filterRemovalLabels = require('./utils/filter-removal-labels.js');
const removeAllFromStr = require('./utils/remove-all-from-str.js');
const validateRemovals = require('./utils/validate-removals.js');
// PROMPTS ARE THE PROMPTS ARRAYS FOR VARIOUS QUESTIONS
const prompts = {
const prompts = {
addCustom: require('./prompts/add-custom.js'),
deleteConfirm: require('./prompts/delete-confirm.js'),
mainMenu: require('./prompts/main-menu.js'),
};
// MODULES ARE UTILS WITH DEPENDENCIES
const convertRGBToHex = require('./modules/convert-rgb-to-hex.js');
const convertRGBToHex = require('./modules/convert-rgb-to-hex.js');
const doCustomLabelPrompts = require('./modules/do-custom-label-prompts.js')(prompts.addCustom);
const readRepo = require('./modules/read-repo.js');
const setToken = require('./modules/set-token.js');
const fetchToken = require('./modules/fetch-token.js');
const isGitRepo = require('./modules/is-git-repo.js');
const readGitConfig = require('./modules/read-git-config.js');
const requestLabels = require('./modules/request-labels.js');
const prompt = require('./modules/prompt.js');
const validateAddPackages = require('./modules/validate-add-packages.js');
const readRepo = require('./modules/read-repo.js');
const setToken = require('./modules/set-token.js');
const fetchToken = require('./modules/fetch-token.js');
const isGitRepo = require('./modules/is-git-repo.js');
const readGitConfig = require('./modules/read-git-config.js');
const requestLabels = require('./modules/request-labels.js');
const validateAddPackages = require('./modules/validate-add-packages.js');

// Kicks things off, named so that it can be called at any time
// The params will sometimes come thru if we've just set the token, so if we got them we alter the call a lil...
const gitLabelmaker = (token) => {
Promise.all([isGitRepo(), readGitConfig(), fetchToken(token)])
.then(values => {
// TODO: this is a bit callback-hellish
readRepo(values[1])
.then(_repo => {
const _token = values[2];
banner.welcome();
iq.prompt(prompts.mainMenu, handleMainPrompts.bind(null, _repo, _token)); // eslint-disable-line no-use-before-define
})
.catch(e => {
console.error(e);
process.exit(1);
});
})
.catch(e => {
switch (e.id) {
case 'TOKEN':
return setToken(gitLabelmaker);
case 'QUIT':
banner.seeYa();
return process.exit(0);
case 'PASSWORD':
banner.wrongPassword();
return gitLabelmaker();
default:
console.warn(e);
return process.exit(1);
}
});
.then(values => {
// TODO: this is a bit callback-hellish
readRepo(values[1])
.then(_repo => {
const _token = values[2];
banner.welcome();
prompt(prompts.mainMenu, handleMainPrompts.bind(null, _repo, _token)); // eslint-disable-line no-use-before-define
})
.catch(e => {
console.error(e);
process.exit(1);
});
})
.catch(e => {
switch (e.id) {
case 'TOKEN':
return setToken(gitLabelmaker);
case 'QUIT':
banner.seeYa();
return process.exit(0);
case 'PASSWORD':
banner.wrongPassword();
return gitLabelmaker();
default:
console.warn(e);
return process.exit(1);
}
});
};
exports.gitLabelmaker = gitLabelmaker;

Expand All @@ -83,7 +82,7 @@ const addCustom = (repo, token) => {
try {
newLabel.color = convertRGBToHex(newLabel.color); // eslint-disable-line
} catch (e) {
// graceful quit if one of the values isn't actually rgb;
// graceful quit if one of the values isn't actually rgb;
console.log(e);
gitLabelmaker(token);
}
Expand All @@ -95,20 +94,20 @@ const addCustom = (repo, token) => {
return _newLabel;
});
gitLabel.add(configGitLabel(repo, token), newLabels)
.then(console.log)
.catch(console.warn);
.then(console.log)
.catch(console.warn);
});
};
exports.addCustom = addCustom;

// addFromPackage function
const addFromPackage = (repo, token, path) => {
gitLabel.find(removeAllFromStr(path, ['`', '"', '\'']))
.then(newLabels => gitLabel.add(
configGitLabel(repo, token), newLabels)
)
.then(console.log)
.catch(console.warn);
.then(newLabels => gitLabel.add(
configGitLabel(repo, token), newLabels)
)
.then(console.log)
.catch(console.warn);
};
exports.addFromPackage = addFromPackage;

Expand All @@ -119,14 +118,14 @@ const removeLabels = (repo, token, answers) => {
alertDeletes(answers.removals);// alerts the list of labels to be removed
// Ya sure ya wanna do this bud?
prompt(prompts.deleteConfirm)
.then(confirmRemove => {
if (confirmRemove.youSure) {
return gitLabel.remove(configGitLabel(repo, token), answers.removals);
}
return gitLabelmaker();
})
.then(console.log)
.catch(console.warn);
.then(confirmRemove => {
if (confirmRemove.youSure) {
return gitLabel.remove(configGitLabel(repo, token), answers.removals);
}
return gitLabelmaker();
})
.then(console.log)
.catch(console.warn);
};
exports.removeLabels = removeLabels;

Expand All @@ -152,76 +151,76 @@ const handleMainPrompts = (repo, token, ans) => {
message: 'What is the path & name of the package you want to use? (eg: `packages/my-label-pkg.json`)',
validate: validateAddPackages,
}])
.then(answer => addFromPackage(repo, token, answer.path))
.catch(console.warn);
.then(answer => addFromPackage(repo, token, answer.path))
.catch(console.warn);

case 'create a package from labels':
banner.createPkgFromLabels();
return requestLabels(repo, token)
.then(labels => {
if (labels.length <= 0) return new Error('This repo has no labels to generate a package with!');
// tell the user what labels we are going to remove
console.log('Creating a labels package from these labels:');
labelPkg = labels.map(label => {
console.log(` - ${label.name}`);
return { name: label.name, color: `#${label.color}`, };
});
return prompt([{
name: 'name',
type: 'input',
message: 'What would you like to name this .json file?',
default: 'labels.json',
}]);
})
.then(answer => {
const _answer = answer;
if (answer.name.indexOf('.json') < 0) {
_answer.name = `${answer.name}.json`;
}
fs.writeFile(answer.name, JSON.stringify(labelPkg, null, 2), e => {
if (e) throw e;
console.log(`Saved labels as ${answer.name}`);
gitLabelmaker(token);
});
})
.catch(console.warn);
.then(labels => {
if (labels.length <= 0) return new Error('This repo has no labels to generate a package with!');
// tell the user what labels we are going to remove
console.log('Creating a labels package from these labels:');
labelPkg = labels.map(label => {
console.log(` - ${label.name}`);
return { name: label.name, color: `#${label.color}`, };
});
return prompt([{
name: 'name',
type: 'input',
message: 'What would you like to name this .json file?',
default: 'labels.json',
}]);
})
.then(answer => {
const _answer = answer;
if (answer.name.indexOf('.json') < 0) {
_answer.name = `${answer.name}.json`;
}
fs.writeFile(answer.name, JSON.stringify(labelPkg, null, 2), e => {
if (e) throw e;
console.log(`Saved labels as ${answer.name}`);
gitLabelmaker(token);
});
})
.catch(console.warn);

case 'remove labels':
banner.removeLabels();
// If there are no labels to be removed then we can skip this part
return requestLabels(repo, token)
.then(labels => {
if (labels.length <= 0) return new Error('This repo has no labels to remove!');
return prompt([{
name: 'removals',
type: 'checkbox',
message: 'Which labels would you like to remove?',
choices: labels.map((label) => label.name),
validate: validateRemovals,
filter: filterRemovalLabels.bind(null, labels),
}]);
})
.then(answers => {
if (answers.removals) {
return removeLabels(repo, token, answers);
}
return console.log(answers);
})
.catch(console.warn);
.then(labels => {
if (labels.length <= 0) return new Error('This repo has no labels to remove!');
return prompt([{
name: 'removals',
type: 'checkbox',
message: 'Which labels would you like to remove?',
choices: labels.map((label) => label.name),
validate: validateRemovals,
filter: filterRemovalLabels.bind(null, labels),
}]);
})
.then(answers => {
if (answers.removals) {
return removeLabels(repo, token, answers);
}
return console.log(answers);
})
.catch(console.warn);

case 'remove all labels':
banner.removeAllLabels();
return requestLabels(repo, token)
.then(labels => {
if (labels.length !== 0) {
removeLabels(repo, token, {
removals: labels,
});
} else {
console.log('No labels to remove.');
}
})
.catch(console.warn);
.then(labels => {
if (labels.length !== 0) {
removeLabels(repo, token, {
removals: labels,
});
} else {
console.log('No labels to remove.');
}
})
.catch(console.warn);

default:
return gitLabelmaker();
Expand Down
7 changes: 5 additions & 2 deletions bin/modules/__tests__/convert-rgb-to-hex.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const convertRGBToHex = require('../convert-rgb-to-hex.js');

test('it throws an error if more than 3 RGB values are detected', () => {
expect(convertRGBToHex('rgb(2, 3, 4, 5)')).toThrow();
expect(() => {
convertRGBToHex('rgb(2, 3, 4, 5)');
}).toThrow();
});

test('it returns the expected hex value', () => {
const testStr = 'rgb(0, 170, 204)';
const expected = '00aacc';
expect(convertRGBToHex(testStr)).toBe(expected);
const actual = convertRGBToHex(testStr);
expect(actual).toBe(expected);
});
9 changes: 4 additions & 5 deletions bin/modules/convert-rgb-to-hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ module.exports = (color) => {
const includesNonNumbers = arr => !!(arr.filter(val => isNaN(val)).length);
// actually generates our RGB values
const values = color.split(',')
.map(val => val.toLowerCase())
.map(stripNonValRgbText)
.map(val => parseInt(val));
// console.log(values);
if (values.length > 3 || includesNonNumbers(values)) return new Error('You must pass a valid RGB value to convertRGBToHex!');
.map(val => val.toLowerCase())
.map(stripNonValRgbText)
.map(val => parseInt(val));
if (values.length > 3 || includesNonNumbers(values)) throw new Error('You must pass a valid RGB value to convertRGBToHex!');
return rgbHex(values[0], values[1], values[2]);
};
20 changes: 10 additions & 10 deletions bin/modules/do-custom-label-prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
*/


const prompt = require('./prompt.js');
const prompt = require('inquirer').prompt;

module.exports = (prompts) => {
const doCustomLabelPrompts = (newLabels, done) => {
prompt(prompts)
.then((answers) => {
newLabels.push({ name: answers.labelName, color: answers.labelColor, });
if (answers.addAnother) {
doCustomLabelPrompts(newLabels, done);
} else {
done(newLabels);
}
})
.catch(console.warn);
.then((answers) => {
newLabels.push({ name: answers.labelName, color: answers.labelColor, });
if (answers.addAnother) {
doCustomLabelPrompts(newLabels, done);
} else {
done(newLabels);
}
})
.catch(console.warn);
};
return doCustomLabelPrompts;
};
Loading