1 | const prompt = require('react-dev-utils/prompt');
|
2 | const chalk = require('chalk');
|
3 | const figures = require('figures');
|
4 | const themekit = require('@shopify/themekit').command;
|
5 | const slateEnv = require('@shopify/slate-env');
|
6 | const config = require('./slate-sync.config');
|
7 |
|
8 | let deploying = false;
|
9 | let filesToDeploy = [];
|
10 |
|
11 | function 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 |
|
25 | function _generateConfigFlags() {
|
26 | const ignoreFiles = slateEnv.getIgnoreFilesValue();
|
27 | const flags = {
|
28 | '--password': slateEnv.getPasswordValue(),
|
29 | '--themeid': slateEnv.getThemeIdValue(),
|
30 | '--store': slateEnv.getStoreValue(),
|
31 | };
|
32 |
|
33 |
|
34 | return Array.prototype.concat(...Object.entries(flags));
|
35 | }
|
36 |
|
37 | function _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 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | async 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 |
|
79 | function 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 |
|
101 | function 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 |
|
124 | module.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 | };
|