UNPKG

3.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const core_1 = require("@auto-it/core");
5const endent_1 = tslib_1.__importDefault(require("endent"));
6const file_type_1 = tslib_1.__importDefault(require("file-type"));
7const fs_1 = tslib_1.__importDefault(require("fs"));
8const fast_glob_1 = tslib_1.__importDefault(require("fast-glob"));
9const path_1 = tslib_1.__importDefault(require("path"));
10const util_1 = require("util");
11const t = tslib_1.__importStar(require("io-ts"));
12const stat = util_1.promisify(fs_1.default.stat);
13const readFile = util_1.promisify(fs_1.default.readFile);
14const pluginOptions = t.interface({
15 /** Paths to assets to upload */
16 assets: t.array(t.string),
17});
18/** Convert shorthand options to noraml shape */
19const normalizeOptions = (options) => Array.isArray(options) ? { assets: options } : options;
20/** Attach extra assets to a GitHub Release */
21class UploadAssetsPlugin {
22 /** Initialize the plugin with it's options */
23 constructor(options) {
24 /** The name of the plugin */
25 this.name = "upload-assets";
26 this.options = normalizeOptions(options);
27 }
28 /** Tap into auto plugin points. */
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 // Multiple releases were made
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}
72exports.default = UploadAssetsPlugin;
73//# sourceMappingURL=index.js.map
\No newline at end of file