-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): override theme components from command line (#850)
* feat(cli): override theme components from command line * docs: instruction on how to override theme component
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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); | ||
} | ||
}, | ||
}; |
d67ffb3
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.
Successfully deployed to following URLs: