{"size": 1944, "ext": "js", "max_stars_repo_path": "source/debian11/push-manifest.js", "max_stars_repo_name": "dr-js/docker-stash", "max_stars_repo_stars_event_min_datetime": "2019-08-14T08:40:27.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-10T10:06:20.000Z", "max_issues_repo_path": "source/debian11/push-manifest.js", "max_issues_repo_name": "dr-js/docker-stash", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "source/debian11/push-manifest.js", "max_forks_repo_name": "dr-js/docker-stash", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 4, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 41.3617021277, "max_line_length": 175, "alphanum_fraction": 0.6687242798}
const { oneOf } = require('@dr-js/core/library/common/verify.js')
const { runDockerSync } = require('@dr-js/core/library/node/module/Software/docker.js')
const { runKit } = require('@dr-js/core/library/node/kit.js')

const {
  DOCKER_BUILD_ARCH_INFO_LIST,
  DEBIAN11_BUILD_REPO, DEBIAN11_BUILD_REPO_GHCR, DEBIAN11_BUILD_FLAVOR_LIST // loadDebian11TagCore,
} = require('../function.js')

const [
  , // node
  , // script.js
  PUSH_TARGET = ''
] = process.argv

runKit(async (kit) => {
  const PUSH_TARGET_MAP = {
    'ALL': [ 'BASE', 'GHCR' ],
    'BASE-ONLY': [ 'BASE' ],
    'GHCR-ONLY': [ 'GHCR' ]
  }
  oneOf(PUSH_TARGET, Object.keys(PUSH_TARGET_MAP))
  kit.padLog(`push target: ${PUSH_TARGET}`)
  const hasTarget = (target) => PUSH_TARGET_MAP[ PUSH_TARGET ].includes(target)

  const { version: BUILD_VERSION } = require(kit.fromRoot('package.json'))
  const toGitHubTag = (tag) => tag.replace(DEBIAN11_BUILD_REPO, DEBIAN11_BUILD_REPO_GHCR)

  const TAG_LIST_BASE = [ // NOTE: skip core tag, as local push will need build or manual edit to keep up with latest
    // loadDebian11TagCore(''),
    ...DEBIAN11_BUILD_FLAVOR_LIST.map(({ NAME: flavorName }) => `${DEBIAN11_BUILD_REPO}:11-${flavorName}-${BUILD_VERSION}`),
    // loadDebian11TagCore('CN'),
    ...DEBIAN11_BUILD_FLAVOR_LIST.map(({ NAME: flavorName }) => `${DEBIAN11_BUILD_REPO}:11-${flavorName}-${BUILD_VERSION}-cn`)
  ]
  const TAG_LIST_GHCR = TAG_LIST_BASE.map(toGitHubTag)

  kit.padLog('push manifest')
  for (const tag of [
    ...(hasTarget('GHCR') ? [ ...TAG_LIST_GHCR ].reverse() : []), // faster in CI
    ...(hasTarget('BASE') ? [ ...TAG_LIST_BASE ].reverse() : [])
  ]) {
    kit.log(`push manifest: ${tag}`)
    runDockerSync([ 'manifest', 'create', tag, ...DOCKER_BUILD_ARCH_INFO_LIST.map((DOCKER_BUILD_ARCH_INFO) => [ '--amend', `${tag}-${DOCKER_BUILD_ARCH_INFO.key}` ]).flat(1) ])
    runDockerSync([ 'manifest', 'push', tag ])
  }
}, { title: 'push-manifest' })
