1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const core_1 = require("@auto-it/core");
|
5 | const endent_1 = tslib_1.__importDefault(require("endent"));
|
6 | const file_type_1 = tslib_1.__importDefault(require("file-type"));
|
7 | const fs_1 = tslib_1.__importDefault(require("fs"));
|
8 | const fast_glob_1 = tslib_1.__importDefault(require("fast-glob"));
|
9 | const path_1 = tslib_1.__importDefault(require("path"));
|
10 | const util_1 = require("util");
|
11 | const t = tslib_1.__importStar(require("io-ts"));
|
12 | const stat = util_1.promisify(fs_1.default.stat);
|
13 | const readFile = util_1.promisify(fs_1.default.readFile);
|
14 | const pluginOptions = t.interface({
|
15 |
|
16 | assets: t.array(t.string),
|
17 | });
|
18 |
|
19 | const normalizeOptions = (options) => Array.isArray(options) ? { assets: options } : options;
|
20 |
|
21 | class UploadAssetsPlugin {
|
22 |
|
23 | constructor(options) {
|
24 |
|
25 | this.name = "upload-assets";
|
26 | this.options = normalizeOptions(options);
|
27 | }
|
28 |
|
29 | apply(auto) {
|
30 | auto.hooks.validateConfig.tapPromise(this.name, async (name, options) => {
|
31 | if (name === this.name || name === `@auto-it/${this.name}`) {
|
32 | return core_1.validatePluginConfiguration(this.name, pluginOptions, normalizeOptions(options));
|
33 | }
|
34 | });
|
35 | auto.hooks.afterRelease.tapPromise(this.name, async ({ response }) => {
|
36 | const assets = await fast_glob_1.default(this.options.assets);
|
37 | auto.logger.log.info(endent_1.default `
|
38 | Uploading:
|
39 |
|
40 | ${assets.map((asset) => `\t- ${asset}`).join("\n")}
|
41 |
|
42 | `);
|
43 | await Promise.all(assets.map(async (asset) => {
|
44 | if (!auto.git || !response) {
|
45 | return;
|
46 | }
|
47 | const file = await readFile(asset);
|
48 | const stats = await stat(asset);
|
49 | const type = await file_type_1.default.fromBuffer(file);
|
50 | const options = {
|
51 | data: file,
|
52 | name: path_1.default.basename(asset),
|
53 | owner: auto.git.options.owner,
|
54 | repo: auto.git.options.repo,
|
55 | headers: {
|
56 | "content-length": stats.size,
|
57 | "content-type": type ? type.mime : "application/octet-stream",
|
58 | },
|
59 | };
|
60 |
|
61 | if (Array.isArray(response)) {
|
62 | await Promise.all(response.map((r) => auto.git.github.repos.uploadReleaseAsset(Object.assign(Object.assign({}, options), { release_id: r.data.id }))));
|
63 | }
|
64 | else {
|
65 | await auto.git.github.repos.uploadReleaseAsset(Object.assign(Object.assign({}, options), { release_id: response.data.id }));
|
66 | }
|
67 | auto.logger.log.success(`Uploaded asset: ${asset}`);
|
68 | }));
|
69 | });
|
70 | }
|
71 | }
|
72 | exports.default = UploadAssetsPlugin;
|
73 |
|
\ | No newline at end of file |