Skip to content

Commit

Permalink
feat(templating): Add templating features to the asset parameter.
Browse files Browse the repository at this point in the history
The following variables are available: `branch`, `lastRelease`, `nextRelease` and `commits`. Search on the [plugins](https://github.com/semantic-release/semantic-release/blob/master/docs/developer-guide/plugin.md) documentation to see the type of those objects.
Example: `my-extension_v${nextRelease.version}_${branch.name}.zip` will result in something like `my-extension_v2.3.1_main.zip`

Closes #82
  • Loading branch information
GabeDuarteM committed Jul 8, 2022
1 parent 29f1659 commit db93147
Show file tree
Hide file tree
Showing 6 changed files with 4,635 additions and 4,378 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ This plugin requires some parameters to be set, so be sure to check below and fi

- `manifestPath`: The path of the `manifest.json` file inside the dist folder. Defaults to `<distFolder parameter>/manifest.json`.

The `asset` parameter is parsed with Lodash template. The following variables are available: `branch`, `lastRelease`, `nextRelease` and `commits`. Search on the [plugins](https://github.com/semantic-release/semantic-release/blob/master/docs/developer-guide/plugin.md) documentation to see the type of those objects.

Example: `my-extension_v${nextRelease.version}_${branch.name}.zip` will result in something like `my-extension_v2.3.1_main.zip`

### `publish`

Uploads the generated zip file to the webstore and publishes a new release.
Expand All @@ -76,6 +80,10 @@ If you decide to make the draft, make sure to fill all the required fields on th
- `draft`: Uploads the extension to the webstore, but skips the publishing step.
- `trustedTesters`: Releases the extension as a [private extension](https://support.google.com/chrome/a/answer/2663860). Defaults to `default`.

The `asset` parameter is parsed with Lodash template. The following variables are available: `branch`, `lastRelease`, `nextRelease` and `commits`. Search on the [plugins](https://github.com/semantic-release/semantic-release/blob/master/docs/developer-guide/plugin.md) documentation to see the type of those objects.

Example: `my-extension_v${nextRelease.version}_${branch.name}.zip` will result in something like `my-extension_v2.3.1_main.zip`

### Chrome webstore authentication

You will need to get three parameters from the Google API: a `clientId`, a `clientSecret` and a `refreshToken`. For more information on how to get those parameters and how to set the environment variables which are required in order for this plugin to work properly, read [this guide](Authentication.md).
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@babel/preset-typescript": "7.18.6",
"@types/archiver": "5.3.1",
"@types/fs-extra": "9.0.13",
"@types/lodash.template": "4.5.1",
"@types/node": "18.0.1",
"@types/semantic-release": "17.2.3",
"@typescript-eslint/eslint-plugin": "5.30.5",
Expand All @@ -51,7 +52,8 @@
"aggregate-error": "4.0.1",
"archiver": "5.3.1",
"chrome-webstore-upload": "1.0.0",
"fs-extra": "10.1.0"
"fs-extra": "10.1.0",
"lodash.template": "4.5.0"
},
"repository": {
"type": "git",
Expand Down
35 changes: 35 additions & 0 deletions src/@types/semantic-release/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
type LoggerFunction = (...message: any[]) => void

export interface Logger {
await: LoggerFunction
complete: LoggerFunction
debug: LoggerFunction
error: LoggerFunction
fatal: LoggerFunction
fav: LoggerFunction
info: LoggerFunction
log: LoggerFunction
note: LoggerFunction
pause: LoggerFunction
pending: LoggerFunction
star: LoggerFunction
start: LoggerFunction
success: LoggerFunction
wait: LoggerFunction
warn: LoggerFunction
watch: LoggerFunction
}

declare module 'semantic-release' {
interface Context {
branch: {
channel: string | undefined
tags: unknown[]
type: string
name: string
range: string
accept: string[]
main: boolean
}
}
}
13 changes: 10 additions & 3 deletions src/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import SemanticReleaseError from '@semantic-release/error'
import archiver from 'archiver'
import { readJsonSync, writeJsonSync } from 'fs-extra'
import template from 'lodash.template'

import { createWriteStream } from 'fs'
import { resolve } from 'path'
Expand All @@ -23,7 +24,6 @@ const prepareManifest = (
const zipFolder = (
asset: string,
distFolder: string,
version: string,
logger: Context['logger'],
) => {
const zipPath = resolve(asset)
Expand All @@ -42,7 +42,7 @@ const zipFolder = (

const prepare = (
{ manifestPath, distFolder, asset }: PluginConfig,
{ nextRelease, logger }: Context,
{ nextRelease, logger, lastRelease, branch, commits }: Context,
) => {
if (!asset) {
throw new SemanticReleaseError(
Expand All @@ -60,12 +60,19 @@ const prepare = (

const normalizedDistFolder = distFolder || 'dist'

const compiledAssetString = template(asset)({
branch,
lastRelease,
nextRelease,
commits,
})

prepareManifest(
manifestPath || `${normalizedDistFolder}/manifest.json`,
version,
logger,
)
zipFolder(asset, normalizedDistFolder, version, logger)
zipFolder(compiledAssetString, normalizedDistFolder, logger)
}

export default prepare
12 changes: 10 additions & 2 deletions src/publish.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import SemanticReleaseError from '@semantic-release/error'
import { createReadStream } from 'fs-extra'
import { Context } from 'semantic-release'
import template from 'lodash.template'

import type PluginConfig from './@types/pluginConfig'
import getEsModule from './getEsModule'
Expand All @@ -9,7 +10,7 @@ const errorWhitelist = ['PUBLISHED_WITH_FRICTION_WARNING']

const publish = async (
{ extensionId, target, asset }: PluginConfig,
{ logger }: Context,
{ logger, branch, lastRelease, nextRelease, commits }: Context,
) => {
const {
GOOGLE_CLIENT_ID: clientId,
Expand Down Expand Up @@ -44,7 +45,14 @@ const publish = async (

logger.log('Creating zip file...')

const zipFile = createReadStream(asset)
const compiledAssetString = template(asset)({
branch,
lastRelease,
nextRelease,
commits,
})

const zipFile = createReadStream(compiledAssetString)
const errorMessage = `
[ERROR] Semantic Release Chrome
Expand Down
Loading

0 comments on commit db93147

Please sign in to comment.