|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +// Grab last git commit |
| 7 | +function getCommitHasForPath(path) { |
| 8 | + return require('child_process') |
| 9 | + .execSync(`git log -1 --format=%H HEAD -- ${path}`) |
| 10 | + .toString().trim() |
| 11 | +} |
| 12 | + |
| 13 | +const stackbrewPath = path.basename(__filename); |
| 14 | + |
| 15 | +// Header |
| 16 | +let stackbrew = `# this file is generated via https://github.com/nodejs/docker-node/blob/${getCommitHasForPath(stackbrewPath)}/${stackbrewPath} |
| 17 | +
|
| 18 | +Maintainers: The Node.js Docker Team <https://github.com/nodejs/docker-node> (@nodejs) |
| 19 | +GitRepo: https://github.com/nodejs/docker-node.git |
| 20 | +GitFetch: refs/heads/main\n`; |
| 21 | + |
| 22 | +// Loop versions |
| 23 | + |
| 24 | +const config = require('./versions.json'); |
| 25 | + |
| 26 | +const versions = Object.keys(config).reverse() |
| 27 | + |
| 28 | +const now = new Date().getTime() |
| 29 | +const aplineRE = new RegExp(/alpine*/); |
| 30 | +const slimRE = new RegExp(/\*-slim/); |
| 31 | + |
| 32 | +for(version of versions) { |
| 33 | + let lts = new Date(config[version].lts).getTime(); |
| 34 | + let maintenance = new Date(config[version].maintenance).getTime(); |
| 35 | + let isCurrent = lts > now; |
| 36 | + let isLTS = (maintenance > now) && (now > lts); |
| 37 | + let codename = config[version].codename |
| 38 | + let defaultAlpine = config[version]['alpine-default'] |
| 39 | + let defaultDebian = config[version]['debian-default'] |
| 40 | + let variants = config[version].variants |
| 41 | + let fullversion; |
| 42 | + for(variant in variants) { |
| 43 | + let dockerfilePath = path.join(version, variant, 'Dockerfile'); |
| 44 | + let isAlpine = aplineRE.test(variant) |
| 45 | + let isSlim = slimRE.test(variant) |
| 46 | + let isDefaultSlim = new RegExp(`${defaultDebian}-slim`).test(variant) |
| 47 | + |
| 48 | + // Get full version from the first Dockerfile |
| 49 | + if (!fullversion) { |
| 50 | + let dockerfile = fs.readFileSync(dockerfilePath, 'utf-8') |
| 51 | + fullversion = dockerfile.match(/ENV NODE_VERSION (?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)/) |
| 52 | + } |
| 53 | + let tags = [ |
| 54 | + `${fullversion.groups.major}.${fullversion.groups.minor}.${fullversion.groups.patch}-${variant}`, |
| 55 | + `${fullversion.groups.major}.${fullversion.groups.minor}-${variant}`, |
| 56 | + `${fullversion.groups.major}-${variant}`, |
| 57 | + ] |
| 58 | + |
| 59 | + if (codename) { |
| 60 | + tags.push(`${codename}-${variant}`) |
| 61 | + } |
| 62 | + |
| 63 | + if (variant === defaultAlpine) { |
| 64 | + tags.push(`${fullversion.groups.major}.${fullversion.groups.minor}.${fullversion.groups.patch}-alpine`) |
| 65 | + tags.push(`${fullversion.groups.major}.${fullversion.groups.minor}-alpine`) |
| 66 | + tags.push(`${fullversion.groups.major}-alpine`) |
| 67 | + if (codename) { |
| 68 | + tags.push(`${codename}-alpine`) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (variant === defaultDebian) { |
| 73 | + tags.push(`${fullversion.groups.major}.${fullversion.groups.minor}.${fullversion.groups.patch}`) |
| 74 | + tags.push(`${fullversion.groups.major}.${fullversion.groups.minor}`) |
| 75 | + tags.push(`${fullversion.groups.major}`) |
| 76 | + if (isSlim) { |
| 77 | + tags.push(`${fullversion.groups.major}.${fullversion.groups.minor}.${fullversion.groups.patch}-slim`) |
| 78 | + tags.push(`${fullversion.groups.major}.${fullversion.groups.minor}-slim`) |
| 79 | + tags.push(`${fullversion.groups.major}-slim`) |
| 80 | + } |
| 81 | + if (codename) { |
| 82 | + tags.push(`${codename}`) |
| 83 | + } |
| 84 | + } |
| 85 | + if (isDefaultSlim) { |
| 86 | + tags.push(`${fullversion.groups.major}.${fullversion.groups.minor}.${fullversion.groups.patch}-slim`) |
| 87 | + tags.push(`${fullversion.groups.major}.${fullversion.groups.minor}-slim`) |
| 88 | + tags.push(`${fullversion.groups.major}-slim`) |
| 89 | + if (codename) { |
| 90 | + tags.push(`${codename}-slim`) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (isCurrent) { |
| 95 | + if (variant === defaultAlpine) { |
| 96 | + tags.push(variant) |
| 97 | + tags.push(`${fullversion.groups.major}.${fullversion.groups.minor}.${fullversion.groups.patch}-alpine`) |
| 98 | + tags.push(`${fullversion.groups.major}.${fullversion.groups.minor}-alpine`) |
| 99 | + tags.push(`${fullversion.groups.major}-alpine`) |
| 100 | + tags.push('alpine') |
| 101 | + tags.push('current-alpine') |
| 102 | + } |
| 103 | + if (variant === defaultDebian) { |
| 104 | + tags.push(variant) |
| 105 | + tags.push('latest') |
| 106 | + tags.push('current') |
| 107 | + } |
| 108 | + if (isAlpine) { |
| 109 | + tags.push(`${variant}`) |
| 110 | + tags.push(`current-${variant}`) |
| 111 | + } |
| 112 | + if (!isAlpine) { |
| 113 | + tags.push(`${variant}`) |
| 114 | + tags.push(`current-${variant}`) |
| 115 | + } |
| 116 | + if (isDefaultSlim) { |
| 117 | + tags.push('slim') |
| 118 | + tags.push('current-slim') |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if (isLTS) { |
| 123 | + tags.push(`lts-${variant}`) |
| 124 | + if (variant === defaultAlpine) { |
| 125 | + } |
| 126 | + if (variant === defaultDebian) { |
| 127 | + tags.push('lts') |
| 128 | + if (codename) { |
| 129 | + tags.push(`lts-${codename}`) |
| 130 | + } |
| 131 | + } |
| 132 | + if (isDefaultSlim) { |
| 133 | + tags.push(`lts-slim`) |
| 134 | + } |
| 135 | + if (variant === defaultAlpine) { |
| 136 | + tags.push(`lts-alpine`) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // remove duplicates |
| 141 | + tags = tags.filter((x, i, a) => a.indexOf(x) == i) |
| 142 | + tags = tags.sort() |
| 143 | + |
| 144 | + stackbrew += `\nTags: ${tags.join(', ')}\n` |
| 145 | + stackbrew += `Architectures: ${config[version].variants[variant].join(', ')}\n` |
| 146 | + stackbrew += `GitCommit: ${getCommitHasForPath(dockerfilePath)}\n` |
| 147 | + stackbrew += `Directory: ${version}/${variant}\n` |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// output |
| 152 | +console.log(stackbrew) |
0 commit comments