-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feature/image to media migration #11
Open
icazevedo
wants to merge
2
commits into
master
Choose a base branch
from
feature/image-to-media-migration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ $ npm install -g @tiagonapoli/vtex-scripts | |
$ vtex-dev COMMAND | ||
running command... | ||
$ vtex-dev (-v|--version|version) | ||
@tiagonapoli/vtex-scripts/0.0.7 linux-x64 node-v12.16.2 | ||
@tiagonapoli/vtex-scripts/0.0.7 darwin-x64 node-v12.18.0 | ||
$ vtex-dev --help [COMMAND] | ||
USAGE | ||
$ vtex-dev COMMAND | ||
|
@@ -27,6 +27,7 @@ USAGE | |
|
||
<!-- commands --> | ||
* [`vtex-dev app:bundle APPID`](#vtex-dev-appbundle-appid) | ||
* [`vtex-dev app:imageToMedia [WORKSPACE]`](#vtex-dev-appimagetomedia-workspace) | ||
* [`vtex-dev app:types APPID`](#vtex-dev-apptypes-appid) | ||
* [`vtex-dev help [COMMAND]`](#vtex-dev-help-command) | ||
|
||
|
@@ -50,6 +51,23 @@ EXAMPLES | |
vtex-dev app:bundle [email protected] --linked | ||
``` | ||
|
||
## `vtex-dev app:imageToMedia [WORKSPACE]` | ||
|
||
Migrate image content to media content | ||
|
||
``` | ||
USAGE | ||
$ vtex-dev app:imageToMedia [WORKSPACE] | ||
|
||
OPTIONS | ||
-h, --help show CLI help | ||
-o, --override Override existing media content if an image with the same treePath can be migrated | ||
|
||
EXAMPLES | ||
vtex-dev workspace:migrateToImage | ||
vtex-dev workspace:migrateToImage "myworkspace" | ||
``` | ||
|
||
## `vtex-dev app:types APPID` | ||
|
||
Download app types | ||
|
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@tiagonapoli/vtex-scripts", | ||
"version": "0.0.7", | ||
"version": "0.0.8-beta.0", | ||
"author": "Tiago Nápoli <[email protected]>", | ||
"license": "ISC", | ||
"files": [ | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the user doesn't pass workspace and he is @master workspace, he will put a file into the master workspace, am I right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, you're right. Actually, the user is going to edit an existing file, but yes, they will migrate de master workspace