shopify-accelerate
Version:
Shopify Theme development with full Typescript Support
104 lines (98 loc) • 3.63 kB
text/typescript
import chalk from "chalk";
import { exec } from "child_process";
import fs from "fs";
import path from "path";
import { config } from "../../shopify-accelerate";
import { buildTheme } from "../scaffold-theme/build-theme";
import { generateConfigFiles } from "../scaffold-theme/generate-config-files";
import { validateCliOptions } from "../validate-cli-options";
export const shopifyCliPull = async () => {
const { store, theme_id, environment, theme_path } = config;
const cleanThemePath = theme_path?.replace(/^\.\//gi, "")?.replace(/\\/gi, "/");
let second_attempt = false;
await new Promise((resolve, reject) => {
const interval = setInterval(() => {
console.log(
`[${chalk.gray(new Date().toLocaleTimeString())}]: ${chalk.yellowBright(
`Downloading Theme Files from https://admin.shopify.com/store/${store}/themes/${theme_id}`
)}`
);
}, 1000);
exec(
`shopify theme pull --environment ${environment} && cd ${cleanThemePath} && git init && git add . && git commit -m init`,
async (error, stdout, stderr) => {
const vcsPath = path.join(process.cwd(), ".idea/vcs.xml");
const workspace = path.join(process.cwd(), ".idea/workspace.xml");
if (fs.existsSync(workspace)) {
if (fs.existsSync(vcsPath)) {
const vcsContent = fs.readFileSync(vcsPath, { encoding: "utf-8" });
if (
!vcsContent.includes(
`<mapping directory="$PROJECT_DIR$/${cleanThemePath}" vcs="Git" />`
)
) {
const newVcsContent = vcsContent.replace(
"</component>",
` <mapping directory="$PROJECT_DIR$/${cleanThemePath}" vcs="Git" />\n </component>`
);
fs.writeFileSync(vcsPath, newVcsContent);
}
} else {
fs.writeFileSync(
vcsPath,
`<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/${cleanThemePath}" vcs="Git" />
</component>
</project>`
);
}
}
if (
stderr &&
stderr.includes(`doesn't exist`) &&
stderr.includes(`Theme`) &&
!second_attempt
) {
clearInterval(interval);
second_attempt = true;
console.log(stderr);
console.log(
`[${chalk.gray(new Date().toLocaleTimeString())}]: ${chalk.redBright(
`Error - Theme not Found`
)}`
);
await validateCliOptions({
store,
environment,
reset_theme_id: true,
});
buildTheme();
generateConfigFiles();
await shopifyCliPull();
resolve(true);
return;
}
if (stderr && stderr.includes(`doesn't exist`) && stderr.includes(`Theme`)) {
console.log(stderr);
console.log(
`[${chalk.gray(new Date().toLocaleTimeString())}]: ${chalk.redBright(
`Error - Could not initialize the Theme`
)}`
);
clearInterval(interval);
resolve(true);
}
console.log(
`[${chalk.gray(new Date().toLocaleTimeString())}]: ${chalk.greenBright(
`Theme Files & Git Initialized`
)}`
);
clearInterval(interval);
resolve(true);
}
);
});
};