/[sudobot]/trunk/scripts/extensions.js
ViewVC logotype

Diff of /trunk/scripts/extensions.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 623 by rakinar2, Mon Jul 29 17:59:26 2024 UTC revision 624 by rakinar2, Fri Aug 30 10:10:33 2024 UTC
# Line 25  require("dotenv/config"); Line 25  require("dotenv/config");
25  const chalk = require("chalk");  const chalk = require("chalk");
26  const { spawnSync } = require("child_process");  const { spawnSync } = require("child_process");
27  const { existsSync, lstatSync, readdirSync, readFileSync, writeFileSync, rmSync } = require("fs");  const { existsSync, lstatSync, readdirSync, readFileSync, writeFileSync, rmSync } = require("fs");
28  const { readFile } = require("fs/promises");  const { readFile, symlink, unlink } = require("fs/promises");
29  const path = require("path");  const path = require("path");
30  const { chdir, cwd } = require("process");  const { chdir, cwd } = require("process");
31  const { z } = require("zod");  const { z } = require("zod");
# Line 39  function error(...args) { Line 39  function error(...args) {
39  }  }
40    
41  if (!existsSync(extensionsPath)) {  if (!existsSync(extensionsPath)) {
42      error("You're not using any extension! To get started, create an `extensions` folder in the project root.");      error(
43            "You're not using any extension! To get started, create an `extensions` folder in the project root."
44        );
45  }  }
46    
47  function getRecuriveJavaScriptFiles(dir) {  function getRecuriveJavaScriptFiles(dir) {
# Line 137  async function writeCacheIndex() { Line 139  async function writeCacheIndex() {
139    
140          if (commandPaths === null) {          if (commandPaths === null) {
141              const directory = path.join(              const directory = path.join(
142                  ...(process.env.EXTENSIONS_DIRECTORY ? [process.env.EXTENSIONS_DIRECTORY] : [__dirname, "../extensions"]),                  ...(process.env.EXTENSIONS_DIRECTORY
143                        ? [process.env.EXTENSIONS_DIRECTORY]
144                        : [__dirname, "../extensions"]),
145                  extensionName,                  extensionName,
146                  commands                  commands
147              );              );
# Line 149  async function writeCacheIndex() { Line 153  async function writeCacheIndex() {
153    
154          if (eventPaths === null) {          if (eventPaths === null) {
155              const directory = path.join(              const directory = path.join(
156                  ...(process.env.EXTENSIONS_DIRECTORY ? [process.env.EXTENSIONS_DIRECTORY] : [__dirname, "../extensions"]),                  ...(process.env.EXTENSIONS_DIRECTORY
157                        ? [process.env.EXTENSIONS_DIRECTORY]
158                        : [__dirname, "../extensions"]),
159                  extensionName,                  extensionName,
160                  events                  events
161              );              );
# Line 161  async function writeCacheIndex() { Line 167  async function writeCacheIndex() {
167    
168          if (servicePaths === null) {          if (servicePaths === null) {
169              const directory = path.join(              const directory = path.join(
170                  ...(process.env.EXTENSIONS_DIRECTORY ? [process.env.EXTENSIONS_DIRECTORY] : [__dirname, "../extensions"]),                  ...(process.env.EXTENSIONS_DIRECTORY
171                        ? [process.env.EXTENSIONS_DIRECTORY]
172                        : [__dirname, "../extensions"]),
173                  extensionName,                  extensionName,
174                  services                  services
175              );              );
# Line 220  const MAX_CHARS = 7; Line 228  const MAX_CHARS = 7;
228    
229  function actionLog(action, description) {  function actionLog(action, description) {
230      console.log(      console.log(
231          chalk.green.bold(`${action}${action.length >= MAX_CHARS ? "" : " ".repeat(MAX_CHARS - action.length)} `),          chalk.green.bold(
232                `${action}${action.length >= MAX_CHARS ? "" : " ".repeat(MAX_CHARS - action.length)} `
233            ),
234          description          description
235      );      );
236  }  }
# Line 256  async function buildExtensions() { Line 266  async function buildExtensions() {
266              });              });
267    
268              actionLog("RELINK", extensionName);              actionLog("RELINK", extensionName);
269              spawnSyncCatchExit(`npm install --save ${path.relative(cwd(), path.resolve(__dirname, ".."))}`, {              spawnSyncCatchExit(
270                  encoding: "utf-8",                  `npm install --save ${path.relative(cwd(), path.resolve(__dirname, ".."))}`,
271                  shell: true,                  {
272                  stdio: "inherit"                      encoding: "utf-8",
273              });                      shell: true,
274                        stdio: "inherit"
275                    }
276                );
277          }          }
278    
279            actionLog("PREPARE", extensionName);
280    
281            if (existsSync("tsconfig.json")) {
282                await unlink("tsconfig.json").catch(() => {});
283            }
284    
285            await symlink(
286                `tsconfig.${process.env.BUN === "1" ? "bun" : "node"}.json`,
287                "tsconfig.json"
288            ).catch(() => {});
289    
290          actionLog("BUILD", extensionName);          actionLog("BUILD", extensionName);
291          const { build_command } = readMeta(extensionName, extensionDirectory);          const { build_command } = readMeta(extensionName, extensionDirectory);
292    
# Line 280  async function buildExtensions() { Line 304  async function buildExtensions() {
304          count++;          count++;
305      }      }
306    
307      actionLog("SUCCESS", `in ${((Date.now() - startTime) / 1000).toFixed(2)}s, built ${count} extensions`);      actionLog(
308            "SUCCESS",
309            `in ${((Date.now() - startTime) / 1000).toFixed(2)}s, built ${count} extensions`
310        );
311      chdir(workingDirectory);      chdir(workingDirectory);
312  }  }
313    
# Line 340  async function writeExtensionIndex() { Line 367  async function writeExtensionIndex() {
367              const revB = isNaN(dashVB) ? 0 : parseInt(dashVB);              const revB = isNaN(dashVB) ? 0 : parseInt(dashVB);
368              const result = semver.rcompare(vA, vB);              const result = semver.rcompare(vA, vB);
369    
370              if (splitA[0] === splitB[0] && vA.includes("-") && !isNaN(dashVA) && (!vB.includes("-") || isNaN(dashVB))) {              if (
371                    splitA[0] === splitB[0] &&
372                    vA.includes("-") &&
373                    !isNaN(dashVA) &&
374                    (!vB.includes("-") || isNaN(dashVB))
375                ) {
376                  return -1;                  return -1;
377              }              }
378    
379              if (splitA[0] === splitB[0] && vB.includes("-") && !isNaN(dashVB) && (!vA.includes("-") || isNaN(dashVA))) {              if (
380                    splitA[0] === splitB[0] &&
381                    vB.includes("-") &&
382                    !isNaN(dashVB) &&
383                    (!vA.includes("-") || isNaN(dashVA))
384                ) {
385                  return 1;                  return 1;
386              }              }
387    
# Line 385  async function writeExtensionIndex() { Line 422  async function writeExtensionIndex() {
422              services: servicePaths.length,              services: servicePaths.length,
423              main,              main,
424              iconURL: icon              iconURL: icon
425                  ? "https://raw.githubusercontent.com/onesoft-sudo/sudobot/main/extensions" + path.join("/", extensionName, icon)                  ? "https://raw.githubusercontent.com/onesoft-sudo/sudobot/main/extensions" +
426                      path.join("/", extensionName, icon)
427                  : null,                  : null,
428              author:              author:
429                  typeof packageJson.author === "string"                  typeof packageJson.author === "string"
# Line 396  async function writeExtensionIndex() { Line 434  async function writeExtensionIndex() {
434              license: packageJson.license,              license: packageJson.license,
435              licenseURL: `https://spdx.org/licenses/${packageJson.license}.html`,              licenseURL: `https://spdx.org/licenses/${packageJson.license}.html`,
436              homepage: packageJson.homepage,              homepage: packageJson.homepage,
437              repository: typeof packageJson.repository === "string" ? packageJson.repository : packageJson.repository?.url,              repository:
438                    typeof packageJson.repository === "string"
439                        ? packageJson.repository
440                        : packageJson.repository?.url,
441              issues: typeof packageJson.bugs === "string" ? packageJson.bugs : packageJson.bugs?.url,              issues: typeof packageJson.bugs === "string" ? packageJson.bugs : packageJson.bugs?.url,
442              lastUpdated: new Date(),              lastUpdated: new Date(),
443              readmeFileName: readmeFileName,              readmeFileName: readmeFileName,
# Line 421  async function writeExtensionIndex() { Line 462  async function writeExtensionIndex() {
462      console.log("Wrote index file: ", indexFile);      console.log("Wrote index file: ", indexFile);
463  }  }
464    
465  if (process.argv.includes("--clear-cache") || process.argv.includes("--clean") || process.argv.includes("--delcache")) {  if (
466        process.argv.includes("--clear-cache") ||
467        process.argv.includes("--clean") ||
468        process.argv.includes("--delcache")
469    ) {
470      const indexFile = path.join(extensionsPath, "index.json");      const indexFile = path.join(extensionsPath, "index.json");
471    
472      if (!existsSync(indexFile)) {      if (!existsSync(indexFile)) {
# Line 458  if (process.argv.includes("--clear-cache Line 503  if (process.argv.includes("--clear-cache
503      console.log("Options:");      console.log("Options:");
504      console.log("  --build [--tsc]    |  Builds all the installed extensions, if needed.");      console.log("  --build [--tsc]    |  Builds all the installed extensions, if needed.");
505      console.log("                     |  The `--tsc` flag will only run the typescript compiler.");      console.log("                     |  The `--tsc` flag will only run the typescript compiler.");
506      console.log("  --cache            |  Creates cache indexes for installed extensions, to improve the startup time.");      console.log(
507            "  --cache            |  Creates cache indexes for installed extensions, to improve the startup time."
508        );
509      console.log("  --clean            |  Clears all installed extension cache.");      console.log("  --clean            |  Clears all installed extension cache.");
510      console.log(      console.log(
511          "  --mkindex          |  Creates indexes for all the available extensions, in the extensions/ top level directory."          "  --mkindex          |  Creates indexes for all the available extensions, in the extensions/ top level directory."

Legend:
Removed from v.623  
changed lines
  Added in v.624

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26