mod-build 4.0.83-beta.3 → 4.0.83-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/tasks/grab-global-fonts.js +60 -85
package/package.json
CHANGED
|
@@ -11,16 +11,22 @@ import addFilesToGitignore from './add-files-to-gitignore.js';
|
|
|
11
11
|
// Font variants and extensions to download for each font
|
|
12
12
|
const fontVariants = ['regular', 'bold'];
|
|
13
13
|
const fontExtensions = ['ttf', 'woff2'];
|
|
14
|
+
const finished = promisify(stream.finished);
|
|
14
15
|
|
|
15
16
|
const axiosInstance = axios.create();
|
|
16
17
|
responseInterceptor(axiosInstance);
|
|
17
18
|
|
|
19
|
+
const getFontPath = (...parts) => path.join(
|
|
20
|
+
defaultSettings.publicFolder,
|
|
21
|
+
defaultSettings.fontsSubfolder,
|
|
22
|
+
...parts
|
|
23
|
+
);
|
|
24
|
+
|
|
18
25
|
const directoryHasItems = (dirPath) => {
|
|
19
26
|
if (!fs.existsSync(dirPath)) {
|
|
20
27
|
return false;
|
|
21
28
|
}
|
|
22
|
-
|
|
23
|
-
return items.length > 0;
|
|
29
|
+
return fs.readdirSync(dirPath).length > 0;
|
|
24
30
|
};
|
|
25
31
|
|
|
26
32
|
const getFontDownloadUrls = async (fontName) => {
|
|
@@ -29,21 +35,17 @@ const getFontDownloadUrls = async (fontName) => {
|
|
|
29
35
|
const response = await axiosInstance.get(url);
|
|
30
36
|
const fontData = response.data;
|
|
31
37
|
|
|
32
|
-
if (!fontData
|
|
38
|
+
if (!fontData?.variants) {
|
|
33
39
|
throw new Error('Invalid font data received');
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
const downloadUrls = {};
|
|
37
|
-
|
|
38
43
|
const targetWeights = ['400', '700'];
|
|
39
44
|
|
|
40
45
|
fontData.variants.forEach(variant => {
|
|
41
46
|
if (variant.fontStyle === 'normal' && targetWeights.includes(variant.fontWeight)) {
|
|
42
47
|
const variantName = variant.fontWeight === '400' ? 'regular' : 'bold';
|
|
43
|
-
|
|
44
|
-
if (!downloadUrls[variantName]) {
|
|
45
|
-
downloadUrls[variantName] = {};
|
|
46
|
-
}
|
|
48
|
+
downloadUrls[variantName] = downloadUrls[variantName] || {};
|
|
47
49
|
|
|
48
50
|
if (variant.ttf) {
|
|
49
51
|
downloadUrls[variantName].ttf = variant.ttf;
|
|
@@ -53,7 +55,7 @@ const getFontDownloadUrls = async (fontName) => {
|
|
|
53
55
|
}
|
|
54
56
|
}
|
|
55
57
|
});
|
|
56
|
-
|
|
58
|
+
|
|
57
59
|
return downloadUrls;
|
|
58
60
|
} catch (error) {
|
|
59
61
|
console.error(`Error fetching font data for ${fontName}:`, error.message);
|
|
@@ -61,61 +63,44 @@ const getFontDownloadUrls = async (fontName) => {
|
|
|
61
63
|
}
|
|
62
64
|
};
|
|
63
65
|
|
|
64
|
-
const downloadFontFile = async (
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const fileName = `${fontName}-${variant}.${extension}`;
|
|
69
|
-
const filePath = path.join(
|
|
70
|
-
defaultSettings.publicFolder,
|
|
71
|
-
defaultSettings.fontsSubfolder,
|
|
72
|
-
fontName,
|
|
73
|
-
fileName
|
|
74
|
-
);
|
|
75
|
-
const folderPath = path.dirname(filePath);
|
|
76
|
-
|
|
77
|
-
if (!fs.existsSync(folderPath)) {
|
|
78
|
-
fs.mkdirSync(folderPath, { recursive: true });
|
|
79
|
-
}
|
|
66
|
+
const downloadFontFile = async (fontName, variant, extension, downloadUrl) => {
|
|
67
|
+
const fileName = `${fontName}-${variant}.${extension}`;
|
|
68
|
+
const filePath = getFontPath(fontName, fileName);
|
|
69
|
+
const folderPath = path.dirname(filePath);
|
|
80
70
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
71
|
+
if (!fs.existsSync(folderPath)) {
|
|
72
|
+
fs.mkdirSync(folderPath, { recursive: true });
|
|
73
|
+
}
|
|
86
74
|
|
|
87
|
-
|
|
75
|
+
if (fs.existsSync(filePath)) {
|
|
76
|
+
console.log(`${filePath} already exists, skipping...`);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
88
79
|
|
|
89
|
-
|
|
80
|
+
try {
|
|
81
|
+
const resp = await axiosInstance({
|
|
90
82
|
url: downloadUrl,
|
|
91
83
|
method: 'get',
|
|
92
84
|
responseType: 'stream'
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
reject(new Error(`Failed to download ${filePath}: ${error.message}`));
|
|
109
|
-
});
|
|
110
|
-
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
if (resp.status !== 200) {
|
|
88
|
+
throw new Error(`${resp.status}: Error while fetching ${downloadUrl}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
console.log(`Downloading ${filePath}...`);
|
|
92
|
+
const writer = createWriteStream(filePath);
|
|
93
|
+
resp.data.pipe(writer);
|
|
94
|
+
await finished(writer);
|
|
95
|
+
console.log(`${filePath} downloaded successfully`);
|
|
96
|
+
} catch (error) {
|
|
97
|
+
console.error(`Error downloading ${filePath} from ${downloadUrl}:`, error.message);
|
|
98
|
+
throw new Error(`Failed to download ${filePath}: ${error.message}`);
|
|
99
|
+
}
|
|
111
100
|
};
|
|
112
101
|
|
|
113
|
-
const downloadFont = async (
|
|
114
|
-
const fontDir =
|
|
115
|
-
defaultSettings.publicFolder,
|
|
116
|
-
defaultSettings.fontsSubfolder,
|
|
117
|
-
fontName
|
|
118
|
-
);
|
|
102
|
+
const downloadFont = async (fontName) => {
|
|
103
|
+
const fontDir = getFontPath(fontName);
|
|
119
104
|
|
|
120
105
|
if (directoryHasItems(fontDir)) {
|
|
121
106
|
console.log(`Font directory ${fontDir} already has items, skipping ${fontName}...`);
|
|
@@ -130,45 +115,35 @@ const downloadFont = async (defaultSettings, fontName) => {
|
|
|
130
115
|
throw new Error(`Could not find download URLs for ${fontName}`);
|
|
131
116
|
}
|
|
132
117
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
118
|
+
await Promise.all(
|
|
119
|
+
fontVariants.flatMap(variant =>
|
|
120
|
+
fontExtensions.map(ext => {
|
|
121
|
+
const url = downloadUrls[variant]?.[ext];
|
|
122
|
+
if (!url) {
|
|
123
|
+
console.warn(`Warning: No ${ext} URL found for ${fontName} ${variant}, skipping...`);
|
|
124
|
+
return Promise.resolve();
|
|
125
|
+
}
|
|
126
|
+
return downloadFontFile(fontName, variant, ext, url);
|
|
127
|
+
})
|
|
128
|
+
)
|
|
129
|
+
);
|
|
145
130
|
};
|
|
146
131
|
|
|
147
|
-
const updateGitignore = async (
|
|
148
|
-
const fontEntries = fontNames.map(fontName =>
|
|
149
|
-
`${defaultSettings.publicFolder}/${defaultSettings.fontsSubfolder}/${fontName}/`
|
|
150
|
-
);
|
|
151
|
-
|
|
132
|
+
const updateGitignore = async (fontNames) => {
|
|
133
|
+
const fontEntries = fontNames.map(fontName => `${getFontPath(fontName)}/`);
|
|
152
134
|
await addFilesToGitignore(fontEntries, 'Global fonts');
|
|
153
135
|
};
|
|
154
136
|
|
|
155
137
|
// If a site has a branded font that is outside of this API - we should add it to /public/fonts/branded/{fontName}/*
|
|
156
|
-
export default function(config) {
|
|
138
|
+
export default async function(config) {
|
|
157
139
|
// Download fonts from config.fontsToDownload or will default to ['roboto']
|
|
158
|
-
const fontNames = config?.fontsToDownload
|
|
159
|
-
? config.fontsToDownload
|
|
160
|
-
: ['roboto'];
|
|
140
|
+
const fontNames = config?.fontsToDownload?.length > 0 ? config.fontsToDownload : ['roboto'];
|
|
161
141
|
|
|
162
|
-
const fontsPath =
|
|
142
|
+
const fontsPath = getFontPath();
|
|
163
143
|
if (!fs.existsSync(fontsPath)) {
|
|
164
144
|
fs.mkdirSync(fontsPath, { recursive: true });
|
|
165
145
|
}
|
|
166
146
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
return Promise.all(fontPromises).then(async () => {
|
|
172
|
-
await updateGitignore(defaultSettings, fontNames);
|
|
173
|
-
});
|
|
147
|
+
await Promise.all(fontNames.map(downloadFont));
|
|
148
|
+
await updateGitignore(fontNames);
|
|
174
149
|
}
|