Skip to content

Commit

Permalink
feat(deploy): must run hlx build (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
rofe authored Jul 8, 2019
1 parent 6e05a13 commit 3981455
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 58 deletions.
7 changes: 7 additions & 0 deletions src/deploy.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class DeployCommand extends AbstractCommand {
this._fastly_namespace = null;
this._fastly_auth = null;
this._target = null;
this._files = null;
this._prefix = null;
this._default = null;
this._enableDirty = false;
Expand Down Expand Up @@ -110,6 +111,11 @@ class DeployCommand extends AbstractCommand {
return this;
}

withFiles(value) {
this._files = value;
return this;
}

withDefault(value) {
this._default = value;
return this;
Expand Down Expand Up @@ -340,6 +346,7 @@ Alternatively you can auto-add one using the {grey --add <name>} option.`);
if (this._createPackages !== 'ignore') {
const pgkCommand = new PackageCommand(this.log)
.withTarget(this._target)
.withFiles(this._files)
.withDirectory(this.directory)
.withOnlyModified(this._createPackages === 'auto')
.withMinify(this._enableMinify);
Expand Down
3 changes: 3 additions & 0 deletions src/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

const yargsOpenwhisk = require('./yargs-openwhisk.js');
const yargsFastly = require('./yargs-fastly.js');
const yargsBuild = require('./yargs-build.js');
const { makeLogger } = require('./log-common.js');

module.exports = function deploy() {
Expand All @@ -28,6 +29,7 @@ module.exports = function deploy() {
builder: (yargs) => {
yargsOpenwhisk(yargs);
yargsFastly(yargs);
yargsBuild(yargs);
yargs
.option('auto', {
describe: 'Enable auto-deployment',
Expand Down Expand Up @@ -155,6 +157,7 @@ module.exports = function deploy() {
.withLogglyHost(argv.logglyHost)
.withLogglyAuth(argv.logglyAuth)
.withTarget(argv.target)
.withFiles(argv.files)
.withDefault(argv.default)
.withDryRun(argv.dryRun)
.withCircleciAuth(argv.circleciAuth)
Expand Down
13 changes: 13 additions & 0 deletions src/package.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const fs = require('fs-extra');
const ProgressBar = require('progress');
const archiver = require('archiver');
const AbstractCommand = require('./abstract.cmd.js');
const BuildCommand = require('./build.cmd.js');
const ActionBundler = require('./parcel/ActionBundler.js');
const { flattenDependencies } = require('./packager-utils.js');

Expand Down Expand Up @@ -43,6 +44,7 @@ class PackageCommand extends AbstractCommand {
constructor(logger) {
super(logger);
this._target = null;
this._files = null;
this._onlyModified = false;
this._enableMinify = false;
}
Expand All @@ -57,6 +59,11 @@ class PackageCommand extends AbstractCommand {
return this;
}

withFiles(value) {
this._files = value;
return this;
}

withOnlyModified(value) {
this._onlyModified = value;
return this;
Expand Down Expand Up @@ -193,6 +200,12 @@ class PackageCommand extends AbstractCommand {
async run() {
await this.init();

// always run build first to make sure scripts are up to date
await new BuildCommand(this.log)
.withFiles(this._files)
.withTargetDir(this._target)
.run();

// get the list of scripts from the info files
const infos = [...glob.sync(`${this._target}/**/*.info.json`)];
const scriptInfos = await Promise.all(infos.map(info => fs.readJSON(info)));
Expand Down
5 changes: 4 additions & 1 deletion src/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
'use strict';

const { makeLogger } = require('./log-common.js');
const yargsBuild = require('./yargs-build.js');

module.exports = function deploy() {
module.exports = function pack() {
let executor;

return {
Expand All @@ -24,6 +25,7 @@ module.exports = function deploy() {
command: 'package',
desc: 'Create Adobe I/O runtime packages',
builder: (yargs) => {
yargsBuild(yargs);
// eslint-disable-next-line global-require
yargs
.option('force', {
Expand Down Expand Up @@ -54,6 +56,7 @@ module.exports = function deploy() {

await executor
.withTarget(argv.target)
.withFiles(argv.files)
.withOnlyModified(!argv.force)
.withMinify(argv.minify)
.run();
Expand Down
2 changes: 1 addition & 1 deletion src/yargs-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = function commonArgs(yargs) {
type: 'string',
})
// allow for comma separated values
.coerce('files', value => value.reduce((acc, curr) => {
.coerce('files', value => (!Array.isArray(value) ? [value] : value).reduce((acc, curr) => {
acc.push(...curr.split(/\s*,\s*/));
return acc;
}, []));
Expand Down
3 changes: 3 additions & 0 deletions test/testDeployCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('hlx deploy', () => {
mockDeploy.withLogglyHost.returnsThis();
mockDeploy.withLogglyAuth.returnsThis();
mockDeploy.withTarget.returnsThis();
mockDeploy.withFiles.returnsThis();
mockDeploy.withDefault.returnsThis();
mockDeploy.withEnableDirty.returnsThis();
mockDeploy.withDryRun.returnsThis();
Expand Down Expand Up @@ -123,6 +124,7 @@ OpenWhisk Namespace is required`);
sinon.assert.calledWith(mockDeploy.withFastlyAuth, '');
sinon.assert.calledWith(mockDeploy.withFastlyNamespace, undefined);
sinon.assert.calledWith(mockDeploy.withTarget, '.hlx/build');
sinon.assert.calledWith(mockDeploy.withFiles, ['src/**/*.htl', 'src/**/*.js', 'src/**/*.jsx', 'cgi-bin/**/*.js']);
sinon.assert.calledWith(mockDeploy.withDefault, undefined);
sinon.assert.calledWith(mockDeploy.withCreatePackages, 'auto');
sinon.assert.calledWith(mockDeploy.withCircleciAuth, '');
Expand All @@ -148,6 +150,7 @@ OpenWhisk Namespace is required`);
sinon.assert.calledWith(mockDeploy.withFastlyAuth, 'foobar');
sinon.assert.calledWith(mockDeploy.withFastlyNamespace, '1234');
sinon.assert.calledWith(mockDeploy.withTarget, 'foo');
sinon.assert.calledWith(mockDeploy.withFiles, ['*.htl', '*.js']);
sinon.assert.calledWith(mockDeploy.withDefault, undefined);
sinon.assert.calledWith(mockDeploy.withCircleciAuth, 'foobar');
sinon.assert.calledWith(mockDeploy.withDryRun, true);
Expand Down
67 changes: 29 additions & 38 deletions test/testDeployCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const { setupMocha: setupPolly } = require('@pollyjs/core');
const { HelixConfig, Logger } = require('@adobe/helix-shared');
const { initGit, createTestRoot } = require('./utils.js');
const GitUtils = require('../src/git-utils');
const BuildCommand = require('../src/build.cmd.js');
const DeployCommand = require('../src/deploy.cmd.js');

const CI_TOKEN = 'nope';
Expand Down Expand Up @@ -347,18 +346,6 @@ describe('hlx deploy (Integration)', () => {
initGit(testRoot, '[email protected]:adobe/project-helix.io.git');
const logger = Logger.getTestLogger();

await new BuildCommand(logger)
.withDirectory(testRoot)
.withFiles([
path.resolve(testRoot, 'src/html.htl'),
path.resolve(testRoot, 'src/html.pre.js'),
path.resolve(testRoot, 'src/helper.js'),
path.resolve(testRoot, 'src/utils/another_helper.js'),
path.resolve(testRoot, 'src/third_helper.js'),
])
.withTargetDir(buildDir)
.run();

const cmd = await new DeployCommand(logger)
.withDirectory(testRoot)
.withWskHost('adobeioruntime.net')
Expand All @@ -368,6 +355,13 @@ describe('hlx deploy (Integration)', () => {
.withEnableDirty(false)
.withDryRun(true)
.withTarget(buildDir)
.withFiles([
path.resolve(testRoot, 'src/html.htl'),
path.resolve(testRoot, 'src/html.pre.js'),
path.resolve(testRoot, 'src/helper.js'),
path.resolve(testRoot, 'src/utils/another_helper.js'),
path.resolve(testRoot, 'src/third_helper.js'),
])
.withMinify(false)
.run();

Expand All @@ -386,19 +380,6 @@ describe('hlx deploy (Integration)', () => {
initGit(testRoot, '[email protected]:adobe/project-helix.io.git');
const logger = Logger.getTestLogger();

await new BuildCommand(logger)
.withDirectory(testRoot)
.withFiles([
path.resolve(testRoot, 'src/html.htl'),
path.resolve(testRoot, 'src/html.pre.js'),
path.resolve(testRoot, 'src/helper.js'),
path.resolve(testRoot, 'src/utils/another_helper.js'),
path.resolve(testRoot, 'src/third_helper.js'),
path.resolve(testRoot, 'cgi-bin/hello.js'),
])
.withTargetDir(buildDir)
.run();

const cmd = await new DeployCommand(logger)
.withDirectory(testRoot)
.withWskHost('adobeioruntime.net')
Expand All @@ -409,6 +390,14 @@ describe('hlx deploy (Integration)', () => {
.withDryRun(true)
.withMinify(false)
.withTarget(buildDir)
.withFiles([
path.resolve(testRoot, 'src/html.htl'),
path.resolve(testRoot, 'src/html.pre.js'),
path.resolve(testRoot, 'src/helper.js'),
path.resolve(testRoot, 'src/utils/another_helper.js'),
path.resolve(testRoot, 'src/third_helper.js'),
path.resolve(testRoot, 'cgi-bin/hello.js'),
])
.run();

const ref = await GitUtils.getCurrentRevision(testRoot);
Expand All @@ -429,18 +418,6 @@ describe('hlx deploy (Integration)', () => {
initGit(testRoot, '[email protected]:adobe/project-helix.io.git');
const logger = Logger.getTestLogger();

await new BuildCommand(logger)
.withDirectory(testRoot)
.withFiles([
path.resolve(testRoot, 'src/html.htl'),
path.resolve(testRoot, 'src/html.pre.js'),
path.resolve(testRoot, 'src/helper.js'),
path.resolve(testRoot, 'src/utils/another_helper.js'),
path.resolve(testRoot, 'src/third_helper.js'),
])
.withTargetDir(buildDir)
.run();

const ref = await GitUtils.getCurrentRevision(testRoot);

this.polly.server.any().on('beforeResponse', (req, res) => {
Expand Down Expand Up @@ -503,6 +480,13 @@ describe('hlx deploy (Integration)', () => {
.withEnableDirty(false)
.withDryRun(false)
.withTarget(buildDir)
.withFiles([
path.resolve(testRoot, 'src/html.htl'),
path.resolve(testRoot, 'src/html.pre.js'),
path.resolve(testRoot, 'src/helper.js'),
path.resolve(testRoot, 'src/utils/another_helper.js'),
path.resolve(testRoot, 'src/third_helper.js'),
])
.withMinify(false)
.withLogglyAuth('loggly-auth')
.withLogglyHost('loggly-host')
Expand Down Expand Up @@ -562,6 +546,13 @@ describe('hlx deploy (Integration)', () => {
.withDryRun(false)
.withMinify(false)
.withTarget(buildDir)
.withFiles([
path.resolve(testRoot, 'src/html.htl'),
path.resolve(testRoot, 'src/html.pre.js'),
path.resolve(testRoot, 'src/helper.js'),
path.resolve(testRoot, 'src/utils/another_helper.js'),
path.resolve(testRoot, 'src/third_helper.js'),
])
.run();
assert.fail('Expected deploy to fail.');
} catch (e) {
Expand Down
3 changes: 3 additions & 0 deletions test/testPackageCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('hlx package', () => {
clearHelixEnv();
mockPackage = sinon.createStubInstance(PackageCommand);
mockPackage.withTarget.returnsThis();
mockPackage.withFiles.returnsThis();
mockPackage.withOnlyModified.returnsThis();
mockPackage.withMinify.returnsThis();
mockPackage.run.returnsThis();
Expand All @@ -45,6 +46,7 @@ describe('hlx package', () => {

sinon.assert.calledWith(mockPackage.withOnlyModified, true);
sinon.assert.calledWith(mockPackage.withTarget, '.hlx/build');
sinon.assert.calledWith(mockPackage.withFiles, ['src/**/*.htl', 'src/**/*.js', 'src/**/*.jsx', 'cgi-bin/**/*.js']);
sinon.assert.calledWith(mockPackage.withMinify, false);
sinon.assert.calledOnce(mockPackage.run);
});
Expand All @@ -55,6 +57,7 @@ describe('hlx package', () => {
.withCommandExecutor('package', mockPackage)
.run(['package']);
sinon.assert.calledWith(mockPackage.withTarget, 'foo');
sinon.assert.calledWith(mockPackage.withFiles, ['*.htl', '*.js']);
sinon.assert.calledWith(mockPackage.withOnlyModified, false);
sinon.assert.calledWith(mockPackage.withMinify, true);
sinon.assert.calledOnce(mockPackage.run);
Expand Down
37 changes: 19 additions & 18 deletions test/testPackageCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ const assert = require('assert');
const fs = require('fs-extra');
const path = require('path');
const { Logger } = require('@adobe/helix-shared');
const { createTestRoot, assertZipEntries } = require('./utils.js');
const BuildCommand = require('../src/build.cmd.js');
const { createTestRoot, assertFile, assertZipEntries } = require('./utils.js');
const PackageCommand = require('../src/package.cmd.js');

describe('hlx package (Integration)', () => {
Expand All @@ -36,22 +35,18 @@ describe('hlx package (Integration)', () => {
});

it('package creates correct package', async () => {
await new BuildCommand()
const created = {};
const ignored = {};
await new PackageCommand()
.withDirectory(testRoot)
.withTarget(buildDir)
.withFiles([
'test/integration/src/html.htl',
'test/integration/src/html.pre.js',
'test/integration/src/helper.js',
'test/integration/src/utils/another_helper.js',
'test/integration/src/third_helper.js',
])
.withTargetDir(buildDir)
.run();

const created = {};
const ignored = {};
await new PackageCommand()
.withDirectory(testRoot)
.withTarget(buildDir)
.withOnlyModified(false)
.withMinify(false)
.on('create-package', (info) => {
Expand All @@ -62,6 +57,16 @@ describe('hlx package (Integration)', () => {
})
.run();

// verify build output
assertFile(path.resolve(buildDir, 'html.js'));
assertFile(path.resolve(buildDir, 'html.js.map'));
assertFile(path.resolve(buildDir, 'helper.js'));
assertFile(path.resolve(buildDir, 'helper.js.map'));
assertFile(path.resolve(buildDir, 'utils/another_helper.js'));
assertFile(path.resolve(buildDir, 'utils/another_helper.js.map'));
assertFile(path.resolve(buildDir, 'third_helper.js'));
assertFile(path.resolve(buildDir, 'third_helper.js.map'));

assert.deepEqual(created, {
html: true,
}, 'created packages');
Expand Down Expand Up @@ -100,16 +105,12 @@ describe('hlx package (Integration)', () => {

it('package reports bundling errors and warnings', async () => {
const logger = Logger.getTestLogger();
await new BuildCommand()
.withFiles([
'test/integration/src/broken_html.pre.js',
])
.withTargetDir(buildDir)
.run();

await new PackageCommand(logger)
.withDirectory(testRoot)
.withTarget(buildDir)
.withFiles([
'test/integration/src/broken_html.pre.js',
])
.withOnlyModified(true)
.withMinify(false)
.run();
Expand Down

0 comments on commit 3981455

Please sign in to comment.