Skip to content

Commit

Permalink
feat(cli): override theme components from command line (#850)
Browse files Browse the repository at this point in the history
* feat(cli): override theme components from command line

* docs: instruction on how to override theme component
  • Loading branch information
patzick authored Jun 8, 2020
1 parent aabc31e commit d67ffb3
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
Binary file added docs/assets/shopware-pwa-components-override.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions docs/guide/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ sidebar: auto
---

`*` - if you are using your self-hosted Shopware instance


### Question: How can I override theme component?

You can override theme component easily by typing `yarn shopware-pwa override` and picking component to override.
:::warning
Please remember, that overriding component means you no longer use theme component so you'll have no updates for overrided components. If you feel that you need to override couple of components to accomplish some effect maybe we can help you upgrading theme to allow you to override just one component to have this. Feel free to ask your questions on slack or create an issue for that.
:::

Here's how overriding component looks like:
![overriding theme components](../assets/shopware-pwa-components-override.gif)
68 changes: 68 additions & 0 deletions packages/cli/src/commands/override.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { GluegunToolbox } from "gluegun";

// TODO: method from nuxt-module/src/files.ts - move it to shared package - https://github.com/DivanteLtd/shopware-pwa/issues/849
function getAllFiles(
dirPath: string,
arrayOfFiles: string[] = [],
excludeHidden: boolean = true
): string[] {
const jetpack = require("fs-jetpack");
const path = require("path");
if (!dirPath || !jetpack.exists(dirPath)) return [];
const files: string[] = jetpack.list(dirPath) as string[];
files.forEach((file) => {
if (jetpack.exists(path.join(dirPath, file)) === "dir") {
arrayOfFiles = getAllFiles(path.join(dirPath, file), arrayOfFiles);
} else {
const fileName = path.join(dirPath, file).replace(__dirname + "/", "");
if (!(excludeHidden && file.startsWith("."))) {
arrayOfFiles.push(path.normalize(fileName));
}
}
});

return arrayOfFiles;
}

module.exports = {
name: "override",
alias: ["o"],
hidden: false,
description:
"Allows you to override theme component. Component will appear in project ready to be edited.",
run: async (toolbox: GluegunToolbox) => {
const directoryPath = `${toolbox.defaultThemeLocation}/components/`;

const componentsFullPaths = getAllFiles(directoryPath);
const themeComponents = componentsFullPaths.map((path) =>
path.replace(directoryPath, "")
);

const componentToOverrideQuestion = {
type: "autocomplete",
name: "componentToOverride",
message: "Type or select component to override",
limit: 10,
choices: themeComponents,
};

const answers = await toolbox.prompt.ask([componentToOverrideQuestion]);

const copyFrom = `${directoryPath}${answers.componentToOverride}`;
const copyTo = `components/${answers.componentToOverride}`;

try {
await toolbox.filesystem.copyAsync(copyFrom, copyTo);
toolbox.print.success(
`Component overrided. You can edit it in ${copyTo}`
);
const ua = require("universal-analytics");
const visitor = ua("UA-167979975-1");
visitor
.event("CLI", "override-component", answers.componentToOverride)
.send();
} catch (e) {
toolbox.print.error(e.message);
}
},
};

1 comment on commit d67ffb3

@vercel
Copy link

@vercel vercel bot commented on d67ffb3 Jun 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.