-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ícaro
committed
Jan 21, 2021
1 parent
2d10437
commit 3b17e89
Showing
4 changed files
with
158 additions
and
1 deletion.
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,52 @@ | ||
import { IOContext } from '@vtex/api' | ||
import { AxiosRequestConfig } from 'axios' | ||
import { HttpClient } from './HttpClient' | ||
|
||
const createRoutes = ({ account, workspace }: IOContext) => { | ||
const routes = { | ||
Templates: (argWorkspace: string) => | ||
`/${account}/${argWorkspace || workspace}/buckets/vtex.pages-graphql/userData/files/store/templates.json`, | ||
} | ||
return routes | ||
} | ||
|
||
export class Templates { | ||
private http: HttpClient | ||
private _routes: ReturnType<typeof createRoutes> | ||
|
||
constructor(ctx: IOContext, options?: AxiosRequestConfig) { | ||
const { authToken } = ctx | ||
this.http = new HttpClient({ | ||
...options, | ||
authToken, | ||
baseURL: `https://infra.io.vtex.com/vbase/v2`, | ||
}) | ||
this._routes = createRoutes(ctx) | ||
} | ||
|
||
private get routes() { | ||
return this._routes | ||
} | ||
|
||
public getWorkspaceTemplate = (workspace?: string) => { | ||
return this.http.get<TemplatesJSON>(this.routes.Templates(workspace), { | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
}) | ||
} | ||
|
||
public updateWorkspaceTemplate = (migratedTemplate: TemplatesJSON, workspace?: string) => { | ||
return this.http.putRaw<string>(this.routes.Templates(workspace), migratedTemplate, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
export type TemplatesJSON = Record<string, TreePathContainer> | ||
|
||
export interface TreePathContainer { | ||
treePathContentMap: Record<string, string> | ||
} |
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,100 @@ | ||
import { flags as oclifFlags } from '@oclif/command' | ||
import { AxiosError } from 'axios' | ||
import { createContext } from '../../clients' | ||
import { Templates, TemplatesJSON } from '../../clients/Templates' | ||
import { CustomCommand } from '../../oclif/CustomCommand' | ||
import { VtexConfig } from '../../VtexConfig' | ||
|
||
function objectHasImageContent(object: any) { | ||
return valueHasImageContent(JSON.stringify(object)) | ||
} | ||
|
||
function valueHasImageContent(value: string) { | ||
return value.search('/image') !== -1 | ||
} | ||
|
||
function replaceImageTreepath(imageTreePath: string) { | ||
return imageTreePath.replace('/image', '/media') | ||
} | ||
|
||
function getMediaTemplates(jsonTemplates: TemplatesJSON, overrideMedia: boolean) { | ||
const mediaTemplates = jsonTemplates | ||
|
||
if (!objectHasImageContent(jsonTemplates)) { | ||
return null | ||
} | ||
|
||
Object.entries(jsonTemplates).forEach(([pageId, container]) => { | ||
if (!objectHasImageContent(container)) { | ||
return | ||
} | ||
|
||
const migratedContainer = container | ||
|
||
Object.entries(container.treePathContentMap).forEach(([treePath, contentValue]) => { | ||
// If it doesn't have an image content or (already has a media content defined and cannot override), just pass | ||
if (!valueHasImageContent(treePath) || (valueHasImageContent(replaceImageTreepath(treePath)) && !overrideMedia)) { | ||
return | ||
} | ||
|
||
const migratedTreePath = replaceImageTreepath(treePath) | ||
migratedContainer.treePathContentMap[migratedTreePath] = contentValue | ||
}) | ||
|
||
mediaTemplates[pageId] = migratedContainer | ||
}) | ||
|
||
return mediaTemplates | ||
} | ||
|
||
export default class AppBundle extends CustomCommand { | ||
static description = 'Migrate image content to media content' | ||
|
||
static examples = ['vtex-dev workspace:migrateToImage', 'vtex-dev workspace:migrateToImage "myworkspace"'] | ||
|
||
static flags = { | ||
help: oclifFlags.help({ char: 'h' }), | ||
override: oclifFlags.boolean({ | ||
char: 'o', | ||
description: 'Override existing media content if an image with the same treePath can be migrated', | ||
default: false, | ||
}), | ||
} | ||
|
||
static args = [{ name: 'workspace', required: false }] | ||
|
||
async run() { | ||
const { flags, args } = this.parse(AppBundle) | ||
const { workspace: workspaceArg } = args | ||
const { override: overrideMedia } = flags | ||
const ioContext = createContext(VtexConfig) | ||
|
||
const { account, workspace } = ioContext | ||
const templates = new Templates(createContext(VtexConfig), { timeout: 30000 }) | ||
|
||
console.log(`Downloading template from account ${account} and workspace ${workspaceArg || workspace}`) | ||
|
||
try { | ||
const jsonTemplates = await templates.getWorkspaceTemplate(workspaceArg) | ||
|
||
console.log(`Finding image content...`) | ||
|
||
const mediaTemplates = getMediaTemplates(jsonTemplates, overrideMedia) | ||
|
||
if (!mediaTemplates) { | ||
console.log('There was no image content to migrate') | ||
return | ||
} | ||
|
||
console.log(`Migrating image content to media...`) | ||
|
||
await templates.updateWorkspaceTemplate(mediaTemplates, workspaceArg) | ||
|
||
console.log('All done!') | ||
return | ||
} catch (err) { | ||
const axiosErr: AxiosError = err | ||
console.log(axiosErr.response.status, axiosErr.response.statusText, axiosErr.message, axiosErr.stack) | ||
} | ||
} | ||
} |
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