Skip to content

[VEG-BUG] Fix bug in App package creation #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions packages/zcli-apps/src/lib/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,33 @@
describe('validatePkg', () => {
test
.stub(fs, 'pathExistsSync', () => true)
.stub(fs, 'readFile', () => Promise.resolve('file content'))
.stub(request, 'requestAPI', () => Promise.resolve({ status: 200 }))
.it('should return true if package is valid', async () => {
expect(await validatePkg('./app-path')).to.equal(true)
})

test
.stub(fs, 'pathExistsSync', () => true)
.stub(fs, 'readFile', () => Promise.resolve('file content'))
.stub(request, 'requestAPI', () => Promise.resolve({ status: 400, data: { description: 'invalid location' } }))
.do(async () => {
await validatePkg('./app-path')
.it('should throw if package has validation errors', async () => {
try {
await validatePkg('./app-path')
} catch (error: any) {

Check warning on line 23 in packages/zcli-apps/src/lib/package.test.ts

View workflow job for this annotation

GitHub Actions / build-and-check (ubuntu-latest, 18.x)

Unexpected any. Specify a different type

Check warning on line 23 in packages/zcli-apps/src/lib/package.test.ts

View workflow job for this annotation

GitHub Actions / build-and-check (macos-latest, 18.x)

Unexpected any. Specify a different type
expect(error.message).to.equal('invalid location')
}
})
.catch('invalid location')
.it('should throw if package has validation errors')

test
.stub(fs, 'pathExistsSync', () => false)
.do(async () => {
await validatePkg('./bad-path')
.stub(fs, 'readFile', () => Promise.reject())

Check failure on line 30 in packages/zcli-apps/src/lib/package.test.ts

View workflow job for this annotation

GitHub Actions / build-and-check (ubuntu-latest, 18.x)

Expected the Promise rejection reason to be an Error

Check failure on line 30 in packages/zcli-apps/src/lib/package.test.ts

View workflow job for this annotation

GitHub Actions / build-and-check (macos-latest, 18.x)

Expected the Promise rejection reason to be an Error
.it('should throw if app path is invalid', async () => {
try {
await validatePkg('./bad-path')
} catch (error: any) {

Check warning on line 34 in packages/zcli-apps/src/lib/package.test.ts

View workflow job for this annotation

GitHub Actions / build-and-check (ubuntu-latest, 18.x)

Unexpected any. Specify a different type

Check warning on line 34 in packages/zcli-apps/src/lib/package.test.ts

View workflow job for this annotation

GitHub Actions / build-and-check (macos-latest, 18.x)

Unexpected any. Specify a different type
expect(error.message).to.equal('Package not found at ./bad-path')
}
})
.catch('Package not found at ./bad-path')
.it('should throw if app path is invalid')
})
})
72 changes: 42 additions & 30 deletions packages/zcli-apps/src/lib/package.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,78 @@
import * as path from 'path'
import * as fs from 'fs-extra'
import * as FormData from 'form-data'
import { request } from '@zendesk/zcli-core'
import { CLIError } from '@oclif/core/lib/errors'
import * as archiver from 'archiver'
import { validateAppPath } from './appPath'
import * as FormData from 'form-data'

const getDateTimeFileName = () => (new Date()).toISOString().replace(/[^0-9]/g, '')

export const createAppPkg = async (
export const createAppPkg = (
relativeAppPath: string,
pkgDir = 'tmp'
) => {
const appPath = path.resolve(relativeAppPath)
validateAppPath(appPath)
): Promise<string> => {
return new Promise((resolve, reject) => {
const appPath = path.resolve(relativeAppPath)
validateAppPath(appPath)

const pkgName = `app-${getDateTimeFileName()}`
const pkgPath = `${appPath}/${pkgDir}/${pkgName}.zip`
const pkgName = `app-${getDateTimeFileName()}`
const pkgPath = `${appPath}/${pkgDir}/${pkgName}.zip`

await fs.ensureDir(`${appPath}/${pkgDir}`)
const output = fs.createWriteStream(pkgPath)
const archive = archiver('zip')
fs.ensureDirSync(`${appPath}/${pkgDir}`)
const output = fs.createWriteStream(pkgPath)

archive.pipe(output)
output.on('close', () => {
resolve(pkgPath)
})

let archiveIgnore = ['tmp/**']
output.on('error', (err) => {
reject(err)
})

if (fs.pathExistsSync(`${appPath}/.zcliignore`)) {
archiveIgnore = archiveIgnore.concat(fs.readFileSync(`${appPath}/.zcliignore`).toString().replace(/\r\n/g, '\n').split('\n').filter((item) => {
return (item.trim().startsWith('#') ? null : item.trim())
}))
}
const archive = archiver('zip')

archive.glob('**', {
cwd: appPath,
ignore: archiveIgnore
})
let archiveIgnore = ['tmp/**']

await archive.finalize()
if (fs.pathExistsSync(`${appPath}/.zcliignore`)) {
archiveIgnore = archiveIgnore.concat(fs.readFileSync(`${appPath}/.zcliignore`).toString().replace(/\r\n/g, '\n').split('\n').filter((item) => {
return (item.trim().startsWith('#') ? null : item.trim())
}))
}

if (!fs.pathExistsSync(pkgPath)) {
throw new CLIError(`Failed to create package at ${pkgPath}`)
}
archive.glob('**', {
cwd: appPath,
ignore: archiveIgnore
})

return pkgPath
archive.pipe(output)

archive.finalize()

return pkgPath
})
}

export const validatePkg = async (pkgPath: string) => {
if (!fs.pathExistsSync(pkgPath)) {
throw new CLIError(`Package not found at ${pkgPath}`)
}

const file = await fs.readFile(pkgPath)

const form = new FormData()
form.append('file', fs.createReadStream(pkgPath))
form.append('file', file, {
filename: path.basename(pkgPath)
})

const res = await request.requestAPI('api/v2/apps/validate', {
method: 'POST',
data: form
data: form.getBuffer(),
headers: form.getHeaders()
})

if (res.status !== 200) {
const { description } = await res.data
throw new CLIError(description)
throw new CLIError(res.data?.description)
}

return true
Expand Down
Loading