oaeproject/Hilary

View on GitHub
bin/package

Summary

Maintainability
Test Coverage
#!/usr/bin/env node

var path = require('path');
var util = require('util');

var CoreUtil = require('oae-release-tools').CoreUtil;
var PackageUtil = require('oae-release-tools').PackageUtil;


//////////////////////
// HANDLE ARGUMENTS //
//////////////////////

var argv = require('optimist')
    .usage('Usage: $0 [-s] [-u] [-o <output dir>] [-v <force hilary version>]')

    .alias('h', 'help')
    .describe('h', 'Show this help information')

    .demand('o')
    .alias('o', 'output')
    .describe('o', 'The target output directory to copy the release artifacts')
    .default('o', 'dist')

    .alias('v', 'version')
    .describe('v', 'The version of hilary that we are packaging. If not specified, it will be derived from the hilary version in package.json and git describe')

    .alias('s', 'skip-tests')
    .describe('s', 'Skip the unit tests in the packaging process')

    .alias('u', 'upload')
    .describe('u', 'Upload the package and checksum to Amazon S3')

    .describe('upload-bucket', 'The Amazon S3 bucket to which to upload the packaged release files')
    .default('upload-bucket', 'oae-releases-travis')

    .describe('upload-region', 'The Amazon S3 region to which to upload the packaged release files')
    .default('upload-region', 'eu-west-1')

    .argv;

// Display the help if requested
if (argv.h) {
    require('optimist').showHelp();
    return process.exit(0);
}


///////////////////////
// PERFORM PACKAGING //
///////////////////////

// Gather system information so we know what system we built on
var systemInfo = CoreUtil.getSystemInfo();
var packageJsonPath = path.resolve('package.json');

// Load and validate the package.json
var packageJson = CoreUtil.loadPackageJson(packageJsonPath, 'Hilary', 4);

if (!argv.s) {
    CoreUtil.runUnitTests(6);
} else {
    CoreUtil.logWarn('Skipping unit tests because of -s parameter');
}

// Find the Hilary version. If not specified explicitly in the command with a -v parameter, it will be derived
// using a combination of the package.json version and the closest matching tag using git describe
var hilaryVersion = argv.v || CoreUtil.gitVersion(packageJson.version, 7);

CoreUtil.logSuccess('Resolved hilary version to be '.text + hilaryVersion.white);

// Copy the release files to the source directory
var srcDir = PackageUtil.copyHilaryReleaseFiles(argv.o, 8).srcDir;

PackageUtil.saveBuildInfo(srcDir, hilaryVersion, systemInfo, 9);

// Package the source directory into a tarball
var packageFilename = util.format('hilary-%s_node-%s', hilaryVersion, systemInfo.nodeVersion);
var packageResult = PackageUtil.packageRelease(srcDir, argv.o, packageFilename, 10);
var checksumResult = PackageUtil.checksumPackage(packageResult.packagePath, 11);

if (argv.u) {
    CoreUtil.exec(util.format('bin/upload -b "%s" -p "%s" -c "%s" -r "%s"', argv['upload-bucket'], packageResult.packagePath, checksumResult.checksumPath, argv['upload-region']), 'Failed to upload release artifacts to Amazon S3', 12, true);
}