-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathshopify-config.ts
196 lines (160 loc) · 4.62 KB
/
shopify-config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import {resolvePath, dirname} from '@shopify/cli-kit/node/path';
import {readFile, mkdir, fileExists, writeFile} from '@shopify/cli-kit/node/fs';
import {AbortError} from '@shopify/cli-kit/node/error';
export const SHOPIFY_DIR = '.shopify';
export const SHOPIFY_DIR_PROJECT = 'project.json';
export interface ShopifyConfig {
shop: string;
shopName: string;
email: string;
storefront?: {
id: string;
title: string;
customerAccountConfig?: {
redirectUri?: string;
javascriptOrigin?: string;
logoutUri?: string;
};
};
}
export async function resetConfig(root: string): Promise<void> {
const filePath = resolvePath(root, SHOPIFY_DIR, SHOPIFY_DIR_PROJECT);
if (!(await fileExists(filePath))) {
return;
}
await writeFile(filePath, JSON.stringify({}));
}
export async function getConfig(root: string): Promise<Partial<ShopifyConfig>> {
const filePath = resolvePath(root, SHOPIFY_DIR, SHOPIFY_DIR_PROJECT);
if (!(await fileExists(filePath))) {
return {};
}
return JSON.parse(await readFile(filePath));
}
export async function setUserAccount(
root: string,
{shop, shopName, email}: Omit<ShopifyConfig, 'storefront'>,
) {
const filePath = resolvePath(root, SHOPIFY_DIR, SHOPIFY_DIR_PROJECT);
let existingConfig: Partial<ShopifyConfig> = {};
if (await fileExists(filePath)) {
existingConfig = JSON.parse(await readFile(filePath));
} else {
await mkdir(dirname(filePath));
}
const newConfig = {
...existingConfig,
shop,
shopName,
email,
};
await writeFile(filePath, JSON.stringify(newConfig));
await ensureShopifyGitIgnore(root);
return newConfig;
}
/**
* Adds storefront information to the config
*
* @param root the target directory
* @returns the updated config
*/
export async function setStorefront(
root: string,
{id, title}: NonNullable<ShopifyConfig['storefront']>,
) {
try {
const filePath = resolvePath(root, SHOPIFY_DIR, SHOPIFY_DIR_PROJECT);
const existingConfig: ShopifyConfig = JSON.parse(await readFile(filePath));
const config = {
...existingConfig,
storefront: {id, title},
};
await writeFile(filePath, JSON.stringify(config));
await ensureShopifyGitIgnore(root);
return config;
} catch {
throw new AbortError('Project configuration could not be found.');
}
}
/**
* Removes storefront information from the config
*
* @param root the target directory
* @returns the updated config
*/
export async function unsetStorefront(
root: string,
): Promise<Partial<ShopifyConfig>> {
try {
const filePath = resolvePath(root, SHOPIFY_DIR, SHOPIFY_DIR_PROJECT);
const existingConfig: Partial<ShopifyConfig> = JSON.parse(
await readFile(filePath),
);
const config = {
...existingConfig,
storefront: undefined,
};
await writeFile(filePath, JSON.stringify(config));
await ensureShopifyGitIgnore(root);
return config;
} catch {
throw new AbortError('Project configuration could not be found.');
}
}
/**
* Conditionally adds .shopify to the directory's .gitignore file
* @param root
* @returns A boolean; true if the .gitignore was updated, false if there was
* an error or the .gitignore didn't need updating
*/
export async function ensureShopifyGitIgnore(root: string): Promise<boolean> {
try {
const gitIgnoreFilePath = resolvePath(root, '.gitignore');
let gitIgnoreContents = (await fileExists(gitIgnoreFilePath))
? await readFile(gitIgnoreFilePath)
: '';
if (gitIgnoreContents.includes('.shopify')) {
return false;
}
if (gitIgnoreContents.length > 0) {
gitIgnoreContents += `\n`;
}
gitIgnoreContents += `${SHOPIFY_DIR}\r\n`;
await writeFile(gitIgnoreFilePath, gitIgnoreContents);
return true;
} catch {
return false;
}
}
/**
* The Customer Account Config
*
* @param root the target directory
* @param customerAccountConfig the config to be save
* @returns the updated config
*/
export async function setCustomerAccountConfig(
root: string,
customerAccountConfig: {
redirectUri?: string;
javascriptOrigin?: string;
logoutUri?: string;
},
) {
try {
const filePath = resolvePath(root, SHOPIFY_DIR, SHOPIFY_DIR_PROJECT);
const existingConfig: ShopifyConfig = JSON.parse(await readFile(filePath));
const config = {
...existingConfig,
storefront: {
...existingConfig.storefront,
customerAccountConfig,
},
};
await writeFile(filePath, JSON.stringify(config));
await ensureShopifyGitIgnore(root);
return config;
} catch {
throw new AbortError('Project configuration could not be found.');
}
}