Skip to content

Commit db93147

Browse files
committed
feat(templating): Add templating features to the asset parameter.
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
1 parent 29f1659 commit db93147

File tree

6 files changed

+4635
-4378
lines changed

6 files changed

+4635
-4378
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ This plugin requires some parameters to be set, so be sure to check below and fi
5858

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

61+
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.
62+
63+
Example: `my-extension_v${nextRelease.version}_${branch.name}.zip` will result in something like `my-extension_v2.3.1_main.zip`
64+
6165
### `publish`
6266

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

83+
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.
84+
85+
Example: `my-extension_v${nextRelease.version}_${branch.name}.zip` will result in something like `my-extension_v2.3.1_main.zip`
86+
7987
### Chrome webstore authentication
8088

8189
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).

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@babel/preset-typescript": "7.18.6",
3636
"@types/archiver": "5.3.1",
3737
"@types/fs-extra": "9.0.13",
38+
"@types/lodash.template": "4.5.1",
3839
"@types/node": "18.0.1",
3940
"@types/semantic-release": "17.2.3",
4041
"@typescript-eslint/eslint-plugin": "5.30.5",
@@ -51,7 +52,8 @@
5152
"aggregate-error": "4.0.1",
5253
"archiver": "5.3.1",
5354
"chrome-webstore-upload": "1.0.0",
54-
"fs-extra": "10.1.0"
55+
"fs-extra": "10.1.0",
56+
"lodash.template": "4.5.0"
5557
},
5658
"repository": {
5759
"type": "git",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
type LoggerFunction = (...message: any[]) => void
2+
3+
export interface Logger {
4+
await: LoggerFunction
5+
complete: LoggerFunction
6+
debug: LoggerFunction
7+
error: LoggerFunction
8+
fatal: LoggerFunction
9+
fav: LoggerFunction
10+
info: LoggerFunction
11+
log: LoggerFunction
12+
note: LoggerFunction
13+
pause: LoggerFunction
14+
pending: LoggerFunction
15+
star: LoggerFunction
16+
start: LoggerFunction
17+
success: LoggerFunction
18+
wait: LoggerFunction
19+
warn: LoggerFunction
20+
watch: LoggerFunction
21+
}
22+
23+
declare module 'semantic-release' {
24+
interface Context {
25+
branch: {
26+
channel: string | undefined
27+
tags: unknown[]
28+
type: string
29+
name: string
30+
range: string
31+
accept: string[]
32+
main: boolean
33+
}
34+
}
35+
}

src/prepare.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import SemanticReleaseError from '@semantic-release/error'
22
import archiver from 'archiver'
33
import { readJsonSync, writeJsonSync } from 'fs-extra'
4+
import template from 'lodash.template'
45

56
import { createWriteStream } from 'fs'
67
import { resolve } from 'path'
@@ -23,7 +24,6 @@ const prepareManifest = (
2324
const zipFolder = (
2425
asset: string,
2526
distFolder: string,
26-
version: string,
2727
logger: Context['logger'],
2828
) => {
2929
const zipPath = resolve(asset)
@@ -42,7 +42,7 @@ const zipFolder = (
4242

4343
const prepare = (
4444
{ manifestPath, distFolder, asset }: PluginConfig,
45-
{ nextRelease, logger }: Context,
45+
{ nextRelease, logger, lastRelease, branch, commits }: Context,
4646
) => {
4747
if (!asset) {
4848
throw new SemanticReleaseError(
@@ -60,12 +60,19 @@ const prepare = (
6060

6161
const normalizedDistFolder = distFolder || 'dist'
6262

63+
const compiledAssetString = template(asset)({
64+
branch,
65+
lastRelease,
66+
nextRelease,
67+
commits,
68+
})
69+
6370
prepareManifest(
6471
manifestPath || `${normalizedDistFolder}/manifest.json`,
6572
version,
6673
logger,
6774
)
68-
zipFolder(asset, normalizedDistFolder, version, logger)
75+
zipFolder(compiledAssetString, normalizedDistFolder, logger)
6976
}
7077

7178
export default prepare

src/publish.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import SemanticReleaseError from '@semantic-release/error'
22
import { createReadStream } from 'fs-extra'
33
import { Context } from 'semantic-release'
4+
import template from 'lodash.template'
45

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

1011
const publish = async (
1112
{ extensionId, target, asset }: PluginConfig,
12-
{ logger }: Context,
13+
{ logger, branch, lastRelease, nextRelease, commits }: Context,
1314
) => {
1415
const {
1516
GOOGLE_CLIENT_ID: clientId,
@@ -44,7 +45,14 @@ const publish = async (
4445

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

47-
const zipFile = createReadStream(asset)
48+
const compiledAssetString = template(asset)({
49+
branch,
50+
lastRelease,
51+
nextRelease,
52+
commits,
53+
})
54+
55+
const zipFile = createReadStream(compiledAssetString)
4856
const errorMessage = `
4957
5058
[ERROR] Semantic Release Chrome

0 commit comments

Comments
 (0)