-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add blocks publish --all command (#466)
* feat: add blocks publish --all command * feat: add blocks release command --------- Co-authored-by: Thomas Timmer <[email protected]>
- Loading branch information
1 parent
f2de167
commit b800eac
Showing
5 changed files
with
163 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import program from 'commander'; | ||
import releaseBlocks from './blocks/releaseBlocks'; | ||
|
||
program | ||
.usage('[options] [blockIds...]') | ||
.option( | ||
'-a, --all', | ||
'release all dev blocks that are attached to your account', | ||
) | ||
.name('bb blocks release') | ||
.parse(process.argv); | ||
|
||
const { all }: { all?: boolean } = program.opts(); | ||
const blockIds = program.args; | ||
|
||
if (!all && !blockIds.length) { | ||
console.error('No block IDs provided'); | ||
process.exit(1); | ||
} | ||
|
||
// eslint-disable-next-line no-void | ||
void (async (): Promise<void> => { | ||
await releaseBlocks({ all, blockIds }); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import fetch from 'node-fetch'; | ||
import type { Response, RequestInit } from 'node-fetch'; | ||
import Config from '../functions/config'; | ||
import FusionAuth from '../utils/login'; | ||
|
||
const GET_DEV_BLOCKS = '/blocks/my-dev-blocks'; | ||
const POST_RELEASE_BLOCKS = '/blocks/release'; | ||
|
||
const sendBlockstoreRequest = async ( | ||
urlPath: string, | ||
method: string, | ||
body: RequestInit['body'], | ||
config: Config, | ||
fusionAuth: FusionAuth, | ||
applicationId: string, | ||
): Promise<Response> => { | ||
const url = `${config.blockstoreApiUrl}${urlPath}`; | ||
return fetch(url, { | ||
agent: config.agent, | ||
method, | ||
body, | ||
headers: { | ||
'content-type': 'application/json', | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
Authorization: `Bearer ${fusionAuth.jwt()}`, | ||
ApplicationId: applicationId, | ||
Accept: 'application/json', | ||
}, | ||
}).then(async (res) => { | ||
if (res.status === 401 || res.status === 403) { | ||
await fusionAuth.ensureLogin(); | ||
return sendBlockstoreRequest( | ||
urlPath, | ||
method, | ||
body, | ||
config, | ||
fusionAuth, | ||
applicationId, | ||
); | ||
} | ||
|
||
return res; | ||
}); | ||
}; | ||
|
||
const fetchAllDevBlocks = async ( | ||
config: Config, | ||
fusionAuth: FusionAuth, | ||
applicationId: string, | ||
): Promise<Response> => { | ||
return sendBlockstoreRequest( | ||
GET_DEV_BLOCKS, | ||
'GET', | ||
undefined, | ||
config, | ||
fusionAuth, | ||
applicationId, | ||
); | ||
}; | ||
|
||
const releaseBlocksInBlockstore = async ( | ||
blockIds: string[], | ||
config: Config, | ||
fusionAuth: FusionAuth, | ||
applicationId: string, | ||
): Promise<boolean> => { | ||
const response = await sendBlockstoreRequest( | ||
POST_RELEASE_BLOCKS, | ||
'POST', | ||
JSON.stringify({ block_ids: blockIds }), | ||
config, | ||
fusionAuth, | ||
applicationId, | ||
); | ||
if (!response.ok) { | ||
await response | ||
.text() | ||
.then((text) => | ||
console.error(`Failed to release blocks in Blockstore: ${text}`), | ||
); | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
const releaseBlocks = async ({ | ||
all, | ||
blockIds, | ||
}: { | ||
all?: boolean; | ||
blockIds: string[]; | ||
}): Promise<boolean> => { | ||
const config = new Config(); | ||
const fusionAuth = new FusionAuth(config); | ||
const applicationId = await config.applicationId(); | ||
if (!applicationId) { | ||
throw new Error( | ||
"Couldn't publish block(s), Error: application id not found", | ||
); | ||
} | ||
|
||
let blockIdsToBeReleased: string[] = blockIds; | ||
if (all) { | ||
const res = await fetchAllDevBlocks(config, fusionAuth, applicationId); | ||
blockIdsToBeReleased = (await res.json()) as string[]; | ||
} | ||
|
||
return releaseBlocksInBlockstore( | ||
blockIdsToBeReleased, | ||
config, | ||
fusionAuth, | ||
applicationId, | ||
); | ||
}; | ||
|
||
export default releaseBlocks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters