@cappa/cli 0.0.2 → 0.0.4
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.
- package/dist/index.d.mts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +158 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +130 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +18 -11
- package/CHANGELOG.md +0 -9
- package/src/config.ts +0 -136
- package/src/index.ts +0 -41
- package/src/types.d.ts +0 -37
- package/src/utils/getConfig.test.ts +0 -122
- package/src/utils/getConfig.ts +0 -54
- package/src/utils/getCosmiConfig.ts +0 -72
- package/src/utils/getPlugins.ts +0 -33
- package/tsconfig.json +0 -9
- package/tsdown.config.ts +0 -11
- package/vitest.config.ts +0 -8
package/dist/index.d.mts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
let citty = require("citty");
|
|
25
|
+
citty = __toESM(citty);
|
|
26
|
+
let consola = require("consola");
|
|
27
|
+
consola = __toESM(consola);
|
|
28
|
+
let cosmiconfig = require("cosmiconfig");
|
|
29
|
+
cosmiconfig = __toESM(cosmiconfig);
|
|
30
|
+
let jiti = require("jiti");
|
|
31
|
+
jiti = __toESM(jiti);
|
|
32
|
+
let __cappa_core = require("@cappa/core");
|
|
33
|
+
__cappa_core = __toESM(__cappa_core);
|
|
34
|
+
|
|
35
|
+
//#region package.json
|
|
36
|
+
var version = "0.0.3";
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/utils/getPlugins.ts
|
|
40
|
+
async function getPlugins(plugins) {
|
|
41
|
+
if (!plugins) return [];
|
|
42
|
+
const loadedPlugins = [];
|
|
43
|
+
for (const plugin of plugins) if (typeof plugin === "function") {
|
|
44
|
+
const pluginDef = plugin();
|
|
45
|
+
loadedPlugins.push(pluginDef);
|
|
46
|
+
} else loadedPlugins.push(plugin);
|
|
47
|
+
return loadedPlugins;
|
|
48
|
+
}
|
|
49
|
+
function isPromise(value) {
|
|
50
|
+
return value !== null && typeof value === "object" && "then" in value && typeof value.then === "function";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/utils/getConfig.ts
|
|
55
|
+
/**
|
|
56
|
+
* Converting UserConfig to Config without a change in the object beside the JSON convert.
|
|
57
|
+
*/
|
|
58
|
+
async function getConfig(result) {
|
|
59
|
+
const config = result?.config;
|
|
60
|
+
let kubbUserConfig = Promise.resolve(config);
|
|
61
|
+
if (typeof config === "function") {
|
|
62
|
+
const possiblePromise = config();
|
|
63
|
+
if (isPromise(possiblePromise)) kubbUserConfig = possiblePromise;
|
|
64
|
+
kubbUserConfig = Promise.resolve(possiblePromise);
|
|
65
|
+
}
|
|
66
|
+
let JSONConfig = await kubbUserConfig;
|
|
67
|
+
if (Array.isArray(JSONConfig)) {
|
|
68
|
+
const results = [];
|
|
69
|
+
for (const item of JSONConfig) {
|
|
70
|
+
const plugins = item.plugins ? await getPlugins(item.plugins) : void 0;
|
|
71
|
+
results.push({
|
|
72
|
+
...item,
|
|
73
|
+
plugins
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return results;
|
|
77
|
+
}
|
|
78
|
+
JSONConfig = {
|
|
79
|
+
...JSONConfig,
|
|
80
|
+
plugins: JSONConfig.plugins ? await getPlugins(JSONConfig.plugins) : void 0
|
|
81
|
+
};
|
|
82
|
+
return JSONConfig;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/utils/getCosmiConfig.ts
|
|
87
|
+
const tsLoader = async (configFile) => {
|
|
88
|
+
return await (0, jiti.createJiti)(require("url").pathToFileURL(__filename).href, {
|
|
89
|
+
jsx: {
|
|
90
|
+
runtime: "automatic",
|
|
91
|
+
importSource: "@cappa/core"
|
|
92
|
+
},
|
|
93
|
+
sourceMaps: true
|
|
94
|
+
}).import(configFile, { default: true });
|
|
95
|
+
};
|
|
96
|
+
async function getCosmiConfig(moduleName, config) {
|
|
97
|
+
const searchPlaces = [
|
|
98
|
+
"package.json",
|
|
99
|
+
`.${moduleName}rc`,
|
|
100
|
+
`.${moduleName}rc.json`,
|
|
101
|
+
`.${moduleName}rc.yaml`,
|
|
102
|
+
`.${moduleName}rc.yml`,
|
|
103
|
+
`.${moduleName}rc.ts`,
|
|
104
|
+
`.${moduleName}rc.js`,
|
|
105
|
+
`.${moduleName}rc.mjs`,
|
|
106
|
+
`.${moduleName}rc.cjs`,
|
|
107
|
+
`${moduleName}.config.ts`,
|
|
108
|
+
`${moduleName}.config.js`,
|
|
109
|
+
`${moduleName}.config.mjs`,
|
|
110
|
+
`${moduleName}.config.cjs`
|
|
111
|
+
];
|
|
112
|
+
const explorer = (0, cosmiconfig.cosmiconfig)(moduleName, {
|
|
113
|
+
cache: false,
|
|
114
|
+
searchPlaces: [
|
|
115
|
+
...searchPlaces.map((searchPlace) => {
|
|
116
|
+
return `.config/${searchPlace}`;
|
|
117
|
+
}),
|
|
118
|
+
...searchPlaces.map((searchPlace) => {
|
|
119
|
+
return `configs/${searchPlace}`;
|
|
120
|
+
}),
|
|
121
|
+
...searchPlaces
|
|
122
|
+
],
|
|
123
|
+
loaders: { ".ts": tsLoader }
|
|
124
|
+
});
|
|
125
|
+
const result = config ? await explorer.load(config) : await explorer.search();
|
|
126
|
+
if (result?.isEmpty || !result || !result.config) throw new Error("Config not defined, create a cappa.config.js or pass through your config with the option --config");
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region src/index.ts
|
|
132
|
+
const main = (0, citty.defineCommand)({
|
|
133
|
+
meta: {
|
|
134
|
+
name: "cappa",
|
|
135
|
+
version,
|
|
136
|
+
description: "Cappa CLI"
|
|
137
|
+
},
|
|
138
|
+
async setup() {
|
|
139
|
+
consola.default.log("Cappa CLI:");
|
|
140
|
+
const result = await getCosmiConfig("cappa");
|
|
141
|
+
const config = await getConfig(result);
|
|
142
|
+
consola.default.log(config);
|
|
143
|
+
const screenshotTool = new __cappa_core.ScreenshotTool({});
|
|
144
|
+
await screenshotTool.init();
|
|
145
|
+
try {
|
|
146
|
+
for (const plugin of config.plugins) await plugin.execute(screenshotTool);
|
|
147
|
+
} finally {
|
|
148
|
+
await screenshotTool.close();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
async function run(_argv) {
|
|
153
|
+
await (0, citty.runMain)(main);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
//#endregion
|
|
157
|
+
exports.run = run;
|
|
158
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["loadedPlugins: PluginDef[]","results: Array<Config>","ScreenshotTool"],"sources":["../package.json","../src/utils/getPlugins.ts","../src/utils/getConfig.ts","../src/utils/getCosmiConfig.ts","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@cappa/cli\",\n \"version\": \"0.0.3\",\n \"description\": \"Command line interface for Cappa\",\n \"author\": \"Tim Richter\",\n \"keywords\": [\n \"screenshot\",\n \"cli\"\n ],\n \"publishConfig\": {\n \"access\": \"public\"\n },\n \"license\": \"MIT\",\n \"main\": \"./dist/index.js\",\n \"module\": \"./dist/index.mjs\",\n \"types\": \"./dist/index.d.ts\",\n \"exports\": {\n \".\": {\n \"types\": \"./dist/index.d.ts\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\",\n \"default\": \"./dist/index.mjs\"\n },\n \"./package.json\": \"./package.json\"\n },\n \"files\": [\n \"dist\",\n \"bin\",\n \"package.json\",\n \"README.md\",\n \"LICENSE\"\n ],\n \"bin\": {\n \"cappa\": \"./bin/cappa.cjs\"\n },\n \"scripts\": {\n \"test\": \"vitest\",\n \"screenshot\": \"node src/index.ts\",\n \"build\": \"tsdown\"\n },\n \"dependencies\": {\n \"@cappa/core\": \"workspace:*\",\n \"citty\": \"0.1.6\",\n \"consola\": \"3.4.2\",\n \"cosmiconfig\": \"9.0.0\",\n \"jiti\": \"2.5.1\",\n \"picocolors\": \"1.1.1\",\n \"ts-node\": \"10.9.2\"\n },\n \"devDependencies\": {\n \"@types/node\": \"catalog:\",\n \"tsdown\": \"catalog:\",\n \"vitest\": \"catalog:\",\n \"@vitest/ui\": \"catalog:\",\n \"@vitest/coverage-v8\": \"catalog:\",\n \"typescript\": \"catalog:\"\n }\n}\n","import type { UserConfig, PluginDef } from \"@cappa/core\";\n\nexport async function getPlugins(\n plugins: UserConfig[\"plugins\"]\n): Promise<PluginDef[]> {\n if (!plugins) {\n return [];\n }\n\n const loadedPlugins: PluginDef[] = [];\n\n for (const plugin of plugins) {\n if (typeof plugin === \"function\") {\n // Plugin is a function that returns PluginDef\n const pluginDef = plugin();\n loadedPlugins.push(pluginDef);\n } else {\n // Plugin is already a PluginDef\n loadedPlugins.push(plugin);\n }\n }\n\n return loadedPlugins;\n}\n\nexport function isPromise(value: unknown): value is Promise<unknown> {\n return (\n value !== null &&\n typeof value === \"object\" &&\n \"then\" in value &&\n typeof (value as any).then === \"function\"\n );\n}\n","import { getPlugins, isPromise } from \"./getPlugins\";\n\nimport type { UserConfig } from \"@cappa/core\";\nimport type { CosmiconfigResult } from \"./getCosmiConfig.ts\";\nimport type { Config } from \"cosmiconfig\";\n\n/**\n * Converting UserConfig to Config without a change in the object beside the JSON convert.\n */\nexport async function getConfig(\n result: CosmiconfigResult\n): Promise<Array<Config> | Config> {\n const config = result?.config;\n let kubbUserConfig = Promise.resolve(config) as Promise<\n UserConfig | Array<UserConfig>\n >;\n\n // for ts or js files\n if (typeof config === \"function\") {\n const possiblePromise = config();\n if (isPromise(possiblePromise)) {\n kubbUserConfig = possiblePromise as Promise<\n UserConfig | Array<UserConfig>\n >;\n }\n kubbUserConfig = Promise.resolve(possiblePromise);\n }\n\n let JSONConfig = await kubbUserConfig;\n\n if (Array.isArray(JSONConfig)) {\n const results: Array<Config> = [];\n\n for (const item of JSONConfig) {\n const plugins = item.plugins ? await getPlugins(item.plugins) : undefined;\n\n results.push({\n ...item,\n plugins,\n } as Config);\n }\n\n return results;\n }\n\n JSONConfig = {\n ...JSONConfig,\n plugins: JSONConfig.plugins\n ? await getPlugins(JSONConfig.plugins)\n : undefined,\n };\n\n return JSONConfig as Config;\n}\n","import { cosmiconfig } from \"cosmiconfig\";\nimport { createJiti } from \"jiti\";\n\nimport type { UserConfig, defineConfig } from \"@cappa/core\";\n\nexport type CosmiconfigResult = {\n filepath: string;\n isEmpty?: boolean;\n config: ReturnType<typeof defineConfig> | UserConfig;\n};\n\nconst tsLoader = async (configFile: string) => {\n const jiti = createJiti(import.meta.url, {\n jsx: {\n runtime: \"automatic\",\n importSource: \"@cappa/core\",\n },\n sourceMaps: true,\n });\n\n const mod = await jiti.import(configFile, { default: true });\n\n return mod;\n};\n\nexport async function getCosmiConfig(\n moduleName: string,\n config?: string\n): Promise<CosmiconfigResult> {\n const searchPlaces = [\n \"package.json\",\n `.${moduleName}rc`,\n `.${moduleName}rc.json`,\n `.${moduleName}rc.yaml`,\n `.${moduleName}rc.yml`,\n\n `.${moduleName}rc.ts`,\n `.${moduleName}rc.js`,\n `.${moduleName}rc.mjs`,\n `.${moduleName}rc.cjs`,\n\n `${moduleName}.config.ts`,\n `${moduleName}.config.js`,\n `${moduleName}.config.mjs`,\n `${moduleName}.config.cjs`,\n ];\n const explorer = cosmiconfig(moduleName, {\n cache: false,\n searchPlaces: [\n ...searchPlaces.map((searchPlace) => {\n return `.config/${searchPlace}`;\n }),\n ...searchPlaces.map((searchPlace) => {\n return `configs/${searchPlace}`;\n }),\n ...searchPlaces,\n ],\n loaders: {\n \".ts\": tsLoader,\n },\n });\n\n const result = config ? await explorer.load(config) : await explorer.search();\n\n if (result?.isEmpty || !result || !result.config) {\n throw new Error(\n \"Config not defined, create a cappa.config.js or pass through your config with the option --config\"\n );\n }\n\n return result as CosmiconfigResult;\n}\n","import { defineCommand, runMain } from \"citty\";\nimport consola from \"consola\";\nimport { version } from \"../package.json\";\nimport { getConfig } from \"./utils/getConfig\";\nimport { getCosmiConfig } from \"./utils/getCosmiConfig\";\nimport { type PluginDef, ScreenshotTool } from \"@cappa/core\";\n\nconst name = \"cappa\";\n\nconst main = defineCommand({\n meta: {\n name,\n version,\n description: \"Cappa CLI\",\n },\n async setup() {\n consola.log(\"Cappa CLI:\");\n\n const result = await getCosmiConfig(\"cappa\");\n const config = await getConfig(result);\n\n consola.log(config);\n\n const screenshotTool = new ScreenshotTool({});\n await screenshotTool.init();\n\n try {\n // Wait for all plugins to complete\n for (const plugin of config.plugins) {\n await plugin.execute(screenshotTool);\n }\n } finally {\n // Always close the browser to ensure the process exits\n await screenshotTool.close();\n }\n },\n});\n\nexport async function run(_argv?: string[]): Promise<void> {\n await runMain(main);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAEa;;;;ACAb,eAAsB,WACpB,SACsB;AACtB,KAAI,CAAC,QACH,QAAO,EAAE;CAGX,MAAMA,gBAA6B,EAAE;AAErC,MAAK,MAAM,UAAU,QACnB,KAAI,OAAO,WAAW,YAAY;EAEhC,MAAM,YAAY,QAAQ;AAC1B,gBAAc,KAAK,UAAU;OAG7B,eAAc,KAAK,OAAO;AAI9B,QAAO;;AAGT,SAAgB,UAAU,OAA2C;AACnE,QACE,UAAU,QACV,OAAO,UAAU,YACjB,UAAU,SACV,OAAQ,MAAc,SAAS;;;;;;;;ACrBnC,eAAsB,UACpB,QACiC;CACjC,MAAM,SAAS,QAAQ;CACvB,IAAI,iBAAiB,QAAQ,QAAQ,OAAO;AAK5C,KAAI,OAAO,WAAW,YAAY;EAChC,MAAM,kBAAkB,QAAQ;AAChC,MAAI,UAAU,gBAAgB,CAC5B,kBAAiB;AAInB,mBAAiB,QAAQ,QAAQ,gBAAgB;;CAGnD,IAAI,aAAa,MAAM;AAEvB,KAAI,MAAM,QAAQ,WAAW,EAAE;EAC7B,MAAMC,UAAyB,EAAE;AAEjC,OAAK,MAAM,QAAQ,YAAY;GAC7B,MAAM,UAAU,KAAK,UAAU,MAAM,WAAW,KAAK,QAAQ,GAAG;AAEhE,WAAQ,KAAK;IACX,GAAG;IACH;IACD,CAAW;;AAGd,SAAO;;AAGT,cAAa;EACX,GAAG;EACH,SAAS,WAAW,UAChB,MAAM,WAAW,WAAW,QAAQ,GACpC;EACL;AAED,QAAO;;;;;ACzCT,MAAM,WAAW,OAAO,eAAuB;AAW7C,QAFY,0EAR6B;EACvC,KAAK;GACH,SAAS;GACT,cAAc;GACf;EACD,YAAY;EACb,CAAC,CAEqB,OAAO,YAAY,EAAE,SAAS,MAAM,CAAC;;AAK9D,eAAsB,eACpB,YACA,QAC4B;CAC5B,MAAM,eAAe;EACnB;EACA,IAAI,WAAW;EACf,IAAI,WAAW;EACf,IAAI,WAAW;EACf,IAAI,WAAW;EAEf,IAAI,WAAW;EACf,IAAI,WAAW;EACf,IAAI,WAAW;EACf,IAAI,WAAW;EAEf,GAAG,WAAW;EACd,GAAG,WAAW;EACd,GAAG,WAAW;EACd,GAAG,WAAW;EACf;CACD,MAAM,wCAAuB,YAAY;EACvC,OAAO;EACP,cAAc;GACZ,GAAG,aAAa,KAAK,gBAAgB;AACnC,WAAO,WAAW;KAClB;GACF,GAAG,aAAa,KAAK,gBAAgB;AACnC,WAAO,WAAW;KAClB;GACF,GAAG;GACJ;EACD,SAAS,EACP,OAAO,UACR;EACF,CAAC;CAEF,MAAM,SAAS,SAAS,MAAM,SAAS,KAAK,OAAO,GAAG,MAAM,SAAS,QAAQ;AAE7E,KAAI,QAAQ,WAAW,CAAC,UAAU,CAAC,OAAO,OACxC,OAAM,IAAI,MACR,oGACD;AAGH,QAAO;;;;;AC7DT,MAAM,gCAAqB;CACzB,MAAM;EACJ,MAJS;EAKT;EACA,aAAa;EACd;CACD,MAAM,QAAQ;AACZ,kBAAQ,IAAI,aAAa;EAEzB,MAAM,SAAS,MAAM,eAAe,QAAQ;EAC5C,MAAM,SAAS,MAAM,UAAU,OAAO;AAEtC,kBAAQ,IAAI,OAAO;EAEnB,MAAM,iBAAiB,IAAIC,4BAAe,EAAE,CAAC;AAC7C,QAAM,eAAe,MAAM;AAE3B,MAAI;AAEF,QAAK,MAAM,UAAU,OAAO,QAC1B,OAAM,OAAO,QAAQ,eAAe;YAE9B;AAER,SAAM,eAAe,OAAO;;;CAGjC,CAAC;AAEF,eAAsB,IAAI,OAAiC;AACzD,0BAAc,KAAK"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { defineCommand, runMain } from "citty";
|
|
2
|
+
import consola from "consola";
|
|
3
|
+
import { cosmiconfig } from "cosmiconfig";
|
|
4
|
+
import { createJiti } from "jiti";
|
|
5
|
+
import { ScreenshotTool } from "@cappa/core";
|
|
6
|
+
|
|
7
|
+
//#region package.json
|
|
8
|
+
var version = "0.0.3";
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/utils/getPlugins.ts
|
|
12
|
+
async function getPlugins(plugins) {
|
|
13
|
+
if (!plugins) return [];
|
|
14
|
+
const loadedPlugins = [];
|
|
15
|
+
for (const plugin of plugins) if (typeof plugin === "function") {
|
|
16
|
+
const pluginDef = plugin();
|
|
17
|
+
loadedPlugins.push(pluginDef);
|
|
18
|
+
} else loadedPlugins.push(plugin);
|
|
19
|
+
return loadedPlugins;
|
|
20
|
+
}
|
|
21
|
+
function isPromise(value) {
|
|
22
|
+
return value !== null && typeof value === "object" && "then" in value && typeof value.then === "function";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/utils/getConfig.ts
|
|
27
|
+
/**
|
|
28
|
+
* Converting UserConfig to Config without a change in the object beside the JSON convert.
|
|
29
|
+
*/
|
|
30
|
+
async function getConfig(result) {
|
|
31
|
+
const config = result?.config;
|
|
32
|
+
let kubbUserConfig = Promise.resolve(config);
|
|
33
|
+
if (typeof config === "function") {
|
|
34
|
+
const possiblePromise = config();
|
|
35
|
+
if (isPromise(possiblePromise)) kubbUserConfig = possiblePromise;
|
|
36
|
+
kubbUserConfig = Promise.resolve(possiblePromise);
|
|
37
|
+
}
|
|
38
|
+
let JSONConfig = await kubbUserConfig;
|
|
39
|
+
if (Array.isArray(JSONConfig)) {
|
|
40
|
+
const results = [];
|
|
41
|
+
for (const item of JSONConfig) {
|
|
42
|
+
const plugins = item.plugins ? await getPlugins(item.plugins) : void 0;
|
|
43
|
+
results.push({
|
|
44
|
+
...item,
|
|
45
|
+
plugins
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return results;
|
|
49
|
+
}
|
|
50
|
+
JSONConfig = {
|
|
51
|
+
...JSONConfig,
|
|
52
|
+
plugins: JSONConfig.plugins ? await getPlugins(JSONConfig.plugins) : void 0
|
|
53
|
+
};
|
|
54
|
+
return JSONConfig;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/utils/getCosmiConfig.ts
|
|
59
|
+
const tsLoader = async (configFile) => {
|
|
60
|
+
return await createJiti(import.meta.url, {
|
|
61
|
+
jsx: {
|
|
62
|
+
runtime: "automatic",
|
|
63
|
+
importSource: "@cappa/core"
|
|
64
|
+
},
|
|
65
|
+
sourceMaps: true
|
|
66
|
+
}).import(configFile, { default: true });
|
|
67
|
+
};
|
|
68
|
+
async function getCosmiConfig(moduleName, config) {
|
|
69
|
+
const searchPlaces = [
|
|
70
|
+
"package.json",
|
|
71
|
+
`.${moduleName}rc`,
|
|
72
|
+
`.${moduleName}rc.json`,
|
|
73
|
+
`.${moduleName}rc.yaml`,
|
|
74
|
+
`.${moduleName}rc.yml`,
|
|
75
|
+
`.${moduleName}rc.ts`,
|
|
76
|
+
`.${moduleName}rc.js`,
|
|
77
|
+
`.${moduleName}rc.mjs`,
|
|
78
|
+
`.${moduleName}rc.cjs`,
|
|
79
|
+
`${moduleName}.config.ts`,
|
|
80
|
+
`${moduleName}.config.js`,
|
|
81
|
+
`${moduleName}.config.mjs`,
|
|
82
|
+
`${moduleName}.config.cjs`
|
|
83
|
+
];
|
|
84
|
+
const explorer = cosmiconfig(moduleName, {
|
|
85
|
+
cache: false,
|
|
86
|
+
searchPlaces: [
|
|
87
|
+
...searchPlaces.map((searchPlace) => {
|
|
88
|
+
return `.config/${searchPlace}`;
|
|
89
|
+
}),
|
|
90
|
+
...searchPlaces.map((searchPlace) => {
|
|
91
|
+
return `configs/${searchPlace}`;
|
|
92
|
+
}),
|
|
93
|
+
...searchPlaces
|
|
94
|
+
],
|
|
95
|
+
loaders: { ".ts": tsLoader }
|
|
96
|
+
});
|
|
97
|
+
const result = config ? await explorer.load(config) : await explorer.search();
|
|
98
|
+
if (result?.isEmpty || !result || !result.config) throw new Error("Config not defined, create a cappa.config.js or pass through your config with the option --config");
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/index.ts
|
|
104
|
+
const main = defineCommand({
|
|
105
|
+
meta: {
|
|
106
|
+
name: "cappa",
|
|
107
|
+
version,
|
|
108
|
+
description: "Cappa CLI"
|
|
109
|
+
},
|
|
110
|
+
async setup() {
|
|
111
|
+
consola.log("Cappa CLI:");
|
|
112
|
+
const result = await getCosmiConfig("cappa");
|
|
113
|
+
const config = await getConfig(result);
|
|
114
|
+
consola.log(config);
|
|
115
|
+
const screenshotTool = new ScreenshotTool({});
|
|
116
|
+
await screenshotTool.init();
|
|
117
|
+
try {
|
|
118
|
+
for (const plugin of config.plugins) await plugin.execute(screenshotTool);
|
|
119
|
+
} finally {
|
|
120
|
+
await screenshotTool.close();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
async function run(_argv) {
|
|
125
|
+
await runMain(main);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
//#endregion
|
|
129
|
+
export { run };
|
|
130
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["loadedPlugins: PluginDef[]","results: Array<Config>"],"sources":["../package.json","../src/utils/getPlugins.ts","../src/utils/getConfig.ts","../src/utils/getCosmiConfig.ts","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@cappa/cli\",\n \"version\": \"0.0.3\",\n \"description\": \"Command line interface for Cappa\",\n \"author\": \"Tim Richter\",\n \"keywords\": [\n \"screenshot\",\n \"cli\"\n ],\n \"publishConfig\": {\n \"access\": \"public\"\n },\n \"license\": \"MIT\",\n \"main\": \"./dist/index.js\",\n \"module\": \"./dist/index.mjs\",\n \"types\": \"./dist/index.d.ts\",\n \"exports\": {\n \".\": {\n \"types\": \"./dist/index.d.ts\",\n \"import\": \"./dist/index.mjs\",\n \"require\": \"./dist/index.js\",\n \"default\": \"./dist/index.mjs\"\n },\n \"./package.json\": \"./package.json\"\n },\n \"files\": [\n \"dist\",\n \"bin\",\n \"package.json\",\n \"README.md\",\n \"LICENSE\"\n ],\n \"bin\": {\n \"cappa\": \"./bin/cappa.cjs\"\n },\n \"scripts\": {\n \"test\": \"vitest\",\n \"screenshot\": \"node src/index.ts\",\n \"build\": \"tsdown\"\n },\n \"dependencies\": {\n \"@cappa/core\": \"workspace:*\",\n \"citty\": \"0.1.6\",\n \"consola\": \"3.4.2\",\n \"cosmiconfig\": \"9.0.0\",\n \"jiti\": \"2.5.1\",\n \"picocolors\": \"1.1.1\",\n \"ts-node\": \"10.9.2\"\n },\n \"devDependencies\": {\n \"@types/node\": \"catalog:\",\n \"tsdown\": \"catalog:\",\n \"vitest\": \"catalog:\",\n \"@vitest/ui\": \"catalog:\",\n \"@vitest/coverage-v8\": \"catalog:\",\n \"typescript\": \"catalog:\"\n }\n}\n","import type { UserConfig, PluginDef } from \"@cappa/core\";\n\nexport async function getPlugins(\n plugins: UserConfig[\"plugins\"]\n): Promise<PluginDef[]> {\n if (!plugins) {\n return [];\n }\n\n const loadedPlugins: PluginDef[] = [];\n\n for (const plugin of plugins) {\n if (typeof plugin === \"function\") {\n // Plugin is a function that returns PluginDef\n const pluginDef = plugin();\n loadedPlugins.push(pluginDef);\n } else {\n // Plugin is already a PluginDef\n loadedPlugins.push(plugin);\n }\n }\n\n return loadedPlugins;\n}\n\nexport function isPromise(value: unknown): value is Promise<unknown> {\n return (\n value !== null &&\n typeof value === \"object\" &&\n \"then\" in value &&\n typeof (value as any).then === \"function\"\n );\n}\n","import { getPlugins, isPromise } from \"./getPlugins\";\n\nimport type { UserConfig } from \"@cappa/core\";\nimport type { CosmiconfigResult } from \"./getCosmiConfig.ts\";\nimport type { Config } from \"cosmiconfig\";\n\n/**\n * Converting UserConfig to Config without a change in the object beside the JSON convert.\n */\nexport async function getConfig(\n result: CosmiconfigResult\n): Promise<Array<Config> | Config> {\n const config = result?.config;\n let kubbUserConfig = Promise.resolve(config) as Promise<\n UserConfig | Array<UserConfig>\n >;\n\n // for ts or js files\n if (typeof config === \"function\") {\n const possiblePromise = config();\n if (isPromise(possiblePromise)) {\n kubbUserConfig = possiblePromise as Promise<\n UserConfig | Array<UserConfig>\n >;\n }\n kubbUserConfig = Promise.resolve(possiblePromise);\n }\n\n let JSONConfig = await kubbUserConfig;\n\n if (Array.isArray(JSONConfig)) {\n const results: Array<Config> = [];\n\n for (const item of JSONConfig) {\n const plugins = item.plugins ? await getPlugins(item.plugins) : undefined;\n\n results.push({\n ...item,\n plugins,\n } as Config);\n }\n\n return results;\n }\n\n JSONConfig = {\n ...JSONConfig,\n plugins: JSONConfig.plugins\n ? await getPlugins(JSONConfig.plugins)\n : undefined,\n };\n\n return JSONConfig as Config;\n}\n","import { cosmiconfig } from \"cosmiconfig\";\nimport { createJiti } from \"jiti\";\n\nimport type { UserConfig, defineConfig } from \"@cappa/core\";\n\nexport type CosmiconfigResult = {\n filepath: string;\n isEmpty?: boolean;\n config: ReturnType<typeof defineConfig> | UserConfig;\n};\n\nconst tsLoader = async (configFile: string) => {\n const jiti = createJiti(import.meta.url, {\n jsx: {\n runtime: \"automatic\",\n importSource: \"@cappa/core\",\n },\n sourceMaps: true,\n });\n\n const mod = await jiti.import(configFile, { default: true });\n\n return mod;\n};\n\nexport async function getCosmiConfig(\n moduleName: string,\n config?: string\n): Promise<CosmiconfigResult> {\n const searchPlaces = [\n \"package.json\",\n `.${moduleName}rc`,\n `.${moduleName}rc.json`,\n `.${moduleName}rc.yaml`,\n `.${moduleName}rc.yml`,\n\n `.${moduleName}rc.ts`,\n `.${moduleName}rc.js`,\n `.${moduleName}rc.mjs`,\n `.${moduleName}rc.cjs`,\n\n `${moduleName}.config.ts`,\n `${moduleName}.config.js`,\n `${moduleName}.config.mjs`,\n `${moduleName}.config.cjs`,\n ];\n const explorer = cosmiconfig(moduleName, {\n cache: false,\n searchPlaces: [\n ...searchPlaces.map((searchPlace) => {\n return `.config/${searchPlace}`;\n }),\n ...searchPlaces.map((searchPlace) => {\n return `configs/${searchPlace}`;\n }),\n ...searchPlaces,\n ],\n loaders: {\n \".ts\": tsLoader,\n },\n });\n\n const result = config ? await explorer.load(config) : await explorer.search();\n\n if (result?.isEmpty || !result || !result.config) {\n throw new Error(\n \"Config not defined, create a cappa.config.js or pass through your config with the option --config\"\n );\n }\n\n return result as CosmiconfigResult;\n}\n","import { defineCommand, runMain } from \"citty\";\nimport consola from \"consola\";\nimport { version } from \"../package.json\";\nimport { getConfig } from \"./utils/getConfig\";\nimport { getCosmiConfig } from \"./utils/getCosmiConfig\";\nimport { type PluginDef, ScreenshotTool } from \"@cappa/core\";\n\nconst name = \"cappa\";\n\nconst main = defineCommand({\n meta: {\n name,\n version,\n description: \"Cappa CLI\",\n },\n async setup() {\n consola.log(\"Cappa CLI:\");\n\n const result = await getCosmiConfig(\"cappa\");\n const config = await getConfig(result);\n\n consola.log(config);\n\n const screenshotTool = new ScreenshotTool({});\n await screenshotTool.init();\n\n try {\n // Wait for all plugins to complete\n for (const plugin of config.plugins) {\n await plugin.execute(screenshotTool);\n }\n } finally {\n // Always close the browser to ensure the process exits\n await screenshotTool.close();\n }\n },\n});\n\nexport async function run(_argv?: string[]): Promise<void> {\n await runMain(main);\n}\n"],"mappings":";;;;;;;cAEa;;;;ACAb,eAAsB,WACpB,SACsB;AACtB,KAAI,CAAC,QACH,QAAO,EAAE;CAGX,MAAMA,gBAA6B,EAAE;AAErC,MAAK,MAAM,UAAU,QACnB,KAAI,OAAO,WAAW,YAAY;EAEhC,MAAM,YAAY,QAAQ;AAC1B,gBAAc,KAAK,UAAU;OAG7B,eAAc,KAAK,OAAO;AAI9B,QAAO;;AAGT,SAAgB,UAAU,OAA2C;AACnE,QACE,UAAU,QACV,OAAO,UAAU,YACjB,UAAU,SACV,OAAQ,MAAc,SAAS;;;;;;;;ACrBnC,eAAsB,UACpB,QACiC;CACjC,MAAM,SAAS,QAAQ;CACvB,IAAI,iBAAiB,QAAQ,QAAQ,OAAO;AAK5C,KAAI,OAAO,WAAW,YAAY;EAChC,MAAM,kBAAkB,QAAQ;AAChC,MAAI,UAAU,gBAAgB,CAC5B,kBAAiB;AAInB,mBAAiB,QAAQ,QAAQ,gBAAgB;;CAGnD,IAAI,aAAa,MAAM;AAEvB,KAAI,MAAM,QAAQ,WAAW,EAAE;EAC7B,MAAMC,UAAyB,EAAE;AAEjC,OAAK,MAAM,QAAQ,YAAY;GAC7B,MAAM,UAAU,KAAK,UAAU,MAAM,WAAW,KAAK,QAAQ,GAAG;AAEhE,WAAQ,KAAK;IACX,GAAG;IACH;IACD,CAAW;;AAGd,SAAO;;AAGT,cAAa;EACX,GAAG;EACH,SAAS,WAAW,UAChB,MAAM,WAAW,WAAW,QAAQ,GACpC;EACL;AAED,QAAO;;;;;ACzCT,MAAM,WAAW,OAAO,eAAuB;AAW7C,QAFY,MARC,WAAW,OAAO,KAAK,KAAK;EACvC,KAAK;GACH,SAAS;GACT,cAAc;GACf;EACD,YAAY;EACb,CAAC,CAEqB,OAAO,YAAY,EAAE,SAAS,MAAM,CAAC;;AAK9D,eAAsB,eACpB,YACA,QAC4B;CAC5B,MAAM,eAAe;EACnB;EACA,IAAI,WAAW;EACf,IAAI,WAAW;EACf,IAAI,WAAW;EACf,IAAI,WAAW;EAEf,IAAI,WAAW;EACf,IAAI,WAAW;EACf,IAAI,WAAW;EACf,IAAI,WAAW;EAEf,GAAG,WAAW;EACd,GAAG,WAAW;EACd,GAAG,WAAW;EACd,GAAG,WAAW;EACf;CACD,MAAM,WAAW,YAAY,YAAY;EACvC,OAAO;EACP,cAAc;GACZ,GAAG,aAAa,KAAK,gBAAgB;AACnC,WAAO,WAAW;KAClB;GACF,GAAG,aAAa,KAAK,gBAAgB;AACnC,WAAO,WAAW;KAClB;GACF,GAAG;GACJ;EACD,SAAS,EACP,OAAO,UACR;EACF,CAAC;CAEF,MAAM,SAAS,SAAS,MAAM,SAAS,KAAK,OAAO,GAAG,MAAM,SAAS,QAAQ;AAE7E,KAAI,QAAQ,WAAW,CAAC,UAAU,CAAC,OAAO,OACxC,OAAM,IAAI,MACR,oGACD;AAGH,QAAO;;;;;AC7DT,MAAM,OAAO,cAAc;CACzB,MAAM;EACJ,MAJS;EAKT;EACA,aAAa;EACd;CACD,MAAM,QAAQ;AACZ,UAAQ,IAAI,aAAa;EAEzB,MAAM,SAAS,MAAM,eAAe,QAAQ;EAC5C,MAAM,SAAS,MAAM,UAAU,OAAO;AAEtC,UAAQ,IAAI,OAAO;EAEnB,MAAM,iBAAiB,IAAI,eAAe,EAAE,CAAC;AAC7C,QAAM,eAAe,MAAM;AAE3B,MAAI;AAEF,QAAK,MAAM,UAAU,OAAO,QAC1B,OAAM,OAAO,QAAQ,eAAe;YAE9B;AAER,SAAM,eAAe,OAAO;;;CAGjC,CAAC;AAEF,eAAsB,IAAI,OAAiC;AACzD,OAAM,QAAQ,KAAK"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cappa/cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Command line interface for Cappa",
|
|
5
5
|
"author": "Tim Richter",
|
|
6
6
|
"keywords": [
|
|
@@ -12,6 +12,22 @@
|
|
|
12
12
|
},
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"main": "./dist/index.js",
|
|
15
|
+
"module": "./dist/index.mjs",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"require": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./package.json": "./package.json"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"bin",
|
|
27
|
+
"package.json",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
15
31
|
"bin": {
|
|
16
32
|
"cappa": "./bin/cappa.cjs"
|
|
17
33
|
},
|
|
@@ -22,7 +38,7 @@
|
|
|
22
38
|
"jiti": "2.5.1",
|
|
23
39
|
"picocolors": "1.1.1",
|
|
24
40
|
"ts-node": "10.9.2",
|
|
25
|
-
"@cappa/core": "0.0.
|
|
41
|
+
"@cappa/core": "0.0.4"
|
|
26
42
|
},
|
|
27
43
|
"devDependencies": {
|
|
28
44
|
"@types/node": "22.13.10",
|
|
@@ -32,15 +48,6 @@
|
|
|
32
48
|
"@vitest/coverage-v8": "3.2.4",
|
|
33
49
|
"typescript": "5.7.3"
|
|
34
50
|
},
|
|
35
|
-
"module": "./dist/index.mjs",
|
|
36
|
-
"types": "./dist/index.d.ts",
|
|
37
|
-
"exports": {
|
|
38
|
-
".": {
|
|
39
|
-
"import": "./dist/index.mjs",
|
|
40
|
-
"require": "./dist/index.js"
|
|
41
|
-
},
|
|
42
|
-
"./package.json": "./package.json"
|
|
43
|
-
},
|
|
44
51
|
"scripts": {
|
|
45
52
|
"test": "vitest",
|
|
46
53
|
"screenshot": "node src/index.ts",
|
package/CHANGELOG.md
DELETED
package/src/config.ts
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Configuration system for Cappa CLI
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
// TypeScript type definitions
|
|
6
|
-
export interface BrowserConfig {
|
|
7
|
-
type?: "chromium" | "firefox" | "webkit";
|
|
8
|
-
headless?: boolean;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface ScreenshotConfig {
|
|
12
|
-
outputDir?: string;
|
|
13
|
-
viewport?: { width: number; height: number };
|
|
14
|
-
quality?: number;
|
|
15
|
-
fullPage?: boolean;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface PluginConfig {
|
|
19
|
-
name?: string;
|
|
20
|
-
path?: string;
|
|
21
|
-
options?: Record<string, any>;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface CappaConfig {
|
|
25
|
-
browser?: BrowserConfig;
|
|
26
|
-
screenshot?: ScreenshotConfig;
|
|
27
|
-
plugins?: (string | PluginConfig)[];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Define a Cappa configuration
|
|
32
|
-
* @param {CappaConfig} config - Configuration object
|
|
33
|
-
* @returns {CappaConfig} The configuration object
|
|
34
|
-
*/
|
|
35
|
-
export function defineConfig(config: CappaConfig): CappaConfig {
|
|
36
|
-
return config;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Default configuration
|
|
41
|
-
*/
|
|
42
|
-
const defaultConfig: CappaConfig = {
|
|
43
|
-
plugins: [],
|
|
44
|
-
browser: {
|
|
45
|
-
type: "chromium",
|
|
46
|
-
headless: true,
|
|
47
|
-
},
|
|
48
|
-
screenshot: {
|
|
49
|
-
outputDir: "./screenshots",
|
|
50
|
-
viewport: { width: 1920, height: 1080 },
|
|
51
|
-
quality: 90,
|
|
52
|
-
fullPage: true,
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Merge configuration with defaults
|
|
58
|
-
* @param {CappaConfig} userConfig - User configuration
|
|
59
|
-
* @returns {CappaConfig} Merged configuration
|
|
60
|
-
*/
|
|
61
|
-
function mergeConfig(userConfig: CappaConfig): CappaConfig {
|
|
62
|
-
return {
|
|
63
|
-
...defaultConfig,
|
|
64
|
-
...userConfig,
|
|
65
|
-
browser: {
|
|
66
|
-
...defaultConfig.browser,
|
|
67
|
-
...userConfig.browser,
|
|
68
|
-
},
|
|
69
|
-
screenshot: {
|
|
70
|
-
...defaultConfig.screenshot,
|
|
71
|
-
...userConfig.screenshot,
|
|
72
|
-
},
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Load configuration from file
|
|
78
|
-
* @param {string} configPath - Path to config file
|
|
79
|
-
* @returns {Promise<CappaConfig>} Configuration object
|
|
80
|
-
*/
|
|
81
|
-
async function loadConfig(configPath: string): Promise<CappaConfig> {
|
|
82
|
-
try {
|
|
83
|
-
let config: any;
|
|
84
|
-
const path = await import("path");
|
|
85
|
-
const { fileURLToPath } = await import("url");
|
|
86
|
-
const resolvedPath = path.resolve(configPath);
|
|
87
|
-
|
|
88
|
-
if (configPath.endsWith(".ts")) {
|
|
89
|
-
// Load TypeScript config using dynamic import
|
|
90
|
-
const module = await import(resolvedPath);
|
|
91
|
-
config = module.default || module;
|
|
92
|
-
} else if (configPath.endsWith(".js")) {
|
|
93
|
-
// Load JavaScript config
|
|
94
|
-
const module = await import(resolvedPath);
|
|
95
|
-
config = module.default || module;
|
|
96
|
-
} else {
|
|
97
|
-
throw new Error(`Unsupported config file format: ${configPath}`);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Handle default export
|
|
101
|
-
if (config.default) {
|
|
102
|
-
config = config.default;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return mergeConfig(config);
|
|
106
|
-
} catch (error: any) {
|
|
107
|
-
console.error(`Error loading config from ${configPath}:`, error.message);
|
|
108
|
-
throw error;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Find configuration file in current directory
|
|
114
|
-
* @returns {string|null} Path to config file or null if not found
|
|
115
|
-
*/
|
|
116
|
-
async function findConfigFile(): Promise<string | null> {
|
|
117
|
-
const fs = await import("fs");
|
|
118
|
-
const path = await import("path");
|
|
119
|
-
|
|
120
|
-
const configFiles = [
|
|
121
|
-
"cappa.config.ts",
|
|
122
|
-
"cappa.config.js",
|
|
123
|
-
"cappa.config.json",
|
|
124
|
-
];
|
|
125
|
-
|
|
126
|
-
for (const file of configFiles) {
|
|
127
|
-
const filePath = path.join(process.cwd(), file);
|
|
128
|
-
if (fs.existsSync(filePath)) {
|
|
129
|
-
return filePath;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export { defaultConfig, mergeConfig, loadConfig, findConfigFile };
|
package/src/index.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { defineCommand, runMain } from "citty";
|
|
2
|
-
import consola from "consola";
|
|
3
|
-
import { version } from "../package.json";
|
|
4
|
-
import { getConfig } from "./utils/getConfig";
|
|
5
|
-
import { getCosmiConfig } from "./utils/getCosmiConfig";
|
|
6
|
-
import { type PluginDef, ScreenshotTool } from "@cappa/core";
|
|
7
|
-
|
|
8
|
-
const name = "cappa";
|
|
9
|
-
|
|
10
|
-
const main = defineCommand({
|
|
11
|
-
meta: {
|
|
12
|
-
name,
|
|
13
|
-
version,
|
|
14
|
-
description: "Cappa CLI",
|
|
15
|
-
},
|
|
16
|
-
async setup() {
|
|
17
|
-
consola.log("Cappa CLI:");
|
|
18
|
-
|
|
19
|
-
const result = await getCosmiConfig("cappa");
|
|
20
|
-
const config = await getConfig(result);
|
|
21
|
-
|
|
22
|
-
consola.log(config);
|
|
23
|
-
|
|
24
|
-
const screenshotTool = new ScreenshotTool({});
|
|
25
|
-
await screenshotTool.init();
|
|
26
|
-
|
|
27
|
-
try {
|
|
28
|
-
// Wait for all plugins to complete
|
|
29
|
-
for (const plugin of config.plugins) {
|
|
30
|
-
await plugin.execute(screenshotTool);
|
|
31
|
-
}
|
|
32
|
-
} finally {
|
|
33
|
-
// Always close the browser to ensure the process exits
|
|
34
|
-
await screenshotTool.close();
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
export async function run(_argv?: string[]): Promise<void> {
|
|
40
|
-
await runMain(main);
|
|
41
|
-
}
|
package/src/types.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TypeScript type definitions for Cappa CLI configuration
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export interface BrowserConfig {
|
|
6
|
-
type?: "chromium" | "firefox" | "webkit";
|
|
7
|
-
headless?: boolean;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface ScreenshotConfig {
|
|
11
|
-
outputDir?: string;
|
|
12
|
-
viewport?: {
|
|
13
|
-
width: number;
|
|
14
|
-
height: number;
|
|
15
|
-
};
|
|
16
|
-
quality?: number;
|
|
17
|
-
fullPage?: boolean;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface PluginConfig {
|
|
21
|
-
name?: string;
|
|
22
|
-
path?: string;
|
|
23
|
-
options?: Record<string, any>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface CappaConfig {
|
|
27
|
-
browser?: BrowserConfig;
|
|
28
|
-
screenshot?: ScreenshotConfig;
|
|
29
|
-
plugins?: (string | PluginConfig)[];
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Define a Cappa configuration
|
|
34
|
-
* @param config - Configuration object
|
|
35
|
-
* @returns The configuration object
|
|
36
|
-
*/
|
|
37
|
-
export function defineConfig(config: CappaConfig): CappaConfig;
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from "@cappa/core";
|
|
2
|
-
|
|
3
|
-
import { getConfig } from "./getConfig.ts";
|
|
4
|
-
|
|
5
|
-
import type { CosmiconfigResult } from "./getCosmiConfig.ts";
|
|
6
|
-
|
|
7
|
-
test("return Config when config is set with defineConfig", async () => {
|
|
8
|
-
const config: CosmiconfigResult["config"] = defineConfig(() => {
|
|
9
|
-
return {
|
|
10
|
-
input: {
|
|
11
|
-
path: "./",
|
|
12
|
-
},
|
|
13
|
-
output: {
|
|
14
|
-
path: "./dist",
|
|
15
|
-
},
|
|
16
|
-
plugins: [],
|
|
17
|
-
};
|
|
18
|
-
});
|
|
19
|
-
const kubbUserConfig = await getConfig({
|
|
20
|
-
config,
|
|
21
|
-
filepath: "./",
|
|
22
|
-
isEmpty: false,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
expect(kubbUserConfig).toEqual({
|
|
26
|
-
input: {
|
|
27
|
-
path: "./",
|
|
28
|
-
},
|
|
29
|
-
output: {
|
|
30
|
-
path: "./dist",
|
|
31
|
-
},
|
|
32
|
-
plugins: [],
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test("return Config[] when config is set with defineConfig", async () => {
|
|
37
|
-
const config: CosmiconfigResult["config"] = defineConfig(() => {
|
|
38
|
-
return [
|
|
39
|
-
{
|
|
40
|
-
name: "1",
|
|
41
|
-
input: {
|
|
42
|
-
path: "./",
|
|
43
|
-
},
|
|
44
|
-
output: {
|
|
45
|
-
path: "./dist",
|
|
46
|
-
},
|
|
47
|
-
plugins: [],
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
name: "2",
|
|
51
|
-
input: {
|
|
52
|
-
path: "./",
|
|
53
|
-
},
|
|
54
|
-
output: {
|
|
55
|
-
path: "./dist2",
|
|
56
|
-
},
|
|
57
|
-
plugins: [],
|
|
58
|
-
},
|
|
59
|
-
];
|
|
60
|
-
});
|
|
61
|
-
const loadedConfig = await getConfig({
|
|
62
|
-
config,
|
|
63
|
-
filepath: "./",
|
|
64
|
-
isEmpty: false,
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
expect(Array.isArray(loadedConfig)).toBeTruthy();
|
|
68
|
-
|
|
69
|
-
if (Array.isArray(loadedConfig)) {
|
|
70
|
-
const [kubbUserConfig1, kubbUserConfig2] = loadedConfig;
|
|
71
|
-
|
|
72
|
-
expect(kubbUserConfig1).toEqual({
|
|
73
|
-
name: "1",
|
|
74
|
-
input: {
|
|
75
|
-
path: "./",
|
|
76
|
-
},
|
|
77
|
-
output: {
|
|
78
|
-
path: "./dist",
|
|
79
|
-
},
|
|
80
|
-
plugins: [],
|
|
81
|
-
});
|
|
82
|
-
expect(kubbUserConfig2).toEqual({
|
|
83
|
-
name: "2",
|
|
84
|
-
input: {
|
|
85
|
-
path: "./",
|
|
86
|
-
},
|
|
87
|
-
output: {
|
|
88
|
-
path: "./dist2",
|
|
89
|
-
},
|
|
90
|
-
plugins: [],
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test("return object when config is set with defineConfig and returns a promise", async () => {
|
|
96
|
-
const config: CosmiconfigResult["config"] = defineConfig(() => {
|
|
97
|
-
return Promise.resolve({
|
|
98
|
-
input: {
|
|
99
|
-
path: "./",
|
|
100
|
-
},
|
|
101
|
-
output: {
|
|
102
|
-
path: "./dist",
|
|
103
|
-
},
|
|
104
|
-
plugins: [],
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
const kubbUserConfig = await getConfig({
|
|
108
|
-
config,
|
|
109
|
-
filepath: "./",
|
|
110
|
-
isEmpty: false,
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
expect(kubbUserConfig).toEqual({
|
|
114
|
-
input: {
|
|
115
|
-
path: "./",
|
|
116
|
-
},
|
|
117
|
-
output: {
|
|
118
|
-
path: "./dist",
|
|
119
|
-
},
|
|
120
|
-
plugins: [],
|
|
121
|
-
});
|
|
122
|
-
});
|
package/src/utils/getConfig.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { getPlugins, isPromise } from "./getPlugins";
|
|
2
|
-
|
|
3
|
-
import type { UserConfig } from "@cappa/core";
|
|
4
|
-
import type { CosmiconfigResult } from "./getCosmiConfig.ts";
|
|
5
|
-
import type { Config } from "cosmiconfig";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Converting UserConfig to Config without a change in the object beside the JSON convert.
|
|
9
|
-
*/
|
|
10
|
-
export async function getConfig(
|
|
11
|
-
result: CosmiconfigResult
|
|
12
|
-
): Promise<Array<Config> | Config> {
|
|
13
|
-
const config = result?.config;
|
|
14
|
-
let kubbUserConfig = Promise.resolve(config) as Promise<
|
|
15
|
-
UserConfig | Array<UserConfig>
|
|
16
|
-
>;
|
|
17
|
-
|
|
18
|
-
// for ts or js files
|
|
19
|
-
if (typeof config === "function") {
|
|
20
|
-
const possiblePromise = config();
|
|
21
|
-
if (isPromise(possiblePromise)) {
|
|
22
|
-
kubbUserConfig = possiblePromise as Promise<
|
|
23
|
-
UserConfig | Array<UserConfig>
|
|
24
|
-
>;
|
|
25
|
-
}
|
|
26
|
-
kubbUserConfig = Promise.resolve(possiblePromise);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
let JSONConfig = await kubbUserConfig;
|
|
30
|
-
|
|
31
|
-
if (Array.isArray(JSONConfig)) {
|
|
32
|
-
const results: Array<Config> = [];
|
|
33
|
-
|
|
34
|
-
for (const item of JSONConfig) {
|
|
35
|
-
const plugins = item.plugins ? await getPlugins(item.plugins) : undefined;
|
|
36
|
-
|
|
37
|
-
results.push({
|
|
38
|
-
...item,
|
|
39
|
-
plugins,
|
|
40
|
-
} as Config);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return results;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
JSONConfig = {
|
|
47
|
-
...JSONConfig,
|
|
48
|
-
plugins: JSONConfig.plugins
|
|
49
|
-
? await getPlugins(JSONConfig.plugins)
|
|
50
|
-
: undefined,
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
return JSONConfig as Config;
|
|
54
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { cosmiconfig } from "cosmiconfig";
|
|
2
|
-
import { createJiti } from "jiti";
|
|
3
|
-
|
|
4
|
-
import type { UserConfig, defineConfig } from "@cappa/core";
|
|
5
|
-
|
|
6
|
-
export type CosmiconfigResult = {
|
|
7
|
-
filepath: string;
|
|
8
|
-
isEmpty?: boolean;
|
|
9
|
-
config: ReturnType<typeof defineConfig> | UserConfig;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const tsLoader = async (configFile: string) => {
|
|
13
|
-
const jiti = createJiti(import.meta.url, {
|
|
14
|
-
jsx: {
|
|
15
|
-
runtime: "automatic",
|
|
16
|
-
importSource: "@cappa/core",
|
|
17
|
-
},
|
|
18
|
-
sourceMaps: true,
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
const mod = await jiti.import(configFile, { default: true });
|
|
22
|
-
|
|
23
|
-
return mod;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export async function getCosmiConfig(
|
|
27
|
-
moduleName: string,
|
|
28
|
-
config?: string
|
|
29
|
-
): Promise<CosmiconfigResult> {
|
|
30
|
-
const searchPlaces = [
|
|
31
|
-
"package.json",
|
|
32
|
-
`.${moduleName}rc`,
|
|
33
|
-
`.${moduleName}rc.json`,
|
|
34
|
-
`.${moduleName}rc.yaml`,
|
|
35
|
-
`.${moduleName}rc.yml`,
|
|
36
|
-
|
|
37
|
-
`.${moduleName}rc.ts`,
|
|
38
|
-
`.${moduleName}rc.js`,
|
|
39
|
-
`.${moduleName}rc.mjs`,
|
|
40
|
-
`.${moduleName}rc.cjs`,
|
|
41
|
-
|
|
42
|
-
`${moduleName}.config.ts`,
|
|
43
|
-
`${moduleName}.config.js`,
|
|
44
|
-
`${moduleName}.config.mjs`,
|
|
45
|
-
`${moduleName}.config.cjs`,
|
|
46
|
-
];
|
|
47
|
-
const explorer = cosmiconfig(moduleName, {
|
|
48
|
-
cache: false,
|
|
49
|
-
searchPlaces: [
|
|
50
|
-
...searchPlaces.map((searchPlace) => {
|
|
51
|
-
return `.config/${searchPlace}`;
|
|
52
|
-
}),
|
|
53
|
-
...searchPlaces.map((searchPlace) => {
|
|
54
|
-
return `configs/${searchPlace}`;
|
|
55
|
-
}),
|
|
56
|
-
...searchPlaces,
|
|
57
|
-
],
|
|
58
|
-
loaders: {
|
|
59
|
-
".ts": tsLoader,
|
|
60
|
-
},
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
const result = config ? await explorer.load(config) : await explorer.search();
|
|
64
|
-
|
|
65
|
-
if (result?.isEmpty || !result || !result.config) {
|
|
66
|
-
throw new Error(
|
|
67
|
-
"Config not defined, create a cappa.config.js or pass through your config with the option --config"
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return result as CosmiconfigResult;
|
|
72
|
-
}
|
package/src/utils/getPlugins.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { UserConfig, PluginDef } from "@cappa/core";
|
|
2
|
-
|
|
3
|
-
export async function getPlugins(
|
|
4
|
-
plugins: UserConfig["plugins"]
|
|
5
|
-
): Promise<PluginDef[]> {
|
|
6
|
-
if (!plugins) {
|
|
7
|
-
return [];
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const loadedPlugins: PluginDef[] = [];
|
|
11
|
-
|
|
12
|
-
for (const plugin of plugins) {
|
|
13
|
-
if (typeof plugin === "function") {
|
|
14
|
-
// Plugin is a function that returns PluginDef
|
|
15
|
-
const pluginDef = plugin();
|
|
16
|
-
loadedPlugins.push(pluginDef);
|
|
17
|
-
} else {
|
|
18
|
-
// Plugin is already a PluginDef
|
|
19
|
-
loadedPlugins.push(plugin);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return loadedPlugins;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function isPromise(value: unknown): value is Promise<unknown> {
|
|
27
|
-
return (
|
|
28
|
-
value !== null &&
|
|
29
|
-
typeof value === "object" &&
|
|
30
|
-
"then" in value &&
|
|
31
|
-
typeof (value as any).then === "function"
|
|
32
|
-
);
|
|
33
|
-
}
|
package/tsconfig.json
DELETED
package/tsdown.config.ts
DELETED