Skip to content

Commit

Permalink
fix: show more info on required options and env errors (#39)
Browse files Browse the repository at this point in the history
* refactor: explicitly callout required config and env

* cr: remove duplication

* refactor: add more information to env error message
  • Loading branch information
iamogbz committed Jan 15, 2020
1 parent 23ff702 commit 41310a6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
24 changes: 24 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const defaultOptions = {
artifactsDir: './artifacts',
channel: 'unlisted',
manifestPath: 'manifest.json',
sourceDir: 'dist',
}

const requiredOptions = {
extensionId:
'Omitting this would create a new extension instead of a new version.',
targetXpi:
'Omitting this would leave the xpi file unnamed when it is returned from mozilla.',
}

const requiredEnvs = {
FIREFOX_API_KEY: 'Firefox api key used in webext sign command.',
FIREFOX_SECRET_KEY: 'Firefox api secret used in webext signing.',
}

module.exports = {
defaultOptions,
requiredEnvs,
requiredOptions,
}
4 changes: 2 additions & 2 deletions src/prepare.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const fs = require('fs')
const path = require('path')

const { verifyConfig } = require('./utils')
const { verifyOptions } = require('./utils')

const prepare = (options, { nextRelease, logger }) => {
const { sourceDir, manifestPath } = verifyConfig(options)
const { sourceDir, manifestPath } = verifyOptions(options)

const version = nextRelease.version
const normalizedManifestPath = path.join(sourceDir, manifestPath)
Expand Down
4 changes: 2 additions & 2 deletions src/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path')

const webExt = require('web-ext').default

const { verifyConfig } = require('./utils')
const { verifyOptions } = require('./utils')

const publish = async options => {
// This will create an unsigned xpi from sourceDir folder (dist) it will
Expand All @@ -19,7 +19,7 @@ const publish = async options => {
channel,
sourceDir,
targetXpi,
} = verifyConfig(options, ['extensionId', 'targetXpi'])
} = verifyOptions(options, ['extensionId', 'targetXpi'])

const { FIREFOX_API_KEY, FIREFOX_SECRET_KEY } = process.env
const { success, downloadedFiles } = await webExt.cmd.sign({
Expand Down
13 changes: 4 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
const AggregateError = require('aggregate-error')

const { defaultOptions } = require('./constants')

const maybeThrowErrors = errors => {
if (errors.length > 0) {
throw new AggregateError(errors)
}
}

const defaultOptions = {
artifactsDir: './artifacts',
channel: 'unlisted',
manifestPath: 'manifest.json',
sourceDir: 'dist',
}

const verifyConfig = (options, required = []) => {
const verifyOptions = (options, required = []) => {
const errors = []
const mergedOptions = { ...defaultOptions, ...options }
required.forEach(prop => {
Expand All @@ -27,5 +22,5 @@ const verifyConfig = (options, required = []) => {

module.exports = {
maybeThrowErrors,
verifyConfig,
verifyOptions,
}
44 changes: 22 additions & 22 deletions src/verifyConditions.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
const fs = require('fs')
const path = require('path')

const { maybeThrowErrors, verifyConfig } = require('./utils')
const { requiredEnvs, requiredOptions } = require('./constants')
const { maybeThrowErrors, verifyOptions } = require('./utils')

const verifyConditions = options => {
const verified = verifyConfig(options)
const { extensionId, sourceDir, manifestPath, targetXpi } = verified
const { FIREFOX_API_KEY, FIREFOX_SECRET_KEY } = process.env

const verified = verifyOptions(options)
const { manifestPath, sourceDir } = verified
const errors = []
if (!FIREFOX_API_KEY) {
errors.push('FIREFOX_API_KEY is missing from the environment')
}
if (!FIREFOX_SECRET_KEY) {
errors.push('FIREFOX_SECRET_KEY is missing from the environment')
}
if (!extensionId) {
errors.push(
'No extensionId was specified in package.json, this would create a new extension instead of a new version.',
)
}
if (!targetXpi) {
errors.push(
'No targetXpi was specified in package.json, this would leave the xpi file unnamed when it is returned from mozilla.',
)
}

Object.keys(requiredEnvs).forEach(envVarName => {
if (!process.env[envVarName]) {
errors.push(
`${envVarName} is missing from the environment. ${requiredEnvs[envVarName]}`,
)
}
})

Object.keys(requiredOptions).forEach(option => {
if (!verified[option]) {
errors.push(
`No ${option} was specified in package.json. ${requiredOptions[option]}`,
)
}
})

const manifestExists = fs.existsSync(path.join(sourceDir, manifestPath))
if (!manifestExists) {
errors.push(
`${manifestPath} was not found in ${sourceDir}, dist folder needs to exist to run`,
`${manifestPath} was not found in ${sourceDir}, path does not exist.`,
)
}
maybeThrowErrors(errors)
Expand Down

0 comments on commit 41310a6

Please sign in to comment.