@ispriter/cli 2.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 iazrael.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a
6
+ copy of this software and associated documentation files (the "Software"),
7
+ to deal in the Software without restriction, including without limitation
8
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
+ and/or sell copies of the Software, and to permit persons to whom the
10
+ Software is furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
+ DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,2 @@
1
+
2
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,138 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/index.ts
4
+ import { Command } from "commander";
5
+ import { Spriter } from "@ispriter/core";
6
+ import { parseConfig } from "@ispriter/core";
7
+ import { readFile, writeFile, mkdir, glob } from "fs/promises";
8
+ import path from "path";
9
+ import { fileURLToPath } from "url";
10
+ var __dirname = path.dirname(fileURLToPath(import.meta.url));
11
+ var program = new Command();
12
+ program.name("ispriter").description("CSS sprite generator").version("2.0.0-alpha.1");
13
+ function createRunAction() {
14
+ return async (opts) => {
15
+ try {
16
+ const config = await resolveConfig(opts);
17
+ await runSpriter(config);
18
+ if (opts.watch) {
19
+ await startWatch(config);
20
+ }
21
+ } catch (e) {
22
+ if (e.name === "IspriterError") {
23
+ console.error(`\u274C ${e.code}: ${e.message}`);
24
+ process.exit(1);
25
+ }
26
+ throw e;
27
+ }
28
+ };
29
+ }
30
+ program.command("run").description("Generate sprites from CSS files").option("-c, --config <path>", "config file path (JSON)").option("-f, --files <paths>", "CSS files (comma separated)").option("-o, --output <path>", "CSS output directory").option("--watch", "watch mode (not yet implemented)").action(createRunAction());
31
+ program.option("-c, --config <path>", "config file path (JSON)").option("-f, --files <paths>", "CSS files (comma separated)").option("-o, --output <path>", "CSS output directory").option("--watch", "watch for file changes and regenerate").action(createRunAction());
32
+ async function resolveConfig(opts) {
33
+ if (opts.config) {
34
+ const configPath = path.resolve(opts.config);
35
+ try {
36
+ const raw = await readFile(configPath, "utf-8");
37
+ return JSON.parse(raw);
38
+ } catch (e) {
39
+ throw new Error(`Failed to read/parse config file "${configPath}": ${e.message}`);
40
+ }
41
+ }
42
+ if (opts.files) {
43
+ const files = opts.files.split(",").map((f) => f.trim());
44
+ return {
45
+ input: { cssSource: files },
46
+ output: { cssDist: opts.output || "./sprite/css/" }
47
+ };
48
+ }
49
+ throw new Error("Either --config or --files is required");
50
+ }
51
+ async function runSpriter(config) {
52
+ const resolved = parseConfig(config);
53
+ const workspace = path.resolve(resolved.workspace);
54
+ const cssSource = Array.isArray(resolved.input.cssSource) ? resolved.input.cssSource : [resolved.input.cssSource];
55
+ const cssFiles = /* @__PURE__ */ new Map();
56
+ for (const pattern of cssSource) {
57
+ const absPattern = path.resolve(workspace, pattern);
58
+ const matches = await Array.fromAsync(glob(absPattern));
59
+ for (const file of matches) {
60
+ if (!file.endsWith(".css")) continue;
61
+ const content = await readFile(file, "utf-8");
62
+ cssFiles.set(file, content);
63
+ }
64
+ }
65
+ if (cssFiles.size === 0) {
66
+ console.warn("\u26A0\uFE0F No CSS files found");
67
+ return;
68
+ }
69
+ const images = /* @__PURE__ */ new Map();
70
+ const allUrls = [];
71
+ for (const [, css] of cssFiles) {
72
+ const urlMatches = css.matchAll(/url\(['"]?([^'")]+?)['"]?\)/g);
73
+ for (const m of urlMatches) {
74
+ const rawUrl = m[1];
75
+ if (rawUrl.startsWith("data:") || rawUrl.startsWith("http")) continue;
76
+ if (rawUrl.includes("#unsprite")) continue;
77
+ const clean = rawUrl.replace(/\?[^#]*/g, "").replace(/#.*/g, "");
78
+ if (!allUrls.includes(clean)) allUrls.push(clean);
79
+ }
80
+ }
81
+ for (const [cssFile] of cssFiles) {
82
+ const cssDir = path.dirname(cssFile);
83
+ for (const url of allUrls) {
84
+ if (images.has(url)) continue;
85
+ const imgPath = path.resolve(cssDir, url);
86
+ if (!imgPath.startsWith(workspace)) continue;
87
+ try {
88
+ const buf = await readFile(imgPath);
89
+ images.set(url, buf);
90
+ } catch {
91
+ }
92
+ }
93
+ }
94
+ console.log(`\u{1F4E6} Processing ${cssFiles.size} CSS files with ${images.size} images...`);
95
+ const spriter = new Spriter(config);
96
+ const result = await spriter.run({ css: cssFiles, images, cssBaseDir: workspace });
97
+ const cssDist = path.resolve(workspace, resolved.output.cssDist);
98
+ const imgDist = path.resolve(cssDist, resolved.output.imageDist);
99
+ await mkdir(imgDist, { recursive: true });
100
+ for (const [name, buf] of result.spriteImages) {
101
+ await writeFile(path.join(imgDist, name), buf);
102
+ }
103
+ for (const [name, content] of result.cssFiles) {
104
+ const outName = path.basename(name);
105
+ await writeFile(path.join(cssDist, outName), content);
106
+ }
107
+ console.log(`\u2705 Generated ${result.spriteImages.size} sprite(s), updated ${result.cssFiles.size} CSS file(s)`);
108
+ if (result.skippedImages.length > 0) {
109
+ console.warn(`\u26A0\uFE0F Skipped ${result.skippedImages.length} image(s): ${result.skippedImages.join(", ")}`);
110
+ }
111
+ }
112
+ async function startWatch(config) {
113
+ const { watch } = await import("chokidar");
114
+ const resolved = parseConfig(config);
115
+ const workspace = path.resolve(resolved.workspace);
116
+ console.log("\u{1F440} Watching for changes...");
117
+ const cssPatterns = Array.isArray(resolved.input.cssSource) ? resolved.input.cssSource : [resolved.input.cssSource];
118
+ const watcher = watch(cssPatterns.map((p) => path.resolve(workspace, p)), {
119
+ ignoreInitial: true
120
+ });
121
+ watcher.on("change", async (file) => {
122
+ console.log(`\u{1F4DD} ${file} changed, regenerating...`);
123
+ try {
124
+ await runSpriter(config);
125
+ } catch (e) {
126
+ console.error(`\u274C Error: ${e.message}`);
127
+ }
128
+ });
129
+ watcher.on("add", async (file) => {
130
+ console.log(`\u2795 ${file} added, regenerating...`);
131
+ try {
132
+ await runSpriter(config);
133
+ } catch (e) {
134
+ console.error(`\u274C Error: ${e.message}`);
135
+ }
136
+ });
137
+ }
138
+ program.parse();
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@ispriter/cli",
3
+ "version": "2.0.0",
4
+ "type": "module",
5
+ "bin": {
6
+ "ispriter": "./dist/index.js"
7
+ },
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "dependencies": {
11
+ "chokidar": "^5.0.0",
12
+ "commander": "^12.1.0",
13
+ "@ispriter/shared": "2.0.0",
14
+ "@ispriter/core": "2.0.0"
15
+ },
16
+ "devDependencies": {
17
+ "@types/node": "^22.10.0",
18
+ "tsup": "^8.4.0"
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "engines": {
24
+ "node": ">=20.0.0"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "scripts": {
30
+ "build": "tsup",
31
+ "dev": "tsup --watch"
32
+ }
33
+ }