@badisi/agadoo 4.0.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/cli.mjs +49 -46
  2. package/index.mjs +9 -3
  3. package/package.json +1 -1
package/cli.mjs CHANGED
@@ -1,11 +1,42 @@
1
- import { access, readFile, stat } from "node:fs/promises";
2
1
  import { relative, resolve } from "node:path";
3
2
  import { pathToFileURL } from "node:url";
4
3
  import { styleText } from "node:util";
4
+ import { access, readFile, stat } from "node:fs/promises";
5
5
  import { rollup } from "rollup";
6
6
  import virtual from "@rollup/plugin-virtual";
7
7
  //#region package.json
8
- var version = "4.0.0";
8
+ var version = "4.0.1";
9
+ //#endregion
10
+ //#region src/cli-utils.ts
11
+ const ifExists = async (path) => {
12
+ try {
13
+ await access(path);
14
+ return path;
15
+ } catch {
16
+ return null;
17
+ }
18
+ };
19
+ const isDirectory = async (path) => {
20
+ try {
21
+ return (await stat(path)).isDirectory();
22
+ } catch {
23
+ return false;
24
+ }
25
+ };
26
+ const exitWithError = (message) => {
27
+ console.error(styleText("red", `Error: ${message}`));
28
+ process.exit(1);
29
+ };
30
+ const resolveEntry = async (value) => {
31
+ if (await isDirectory(value)) {
32
+ try {
33
+ const pkgJson = JSON.parse(await readFile(resolve(value, "package.json"), "utf-8"));
34
+ const name = pkgJson.module ?? pkgJson.main;
35
+ if (name) return await resolveEntry(resolve(value, name));
36
+ } catch {}
37
+ return await ifExists(resolve(value, "index.mjs")) ?? await ifExists(resolve(value, "index.js")) ?? exitWithError(`Could not resolve entry point in '${value}'`);
38
+ } else return await ifExists(value) ?? await ifExists(`${value}.mjs`) ?? await ifExists(`${value}.js`) ?? exitWithError(`Could not resolve entry point '${value}'`);
39
+ };
9
40
  //#endregion
10
41
  //#region src/index.ts
11
42
  const check = async (path, rollupOptions) => {
@@ -20,11 +51,17 @@ const check = async (path, rollupOptions) => {
20
51
  else if (warning.code !== "EMPTY_BUNDLE") warnings.push(warning.message);
21
52
  }
22
53
  })).generate({ format: "esm" });
