Skip to content

Commit

Permalink
Merge pull request #504 from nyaggah/feat/publish-update
Browse files Browse the repository at this point in the history
feat(publish): update uploadToEdge / getEdgeAccessToken funcs
  • Loading branch information
JoeyDoey authored Jul 30, 2024
2 parents c541cd6 + 4dcdc9e commit a23298a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-insects-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bedframe/cli': patch
---

update uploadToEdge / getEdgeAccessToken funcs
77 changes: 42 additions & 35 deletions packages/cli/src/commands/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ function uploadToFirefox(config: FirefoxUploadConfig) {
const zipName = `${process.env.PACKAGE_NAME}@${process.env.PACKAGE_VERSION}-firefox.zip`
const zipPath = resolve(artifactsDir, zipName)

console.log(`
${lightMagenta('F I R E F O X :')}
└ • name: ${lightGreen(`${process.env.PACKAGE_NAME}`)}
• version: ${lightCyan(`${process.env.PACKAGE_VERSION}`)}
• zip: ${lightYellow(`${basename(zipPath)}`)}
• date/time: ${new Date().toLocaleString().replace(',', '')}
`)

const signCmd = `npx web-ext sign \
--source-dir ${sourceDir} \
--artifacts-dir ${artifactsDir} \
Expand All @@ -131,13 +139,6 @@ function uploadToFirefox(config: FirefoxUploadConfig) {
)
} else {
console.log('uploaded successfully.')
console.log(`
${lightMagenta('F I R E F O X :')}
└ • name: ${lightGreen(`${process.env.PACKAGE_NAME}`)}
• version: ${lightCyan(`${process.env.PACKAGE_VERSION}`)}
• zip: ${lightYellow(`${basename(zipPath)}`)}
• date/time: ${new Date().toLocaleString().replace(',', '')}
`)
}
} catch (error) {
console.log(
Expand All @@ -163,36 +164,42 @@ function uploadToFirefox(config: FirefoxUploadConfig) {
* -d "client_secret={$Client_Secret}" \
* -d "grant_type=client_credentials" \
* -v \
* <ACCESS_TOKEN_URL> e.g. https://login.microsoftonline.com/<$GUID>/oauth2/v2.0/token
* <ACCESS_TOKEN_URL> e.g. https://login.microsoftonline.com/{$TENANT_ID}/oauth2/v2.0/token
* ```
* FULL access token URL. find at: https://partner.microsoft.com/en-us/dashboard/microsoftedge/publishapi
*
* @param {EdgeUploadConfig} config
* @return {*} {Promise<string>}
*/
async function getEdgeAccessToken(config: EdgeUploadConfig): Promise<string> {
// const tokenUrl = `https://login.microsoftonline.com/${config.productId}/oauth2/v2.0/token`
const tokenUrl = `${config.accessTokenUrl}`
try {
const response = await fetch(config.accessTokenUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
client_id: config.clientId,
scope: 'https://api.addons.microsoftedge.microsoft.com/.default',
client_secret: config.clientSecret,
grant_type: 'client_credentials',
}),
})

const response = await fetch(tokenUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
client_id: config.clientId,
scope: 'https://api.addons.microsoftedge.microsoft.com/.default',
client_secret: config.clientSecret,
grant_type: 'client_credentials',
}),
})
if (!response.ok) {
throw new Error(`failed to get Edge access token: ${response.statusText}`)
}

if (!response.ok) {
throw new Error(`failed to get Edge access token: ${response.statusText}`)
try {
const responseBody: any = await response.json()
return responseBody.access_token
} catch (jsonError: any) {
throw new Error(`Error parsing JSON response: ${jsonError.message}`)
}
} catch (error) {
console.error('Error in getEdgeAccessToken:', error)
throw error // Re-throw the error after logging it
}

const responseBody = (await response.json()) as { access_token: string }
return responseBody.access_token
}

/**
Expand All @@ -214,6 +221,14 @@ async function uploadToEdge(config: EdgeUploadConfig, source: string) {
}-edge.zip`
const zipPath = resolve(join(cwd(), 'dist', zipName))

console.log(`
${lightMagenta('E D G E:')}
└ • name: ${lightGreen(`${process.env.PACKAGE_NAME}`)}
• version: ${lightCyan(`${process.env.PACKAGE_VERSION}`)}
• zip: ${lightYellow(`${basename(zipPath)}`)}
• date/time: ${new Date().toLocaleString().replace(',', '')}
`)

const accessToken = await getEdgeAccessToken(config)
const uploadUrl = `https://api.addons.microsoftedge.microsoft.com/v1/products/${config.productId}/submissions/draft/package`

Expand All @@ -238,14 +253,6 @@ async function uploadToEdge(config: EdgeUploadConfig, source: string) {
const submissionId = submissionResponse.id
const submissionUrl = `https://partner.microsoft.com/en-us/dashboard/microsoftedge/${config.productId}/submissions/${submissionId}`

console.log(`
${lightMagenta('E D G E:')}
└ • name: ${lightGreen(`${process.env.PACKAGE_NAME}`)}
• version: ${lightCyan(`${process.env.PACKAGE_VERSION}`)}
• zip: ${lightYellow(`${basename(zipPath)}`)}
• date/time: ${new Date().toLocaleString().replace(',', '')}
`)

console.log(
`Edge extension uploaded successfully. submission url: ${submissionUrl}`,
)
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/lib/make/utils/write-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function writePackageJson(response: prompts.Answers<string>): void {
}
},
"devDependencies": {
"@bedframe/cli": "0.0.82",
"@bedframe/cli": "0.0.83",
"@bedframe/core": "0.0.43",
${
changesets
Expand Down

0 comments on commit a23298a

Please sign in to comment.