From 91bf5de4db38209b0a658cdb055b41ab10de4cf8 Mon Sep 17 00:00:00 2001 From: Yifei Gao Date: Fri, 27 Sep 2024 22:44:20 +0800 Subject: [PATCH] feat: find sketches recursively --- dist/index.js | 46 +++++++++++++++++++++++++++++++++++++--------- lib/util.js | 25 +++++++++++++++++++++++++ main.js | 15 ++++++--------- package-lock.json | 2 +- 4 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 lib/util.js diff --git a/dist/index.js b/dist/index.js index 490879e..d3eb4cb 100644 --- a/dist/index.js +++ b/dist/index.js @@ -179,6 +179,37 @@ module.exports = { }; +/***/ }), + +/***/ 4904: +/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { + +const fs = __nccwpck_require__(9896); +const path = __nccwpck_require__(6928); + +function getSketches(folder) { + const entries = fs.readdirSync(folder); + const sketches = []; + + for (const entry of entries) { + const fullPath = path.join(folder, entry); + const stats = fs.statSync(fullPath); + + if (stats.isDirectory()) { + // Recursively check this directory + sketches.push(...getSketches(fullPath)); + } else if (entry === 'index.html') { + sketches.push(folder); // Add the folder path if it contains index.html + } + } + + return sketches; +} + +module.exports = { + getSketches +}; + /***/ }), /***/ 7524: @@ -40579,6 +40610,7 @@ const core = __nccwpck_require__(2386); (__nccwpck_require__(1223).config)(); const apiService = __nccwpck_require__(4622); +const { getSketches } = __nccwpck_require__(4904); const P5_USERNAME = core.getInput("p5-username") || process.env.P5_USERNAME; const P5_PASSWORD = core.getInput("p5-password") || process.env.P5_PASSWORD; @@ -40586,7 +40618,7 @@ const SKETCHES_FOLDER = path.join( process.cwd(), core.getInput("sketch-folder") || process.env.SKETCHES_FOLDER || "sketches" ); -const SKETCH_INFO_FILE = path.join(process.cwd(), "sketches.json"); +const SKETCH_INFO_FILE = path.join(SKETCHES_FOLDER, "sketchesMap.json"); const COLLECTION_NAME = core.getInput("collection-name") || process.env.COLLECTION_NAME || @@ -40619,15 +40651,11 @@ const COLLECTION_NAME = : []; // Get the list of sketch directories - const sketches = fs - .readdirSync(SKETCHES_FOLDER) - .filter((file) => - fs.statSync(path.join(SKETCHES_FOLDER, file)).isDirectory() - ); - - for (const sketchName of sketches) { - const sketchPath = path.join(SKETCHES_FOLDER, sketchName); + const sketches = getSketches(SKETCHES_FOLDER) + for (const sketchPath of sketches) { + const sketchName = path.basename(sketchPath); + // Read all files in the sketch folder const files = fs.readdirSync(sketchPath).filter((file) => { const fileExtension = path.extname(file).toLowerCase(); diff --git a/lib/util.js b/lib/util.js new file mode 100644 index 0000000..d6a9636 --- /dev/null +++ b/lib/util.js @@ -0,0 +1,25 @@ +const fs = require("fs"); +const path = require("path"); + +function getSketches(folder) { + const entries = fs.readdirSync(folder); + const sketches = []; + + for (const entry of entries) { + const fullPath = path.join(folder, entry); + const stats = fs.statSync(fullPath); + + if (stats.isDirectory()) { + // Recursively check this directory + sketches.push(...getSketches(fullPath)); + } else if (entry === 'index.html') { + sketches.push(folder); // Add the folder path if it contains index.html + } + } + + return sketches; +} + +module.exports = { + getSketches +}; \ No newline at end of file diff --git a/main.js b/main.js index d52c9cb..c0cfb20 100644 --- a/main.js +++ b/main.js @@ -5,6 +5,7 @@ const core = require("@actions/core"); require("dotenv").config(); const apiService = require("./lib/api"); +const { getSketches } = require("./lib/util"); const P5_USERNAME = core.getInput("p5-username") || process.env.P5_USERNAME; const P5_PASSWORD = core.getInput("p5-password") || process.env.P5_PASSWORD; @@ -12,7 +13,7 @@ const SKETCHES_FOLDER = path.join( process.cwd(), core.getInput("sketch-folder") || process.env.SKETCHES_FOLDER || "sketches" ); -const SKETCH_INFO_FILE = path.join(process.cwd(), "sketches.json"); +const SKETCH_INFO_FILE = path.join(SKETCHES_FOLDER, "sketchesMap.json"); const COLLECTION_NAME = core.getInput("collection-name") || process.env.COLLECTION_NAME || @@ -45,15 +46,11 @@ const COLLECTION_NAME = : []; // Get the list of sketch directories - const sketches = fs - .readdirSync(SKETCHES_FOLDER) - .filter((file) => - fs.statSync(path.join(SKETCHES_FOLDER, file)).isDirectory() - ); - - for (const sketchName of sketches) { - const sketchPath = path.join(SKETCHES_FOLDER, sketchName); + const sketches = getSketches(SKETCHES_FOLDER) + for (const sketchPath of sketches) { + const sketchName = path.basename(sketchPath); + // Read all files in the sketch folder const files = fs.readdirSync(sketchPath).filter((file) => { const fileExtension = path.extname(file).toLowerCase(); diff --git a/package-lock.json b/package-lock.json index 6bd707b..e026103 100644 --- a/package-lock.json +++ b/package-lock.json @@ -333,4 +333,4 @@ } } } -} \ No newline at end of file +}