23
- const code = output[0].code.trim();
54
+ let openRegionsCount = 0;
55
+ const code = [...output[0].code.trim().split("\n").filter((line) => {
56
+ const trimmedLine = line.trim();
57
+ if (trimmedLine.startsWith("//#region ")) return ++openRegionsCount;
58
+ else if (trimmedLine.startsWith("//#endregion")) return openRegionsCount > 0 ? openRegionsCount-- : false;
59
+ return true;
60
+ }), ...Array(openRegionsCount).fill("//#endregion")].join("\n");
24
61
  return {
25
62
  isShaken: !code.split("\n").some((line) => {
26
- const t = line.trim();
27
- return t && !t.startsWith("import ") && !t.startsWith("//") && !t.startsWith("/*") && !t.startsWith("*") && !t.startsWith("*/");
63
+ const trimmedLine = line.trim();
64
+ return trimmedLine && !trimmedLine.startsWith("import ") && !trimmedLine.startsWith("//") && !trimmedLine.startsWith("/*") && !trimmedLine.startsWith("*") && !trimmedLine.startsWith("*/");
28
65
  }),
29
66
  code,
30
67
  warnings
@@ -32,41 +69,6 @@ const check = async (path, rollupOptions) => {
32
69
  };
33
70
  //#endregion
34
71
  //#region src/cli.ts
35
- const exitWithError = (message) => {
36
- console.error(message);
37
- process.exit(1);
38
- };
39
- const ifExists = async (path) => {
40
- try {
41
- await access(path);
42
- return path;
43
- } catch {
44
- return null;
45
- }
46
- };
47
- const isDirectory = async (path) => {
48
- try {
49
- return (await stat(path)).isDirectory();
50
- } catch {
51
- return false;
52
- }
53
- };
54
- const resolveEntry = async (value) => {
55
- if (await isDirectory(value)) return await ifExists(`${value}/index.mjs`) ?? await ifExists(`${value}/index.js`);
56
- return await ifExists(value) ?? await ifExists(`${value}.mjs`) ?? await ifExists(`${value}.js`);
57
- };
58
- const getInput = async () => {
59
- let pkgJson;
60
- try {
61
- pkgJson = JSON.parse(await readFile("package.json", "utf-8"));
62
- } catch {
63
- exitWithError(`Could not find or parse 'package.json'`);
64
- }
65
- const name = pkgJson.module ?? pkgJson.main ?? "index";
66
- const entryPath = await resolveEntry(name);
67
- if (!entryPath) exitWithError(`Could not resolve entry point '${name}' from package.json`);
68
- return entryPath;
69
- };
70
72
  const displayHelp = () => {
71
73
  console.log([
72
74
  `🍃 ${styleText(["bgGreen", "black"], " bagadoo ")}`,
@@ -81,8 +83,8 @@ const displayHelp = () => {
81
83
  " $ bagadoo [path] [options]",
82
84
  "",
83
85
  styleText("bold", "ARGUMENTS:"),
84
- ` ${styleText("cyan", "[path]")} Path to an entry file or package directory`,
85
- ` ${styleText(["gray", "italic"], " (default: read module or main from package.json)")}`,
86
+ ` ${styleText("cyan", "[path]")} Path to an entry file or package directory`,
87
+ ` ${styleText(["gray", "italic"], " (default: read module or main from package.json)")}`,
86
88
  "",
87
89
  styleText("bold", "OPTIONS:"),
88
90
  ` ${styleText("cyan", "-c, --config <file>")} Path to a Rollup config file`,
@@ -114,7 +116,7 @@ const getOptions = async () => {
114
116
  rollupOptions = config?.rollup ?? config;
115
117
  }
116
118
  const input = args[0];
117
- const absolutePath = input ? await resolveEntry(resolve(process.cwd(), input)) ?? resolve(process.cwd(), input) : resolve(process.cwd(), await getInput());
119
+ const absolutePath = await resolveEntry(input ? resolve(process.cwd(), input) : process.cwd());
118
120
  const relativePath = relative(process.cwd(), absolutePath);
119
121
  return {
120
122
  rollupOptions,
@@ -134,11 +136,12 @@ const getOptions = async () => {
134
136
  const result = await check(options.absolutePath, options.rollupOptions);
135
137
  result.warnings.forEach((w) => console.error(styleText("yellow", w)));
136
138
  result.code.split("\n").forEach((line) => {
137
- const t = line.trim();
138
- console.error(t.startsWith("//#region") || t.startsWith("//#endregion") ? styleText("gray", line) : line);
139
+ const trimmedLine = line.trim();
140
+ const isRegion = trimmedLine.startsWith("//#region") || trimmedLine.startsWith("//#endregion");
141
+ console.error(isRegion ? styleText("gray", line) : line);
139
142
  });
140
143
  if (result.isShaken) console.log(`\n${styleText("green", "✓ Success:")} ${styleText("cyan", options.relativePath)} ${styleText("green", "is fully tree-shakeable.")}`);
141
- else exitWithError(`\n${styleText("red", "✗ Failed:")} ${styleText("cyan", options.relativePath)} ${styleText("red", "is not fully tree-shakeable.")}`);
144
+ else console.error(`\n${styleText("red", "✗ Failed:")} ${styleText("cyan", options.relativePath)} ${styleText("red", "is not fully tree-shakeable.")}`);
142
145
  }
143
146
  })();
144
147
  //#endregion
package/index.mjs CHANGED
@@ -13,11 +13,17 @@ const check = async (path, rollupOptions) => {
13
13
  else if (warning.code !== "EMPTY_BUNDLE") warnings.push(warning.message);
14
14
  }
15
15
  })).generate({ format: "esm" });
16
- const code = output[0].code.trim();
16
+ let openRegionsCount = 0;
17
+ const code = [...output[0].code.trim().split("\n").filter((line) => {
18
+ const trimmedLine = line.trim();
19
+ if (trimmedLine.startsWith("//#region ")) return ++openRegionsCount;
20
+ else if (trimmedLine.startsWith("//#endregion")) return openRegionsCount > 0 ? openRegionsCount-- : false;
21
+ return true;
22
+ }), ...Array(openRegionsCount).fill("//#endregion")].join("\n");
17
23
  return {
18
24
  isShaken: !code.split("\n").some((line) => {
19
- const t = line.trim();
20
- return t && !t.startsWith("import ") && !t.startsWith("//") && !t.startsWith("/*") && !t.startsWith("*") && !t.startsWith("*/");
25
+ const trimmedLine = line.trim();
26
+ return trimmedLine && !trimmedLine.startsWith("import ") && !trimmedLine.startsWith("//") && !trimmedLine.startsWith("/*") && !trimmedLine.startsWith("*") && !trimmedLine.startsWith("*/");
21
27
  }),
22
28
  code,
23
29
  warnings
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@badisi/agadoo",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "Check whether a package is tree-shakeable.",
5
5
  "homepage": "https://github.com/Badisi/agadoo",
6
6
  "license": "MIT",