-
Notifications
You must be signed in to change notification settings - Fork 3
/
set-theme.js
150 lines (126 loc) · 4.64 KB
/
set-theme.js
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
const fs = require("fs")
const path = require("path")
const https = require("https")
/**
* Retrieves the current working directory of the Node.js process.
*
* @returns {string} The current working directory.
*/
function getCurrentFolder() {
return process.cwd()
}
/**
* Retrieves the command line arguments passed to the Node.js script.
*
* @returns {string[]} An array containing the command line arguments.
*/
function getCommandLineArgs() {
// Remove the first two elements, which are the Node executable and the script file path
const args = process.argv.slice(2)
return args
}
/**
* Ensures that a directory exists. If the directory structure does not exist, it is created.
*
* @param {string} folderPath - The path of the folder to ensure exists.
* @throws {Error} If the directory cannot be created due to an error.
*/
function ensureDirectoryExists(folderPath) {
try {
// Create the directory if it does not exist, including parent directories if necessary
fs.mkdirSync(folderPath, { recursive: true })
} catch (error) {
throw new Error(`Unable to create directory: ${error.message}`)
}
}
/**
* Removes all contents of a specified directory.
*
* @param {string} dirPath - The path of the directory to clear.
* @throws {Error} If the directory cannot be accessed or if an error occurs during deletion.
*/
function clearDirectoryContents(dirPath) {
try {
// Read all items in the directory
const items = fs.readdirSync(dirPath)
// Iterate over each item and remove it
items.forEach((item) => {
const itemPath = path.join(dirPath, item)
const stats = fs.statSync(itemPath)
// Check if it's a directory
if (stats.isDirectory()) {
// Recursively remove directory content
clearDirectoryContents(itemPath)
// Remove the directory itself
fs.rmdirSync(itemPath)
} else {
// Remove file
fs.unlinkSync(itemPath)
}
})
} catch (error) {
throw new Error(`Unable to clear directory: ${error.message}`)
}
}
/**
* Copies a file to a specified directory.
*
* @param {string} sourceFilePath - The path to the file to be copied.
* @param {string} targetDirectoryPath - The path to the directory where the file should be copied.
* @throws {Error} If the file cannot be copied due to an error.
*/
function copyFileToDirectory(sourceFilePath, targetDirectoryPath) {
try {
// Ensure the target directory exists
if (!fs.existsSync(targetDirectoryPath)) {
throw new Error(`Target directory does not exist: ${targetDirectoryPath}`)
}
// Get the filename from the source path
const fileName = path.basename(sourceFilePath)
// Construct the target file path
const targetFilePath = path.join(targetDirectoryPath, fileName)
// Copy the file to the target directory
fs.copyFileSync(sourceFilePath, targetFilePath)
//console.log(`File successfully copied to '${targetFilePath}'`);
} catch (error) {
throw new Error(`Unable to copy file: ${error.message}`)
}
}
const currentFolder = `${getCurrentFolder()}/../quartz`
// Get the command line arguments
const args = getCommandLineArgs()
if (args.length === 0) {
console.error("Please provide a theme name as an argument. Example: node run theme -- my-theme")
process.exit(1)
}
const themeName = args[0]
// check if quartz/styles exists
if (!fs.existsSync(path.join(currentFolder, "quartz", "styles"))) {
console.error(
"The quartz/styles directory does not exist. Please run this script from the root of a Quartz project.",
)
process.exit(1)
}
// ensure the quartz/styles directory exists
ensureDirectoryExists(path.join(currentFolder, "quartz", "styles", "themes"))
// Clear the contents of the quartz/styles/themes directory
clearDirectoryContents(path.join(currentFolder, "quartz", "styles", "themes"))
ensureDirectoryExists(path.join(currentFolder, "quartz", "styles", "themes", "overrides"))
// Copy the theme files
const themeSource = path.join(getCurrentFolder(), "__CONVERTER", "__OUTPUT", themeName)
const themeOverrideSource = path.join(getCurrentFolder(), "__CONVERTER", "__OVERRIDES", themeName)
const themeDest = path.join(currentFolder, "quartz", "styles", "themes")
const themeFiles = ["_index.scss", "_dark.scss", "_light.scss", "_fonts.scss", "README.md"]
// Check if theme exists
if (!fs.existsSync(themeSource)) {
console.error(`Theme ${themeName} does not exist`)
process.exit(1)
}
themeFiles.forEach((file) => {
copyFileToDirectory(path.join(themeSource, file), themeDest)
})
copyFileToDirectory(
path.join(themeOverrideSource, "_index.scss"),
path.join(themeDest, "overrides"),
)
console.log(`Theme ${themeName} has been set successfully`)