-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuild-partials.ts
139 lines (115 loc) · 4.54 KB
/
build-partials.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
#!/usr/bin/env ts-node
/*!
* Script to build partials.
* Copyright 2020-2024 Chocolatey Software
* Licensed under Apache License (https://github.com/chocolatey/choco-theme/blob/main/LICENSE)
*/
import * as fs from 'fs/promises';
import path from 'node:path';
import { updateContent } from './functions/update-content';
export interface UpdateLanguageAttributes {
files: string[];
destination: string;
newExt: string;
}
const updateLanguageAttributes = async ({
files,
destination,
newExt
}: UpdateLanguageAttributes): Promise<void> => {
for await (const file of files) {
const oldExt = path.extname(file);
if (oldExt !== newExt) {
let newFileName = path.basename(file, oldExt);
if (newExt === '.cshtml') {
newFileName = `_${newFileName}`;
}
if (newExt === '.hbs') {
newFileName = newFileName.toLowerCase();
// Add partial front matter
await updateContent({
destination: destination,
targetFile: file,
targetFileDestination: destination,
targetFileContentToReplace: null,
replaceWithContent: file,
replacementContentIsFile: true,
replacementTemplate: `---\npartial: ${newFileName}\n---\n{0}`
});
}
await fs.rename(path.join(destination, file), path.join(destination, `${newFileName}${newExt}`));
}
}
};
const init = async () => {
try {
console.log('🚀 Building partials...');
const destinationTemp = './dist/partials/temp';
const destinationCshtml = './dist/partials/cshtml';
const destinationHbs = './dist/partials/hbs';
const destinationAstro = './dist/partials/astro';
await fs.cp('./partials/', destinationTemp, { recursive: true });
// Update GlobalNavigation.html
await updateContent({
destination: destinationTemp,
targetFile: 'GlobalNavigation.html',
targetFileDestination: destinationTemp,
targetFileContentToReplace: '<input id="themeToggle" />',
replaceWithContent: 'ThemeToggle.html',
replacementContentIsFile: true
});
await fs.cp(destinationTemp, destinationCshtml, { recursive: true });
await fs.cp(destinationTemp, destinationHbs, { recursive: true });
await fs.cp(destinationTemp, destinationAstro, { recursive: true });
await fs.rm(destinationTemp, { recursive: true });
// hbs files
await updateContent({
destination: destinationHbs,
targetFile: 'SocialMedia.html',
targetFileDestination: destinationHbs,
targetFileContentToReplace: '@@chocolatey',
replaceWithContent: '@chocolatey',
replacementContentIsFile: false
});
// Delete TopAlertBanner.html
await fs.rm(path.join(destinationHbs, 'TopAlertBanner.html'));
await fs.rm(path.join(destinationAstro, 'TopAlertBanner.html'));
// cshtml files
await updateContent({
destination: destinationCshtml,
targetFile: 'TopAlertBanner.html',
targetFileDestination: destinationCshtml,
targetFileContentToReplace: 'topNoticeText = ""',
replaceWithContent: 'AlertText.html',
replacementContentIsFile: true,
replacementTemplate: 'topNoticeText = "{0}"'
});
// Delete AlertText.html
await fs.rm(path.join(destinationHbs, 'AlertText.html'));
await fs.rm(path.join(destinationCshtml, 'AlertText.html'));
await fs.rm(path.join(destinationAstro, 'AlertText.html'));
// Update file extensions and casing of names
const filesHbs = await fs.readdir(destinationHbs);
const filesCshtml = await fs.readdir(destinationCshtml);
const filesAstro = await fs.readdir(destinationAstro);
await updateLanguageAttributes({
files: filesHbs,
destination: destinationHbs,
newExt: '.hbs'
});
await updateLanguageAttributes({
files: filesCshtml,
destination: destinationCshtml,
newExt: '.cshtml'
});
await updateLanguageAttributes({
files: filesAstro,
destination: destinationAstro,
newExt: '.astro'
});
console.log('✅ Partials built');
} catch (error) {
console.error(error);
}
};
init();