From 398145521367eb5b537b106c1cddd68042ea000d Mon Sep 17 00:00:00 2001 From: Raphael Wegmueller Date: Mon, 8 Jul 2019 09:00:56 +0200 Subject: [PATCH] feat(deploy): must run hlx build (#241) --- src/deploy.cmd.js | 7 +++++ src/deploy.js | 3 ++ src/package.cmd.js | 13 ++++++++ src/package.js | 5 +++- src/yargs-build.js | 2 +- test/testDeployCli.js | 3 ++ test/testDeployCmd.js | 67 ++++++++++++++++++------------------------ test/testPackageCli.js | 3 ++ test/testPackageCmd.js | 37 +++++++++++------------ 9 files changed, 82 insertions(+), 58 deletions(-) diff --git a/src/deploy.cmd.js b/src/deploy.cmd.js index e98cc76e4..35b46925f 100644 --- a/src/deploy.cmd.js +++ b/src/deploy.cmd.js @@ -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; @@ -110,6 +111,11 @@ class DeployCommand extends AbstractCommand { return this; } + withFiles(value) { + this._files = value; + return this; + } + withDefault(value) { this._default = value; return this; @@ -340,6 +346,7 @@ Alternatively you can auto-add one using the {grey --add } 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); diff --git a/src/deploy.js b/src/deploy.js index 153399e0c..7e970dba8 100644 --- a/src/deploy.js +++ b/src/deploy.js @@ -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() { @@ -28,6 +29,7 @@ module.exports = function deploy() { builder: (yargs) => { yargsOpenwhisk(yargs); yargsFastly(yargs); + yargsBuild(yargs); yargs .option('auto', { describe: 'Enable auto-deployment', @@ -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) diff --git a/src/package.cmd.js b/src/package.cmd.js index 8063a755f..0d32100a3 100644 --- a/src/package.cmd.js +++ b/src/package.cmd.js @@ -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'); @@ -43,6 +44,7 @@ class PackageCommand extends AbstractCommand { constructor(logger) { super(logger); this._target = null; + this._files = null; this._onlyModified = false; this._enableMinify = false; } @@ -57,6 +59,11 @@ class PackageCommand extends AbstractCommand { return this; } + withFiles(value) { + this._files = value; + return this; + } + withOnlyModified(value) { this._onlyModified = value; return this; @@ -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))); diff --git a/src/package.js b/src/package.js index ebc6bdca9..887498099 100644 --- a/src/package.js +++ b/src/package.js @@ -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 { @@ -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', { @@ -54,6 +56,7 @@ module.exports = function deploy() { await executor .withTarget(argv.target) + .withFiles(argv.files) .withOnlyModified(!argv.force) .withMinify(argv.minify) .run(); diff --git a/src/yargs-build.js b/src/yargs-build.js index c2d0c88e5..4977673f6 100644 --- a/src/yargs-build.js +++ b/src/yargs-build.js @@ -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; }, [])); diff --git a/test/testDeployCli.js b/test/testDeployCli.js index 4c849da16..f9430867b 100644 --- a/test/testDeployCli.js +++ b/test/testDeployCli.js @@ -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(); @@ -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, ''); @@ -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); diff --git a/test/testDeployCmd.js b/test/testDeployCmd.js index 023d00b51..f5b88e257 100644 --- a/test/testDeployCmd.js +++ b/test/testDeployCmd.js @@ -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'; @@ -347,18 +346,6 @@ describe('hlx deploy (Integration)', () => { initGit(testRoot, 'git@github.com: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') @@ -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(); @@ -386,19 +380,6 @@ describe('hlx deploy (Integration)', () => { initGit(testRoot, 'git@github.com: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') @@ -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); @@ -429,18 +418,6 @@ describe('hlx deploy (Integration)', () => { initGit(testRoot, 'git@github.com: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) => { @@ -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') @@ -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) { diff --git a/test/testPackageCli.js b/test/testPackageCli.js index a0de024b2..62f845d77 100644 --- a/test/testPackageCli.js +++ b/test/testPackageCli.js @@ -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(); @@ -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); }); @@ -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); diff --git a/test/testPackageCmd.js b/test/testPackageCmd.js index d0fb9186b..a843ff841 100644 --- a/test/testPackageCmd.js +++ b/test/testPackageCmd.js @@ -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)', () => { @@ -36,7 +35,11 @@ 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', @@ -44,14 +47,6 @@ describe('hlx package (Integration)', () => { '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) => { @@ -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'); @@ -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();