@youcan/cli 1.0.0 → 1.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 (67) hide show
  1. package/README.md +1 -1
  2. package/dist/cli/commands/auth/login.js +105 -0
  3. package/dist/cli/commands/auth/logout.js +20 -0
  4. package/dist/cli/commands/index.js +9 -0
  5. package/dist/cli/commands/theme/delete.js +43 -0
  6. package/dist/cli/commands/theme/dev.js +190 -0
  7. package/dist/cli/commands/theme/init.js +85 -0
  8. package/dist/cli/commands/theme/pack.js +28 -0
  9. package/dist/cli/commands/theme/pull.js +54 -0
  10. package/dist/cli/index.js +51 -0
  11. package/dist/config/index.js +25 -0
  12. package/dist/config/messages.js +26 -0
  13. package/dist/core/client/client.js +65 -0
  14. package/dist/core/themes/preview.js +33 -0
  15. package/dist/index.js +3 -0
  16. package/dist/lib/cli/commands/auth/login.d.ts +2 -0
  17. package/dist/lib/cli/commands/auth/logout.d.ts +2 -0
  18. package/dist/lib/cli/commands/auth/types.d.ts +6 -0
  19. package/dist/lib/cli/commands/index.d.ts +7 -0
  20. package/dist/lib/cli/commands/theme/delete.d.ts +2 -0
  21. package/dist/lib/cli/commands/theme/dev.d.ts +2 -0
  22. package/dist/lib/cli/commands/theme/init.d.ts +2 -0
  23. package/dist/lib/cli/commands/theme/pack.d.ts +2 -0
  24. package/dist/lib/cli/commands/theme/pull.d.ts +2 -0
  25. package/dist/lib/cli/commands/theme/types.d.ts +17 -0
  26. package/dist/lib/cli/commands/types.d.ts +22 -0
  27. package/dist/lib/cli/index.d.ts +12 -0
  28. package/dist/lib/config/index.d.ts +21 -0
  29. package/dist/lib/config/messages.d.ts +25 -0
  30. package/dist/lib/core/client/client.d.ts +17 -0
  31. package/dist/lib/core/client/types.d.ts +52 -0
  32. package/dist/lib/core/themes/preview.d.ts +1 -0
  33. package/dist/lib/index.d.ts +1 -0
  34. package/dist/lib/utils/common.d.ts +22 -0
  35. package/dist/lib/utils/git/cloneRepository.d.ts +6 -0
  36. package/dist/lib/utils/helpers.d.ts +3 -0
  37. package/dist/lib/utils/http.d.ts +11 -0
  38. package/dist/lib/utils/network.d.ts +2 -0
  39. package/dist/lib/utils/system/deleteFile.d.ts +5 -0
  40. package/dist/lib/utils/system/ls.d.ts +1 -0
  41. package/dist/lib/utils/system/openLink.d.ts +1 -0
  42. package/dist/lib/utils/system/saveFile.d.ts +1 -0
  43. package/dist/lib/utils/system/stdout.d.ts +18 -0
  44. package/dist/lib/utils/system/writeToFile.d.ts +6 -0
  45. package/dist/lib/utils/system/zipFolder.d.ts +5 -0
  46. package/dist/lib/utils/system.d.ts +1 -0
  47. package/dist/test/commands/auth/login.d.ts +2 -0
  48. package/dist/test/commands/auth/logout.d.ts +2 -0
  49. package/dist/test/commands/help.d.ts +2 -0
  50. package/dist/test/commands/theme/delete.d.ts +2 -0
  51. package/dist/test/commands/theme/dev.d.ts +2 -0
  52. package/dist/test/commands/theme/init.d.ts +2 -0
  53. package/dist/test/index.test.d.ts +1 -0
  54. package/dist/utils/common.js +64 -0
  55. package/dist/utils/git/cloneRepository.js +18 -0
  56. package/dist/utils/helpers.js +32 -0
  57. package/dist/utils/http.js +25 -0
  58. package/dist/utils/network.js +76 -0
  59. package/dist/utils/system/deleteFile.js +12 -0
  60. package/dist/utils/system/ls.js +9 -0
  61. package/dist/utils/system/openLink.js +15 -0
  62. package/dist/utils/system/saveFile.js +9 -0
  63. package/dist/utils/system/stdout.js +15 -0
  64. package/dist/utils/system/writeToFile.js +12 -0
  65. package/dist/utils/system/zipFolder.js +57 -0
  66. package/dist/utils/system.js +17 -0
  67. package/package.json +1 -1
