local cache_yarn = import 'github/workflows/_cache-yarn.libsonnet';
local utils = import 'utils.libjsonnet';

function(settings) {
  jobs: {
    publish: {
      permissions: {
        contents: 'read',
        'id-token': 'write',
      },
      'runs-on': settings.github.workflows.publish_npm_any.runs_on,
      steps: [
        {
          uses: 'actions/checkout@' + utils.githubLatestActionTag('actions', 'checkout'),
        },
        {
          name: 'Setup Node.js',
          uses: 'actions/setup-node@' + utils.githubLatestActionTag('actions', 'setup-node'),
          with: {
            'node-version': settings.github.workflows.publish_npm_any.node_version,
            'registry-url': settings.github.workflows.publish_npm_any.registry_url,
          },
        },
        {
          name: 'Update npm',
          run: 'npm install -g npm@latest',
        },
        cache_yarn,
        {
          name: 'Install dependencies',
          run: 'yarn',
        },
        {
          name: 'Build',
          run: 'yarn tsc',
        },
        {
          name: 'Publish to NPM',
          run: 'yarn npm publish',
        },
      ],
    },
  },
  name: 'Publish to NPM',
  on: {
    push: {
      tags: [
        'v*',
      ],
    },
  },
}
