-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathbuild-docs.mjs
246 lines (212 loc) · 7.85 KB
/
build-docs.mjs
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* eslint-disable no-undef, no-console */
import fs from 'fs/promises';
import {existsSync} from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';
import {
generateFiles,
copyGeneratedToShopifyDev,
replaceFileContent,
} from '../build-doc-shared.mjs';
const EXTENSIONS_API_VERSION = process.argv[2] || 'unstable';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const rootPath = path.join(__dirname, '../../..');
const docsRelativePath = 'docs/surfaces/admin';
const docsGeneratedRelativePath = 'docs/surfaces/admin/generated';
const srcRelativePath = 'src/surfaces/admin';
const docsPath = path.join(rootPath, docsRelativePath);
const srcPath = path.join(rootPath, srcRelativePath);
const generatedDocsPath = path.join(docsPath, 'generated');
const shopifyDevPath = path.join(rootPath, '../../../shopify-dev');
const shopifyDevDBPath = path.join(
shopifyDevPath,
'db/data/docs/templated_apis',
);
const shopifyDevExists = existsSync(shopifyDevPath);
const generatedDocsDataFile = 'generated_docs_data.json';
const generatedStaticPagesFile = 'generated_static_pages.json';
const componentDefs = path.join(srcPath, 'components.d.ts');
const tempComponentDefs = path.join(srcPath, 'components.ts');
const tsconfig = 'tsconfig.docs.json';
const tsconfigAppBridge = 'tsconfig.ab.docs.json';
const decodeHTML = (str) => {
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'");
};
const htmlWrapper = (htmlString, layout) => {
return `<!DOCTYPE html><html><head><style>html, body {height:100%} body {box-sizing: border-box; margin: 0; padding:0.5rem; ${layout}}</style><script src="https://cdn.shopify.com/shopifycloud/app-bridge-ui-experimental.js"></script></head><body>${decodeHTML(
htmlString,
)}</body></html>`;
};
const templates = {
default: (htmlString) =>
htmlWrapper(htmlString, 'display: grid; place-items: center; gap: 0.5rem;'),
wrapped: (htmlString) =>
htmlWrapper(
`<div>${htmlString}</div>`,
'display: grid; place-items: center; gap: 0.5rem;',
),
inline: (htmlString) =>
htmlWrapper(
htmlString,
'display: flex; justify-content: center; align-items: center; gap: 0.5rem;',
),
section: (htmlString) =>
htmlWrapper(
`<s-section padding="none">${htmlString}</s-section>`,
'display: grid; place-items: center; background: #F1F1F1',
),
page: (htmlString) =>
htmlWrapper(
htmlString,
'display: grid; place-items: center; background: #F1F1F1;',
),
none: (htmlString) => htmlWrapper(htmlString, 'padding: 0'),
};
const transformJson = async (filePath, isExtensions) => {
let jsonData = JSON.parse((await fs.readFile(filePath, 'utf8')).toString());
jsonData.forEach((entry) => {
// Temporary to ensure that isOptional is added to all members
if (entry.definitions && entry.isVisualComponent) {
entry.definitions.forEach((definition) => {
if (definition.typeDefinitions) {
Object.values(definition.typeDefinitions).forEach((typeDef) => {
if (typeDef.members && Array.isArray(typeDef.members)) {
typeDef.members
.sort((first, second) => first.name.localeCompare(second.name))
.forEach((member) => {
// eslint-disable-next-line no-prototype-builtins
if (member.hasOwnProperty('isOptional')) return;
member.isOptional = true;
});
}
});
}
});
}
if (entry.defaultExample?.codeblock?.tabs) {
const newTabs = [];
entry.defaultExample.codeblock.tabs.forEach((tab) => {
if (tab.language !== 'preview') {
newTabs.push(tab);
return;
}
if (tab.layout && !(tab.layout in templates)) {
console.warn(
`${entry.name} has a layout of ${tab.layout} which is not a valid template.`,
);
}
const previewHTML =
tab.layout && tab.layout in templates
? templates[tab.layout](tab.code)
: templates.default(tab.code);
newTabs.push(
{code: tab.code, language: 'html'},
{code: previewHTML, language: 'preview'},
);
});
entry.defaultExample.codeblock.tabs = newTabs;
}
});
// Merge the App Bridge docs with the Shopify Dev docs
if (!isExtensions && shopifyDevExists) {
const shopifyDevDocs = path.join(
shopifyDevDBPath,
'app_bridge/generated_docs_data.json',
);
const shopifyDevDocsContent = await fs.readFile(shopifyDevDocs, 'utf8');
const shopifyDevDocsDocsParsed = JSON.parse(
shopifyDevDocsContent.toString(),
);
const filteredDocs = shopifyDevDocsDocsParsed.filter(
(entry) => entry.category !== 'Polaris web components',
);
// Combine arrays with shopify dev docs first, followed by new data
jsonData = [...filteredDocs, ...jsonData];
}
await fs.writeFile(filePath, JSON.stringify(jsonData, null, 2));
};
const generateExtensionsDocs = async () => {
console.log(
`Building Admin UI Extensions docs for ${EXTENSIONS_API_VERSION} version`,
);
if (EXTENSIONS_API_VERSION === 'unstable') {
console.log(
"You can add a calver version argument (e.g. 'yarn docs:admin 2023-07') to generate the docs for a stable version.",
);
}
const outputDir = `${docsGeneratedRelativePath}/admin_extensions/${EXTENSIONS_API_VERSION}`;
const scripts = [
`yarn tsc --project ${docsRelativePath}/${tsconfig} --moduleResolution node --target esNext --module CommonJS`,
`yarn generate-docs --input ./${srcRelativePath} --typesInput ./${srcRelativePath} --output ./${outputDir}`,
`yarn tsc ${docsRelativePath}/staticPages/*.doc.ts --moduleResolution node --target esNext --module CommonJS`,
`yarn generate-docs --isLandingPage --input ./${docsRelativePath}/staticPages --output ./${outputDir}`,
];
await generateFiles({
scripts,
outputDir,
rootPath,
generatedDocsDataFile,
generatedStaticPagesFile,
transformJson: (filePath) => transformJson(filePath, true),
});
// Replace 'unstable' with the exact API version in relative doc links
await replaceFileContent({
filePaths: path.join(outputDir, generatedDocsDataFile),
searchValue: '/docs/api/admin-extensions/unstable/',
replaceValue: `/docs/api/admin-extensions/${EXTENSIONS_API_VERSION}`,
});
await fs.cp(
path.join(docsPath, 'screenshots'),
path.join(
shopifyDevPath,
'app/assets/images/templated-apis-screenshots/admin-extensions',
EXTENSIONS_API_VERSION,
),
{recursive: true},
);
};
const generateAppBridgeDocs = async () => {
console.log('Building App Bridge docs');
const outputDir = `${docsGeneratedRelativePath}/app_bridge`;
const scripts = [
`yarn tsc --project ${docsRelativePath}/${tsconfigAppBridge} --moduleResolution node --target esNext --module CommonJS`,
`yarn generate-docs --input ./${srcRelativePath} --typesInput ./${srcRelativePath} --output ./${outputDir}`,
];
await generateFiles({
scripts,
outputDir,
rootPath,
generatedDocsDataFile,
transformJson: (filePath) => transformJson(filePath, false),
});
};
try {
if (existsSync(generatedDocsPath)) {
await fs.rm(generatedDocsPath, {recursive: true});
}
await fs.copyFile(componentDefs, tempComponentDefs);
await replaceFileContent({
filePaths: tempComponentDefs,
searchValue: /typeof globalThis\.HTMLElement/g,
replaceValue: 'any',
});
await generateExtensionsDocs();
await generateAppBridgeDocs();
await copyGeneratedToShopifyDev({
generatedDocsPath,
shopifyDevPath,
shopifyDevDBPath,
});
await fs.rm(tempComponentDefs);
} catch (error) {
console.error(error);
console.log(error.stdout.toString());
console.log(error.stderr.toString());
process.exit(1);
}