UNPKG

bicep-assets

Version:
109 lines (108 loc) 4.96 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UploadCommand = void 0; const identity_1 = require("@azure/identity"); const storage_blob_1 = require("@azure/storage-blob"); const clipanion_1 = require("clipanion"); const promises_1 = require("fs/promises"); const fs_1 = require("fs"); const os_1 = require("os"); const path_1 = require("path"); const t = __importStar(require("typanion")); const zip_lib_1 = require("zip-lib"); const manifest_1 = require("../utils/manifest"); class UploadCommand extends clipanion_1.Command { constructor() { super(...arguments); this.distFolder = clipanion_1.Option.String('--dist-folder', '.bicep-assets'); this.asset = clipanion_1.Option.String('-a', { required: true, }); this.targetContainer = clipanion_1.Option.String('-c', { required: true, }); this.targetBlob = clipanion_1.Option.String('-b', { required: true, }); this.targetAccount = clipanion_1.Option.String('-s', { required: true, }); } execute() { return __awaiter(this, void 0, void 0, function* () { this.cli.run(['build']); const manifestFile = (0, path_1.join)(this.distFolder, 'manifest.json'); if (!(0, fs_1.existsSync)(manifestFile)) { throw new Error(`No asset file found in dit folder: '${this.distFolder}'`); } const manifestContent = JSON.parse(yield (0, promises_1.readFile)(manifestFile, 'utf-8')); t.assert(manifestContent, manifest_1.isManifest); const blobServiceClient = new storage_blob_1.BlobServiceClient(`https://${this.targetAccount}.blob.core.windows.net`, new identity_1.AzureCliCredential()); const client = blobServiceClient.getContainerClient(this.targetContainer); yield client.createIfNotExists(); const assetHash = manifestContent.assets[this.asset]; if (!assetHash) { throw new Error('Invalid upload asset'); } const tempFolder = yield (0, promises_1.mkdtemp)((0, os_1.tmpdir)()); try { console.log(`Publishing asset: ${assetHash} (${this.asset})`); const fileName = this.targetBlob; yield (0, zip_lib_1.archiveFolder)((0, path_1.join)(this.distFolder, assetHash), (0, path_1.join)(tempFolder, fileName)); const content = yield (0, promises_1.readFile)((0, path_1.join)(tempFolder, fileName)); yield client.uploadBlockBlob(fileName, content, content.length); } finally { yield (0, promises_1.rm)(tempFolder, { recursive: true, }); } }); } } exports.UploadCommand = UploadCommand; UploadCommand.paths = [ ['upload'], ];