svseeds 0.0.1 → 0.0.2

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/README.md +1 -1
  2. package/package.json +1 -1
  3. package/dist/main.js +0 -271
package/README.md CHANGED
@@ -50,7 +50,7 @@ npx svseeds --uninstall
50
50
  npx svseeds --no-overwrite [components...]
51
51
  ```
52
52
 
53
- - Copy without `__style.ts` file
53
+ - Copy without `_style.ts` file
54
54
  ```
55
55
  npx svseeds --no-style [components...]
56
56
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svseeds",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "A CLI to copy SvSeeds components.",
5
5
  "type": "module",
6
6
  "exports": "./index.js",
package/dist/main.js DELETED
@@ -1,271 +0,0 @@
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
- }