UNPKG

bicep-assets

Version:
118 lines (117 loc) 5.71 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.DeployCommand = 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 configuration_1 = require("../configuration"); const manifest_1 = require("../utils/manifest"); class DeployCommand extends clipanion_1.Command { constructor() { super(...arguments); this.distFolder = clipanion_1.Option.String('--dist-folder', '.bicep-assets'); this.executeBuild = clipanion_1.Option.Boolean('--build', true); } execute() { return __awaiter(this, void 0, void 0, function* () { if (this.executeBuild) { yield 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); for (const [name, fileName] of Object.entries(manifestContent.assets)) { const stat = (0, fs_1.statSync)((0, path_1.join)(this.distFolder, fileName)); const targetFileName = stat.isDirectory() ? `${fileName}.zip` : fileName; yield this.uploadAsset(name, fileName, targetFileName, stat.isDirectory()); } }); } uploadAsset(name, fileName, targetFileName, compress) { return __awaiter(this, void 0, void 0, function* () { const configuration = yield configuration_1.Configuration.load(false); const creds = new identity_1.AzureCliCredential(); const blobServiceClient = new storage_blob_1.BlobServiceClient(`https://${configuration.storageAccountName}.blob.core.windows.net`, creds); const containerClient = blobServiceClient.getContainerClient('assets'); const tempFolder = yield (0, promises_1.mkdtemp)((0, path_1.join)((0, os_1.tmpdir)(), 'bicep-assets-')); try { console.log(`Publishing asset: ${fileName} (${name})`); if (compress) { yield (0, zip_lib_1.archiveFolder)((0, path_1.join)(this.distFolder, fileName), (0, path_1.join)(tempFolder, targetFileName)); const zipFile = yield (0, promises_1.readFile)((0, path_1.join)(tempFolder, targetFileName)); containerClient.uploadBlockBlob(targetFileName, zipFile, zipFile.length); } else { const content = yield (0, promises_1.readFile)((0, path_1.join)(this.distFolder, fileName)); containerClient.uploadBlockBlob(targetFileName, content, content.length); } } finally { yield (0, promises_1.rm)(tempFolder, { recursive: true, }); } }); } } exports.DeployCommand = DeployCommand; DeployCommand.paths = [ ['deploy'], ]; DeployCommand.usage = clipanion_1.Command.Usage({ description: 'Deploys the assets to the storage account', details: ` This command will deploy the assets in the dist folder to the storage account. `, });