Skip to content
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

[VEG-2395] axios fetch adapter #253

Merged
merged 30 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
dced827
fix create part 1
anushkafka Aug 26, 2024
af15f40
fix create part 2
anushkafka Aug 26, 2024
746775e
fix update
anushkafka Aug 26, 2024
535f22f
update
anushkafka Aug 26, 2024
1468f51
working apps package tests
anushkafka Aug 26, 2024
8b2a1d6
working apps validate func tests
anushkafka Aug 26, 2024
bd824e7
Working themes delete tests
anushkafka Aug 27, 2024
959fe54
working import tests
anushkafka Aug 27, 2024
b3cac14
working themes list tests
anushkafka Aug 27, 2024
9993eaa
working themes publish tests
anushkafka Aug 27, 2024
cddd8ce
fix themes preview func tests
anushkafka Aug 27, 2024
7a01ce5
working request unit tests
anushkafka Aug 27, 2024
c07dc9e
restore package.json
anushkafka Aug 28, 2024
087e0cb
Fix lint
anushkafka Aug 28, 2024
9741a0c
wip
anushkafka Aug 28, 2024
eacda19
loginInteractively test fix wip
anushkafka Aug 28, 2024
8698cd8
auth tests wip
anushkafka Aug 29, 2024
904f5dd
working auth tests
anushkafka Aug 29, 2024
3980774
Restore package.json original test scripts
anushkafka Aug 29, 2024
3eb9e72
Remove nock from the main package
anushkafka Aug 29, 2024
4c895f3
Update form data
anushkafka Sep 3, 2024
7fb536d
Fix linting
anushkafka Sep 4, 2024
9c8d179
Tidy up test file
anushkafka Sep 4, 2024
2586471
Update tests after rebase
anushkafka Sep 4, 2024
cf3d908
Themes change wip
anushkafka Sep 4, 2024
f2a6f18
Working themes code
anushkafka Sep 4, 2024
5377c27
Clean up code
anushkafka Sep 4, 2024
48d5ea1
Adjust themes tests
anushkafka Sep 4, 2024
b67950b
Fix linting
anushkafka Sep 4, 2024
6ece334
Clean up
anushkafka Sep 5, 2024
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"lerna": "^5.1.8",
"lerna-changelog": "^2.2.0",
"mocha": "^10.0.0",
"nock": "^13.2.8",
"nyc": "^15.1.0",
"rimraf": "^3.0.2",
"standard": "^17.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/zcli-apps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"dependencies": {
"adm-zip": "0.5.10",
"archiver": "^5.3.1",
"axios": "^0.27.2",
"axios": "^1.7.5",
"chalk": "^4.1.2",
"cors": "^2.8.5",
"express": "^4.17.1",
Expand Down
4 changes: 3 additions & 1 deletion packages/zcli-apps/src/commands/apps/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
CliUx.ux.action.stop('Uploaded')
CliUx.ux.action.start('Deploying app')
const { job_id } = await deployApp('POST', 'api/apps.json', upload_id, manifest.name)
console.error('job_id', job_id)
EmilyBie marked this conversation as resolved.
Show resolved Hide resolved

try {
const { app_id }: any = await getUploadJobStatus(job_id, appPath)

Check warning on line 48 in packages/zcli-apps/src/commands/apps/create.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 48 in packages/zcli-apps/src/commands/apps/create.ts

View workflow job for this annotation

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

Unexpected any. Specify a different type
CliUx.ux.action.stop('Deployed')

const allConfigs = getAllConfigs(appPath)
Expand All @@ -61,7 +62,8 @@
this.log(chalk.green(`Successfully installed app: ${manifest.name} with app_id: ${app_id}`))
} catch (error) {
CliUx.ux.action.stop('Failed')
this.error(chalk.red(error))
// this.error(chalk.red(error))
console.log(error)
}
}
}
Expand Down
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(new Error('Package not found at ./bad-path')))
.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
15 changes: 11 additions & 4 deletions packages/zcli-apps/src/utils/createApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ import { getManifestFile } from '../utils/manifest'
import { request } from '@zendesk/zcli-core'
import { CliUx } from '@oclif/core'
import * as chalk from 'chalk'
import * as path from 'path'

export const getManifestAppName = (appPath: string): string | undefined => {
return getManifestFile(appPath).name
}

export const uploadAppPkg = async (pkgPath: string): Promise<any> => {
const pkgBuffer = await fs.readFile(pkgPath)

const formData = new FormData()
const pkgBuffer = fs.createReadStream(pkgPath)
formData.append('uploaded_data', pkgBuffer)
formData.append('uploaded_data', pkgBuffer, {
filename: path.basename(pkgPath),
contentType: 'application/zip'
})

const response = await request.requestAPI('api/v2/apps/uploads.json', {
data: formData,
method: 'POST'
method: 'POST',
data: formData.getBuffer(),
headers: formData.getHeaders()
})

// clean up
Expand Down
Loading
Loading