diff --git a/package-lock.json b/package-lock.json
index 62f9e2b..f655bde 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -25,6 +25,7 @@
"socket.io": "^2.4.1",
"table": "^6.8.0",
"traverse": "^0.6.6",
+ "ts-invariant": "^0.10.3",
"untildify": "^4.0.0",
"watch": "^0.13.0",
"yargonaut": "^1.1.4",
@@ -4472,6 +4473,22 @@
"resolved": "https://registry.npmjs.org/traverse/-/traverse-0.6.6.tgz",
"integrity": "sha1-y99WD9e5r2MlAv7UD5GMFX6pcTc="
},
+ "node_modules/ts-invariant": {
+ "version": "0.10.3",
+ "resolved": "https://registry.npmjs.org/ts-invariant/-/ts-invariant-0.10.3.tgz",
+ "integrity": "sha512-uivwYcQaxAucv1CzRp2n/QdYPo4ILf9VXgH19zEIjFx2EJufV16P0JtJVpYHy89DItG6Kwj2oIUjrcK5au+4tQ==",
+ "dependencies": {
+ "tslib": "^2.1.0"
+ },
+ "engines": {
+ "node": ">=8"
+ }
+ },
+ "node_modules/ts-invariant/node_modules/tslib": {
+ "version": "2.6.1",
+ "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.1.tgz",
+ "integrity": "sha512-t0hLfiEKfMUoqhG+U1oid7Pva4bbDPHYfJNiB7BiIjRkj1pyC++4N3huJfqY6aRH6VTB0rvtzQwjM4K6qpfOig=="
+ },
"node_modules/ts-node": {
"version": "8.0.3",
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-8.0.3.tgz",
@@ -8223,6 +8240,21 @@
"resolved": "https://registry.npmjs.org/traverse/-/traverse-0.6.6.tgz",
"integrity": "sha1-y99WD9e5r2MlAv7UD5GMFX6pcTc="
},
+ "ts-invariant": {
+ "version": "0.10.3",
+ "resolved": "https://registry.npmjs.org/ts-invariant/-/ts-invariant-0.10.3.tgz",
+ "integrity": "sha512-uivwYcQaxAucv1CzRp2n/QdYPo4ILf9VXgH19zEIjFx2EJufV16P0JtJVpYHy89DItG6Kwj2oIUjrcK5au+4tQ==",
+ "requires": {
+ "tslib": "^2.1.0"
+ },
+ "dependencies": {
+ "tslib": {
+ "version": "2.6.1",
+ "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.1.tgz",
+ "integrity": "sha512-t0hLfiEKfMUoqhG+U1oid7Pva4bbDPHYfJNiB7BiIjRkj1pyC++4N3huJfqY6aRH6VTB0rvtzQwjM4K6qpfOig=="
+ }
+ }
+ },
"ts-node": {
"version": "8.0.3",
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-8.0.3.tgz",
diff --git a/package.json b/package.json
index 2c23f37..7edf819 100644
--- a/package.json
+++ b/package.json
@@ -20,6 +20,7 @@
"socket.io": "^2.4.1",
"table": "^6.8.0",
"traverse": "^0.6.6",
+ "ts-invariant": "^0.10.3",
"untildify": "^4.0.0",
"watch": "^0.13.0",
"yargonaut": "^1.1.4",
diff --git a/src/commands/templates/preview.ts b/src/commands/templates/preview.ts
index 28ca8a8..4a4b800 100644
--- a/src/commands/templates/preview.ts
+++ b/src/commands/templates/preview.ts
@@ -1,16 +1,17 @@
import chalk from 'chalk'
-import { existsSync } from 'fs-extra'
-import { filter, find, replace, debounce } from 'lodash'
+import path from 'path'
import untildify from 'untildify'
import express from 'express'
-import { createMonitor } from 'watch'
import consolidate from 'consolidate'
+import { filter, find, replace, debounce } from 'lodash'
+import { createMonitor } from 'watch'
import { ServerClient } from 'postmark'
-import { createManifest } from './helpers'
-import { TemplatePreviewArguments } from '../../types'
import { TemplateValidationOptions } from 'postmark/dist/client/models'
-import { log, validateToken } from '../../utils'
-import path from 'path'
+
+import { fatalError, log, validateToken } from '../../utils'
+
+import { validatePushDirectory } from './push'
+import { createManifest } from './helpers'
const previewPath = path.join(__dirname, 'preview')
const templateLinks = ''
@@ -29,39 +30,29 @@ export const builder = {
alias: 'p',
},
}
-export const handler = (args: TemplatePreviewArguments) => exec(args)
-/**
- * Execute the command
- */
-const exec = (args: TemplatePreviewArguments) => {
- const { serverToken } = args
-
- return validateToken(serverToken).then(token => {
- validateDirectory(token, args)
- })
+interface TemplatePreviewArguments {
+ serverToken: string
+ templatesdirectory: string
+ port: number
}
+export async function handler(args: TemplatePreviewArguments): Promise {
+ const serverToken = await validateToken(args.serverToken)
-const validateDirectory = (
- serverToken: string,
- args: TemplatePreviewArguments
-) => {
- const { templatesdirectory } = args
- const rootPath: string = untildify(templatesdirectory)
-
- // Check if path exists
- if (!existsSync(rootPath)) {
- log('The provided path does not exist', { error: true })
- return process.exit(1)
+ try {
+ validatePushDirectory(args.templatesdirectory)
+ } catch (e) {
+ return fatalError(e)
}
return preview(serverToken, args)
}
+
/**
* Preview
*/
-const preview = (serverToken: string, args: TemplatePreviewArguments) => {
+async function preview(serverToken: string, args: TemplatePreviewArguments): Promise {
const { port, templatesdirectory } = args
log(`${title} Starting template preview server...`)
@@ -77,7 +68,7 @@ const preview = (serverToken: string, args: TemplatePreviewArguments) => {
// Static assets
app.use(express.static(`${previewPath}/assets`))
- const updateEvent = () => {
+ function updateEvent() {
// Generate new manifest
manifest = createManifest(templatesdirectory)
@@ -187,11 +178,9 @@ const preview = (serverToken: string, args: TemplatePreviewArguments) => {
log(divider)
})
- const validateTemplateRequest = (
- version: 'html' | 'text',
+ function validateTemplateRequest(version: 'html' | 'text',
payload: TemplateValidationOptions,
- res: express.Response
- ) => {
+ res: express.Response) {
const versionKey = version === 'html' ? 'HtmlBody' : 'TextBody'
// Make request to Postmark
@@ -199,8 +188,7 @@ const preview = (serverToken: string, args: TemplatePreviewArguments) => {
.validateTemplate(payload)
.then(result => {
if (result[versionKey].ContentIsValid) {
- const renderedContent =
- result[versionKey].RenderedContent + templateLinks
+ const renderedContent = result[versionKey].RenderedContent + templateLinks
io.emit('subject', { ...result.Subject, rawSubject: payload.Subject })
// Render raw source if HTML
@@ -220,8 +208,9 @@ const preview = (serverToken: string, args: TemplatePreviewArguments) => {
}
}
-const combineTemplate = (layout: string, template: string): string =>
- replace(layout, /({{{)(.?@content.?)(}}})/g, template)
+function combineTemplate(layout: string, template: string): string {
+ return replace(layout, /({{{)(.?@content.?)(}}})/g, template)
+}
/* Console helpers */
@@ -230,7 +219,7 @@ const divider = chalk.gray('-'.repeat(34))
/* Render Templates */
-const getSource = (version: 'html' | 'text', template: any, layout?: any) => {
+function getSource(version: 'html' | 'text', template: any, layout?: any) {
const versionKey = version === 'html' ? 'HtmlBody' : 'TextBody'
if (layout) return combineTemplate(layout[versionKey], template[versionKey])
@@ -238,28 +227,31 @@ const getSource = (version: 'html' | 'text', template: any, layout?: any) => {
return template[versionKey]
}
-const renderTemplateText = (res: express.Response, body: string) =>
- consolidate.ejs(`${previewPath}/templateText.ejs`, { body }, (err, html) =>
- renderTemplateContents(res, err, html)
+function renderTemplateText(res: express.Response, body: string) {
+ return consolidate.ejs(
+ `${previewPath}/templateText.ejs`,
+ { body },
+ (err, html) => renderTemplateContents(res, err, html)
)
+}
-const renderTemplateInvalid = (res: express.Response, errors: any) =>
- consolidate.ejs(
+function renderTemplateInvalid(res: express.Response, errors: any) {
+ return consolidate.ejs(
`${previewPath}/templateInvalid.ejs`,
{ errors },
(err, html) => renderTemplateContents(res, err, html)
)
+}
-const renderTemplate404 = (res: express.Response, version: string) =>
- consolidate.ejs(`${previewPath}/template404.ejs`, { version }, (err, html) =>
- renderTemplateContents(res, err, html)
+function renderTemplate404(res: express.Response, version: string) {
+ return consolidate.ejs(
+ `${previewPath}/template404.ejs`,
+ { version },
+ (err, html) => renderTemplateContents(res, err, html)
)
+}
-const renderTemplateContents = (
- res: express.Response,
- err: any,
- html: string
-) => {
+function renderTemplateContents(res: express.Response, err: any, html: string) {
if (err) return res.send(err)
return res.send(html)
diff --git a/src/commands/templates/pull.ts b/src/commands/templates/pull.ts
index 836fcd7..c8d6cf3 100644
--- a/src/commands/templates/pull.ts
+++ b/src/commands/templates/pull.ts
@@ -1,18 +1,14 @@
+import ora from 'ora'
+import untildify from 'untildify'
+import invariant from 'ts-invariant'
import { join } from 'path'
import { outputFileSync, existsSync, ensureDirSync } from 'fs-extra'
import { prompt } from 'inquirer'
-import ora from 'ora'
-import untildify from 'untildify'
import { ServerClient } from 'postmark'
+import type { Template, Templates } from 'postmark/dist/client/models'
-import {
- ProcessTemplatesOptions,
- Template,
- TemplateListOptions,
- TemplatePullArguments,
- MetaFile,
-} from '../../types'
-import { log, validateToken, pluralize } from '../../utils'
+import { MetaFile } from '../../types'
+import { log, validateToken, pluralize, logError, fatalError } from '../../utils'
export const command = 'pull