UNPKG

3.03 kBJavaScriptView Raw
1const prompt = require('react-dev-utils/prompt');
2const chalk = require('chalk');
3const figures = require('figures');
4const themekit = require('@shopify/themekit').command;
5const slateEnv = require('@shopify/slate-env');
6const config = require('./slate-sync.config');
7
8let deploying = false;
9let filesToDeploy = [];
10
11function maybeDeploy() {
12 if (deploying) {
13 return Promise.reject('Deploy already in progress.');
14 }
15
16 if (filesToDeploy.length) {
17 const files = [...filesToDeploy];
18 filesToDeploy = [];
19 return deploy('upload', files);
20 }
21
22 return Promise.resolve();
23}
24
25function _generateConfigFlags() {
26 const ignoreFiles = slateEnv.getIgnoreFilesValue();
27 const flags = {
28 '--password': slateEnv.getPasswordValue(),
29 '--themeid': slateEnv.getThemeIdValue(),
30 '--store': slateEnv.getStoreValue(),
31 };
32
33 // Convert object to key value pairs and flatten the array
34 return Array.prototype.concat(...Object.entries(flags));
35}
36
37function _generateIgnoreFlags() {
38 const ignoreFiles = slateEnv.getIgnoreFilesValue().split(':');
39 const flags = [];
40
41 ignoreFiles.forEach((pattern) => {
42 flags.push('--ignored-file');
43 flags.push(pattern);
44 });
45
46 return flags;
47}
48
49/**
50 * Deploy to Shopify using themekit.
51 *
52 * @param cmd String The command to run
53 * @param files Array An array of files to deploy
54 * @return Promise
55 */
56async function deploy(cmd = '', files = []) {
57 if (!['upload', 'replace'].includes(cmd)) {
58 throw new Error(
59 'shopify-deploy.deploy() first argument must be either "upload", "replace"',
60 );
61 }
62
63 deploying = true;
64
65 console.log(chalk.magenta(`\n${figures.arrowUp} Uploading to Shopify...\n`));
66
67 try {
68 await promiseThemekitConfig();
69 await promiseThemekitDeploy(cmd, files);
70 } catch (error) {
71 console.error(error);
72 }
73
74 deploying = false;
75
76 return maybeDeploy;
77}
78
79function promiseThemekitConfig() {
80 return new Promise((resolve, reject) => {
81 themekit(
82 {
83 args: [
84 'configure',
85 ..._generateConfigFlags(),
86 ..._generateIgnoreFlags(),
87 ],
88 cwd: config.paths.dist,
89 },
90 (err) => {
91 if (err) {
92 reject(err);
93 } else {
94 resolve();
95 }
96 },
97 );
98 });
99}
100
101function promiseThemekitDeploy(cmd, files) {
102 return new Promise((resolve, reject) => {
103 themekit(
104 {
105 args: [
106 cmd,
107 '--no-update-notifier',
108 ..._generateConfigFlags(),
109 ...files,
110 ],
111 cwd: config.paths.dist,
112 },
113 (err) => {
114 if (err) {
115 reject(err);
116 } else {
117 resolve();
118 }
119 },
120 );
121 });
122}
123
124module.exports = {
125 async sync(files = []) {
126 if (!files.length) {
127 return Promise.reject('No files to deploy.');
128 }
129
130 filesToDeploy = [...new Set([...filesToDeploy, ...files])];
131
132 return maybeDeploy();
133 },
134
135 async replace() {
136 return deploy('replace');
137 },
138
139 async upload() {
140 return deploy('upload');
141 },
142};