Skip to content

Commit 6c0c16f

Browse files
refactor: dedupe key resolver for static publishers (#3421)
1 parent c100186 commit 6c0c16f

File tree

7 files changed

+109
-24
lines changed

7 files changed

+109
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "@electron-forge/publisher-static",
3+
"version": "7.1.0",
4+
"description": "Base publisher for Electron Forge",
5+
"repository": "https://github.com/electron/forge",
6+
"author": "Samuel Attard",
7+
"license": "MIT",
8+
"main": "dist/PublisherStatic.js",
9+
"typings": "dist/PublisherStatic.d.ts",
10+
"scripts": {
11+
"test": "yarn test:base test/**/*_spec.ts",
12+
"test:base": "cross-env TS_NODE_FILES=1 mocha --config ../../../.mocharc.js"
13+
},
14+
"dependencies": {
15+
"@electron-forge/publisher-base": "7.1.0",
16+
"@electron-forge/shared-types": "7.1.0"
17+
},
18+
"devDependencies": {
19+
"chai": "^4.3.3",
20+
"mocha": "^9.0.1"
21+
},
22+
"engines": {
23+
"node": ">= 16.4.0"
24+
},
25+
"publishConfig": {
26+
"access": "public"
27+
},
28+
"files": [
29+
"dist",
30+
"src"
31+
]
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import path from 'path';
2+
3+
import { PublisherBase, PublisherOptions } from '@electron-forge/publisher-base';
4+
import { ForgeArch, ForgePlatform } from '@electron-forge/shared-types';
5+
6+
interface StaticArtifact {
7+
path: string;
8+
platform: ForgePlatform;
9+
arch: ForgeArch;
10+
keyPrefix: string;
11+
}
12+
13+
interface StaticPublisherConfig {
14+
/**
15+
* Custom function to provide the key to upload a given file to
16+
*/
17+
keyResolver?: (fileName: string, platform: string, arch: string) => string;
18+
}
19+
20+
export default abstract class PublisherStatic<C extends StaticPublisherConfig> extends PublisherBase<C> {
21+
protected keyForArtifact(artifact: StaticArtifact): string {
22+
if (this.config.keyResolver) {
23+
return this.config.keyResolver(path.basename(artifact.path), artifact.platform, artifact.arch);
24+
}
25+
26+
return `${artifact.keyPrefix}/${artifact.platform}/${artifact.arch}/${path.basename(artifact.path)}`;
27+
}
28+
}
29+
30+
export { PublisherStatic, StaticPublisherConfig, PublisherOptions };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { expect } from 'chai';
2+
3+
import { PublisherStatic, StaticPublisherConfig } from '../src/PublisherStatic';
4+
5+
class PublisherImpl extends PublisherStatic<StaticPublisherConfig> {
6+
defaultPlatforms = [];
7+
8+
name = 'test';
9+
10+
public exposedKeyForArtifact = this.keyForArtifact;
11+
}
12+
13+
describe('PublisherStatic', () => {
14+
describe('keyForArtifact', () => {
15+
it('should by default concat prefix, platform, arch and filename', () => {
16+
const publisher = new PublisherImpl({});
17+
expect(
18+
publisher.exposedKeyForArtifact({
19+
platform: 'plat',
20+
arch: 'arch',
21+
keyPrefix: 'stuff',
22+
path: __filename,
23+
})
24+
).to.equal('stuff/plat/arch/StaticPublisher_spec.ts');
25+
});
26+
27+
it('should call the provided method', () => {
28+
const publisher = new PublisherImpl({
29+
keyResolver: () => 'lololol',
30+
});
31+
expect(
32+
publisher.exposedKeyForArtifact({
33+
platform: 'plat',
34+
arch: 'arch',
35+
keyPrefix: 'stuff',
36+
path: __filename,
37+
})
38+
).to.equal('lololol');
39+
});
40+
});
41+
});

packages/publisher/gcs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"node": ">= 16.4.0"
1616
},
1717
"dependencies": {
18-
"@electron-forge/publisher-base": "7.1.0",
18+
"@electron-forge/publisher-static": "7.1.0",
1919
"@electron-forge/shared-types": "7.1.0",
2020
"@google-cloud/storage": "^7.5.0",
2121
"debug": "^4.3.1"

packages/publisher/gcs/src/PublisherGCS.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import path from 'path';
2-
3-
import { PublisherBase, PublisherOptions } from '@electron-forge/publisher-base';
1+
import { PublisherOptions, PublisherStatic } from '@electron-forge/publisher-static';
42
import { Storage } from '@google-cloud/storage';
53
import debug from 'debug';
64

@@ -15,7 +13,7 @@ type GCSArtifact = {
1513
arch: string;
1614
};
1715

18-
export default class PublisherGCS extends PublisherBase<PublisherGCSConfig> {
16+
export default class PublisherGCS extends PublisherStatic<PublisherGCSConfig> {
1917
name = 'gcs';
2018

2119
private GCSKeySafe = (key: string) => {
@@ -67,14 +65,6 @@ export default class PublisherGCS extends PublisherBase<PublisherGCSConfig> {
6765
})
6866
);
6967
}
70-
71-
keyForArtifact(artifact: GCSArtifact): string {
72-
if (this.config.keyResolver) {
73-
return this.config.keyResolver(path.basename(artifact.path), artifact.platform, artifact.arch);
74-
}
75-
76-
return `${artifact.keyPrefix}/${artifact.platform}/${artifact.arch}/${path.basename(artifact.path)}`;
77-
}
7868
}
7969

8070
export { PublisherGCS, PublisherGCSConfig };

packages/publisher/s3/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"@aws-sdk/client-s3": "^3.28.0",
2020
"@aws-sdk/lib-storage": "^3.28.0",
2121
"@aws-sdk/types": "^3.25.0",
22-
"@electron-forge/publisher-base": "7.1.0",
22+
"@electron-forge/publisher-static": "7.1.0",
2323
"@electron-forge/shared-types": "7.1.0",
2424
"debug": "^4.3.1"
2525
},

packages/publisher/s3/src/PublisherS3.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from 'path';
44
import { S3Client } from '@aws-sdk/client-s3';
55
import { Progress, Upload } from '@aws-sdk/lib-storage';
66
import { Credentials } from '@aws-sdk/types';
7-
import { PublisherBase, PublisherOptions } from '@electron-forge/publisher-base';
7+
import { PublisherOptions, PublisherStatic } from '@electron-forge/publisher-static';
88
import debug from 'debug';
99

1010
import { PublisherS3Config } from './Config';
@@ -18,7 +18,7 @@ type S3Artifact = {
1818
arch: string;
1919
};
2020

21-
export default class PublisherS3 extends PublisherBase<PublisherS3Config> {
21+
export default class PublisherS3 extends PublisherStatic<PublisherS3Config> {
2222
name = 's3';
2323

2424
private s3KeySafe = (key: string) => {
@@ -84,14 +84,6 @@ export default class PublisherS3 extends PublisherBase<PublisherS3Config> {
8484
);
8585
}
8686

87-
keyForArtifact(artifact: S3Artifact): string {
88-
if (this.config.keyResolver) {
89-
return this.config.keyResolver(path.basename(artifact.path), artifact.platform, artifact.arch);
90-
}
91-
92-
return `${artifact.keyPrefix}/${artifact.platform}/${artifact.arch}/${path.basename(artifact.path)}`;
93-
}
94-
9587
generateCredentials(): Credentials | undefined {
9688
const accessKeyId = this.config.accessKeyId;
9789
const secretAccessKey = this.config.secretAccessKey;

0 commit comments

Comments
 (0)