2626 * - The script reads configurations from the `package.json` and `.buildignore` files; ensure they are correctly set up.
2727 *
2828 * @author Refringe
29- * @version v1.0.0
29+ * @version v1.0.1
3030 */
3131
3232import archiver from "archiver" ;
3333import fs from "fs-extra" ;
3434import ignore from "ignore" ;
35- import os from "os" ;
36- import path , { dirname } from "path" ;
37- import { fileURLToPath } from "url" ;
35+ import os from "node: os" ;
36+ import path , { dirname } from "node: path" ;
37+ import { fileURLToPath } from "node: url" ;
3838import winston from "winston" ;
3939
4040// Get the command line arguments to determine whether to use verbose logging.
@@ -96,9 +96,13 @@ async function main() {
9696 const packageJson = await loadPackageJson ( currentDir ) ;
9797
9898 // Create a descriptive name for the mod package.
99- const projectName = createProjectName ( packageJson ) ;
99+ const projectName = createProjectName ( packageJson , false ) ;
100100 logger . log ( "success" , `Project name created: ${ projectName } ` ) ;
101101
102+ // Create a descriptive name for the ZIP file.
103+ const zipFileName = createProjectName ( packageJson , true ) ;
104+ logger . log ( "success" , `Project package name created: ${ zipFileName } ` ) ;
105+
102106 // Remove the old distribution directory and create a fresh one.
103107 const distDir = await removeOldDistDirectory ( currentDir ) ;
104108 logger . log ( "info" , "Distribution directory successfully cleaned." ) ;
@@ -115,13 +119,13 @@ async function main() {
115119
116120 // Create a zip archive of the project files.
117121 logger . log ( "info" , "Beginning folder compression..." ) ;
118- const zipFilePath = path . join ( path . dirname ( projectDir ) , `${ projectName } .zip` ) ;
119- await createZipFile ( projectDir , zipFilePath , " user/mods/" + projectName ) ;
122+ const zipFilePath = path . join ( path . dirname ( projectDir ) , `${ zipFileName } .zip` ) ;
123+ await createZipFile ( projectDir , zipFilePath , ` user/mods/${ projectName } ` ) ;
120124 logger . log ( "success" , "Archive successfully created." ) ;
121125 logger . log ( "info" , zipFilePath ) ;
122126
123127 // Move the zip file inside of the project directory, within the temporary working directory.
124- const zipFileInProjectDir = path . join ( projectDir , `${ projectName } .zip` ) ;
128+ const zipFileInProjectDir = path . join ( projectDir , `${ zipFileName } .zip` ) ;
125129 await fs . move ( zipFilePath , zipFileInProjectDir ) ;
126130 logger . log ( "success" , "Archive successfully moved." ) ;
127131 logger . log ( "info" , zipFileInProjectDir ) ;
@@ -134,23 +138,23 @@ async function main() {
134138 logger . log ( "success" , "------------------------------------" ) ;
135139 logger . log ( "success" , "Build script completed successfully!" ) ;
136140 logger . log ( "success" , "Your mod package has been created in the 'dist' directory:" ) ;
137- logger . log ( "success" , `/ ${ path . relative ( process . cwd ( ) , path . join ( distDir , `${ projectName } .zip` ) ) } ` ) ;
141+ logger . log ( "success" , `${ path . relative ( process . cwd ( ) , path . join ( distDir , `${ zipFileName } .zip` ) ) } ` ) ;
138142 logger . log ( "success" , "------------------------------------" ) ;
139143 if ( ! verbose ) {
140144 logger . log ( "success" , "To see a detailed build log, use `npm run buildinfo`." ) ;
141145 logger . log ( "success" , "------------------------------------" ) ;
142146 }
143147 } catch ( err ) {
144148 // If any of the file operations fail, log the error.
145- logger . log ( "error" , " An error occurred: " + err ) ;
149+ logger . log ( "error" , ` An error occurred: ${ err } ` ) ;
146150 } finally {
147151 // Clean up the temporary directory, even if the build fails.
148152 if ( projectDir ) {
149153 try {
150154 await fs . promises . rm ( projectDir , { force : true , recursive : true } ) ;
151155 logger . log ( "info" , "Cleaned temporary directory." ) ;
152156 } catch ( err ) {
153- logger . log ( "error" , " Failed to clean temporary directory: " + err ) ;
157+ logger . log ( "error" , ` Failed to clean temporary directory: ${ err } ` ) ;
154158 }
155159 }
156160 }
@@ -220,18 +224,21 @@ async function loadPackageJson(currentDir) {
220224 * @param {Object } packageJson - A JSON object containing the contents of the `package.json` file.
221225 * @returns {string } A string representing the constructed project name.
222226 */
223- function createProjectName ( packageJson ) {
227+ function createProjectName ( packageJson , includeVersion = true ) {
224228 // Remove any non-alphanumeric characters from the author and name.
225229 const author = packageJson . author . replace ( / \W / g, "" ) ;
226230 const name = packageJson . name . replace ( / \W / g, "" ) ;
227231 const version = packageJson . version ;
228232
229233 // Ensure the name is lowercase, as per the package.json specification.
230- return `${ author } -${ name } -${ version } ` . toLowerCase ( ) ;
234+ if ( includeVersion ) {
235+ return `${ author } -${ name } -${ version } ` . toLowerCase ( ) ;
236+ }
237+ return `${ author } -${ name } ` . toLowerCase ( ) ;
231238}
232239
233240/**
234- * Defines the location of the distribution directory where the final mod package will be stored and deletes any
241+ * Defines the location of the distribution directory where the final mod package will be stored and deletes any
235242 * existing distribution directory to ensure a clean slate for the build process.
236243 *
237244 * @param {string } currentDirectory - The absolute path of the current working directory.
@@ -322,7 +329,7 @@ async function copyFiles(srcDir, destDir, ignoreHandler) {
322329 await Promise . all ( copyOperations ) ;
323330 } catch ( err ) {
324331 // Log an error message if any error occurs during the copy process.
325- logger . log ( "error" , " Error copying files: " + err ) ;
332+ logger . log ( "error" , ` Error copying files: ${ err } ` ) ;
326333 }
327334}
328335
@@ -346,14 +353,14 @@ async function createZipFile(directoryToZip, zipFilePath, containerDirName) {
346353 } ) ;
347354
348355 // Set up an event listener for the 'close' event to resolve the promise when the archiver has finalized.
349- output . on ( "close" , function ( ) {
356+ output . on ( "close" , ( ) => {
350357 logger . log ( "info" , "Archiver has finalized. The output and the file descriptor have closed." ) ;
351358 resolve ( ) ;
352359 } ) ;
353360
354361 // Set up an event listener for the 'warning' event to handle warnings appropriately, logging them or rejecting
355362 // the promise based on the error code.
356- archive . on ( "warning" , function ( err ) {
363+ archive . on ( "warning" , err => {
357364 if ( err . code === "ENOENT" ) {
358365 logger . log ( "warn" , `Archiver issued a warning: ${ err . code } - ${ err . message } ` ) ;
359366 } else {
@@ -362,7 +369,7 @@ async function createZipFile(directoryToZip, zipFilePath, containerDirName) {
362369 } ) ;
363370
364371 // Set up an event listener for the 'error' event to reject the promise if any error occurs during archiving.
365- archive . on ( "error" , function ( err ) {
372+ archive . on ( "error" , err => {
366373 reject ( err ) ;
367374 } ) ;
368375
0 commit comments