Last active
October 10, 2017 00:35
-
-
Save todthomson/4e3c4af87d1248ff51aecfa81cbd5c4a to your computer and use it in GitHub Desktop.
npm run octopack -- --apikey=%octopus.api.key% --host=%octopus.url% --version=%app.build% (node octopack.js)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| (() => { | |
| console.log('Running octopack.js.'); | |
| const empty = require('fs-extra/lib/empty'); | |
| const filesize = require('filesize'); | |
| const fs = require('fs'); | |
| const minimist = require('minimist'); | |
| const octopackjs = require('@octopusdeploy/octopackjs'); | |
| const path = require('path'); | |
| const util = require('util'); | |
| const distDir = 'dist'; | |
| const outputDir = 'output'; | |
| const webDotConfig = 'Web.config'; | |
| const appendToPack = (methods, filePath, filePathInArchive) => { | |
| if (!filePathInArchive) { | |
| filePathInArchive = filePath; | |
| } | |
| console.log(util.format('Appending "%s" to archive at "%s".', filePath, filePathInArchive)); | |
| return methods.append(filePathInArchive, filePath); | |
| }; | |
| const outputArgumentExampleAndExit = (argument, exitCode) => { | |
| console.error('Argument --' + argument + ' must be provided.'); | |
| console.error('e.g. "npm run octopack -- --apikey=API-XXX --host=https://octopus.deploy --version=9.8.7.6".'); | |
| process.exit(exitCode); | |
| }; | |
| const args = minimist(process.argv.slice(2)); | |
| // noinspection JSUnresolvedVariable | |
| if (!args.apikey) { | |
| outputArgumentExampleAndExit('apikey', 1); | |
| } | |
| // noinspection JSUnresolvedVariable | |
| if (!args.host) { | |
| outputArgumentExampleAndExit('host', 2); | |
| } | |
| let options = {}; | |
| // noinspection JSUnresolvedVariable | |
| if (args.version) { | |
| // noinspection JSUnresolvedVariable | |
| options.version = args.version; | |
| } else { | |
| outputArgumentExampleAndExit('version', 3); | |
| } | |
| if (!fs.existsSync(distDir)) { | |
| const errorMessage = util.format('Unable to locate the "%s" directory.', distDir); | |
| console.error(errorMessage + ' Run "npm run build" or "npm run build-ci".'); | |
| process.exit(4); | |
| } else { | |
| console.log(util.format('Found the "%s" directory.', distDir)); | |
| if (fs.existsSync(outputDir)) { | |
| empty.emptyDirSync(outputDir); | |
| console.log(util.format('Emptied the "%s" directory.', outputDir)); | |
| } else { | |
| fs.mkdirSync(outputDir); | |
| console.log(util.format('Created the "%s" directory.', outputDir)); | |
| } | |
| let pack = octopackjs.pack('zip', options); | |
| appendToPack(pack, webDotConfig); | |
| fs.readdirSync(distDir).forEach(fileName => { | |
| const locationOnDisk = path.join(distDir, fileName); | |
| appendToPack(pack, locationOnDisk, fileName); | |
| }); | |
| pack.toFile(outputDir, (err, data) => { | |
| if (err) { | |
| console.error(err); | |
| process.exit(5); | |
| } else { | |
| console.log(util.format('Package saved to "%s".', data.path)); | |
| console.log(util.format('Package size is "%s".', filesize(data.size))); | |
| // noinspection SpellCheckingInspection | |
| // noinspection JSUnresolvedVariable | |
| octopackjs.push(data.path, { | |
| host: args.host, | |
| apikey: args.apikey, | |
| replace: true | |
| }, (err, body) => { | |
| if (err) { | |
| // noinspection JSUnresolvedVariable | |
| console.error(err.body.ErrorMessage); | |
| process.exit(6); | |
| } else { | |
| // noinspection JSUnresolvedVariable | |
| console.log(util.format('Successfully pushed package "%s.%s%s" to "%s".', | |
| body.PackageId, body.Version, body.FileExtension, args.host)); | |
| console.log(body); | |
| } | |
| }); | |
| } | |
| }); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment