Skip to content

Commit

Permalink
feat: Added the ability to customise a header at the top of the gener…
Browse files Browse the repository at this point in the history
…ated file
  • Loading branch information
Makeshift committed Jan 5, 2023
1 parent 7afef72 commit b27a874
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
10 changes: 8 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ inputs:
default: .github/dependabot.template.yml

follow-symbolic-links:
description: 'Indicates whether to follow symbolic links'
default: true
description: 'Indicates whether to follow symbolic links (If you want to put your template in a weird place)'
default: 'true'

file-header:
description: 'Header to add to the generated file. {input-name} will be replaced with the value of the given input.'
default: |
# This file was generated by the "Generate Dependabot Glob" action. Do not edit it directly.
# Make changes to `{template-file}` and a PR will be automatically created.
runs:
using: 'node16'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"js-yaml": "^4.1.0"
},
"devDependencies": {
"@vercel/ncc": "^0.34.0",
"@vercel/ncc": "^0.36.0",
"standard": "^17.0.0"
}
}
30 changes: 22 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,35 @@ const util = require('util')
const glob = util.promisify(require('glob'))
const path = require('path')

const actionName = 'Makeshift/generate-dependabot-glob-action'
const actionOpts = {
'template-file': core.getInput('template-file') || '.github/dependabot.template.yml',
'follow-symbolic-links': core.getInput('follow-symbolic-links') === 'true',
// eslint-disable-next-line no-template-curly-in-string
'file-header': core.getInput('file-header') || '# This file is generated from ${template-file}'
}

const globOpts = {
root: process.cwd(),
mark: true,
matchBase: true,
nomount: true,
follow: core.getInput('follow-symbolic-links') === 'true' || true
follow: actionOpts['follow-symbolic-links']
}

function parseStringTemplate (str, obj) {
const parts = str.split(/\$\{(?!\d)[\wæøåÆØÅ]*\}/)
const args = str.match(/[^{}]+(?=})/g) || []
const parameters = args.map(argument => obj[argument] || (obj[argument] === undefined ? '' : obj[argument]))
return String.raw({ raw: parts }, ...parameters)
}

// Lazy deep clone. Not great, but works for this purpose.
const clone = obj => JSON.parse(JSON.stringify(obj))

async function run () {
const templateFile = core.getInput('template-file') || '.github/dependabot.template.yml'
const template = yaml.load(await fs.readFile(templateFile, 'utf8'))
const { templateFile, warning } = actionOpts

const template = yaml.load(await fs.readFile(templateFile, 'utf8'))
const newUpdates = []

for (const entry of template.updates) {
Expand All @@ -29,7 +42,7 @@ async function run () {
const matchingFiles = await glob(entry.directory, globOpts)
core.info(`Found ${matchingFiles.length} files matching ${entry.directory}`)
const matchingDirs = new Set(matchingFiles.map(file => path.dirname(file)))
core.info(`Found ${matchingDirs.length} directories matching ${entry.directory}`)
core.info(`Found ${matchingDirs.size} directories matching ${entry.directory}`)

for (const dir of matchingDirs) {
core.info(`Creating entry for ${dir} with ecosystem ${entry['package-ecosystem']}`)
Expand All @@ -39,11 +52,12 @@ async function run () {
}
}

core.info(`Here's the final config: ${JSON.stringify(newUpdates)}`)
core.info(`Here's the final config (JSON): ${JSON.stringify(newUpdates)}`)
template.updates = newUpdates
core.info('Writing config to .github/dependabot.yml')
const warning = `# This file is generated by the ${actionName} action via a GitHub Actions Workflow from the template file '${templateFile}'. Do not edit directly.`
await fs.writeFile('.github/dependabot.yml', warning + '\n' + yaml.dump(template))
const finalString = parseStringTemplate(warning, actionOpts) + '\n' + yaml.dump(template)
core.info(finalString)
await fs.writeFile('.github/dependabot.yml', finalString)
}

run().catch(error => {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==

"@vercel/ncc@^0.34.0":
version "0.34.0"
resolved "https://registry.yarnpkg.com/@vercel/ncc/-/ncc-0.34.0.tgz#d0139528320e46670d949c82967044a8f66db054"
integrity sha512-G9h5ZLBJ/V57Ou9vz5hI8pda/YQX5HQszCs3AmIus3XzsmRn/0Ptic5otD3xVST8QLKk7AMk7AqpsyQGN7MZ9A==
"@vercel/ncc@^0.36.0":
version "0.36.0"
resolved "https://registry.yarnpkg.com/@vercel/ncc/-/ncc-0.36.0.tgz#1f262b86fc4f0770bbc0fc1d331d5aaa1bd47334"
integrity sha512-/ZTUJ/ZkRt694k7KJNimgmHjtQcRuVwsST2Z6XfYveQIuBbHR+EqkTc1jfgPkQmMyk/vtpxo3nVxe8CNuau86A==

acorn-jsx@^5.3.2:
version "5.3.2"
Expand Down

0 comments on commit b27a874

Please sign in to comment.