Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
derekrushforth committed Jun 27, 2019
1 parent 9a6cfb5 commit bc57e61
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 52 deletions.
129 changes: 78 additions & 51 deletions src/commands/templates/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
TemplatePushReview,
TemplatePushArguments,
Templates,
MetaTraverse,
MetaFileTraverse,
MetaFile,
} from '../../types'
import { pluralize, log, validateToken } from '../../utils'
Expand Down Expand Up @@ -83,25 +83,19 @@ const push = (serverToken: string, args: TemplatePushArguments) => {
.then(response => {
compareTemplates(response, manifest)

// Show which templates are changing
spinner.stop()
printReview(review)

// Push templates
// Push templates if force arg is present
if (force) {
spinner.text = 'Pushing templates to Postmark...'
spinner.start()
return pushTemplates(spinner, client, manifest)
}

// User confirmation before pushing
prompt([
{
type: 'confirm',
name: 'confirm',
default: false,
message: `Would you like to continue?`,
},
]).then((answer: any) => {
// Ask for user confirmation
confirmation().then(answer => {
if (answer.confirm) {
spinner.text = 'Pushing templates to Postmark...'
spinner.start()
Expand All @@ -123,6 +117,23 @@ const push = (serverToken: string, args: TemplatePushArguments) => {
}
}

/**
* Ask user to confirm the push
*/
const confirmation = (): Promise<any> =>
new Promise<string>((resolve, reject) => {
prompt([
{
type: 'confirm',
name: 'confirm',
default: false,
message: `Would you like to continue?`,
},
])
.then((answer: any) => resolve(answer))
.catch((err: any) => reject(err))
})

/**
* Compare templates on server against local
*/
Expand Down Expand Up @@ -179,48 +190,64 @@ const layoutUsedLabel = (
}

/**
* Parses directory of templates or layouts
* Parses templates folder and files
*/
const createManifest = (path: string): TemplateManifest[] => {
let manifest: TemplateManifest[] = []

// Do not parse if directory does not exist
// Return empty array if path does not exist
if (!existsSync(path)) return manifest

// Find meta files and flatten into collection
const list: MetaTraverse[] = traverse(dirTree(path)).reduce((acc, file) => {
if (file.name === 'meta.json') acc.push(file)
return acc
}, [])
const list: MetaFileTraverse[] = FindMetaFiles(path)

// Parse each directory
list.forEach(file => {
const { path } = file // Path to meta file
const rootPath = dirname(path) // Folder path
const htmlPath = join(rootPath, 'content.html') // HTML path
const textPath = join(rootPath, 'content.txt') // Text path

// Check if meta file exists
if (existsSync(path)) {
const metaFile: MetaFile = readJsonSync(path)
const htmlFile: string = existsSync(htmlPath)
? readFileSync(htmlPath, 'utf-8')
: ''
const textFile: string = existsSync(textPath)
? readFileSync(textPath, 'utf-8')
: ''

manifest.push({
HtmlBody: htmlFile,
TextBody: textFile,
...metaFile,
})
}
const item = createManifestItem(file)
if (item) manifest.push(item)
})

return manifest
}

/**
* Gathers the template's content and metadata based on the metadata file location
*/
const createManifestItem = (file: any): MetaFile | null => {
const { path } = file // Path to meta file
const rootPath = dirname(path) // Folder path
const htmlPath = join(rootPath, 'content.html') // HTML path
const textPath = join(rootPath, 'content.txt') // Text path

// Check if meta file exists
if (existsSync(path)) {
const metaFile: MetaFile = readJsonSync(path)
const htmlFile: string = existsSync(htmlPath)
? readFileSync(htmlPath, 'utf-8')
: ''
const textFile: string = existsSync(textPath)
? readFileSync(textPath, 'utf-8')
: ''

return {
HtmlBody: htmlFile,
TextBody: textFile,
...metaFile,
}
}

return null
}

/**
* Searches for all metadata files and flattens into a collection
*/
const FindMetaFiles = (path: string): MetaFileTraverse[] =>
traverse(dirTree(path)).reduce((acc, file) => {
if (file.name === 'meta.json') acc.push(file)
return acc
}, [])

/**
* Show which templates will change after the publish
*/
Expand Down Expand Up @@ -277,9 +304,9 @@ const pushTemplates = (
client: any,
templates: TemplateManifest[]
) => {
templates.forEach(template => {
templates.forEach(template =>
pushTemplate(spinner, client, template, templates.length)
})
)
}

/**
Expand All @@ -290,26 +317,26 @@ const pushTemplate = (
client: any,
template: TemplateManifest,
total: number
) => {
): void => {
if (template.New) {
client
return client
.createTemplate(template)
.then((response: object) =>
pushComplete(true, response, template, spinner, total)
)
.catch((response: object) =>
pushComplete(false, response, template, spinner, total)
)
} else {
client
.editTemplate(template.Alias, template)
.then((response: object) =>
pushComplete(true, response, template, spinner, total)
)
.catch((response: object) =>
pushComplete(false, response, template, spinner, total)
)
}

return client
.editTemplate(template.Alias, template)
.then((response: object) =>
pushComplete(true, response, template, spinner, total)
)
.catch((response: object) =>
pushComplete(false, response, template, spinner, total)
)
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/types/Template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ export interface MetaFile {
TemplateType: string
Subject?: string
LayoutTemplate?: string | null
HtmlBody?: string
TextBody?: string
}

export interface MetaTraverse {
export interface MetaFileTraverse {
path: string
name: string
size: number
Expand Down

0 comments on commit bc57e61

Please sign in to comment.