dependabot
Version:
Easily scaffold out dependabot config files
153 lines (131 loc) • 4.44 kB
JavaScript
import { parse, stringify } from 'yaml'
import fs from 'fs';
import path from 'path';
import prompts from 'prompts';
console.log('Welcome to the Dependabot config generator!');
const ecosystems = [
{ name: 'Bundler', value: 'bundler' },
{ name: 'Cargo', value: 'cargo' },
{ name: 'Composer', value: 'composer' },
{ name: 'Docker', value: 'docker' },
{ name: 'Hex', value: 'mix' },
{ name: 'elm-package', value: 'elm' },
{ name: 'git submodule', value: 'gitsubmodule' },
{ name: 'GitHub Actions', value: 'github-actions' },
{ name: 'Go modules', value: 'gomod' },
{ name: 'Gradle', value: 'gradle' },
{ name: 'Maven', value: 'maven' },
{ name: 'npm', value: 'npm' },
{ name: 'NuGet', value: 'nuget' },
{ name: 'pip', value: 'pip' },
{ name: 'pipenv', value: 'pip' },
{ name: 'pub', value: 'pub' },
{ name: 'pnpm', value: 'npm' },
{ name: 'poetry', value: 'pip' },
{ name: 'Swift', value: 'swift' },
{ name: 'Terraform', value: 'terraform' },
{ name: 'Yarn', value: 'npm' },
];
const configFileExists = fs.existsSync(path.resolve(process.cwd(), '.github', 'dependabot.yml'));
const main = async () => {
const onSubmit = (answers) => {
console.log('Generating Dependabot config file...');
console.log(answers);
// Check if the dependabot config file exists, if not create it.
// If it does exist, append the new config to the existing file.
const config = {
"version": 2,
"updates": [
{
"package-ecosystem": answers.ecosystem,
"directory": answers.directory,
"schedule": {
"interval": answers.interval,
},
},
],
};
if (answers.assignees.length > 0 && answers.assignees[0] !== '') {
config.updates[0].assignees = answers.assignees;
}
const configPath = path.resolve(process.cwd(), '.github', 'dependabot.yml');
if (! fs.existsSync(configPath)) {
console.log('Creating Dependabot config file');
// Check if the .github directory exists, if not create it.
const githubDir = path.resolve(process.cwd(), '.github');
if (! fs.existsSync(githubDir)) {
fs.mkdirSync(githubDir);
}
fs.writeFileSync(configPath, stringify(config));
return;
}
console.log('Updating Dependabot config file');
const existingConfig = fs.readFileSync(configPath, 'utf8');
const existingConfigJSON = parse(existingConfig);
const existingConfigEcosystems = existingConfigJSON.updates.map((update) => {
return update['package-ecosystem'];
});
if (existingConfigEcosystems.includes(config.updates[0]['package-ecosystem'])) {
// Update the ecosystem config
const existingConfigIndex = existingConfigEcosystems.indexOf(config.updates[0]['package-ecosystem']);
existingConfigJSON.updates[existingConfigIndex] = config.updates[0];
const updatedConfig = stringify(existingConfigJSON);
console.log(updatedConfig);
fs.writeFileSync(configPath, updatedConfig);
return;
}
existingConfigJSON.updates.push(config.updates[0]);
const updatedConfig = stringify(existingConfigJSON);
console.log(updatedConfig);
fs.writeFileSync(configPath, updatedConfig);
console.log('Done!');
};
const options = await prompts([
{
type: 'select',
name: 'ecosystem',
message: 'What ecosystem would you like to generate a config for?',
choices: ecosystems,
initial: ecosystems.findIndex((ecosystem) => ecosystem.value === 'npm'),
},
{
type: 'text',
name: 'directory',
message: 'What directory is the package in?',
initial: '/',
},
{
type: 'select',
name: 'interval',
message: 'How often should Dependabot check for updates?',
choices: [
{ name: 'Daily', value: 'daily' },
{ name: 'Weekly', value: 'weekly' },
{ name: 'Monthly', value: 'monthly' },
],
},
{
type: 'list',
name: 'assignees',
message: 'Who should Dependabot assign Pull Requests to?',
separator: ',',
initial: '',
onRender() {
console.log('If you want to assign Pull Requests to multiple people, separate their usernames with a comma. Leave blank to assign to no one.');
},
},
]);
const { proceed } = await prompts({
type: 'confirm',
name: 'proceed',
message: configFileExists ? 'A Dependabot config file already exists, would you like to update it?' : 'A Dependabot config file does not exist, would you like to create one?',
initial: true,
});
if (! proceed) {
console.log('Exiting...');
return;
}
onSubmit(options);
}
main();