svseeds 0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 scirexs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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 FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # svseeds - the SvSeeds CLI
2
+ A CLI to copy SvSeeds components for Svelte.
3
+
4
+ ## Quick Start
5
+ ```
6
+ npx svseeds
7
+ ```
8
+
9
+ ## Basic Usage
10
+ Copy specified SvSeeds files.
11
+ ```
12
+ npx svseeds [components...]
13
+ ```
14
+
15
+ ## Main Options
16
+ - Specify directory:
17
+ ```
18
+ npx svseeds -d <directory> [components...]
19
+ ```
20
+
21
+ - Specify all components:
22
+ ```
23
+ npx svseeds -a
24
+ ```
25
+
26
+ - Update copied components:
27
+ ```
28
+ npx svseeds -u [components...]
29
+ ```
30
+
31
+ - Remove components:
32
+ ```
33
+ npx svseeds -r [components...]
34
+ ```
35
+ (Of course, the components are just a file, you can use `rm` command instead.)
36
+
37
+ - Run without interactions:
38
+ ```
39
+ npx svseeds --no-confirm [components...]
40
+ ```
41
+
42
+ ## Other Options
43
+ - Remove all components:
44
+ ```
45
+ npx svseeds --uninstall
46
+ ```
47
+
48
+ - Copy without overwrite
49
+ ```
50
+ npx svseeds --no-overwrite [components...]
51
+ ```
52
+
53
+ - Copy without `__style.ts` file
54
+ ```
55
+ npx svseeds --no-style [components...]
56
+ ```
package/dist/main.js ADDED
@@ -0,0 +1,271 @@
1
+ #!/usr/bin/env node
2
+ import path from "node:path";
3
+ import fs from "node:fs/promises";
4
+ import { readdirSync } from "node:fs";
5
+ import { execSync, exec } from "node:child_process";
6
+ import { promisify } from "node:util";
7
+ import { program } from "commander";
8
+ import { x } from "tar";
9
+ import * as p from "@clack/prompts";
10
+ import pkg from "../package.json" with { type: "json" };
11
+ const CANCEL_CODE = -1;
12
+ const ui = {
13
+ package: "@scirexs/svseeds-ui",
14
+ tar: "scirexs-svseeds-ui",
15
+ dir: "_svseeds",
16
+ tmp: "tmp_svseeds_",
17
+ ext: ".svelte",
18
+ prefix: "_",
19
+ core: "__core.ts",
20
+ style: "__style.ts",
21
+ };
22
+ const defaultPath = path.join("src", "lib", ui.dir);
23
+ program
24
+ .version(pkg.version)
25
+ .argument("[components...]", "Target component names")
26
+ .option("-d, --dir <directory>", "Directory path of components", defaultPath)
27
+ .option("-a, --all", "Copy all components", false)
28
+ .option("-u, --update", "Update mode", false)
29
+ .option("-r, --remove", "Remove mode", false)
30
+ .option("--uninstall", "Remove all components", false)
31
+ .option("--no-confirm", "Skip interactions")
32
+ .option("--no-overwrite", "Does not overwrite if exists")
33
+ .option("--no-style", "Exclude copy of __style.ts file");
34
+ export async function main() {
35
+ p.intro("SvSeeds Collector");
36
+ let exitCode = 0;
37
+ let tmp = "";
38
+ const opts = program.parse(process.argv).opts();
39
+ opts.components = program.args;
40
+ opts.copy = !opts.update && !opts.remove && !opts.uninstall;
41
+ try {
42
+ const dest = await getDestinationProcess(opts.dir, opts.confirm);
43
+ if (!opts.copy && !await isExists(dest))
44
+ throw new Error("target directory is not exists");
45
+ tmp = await fs.mkdtemp(ui.tmp).catch();
46
+ if (!tmp)
47
+ throw new Error("failed to create temporary directory");
48
+ const src = await downloadProcess(tmp);
49
+ const avails = getAvailables(src);
50
+ const locals = opts.copy ? [] : getLocalExistingFiles(dest, avails);
51
+ filterAvailables(avails, locals);
52
+ const files = opts.uninstall ? [] : await getSelected(opts.components, opts.all, opts.confirm, avails);
53
+ if (!opts.uninstall && files.length <= 0)
54
+ throw new Error("no components specified");
55
+ if (opts.update) {
56
+ await updateProcess(src, dest, files, locals);
57
+ p.log.success("Components successfully updated.");
58
+ }
59
+ else if (opts.remove) {
60
+ await removeProcess(dest, files, locals);
61
+ p.log.success("Components successfully removed.");
62
+ }
63
+ else if (opts.uninstall) {
64
+ await uninstallProcess(dest, locals);
65
+ p.log.success("SvSeeds successfully uninstalled.");
66
+ }
67
+ else {
68
+ await copyProcess(src, dest, files, opts.overwrite, opts.style);
69
+ p.log.success("Components successfully copied.");
70
+ p.note(`import ${getNoPrefixName(files[0])} from '$lib/_svseeds/${files[0]}';`, "Usage Example");
71
+ p.outro("Import svelte file as usual.");
72
+ }
73
+ }
74
+ catch (e) {
75
+ if (e instanceof Error && e.cause !== CANCEL_CODE) {
76
+ p.log.error(`error: ${e.message}`);
77
+ exitCode = 1;
78
+ }
79
+ }
80
+ finally {
81
+ await fs.rm(tmp, { recursive: true, force: true }).catch();
82
+ if (exitCode)
83
+ process.exit(exitCode);
84
+ }
85
+ }
86
+ async function waitProcess(msg, fn, ...args) {
87
+ const wait = p.spinner();
88
+ wait.start(msg.start);
89
+ let result;
90
+ let success = false;
91
+ try {
92
+ result = await fn(...args);
93
+ success = true;
94
+ }
95
+ finally {
96
+ const [stop, code] = success ? [msg.success, 0] : [msg.fail, 1];
97
+ wait.stop(stop, code);
98
+ }
99
+ return result;
100
+ }
101
+ async function getDestinationProcess(option, confirm) {
102
+ const project = getProjectPath();
103
+ if (!project)
104
+ return project;
105
+ const dest = path.join(project, option);
106
+ if (confirm && option !== defaultPath)
107
+ await confirmPath(`Target directory: ${dest}`);
108
+ return path.normalize(dest);
109
+ }
110
+ function getProjectPath() {
111
+ const dir = path.dirname(execSync("npm root", { encoding: "utf8" }).trim());
112
+ if (dir === path.parse(dir).root)
113
+ throw new Error("current directory seems to be root");
114
+ return dir;
115
+ }
116
+ async function confirmPath(message) {
117
+ const ok = await p.confirm({ message });
118
+ if (p.isCancel(ok) || !ok) {
119
+ p.cancel("cancelled");
120
+ throw new Error("cancelled", { cause: CANCEL_CODE });
121
+ }
122
+ }
123
+ async function downloadProcess(tmp) {
124
+ const msg = { start: "Preparing", success: "Ready.", fail: "Process failed" };
125
+ return await waitProcess(msg, downloadPackage, tmp);
126
+ }
127
+ async function downloadPackage(tmp) {
128
+ const execPromise = promisify(exec);
129
+ await execPromise(`npm pack ${ui.package}`, { cwd: tmp });
130
+ const tgz = readdirSync(tmp).find(file => file.startsWith(ui.tar) && file.endsWith("gz"));
131
+ if (!tgz)
132
+ throw new Error(`package ${ui.package} not found`);
133
+ await x({ file: path.join(tmp, tgz), cwd: tmp });
134
+ return path.resolve(path.join(tmp, "package", ui.dir));
135
+ }
136
+ function getAvailables(src) {
137
+ const avails = new Map();
138
+ readdirSync(src)
139
+ .filter(file => file.endsWith(ui.ext))
140
+ .forEach(file => {
141
+ const name = file.replace(ui.ext, "");
142
+ if (file.startsWith(ui.prefix)) {
143
+ avails.set(name.replace(ui.prefix, ""), file);
144
+ }
145
+ avails.set(name, file);
146
+ });
147
+ return avails;
148
+ }
149
+ function getNoPrefixName(file) {
150
+ return file.replace(ui.ext, "").replace(ui.prefix, "");
151
+ }
152
+ function filterAvailables(avails, locals) {
153
+ if (locals.length <= 0)
154
+ return;
155
+ for (const [name, file] of avails.entries()) {
156
+ if (!locals.includes(file))
157
+ avails.delete(name);
158
+ }
159
+ }
160
+ async function getSelected(components, all, confirm, avails) {
161
+ if (all) {
162
+ return [...new Set([...avails.values()])];
163
+ }
164
+ else if (components.length > 0) {
165
+ return recognizeValidNames(components, avails).map(name => avails.get(name) ?? "");
166
+ }
167
+ else {
168
+ if (!confirm)
169
+ return [];
170
+ return await selectComponentsProcess(avails);
171
+ }
172
+ }
173
+ function recognizeValidNames(names, avails) {
174
+ const valid = { true: [], false: [] };
175
+ names.forEach(name => valid[`${avails.has(name)}`].push(name));
176
+ if (valid.false.length > 0)
177
+ p.log.warn(`warn: components does not exist: ${valid.false.join(", ")}`);
178
+ return [...new Set(valid.true)];
179
+ }
180
+ async function selectComponentsProcess(avails) {
181
+ const message = `Select components.`;
182
+ const opts = [...avails.entries()]
183
+ .filter(([name, _]) => !name.startsWith(ui.prefix))
184
+ .map(([name, file]) => ({ value: file, label: name }));
185
+ return selectComponents(message, opts);
186
+ }
187
+ async function selectComponents(message, options) {
188
+ const selected = await p.multiselect({ message, options, required: true });
189
+ if (p.isCancel(selected)) {
190
+ p.cancel("cancelled");
191
+ throw new Error("cancelled", { cause: -1 });
192
+ }
193
+ return selected;
194
+ }
195
+ function getLocalExistingFiles(dest, avails) {
196
+ const svseeds = new Set(avails.values());
197
+ svseeds.add(ui.core).add(ui.style);
198
+ const files = readdirSync(dest).filter(file => svseeds.has(file));
199
+ return files;
200
+ }
201
+ async function updateProcess(src, dest, locals, files) {
202
+ const msg = { start: "Start to update", success: "Update done.", fail: "Update failed." };
203
+ await waitProcess(msg, updateFiles, src, dest, files, locals);
204
+ }
205
+ function updateFiles(src, dest, locals, files) {
206
+ const target = locals.filter(file => files.includes(file));
207
+ copyOverwrite(src, dest, target);
208
+ }
209
+ async function removeProcess(dest, locals, files) {
210
+ const msg = { start: "Start to remove", success: "Remove done.", fail: "Remove failed." };
211
+ await waitProcess(msg, removeFiles, dest, locals, files);
212
+ }
213
+ async function removeFiles(dest, locals, files) {
214
+ const target = files ? locals.filter(file => files.includes(file)) : locals;
215
+ for (const file of target) {
216
+ await fs.rm(path.join(dest, file));
217
+ }
218
+ }
219
+ async function uninstallProcess(dest, locals) {
220
+ const msg = { start: "Start to uninstall", success: "Uninstall done.", fail: "Uninstall failed." };
221
+ await waitProcess(msg, uninstallFiles, dest, locals);
222
+ }
223
+ async function uninstallFiles(dest, locals) {
224
+ await removeFiles(dest, locals);
225
+ if (readdirSync(dest).length <= 0)
226
+ await fs.rmdir(dest);
227
+ }
228
+ async function copyProcess(src, dest, files, overwrite, style) {
229
+ if (style) {
230
+ files.push(ui.core, ui.style);
231
+ }
232
+ else {
233
+ files.push(ui.core);
234
+ }
235
+ const msg = { start: "Start to copy files", success: "Copy done.", fail: "Copy failed." };
236
+ await waitProcess(msg, copyFiles, src, dest, files, overwrite);
237
+ }
238
+ async function copyFiles(src, dest, files, overwrite) {
239
+ await fs.mkdir(dest, { recursive: true });
240
+ if (overwrite) {
241
+ await copyOverwrite(src, dest, files);
242
+ }
243
+ else {
244
+ await copyNoOverwrite(src, dest, files);
245
+ }
246
+ }
247
+ async function copyOverwrite(src, dest, files) {
248
+ for (const file of files) {
249
+ await fs.cp(path.join(src, file), path.join(dest, file));
250
+ }
251
+ }
252
+ async function copyNoOverwrite(src, dest, files) {
253
+ const skip = [];
254
+ for (const file of files) {
255
+ const d = path.join(dest, file);
256
+ if (await isExists(d))
257
+ skip.push(file);
258
+ await fs.cp(path.join(src, file), d, { force: false });
259
+ }
260
+ if (skip.length >= 0)
261
+ p.log.info(`Skipped ${skip.length} files.`);
262
+ }
263
+ async function isExists(path) {
264
+ try {
265
+ await fs.access(path);
266
+ return true;
267
+ }
268
+ catch (e) {
269
+ return false;
270
+ }
271
+ }
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import { main } from "./dist/main.js";
2
+ main();
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "svseeds",
3
+ "version": "0.0.1",
4
+ "description": "A CLI to copy SvSeeds components.",
5
+ "type": "module",
6
+ "exports": "./index.js",
7
+ "files": [
8
+ "dist/**/*",
9
+ "index.js"
10
+ ],
11
+ "keywords": [
12
+ "svelte",
13
+ "headless",
14
+ "components",
15
+ "cli"
16
+ ],
17
+ "author": "scirexs",
18
+ "license": "MIT",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/scirexs/svseeds-cli-node.git"
22
+ },
23
+ "scripts": {
24
+ "test": "echo \"Error: no test specified\" && exit 1",
25
+ "lint": "eslint .",
26
+ "format": "eslint --fix ."
27
+ },
28
+ "devDependencies": {
29
+ "@eslint/js": "^9.21.0",
30
+ "@stylistic/eslint-plugin": "^4.2.0",
31
+ "@types/node": "^22.13.9",
32
+ "eslint": "^9.21.0",
33
+ "globals": "^16.0.0",
34
+ "typescript": "^5.8.2",
35
+ "typescript-eslint": "^8.26.0"
36
+ },
37
+ "dependencies": {
38
+ "@clack/prompts": "^0.10.0",
39
+ "commander": "^13.1.0",
40
+ "tar": "^7.4.3"
41
+ }
42
+ }