@@ -0,0 +1,76 @@
1
+ import { createServer } from 'net';
2
+ import { platform } from 'process';
3
+ import { execaCommand } from 'execa';
4
+ import stdout from './system/stdout.js';
5
+ import { stripln, getcols } from './helpers.js';
6
+
7
+ async function isPortAvailable(port) {
8
+ return new Promise((resolve, reject) => {
9
+ const server = createServer();
10
+ server.once('error', (err) => {
11
+ server.close();
12
+ if (err.code === 'EADDRINUSE')
13
+ resolve(false);
14
+ reject(err);
15
+ });
16
+ server.once('listening', () => {
17
+ server.close();
18
+ resolve(true);
19
+ });
20
+ server.listen(port);
21
+ });
22
+ }
23
+ const PID_GETTER_CLASS_MAP = {
24
+ async darwin(port) {
25
+ const { stdout: out, stderr: err } = await execaCommand('netstat -anv -p TCP && netstat -anv -p UDP');
26
+ const warning = err.toString().trim();
27
+ warning && stdout.warn(warning);
28
+ const line = stripln(out, 2);
29
+ const col = getcols(line, [0, 3, 8], 10)
30
+ .filter(col => !!col[0].match(/^(udp|tcp)/))
31
+ .find((col) => {
32
+ const matches = col[1].match(/\.(\d+)$/);
33
+ return matches && matches[1] === String(port);
34
+ });
35
+ if (col && col[2].length)
36
+ return (parseInt(col[2], 10));
37
+ return null;
38
+ },
39
+ async linux(port) {
40
+ const { stdout: out, stderr: err } = await execaCommand('netstat -tunlp');
41
+ const warning = err.toString().trim();
42
+ warning && stdout.warn(warning);
43
+ const data = stripln(out.toString(), 2);
44
+ const cols = getcols(data, [3, 6], 7).find((col) => {
45
+ const matches = col[0].match(/:(\d+)$/);
46
+ return matches && matches[1] === String(port);
47
+ });
48
+ if (cols && cols[1]) {
49
+ const pid = cols[1].split('/', 1)[0];
50
+ if (pid.length)
51
+ return parseInt(pid, 10);
52
+ }
53
+ return null;
54
+ },
55
+ async win32(port) {
56
+ const { stdout: out, stderr: err } = await execaCommand('netstat -ano');
57
+ const warning = err.toString().trim();
58
+ warning && stdout.warn(warning);
59
+ const data = stripln(out.toString(), 4);
60
+ const cols = getcols(data, [1, 4], 5).find((col) => {
61
+ const matches = col[0].match(/:(\d+)$/);
62
+ return matches && matches[1] === String(port);
63
+ });
64
+ if (cols && cols[1].length && parseInt(cols[1], 10) > 0)
65
+ return parseInt(cols[1], 10);
66
+ return null;
67
+ },
68
+ };
69
+ async function getPidByPort(port) {
70
+ if (!(platform in PID_GETTER_CLASS_MAP))
71
+ throw new Error('Unsupported platform, process will have to be killed manually.');
72
+ const getter = PID_GETTER_CLASS_MAP[platform];
73
+ return await getter(port);
74
+ }
75
+
76
+ export { getPidByPort, isPortAvailable };
@@ -0,0 +1,12 @@
1
+ import fs from 'fs';
2
+
3
+ /**
4
+ * @param filePath - The path of the file to delete.
5
+ *
6
+ */
7
+ function deleteFile(filePath) {
8
+ if (fs.existsSync(filePath))
9
+ fs.unlinkSync(filePath);
10
+ }
11
+
12
+ export { deleteFile as default };
@@ -0,0 +1,9 @@
1
+ import { readdirSync, statSync } from 'fs';
2
+
3
+ function lsDir(path) {
4
+ return readdirSync(path).filter((file) => {
5
+ return statSync(`${path}/${file}`).isDirectory() && file[0] !== '.';
6
+ });
7
+ }
8
+
9
+ export { lsDir };
@@ -0,0 +1,15 @@
1
+ import { execSync } from 'child_process';
2
+
3
+ const { platform } = process;
4
+ const PLATFORM_OPEN_CMD_MAP = {
5
+ win32: 'cmd /c start',
6
+ linux: 'xdg-open',
7
+ darwin: 'open',
8
+ };
9
+ function openLink(url) {
10
+ if (!(platform in PLATFORM_OPEN_CMD_MAP))
11
+ throw new Error('Platform not supported');
12
+ execSync(`${PLATFORM_OPEN_CMD_MAP[platform]} '${url}'`);
13
+ }
14
+
15
+ export { openLink as default };
@@ -0,0 +1,9 @@
1
+ import fs from 'fs';
2
+ import { pipeline } from 'stream/promises';
3
+
4
+ async function saveHttpFile(res, filename) {
5
+ const fileStream = fs.createWriteStream(filename);
6
+ return await pipeline(res.body, fileStream);
7
+ }
8
+
9
+ export { saveHttpFile };
@@ -0,0 +1,15 @@
1
+ import kleur from 'kleur';
2
+
3
+ const stdout = console;
4
+ /**
5
+ * Print to standard output with meaningful colors
6
+ */
7
+ function log(arg) { return stdout.log(kleur.gray(arg)); }
8
+ function info(arg) { return log(kleur.blue(arg)); }
9
+ function warn(arg) { return log(kleur.yellow(arg)); }
10
+ function error(arg) { return log(kleur.bgRed().white(arg)); }
11
+ function success(arg) { return log(kleur.green(arg)); }
12
+ function clear() { return stdout.clear(); }
13
+ var stdout$1 = { log, info, warn, error, clear, success };
14
+
15
+ export { stdout$1 as default };
@@ -0,0 +1,12 @@
1
+ import fs from 'fs';
2
+
3
+ /**
4
+ * @param filePath - The path to the file to write to.
5
+ * @param content - The content to write to the file.
6
+ * @returns A promise that resolves when the file has been written.
7
+ */
8
+ function writeToFile(filePath, content) {
9
+ return fs.writeFileSync(filePath, content, { encoding: 'utf-8', flag: 'w' });
10
+ }
11
+
12
+ export { writeToFile as default };
@@ -0,0 +1,57 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import archiver from 'archiver';
4
+ import { lsDir } from './ls.js';
5
+
6
+ /**
7
+ * Zip folder and save it to a given path and return zip folder path
8
+ */
9
+ async function zipFolder(folderPath, folderName) {
10
+ return new Promise((resolve, reject) => {
11
+ try {
12
+ const zipPath = path.resolve(folderPath, `${folderName}.zip`);
13
+ const output = fs.createWriteStream(zipPath);
14
+ const archive = archiver('zip', {
15
+ zlib: { level: 9 },
16
+ });
17
+ output.on('close', () => {
18
+ resolve(zipPath);
19
+ });
20
+ archive.on('error', (err) => {
21
+ reject(err);
22
+ });
23
+ archive.pipe(output);
24
+ archive.directory(path.resolve(folderPath, folderName), false);
25
+ archive.finalize();
26
+ }
27
+ catch (err) {
28
+ reject(err);
29
+ }
30
+ });
31
+ }
32
+ async function zipDirectory(dirPath, folderName) {
33
+ const ls = lsDir(dirPath);
34
+ return new Promise((resolve, reject) => {
35
+ try {
36
+ const zip = path.resolve(dirPath, `${folderName}.zip`);
37
+ const output = fs.createWriteStream(zip);
38
+ const archive = archiver('zip', {
39
+ zlib: { level: 9 },
40
+ });
41
+ output.on('close', () => {
42
+ resolve(zip);
43
+ });
44
+ archive.on('error', (err) => {
45
+ reject(err);
46
+ });
47
+ archive.pipe(output);
48
+ ls.forEach(folder => archive.directory(path.resolve(dirPath, folder), folder));
49
+ archive.finalize();
50
+ }
51
+ catch (err) {
52
+ reject(err);
53
+ }
54
+ });
55
+ }
56
+
57
+ export { zipDirectory, zipFolder };
@@ -0,0 +1,17 @@
1
+ const kill = (pid, signal = 'SIGTERM', timeout = 1000) => new Promise((resolve, reject) => {
2
+ process.kill(pid, signal);
3
+ let count = 0;
4
+ setInterval(() => {
5
+ try {
6
+ process.kill(pid, 0);
7
+ }
8
+ catch (e) {
9
+ resolve();
10
+ }
11
+ count += 100;
12
+ if (count > timeout)
13
+ reject(new Error('Timeout process kill'));
14
+ }, 100);
15
+ });
16
+
17
+ export { kill };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@youcan/cli",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "1.0.2",
5
5
  "description": "YouCan CLI for developers.",
6
6
  "author": "YouCan <contact@youcan.shop> (https://youcan.shop)",
7
7
  "keywords": [],