tokocode 0.0.0-stage → 1.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.
@@ -0,0 +1,121 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { CliError } from './errors.js';
3
+ import { EXIT } from '../config/constants.js';
4
+ /** Crockford Base32, the alphabet of installation tokens (no I, L, O, U). */
5
+ export const TOKEN_ALPHABET = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
6
+ const BODY_LENGTH = 20;
7
+ const CHECK_LENGTH = 4;
8
+ /**
9
+ * Four-character checksum of a token body; identical to CliTokenCodec::checksum() on the server.
10
+ */
11
+ export function tokenChecksum(body) {
12
+ const hash = createHash('sha256').update('tokocode-cli-token|' + body).digest();
13
+ const bits = (hash[0] << 12) | (hash[1] << 4) | (hash[2] >> 4);
14
+ let out = '';
15
+ for (let shift = 15; shift >= 0; shift -= 5) {
16
+ out += TOKEN_ALPHABET[(bits >> shift) & 31];
17
+ }
18
+ return out;
19
+ }
20
+ /**
21
+ * Canonical 24-character token, or null when the input cannot be a valid token.
22
+ * Accepts lower case, spaces, dashes and the look-alikes O→0, I/L→1.
23
+ */
24
+ export function normalizeToken(input) {
25
+ let value = input.trim().toUpperCase().replace(/[\s\-_.]+/g, '');
26
+ const full = BODY_LENGTH + CHECK_LENGTH;
27
+ if (value.length === full + 2 && value.startsWith('TK')) {
28
+ value = value.slice(2);
29
+ }
30
+ value = value.replace(/O/g, '0').replace(/[IL]/g, '1');
31
+ if (value.length !== full || [...value].some((char) => !TOKEN_ALPHABET.includes(char))) {
32
+ return null;
33
+ }
34
+ const body = value.slice(0, BODY_LENGTH);
35
+ return tokenChecksum(body) === value.slice(BODY_LENGTH) ? value : null;
36
+ }
37
+ export function formatToken(normalized) {
38
+ return 'TK-' + (normalized.match(/.{1,4}/g) ?? []).join('-');
39
+ }
40
+ /** "TK-••••-…-AB12" for display; never prints the full token. */
41
+ export function maskToken(normalized) {
42
+ return 'TK-••••-••••-••••-••••-••••-' + normalized.slice(-4);
43
+ }
44
+ const WINDOWS_RESERVED = /^(con|prn|aux|nul|com[0-9¹²³]|lpt[0-9¹²³])(\..*)?$/i;
45
+ // eslint-disable-next-line no-control-regex
46
+ const INVALID_CHARS = /[<>:"/\\|?*\u0000-\u001F]/;
47
+ /**
48
+ * Why a folder name cannot be used on Windows, macOS and Linux alike (null = valid).
49
+ * Spaces are allowed; names must be portable so the project can move between systems.
50
+ */
51
+ export function folderNameProblem(name) {
52
+ if (name.trim() === '') {
53
+ return 'Nama folder tidak boleh kosong.';
54
+ }
55
+ if (name.length > 100) {
56
+ return 'Nama folder maksimal 100 karakter.';
57
+ }
58
+ if (INVALID_CHARS.test(name)) {
59
+ return 'Nama folder tidak boleh berisi karakter < > : " / \\ | ? *';
60
+ }
61
+ if (name === '.' || name === '..') {
62
+ return 'Nama folder tidak valid.';
63
+ }
64
+ if (/[. ]$/.test(name)) {
65
+ return 'Nama folder tidak boleh diakhiri titik atau spasi.';
66
+ }
67
+ if (WINDOWS_RESERVED.test(name)) {
68
+ return `"${name}" adalah nama yang dicadangkan Windows.`;
69
+ }
70
+ return null;
71
+ }
72
+ /** Turns a product slug or name into a safe default folder name. */
73
+ export function suggestFolderName(input) {
74
+ const name = input
75
+ .normalize('NFKD')
76
+ .replace(/[̀-ͯ]/g, '')
77
+ .toLowerCase()
78
+ .replace(/[^a-z0-9._ -]+/g, '-')
79
+ .replace(/\s+/g, '-')
80
+ .replace(/-+/g, '-')
81
+ .replace(/^[-.]+|[-. ]+$/g, '')
82
+ .slice(0, 80);
83
+ return name !== '' && folderNameProblem(name) === null ? name : 'tokocode-project';
84
+ }
85
+ /** Compares dotted versions numerically: -1, 0 or 1. */
86
+ export function compareVersions(a, b) {
87
+ const pa = a.split(/[.+-]/).slice(0, 3).map((part) => Number.parseInt(part, 10) || 0);
88
+ const pb = b.split(/[.+-]/).slice(0, 3).map((part) => Number.parseInt(part, 10) || 0);
89
+ for (let i = 0; i < 3; i++) {
90
+ const diff = (pa[i] ?? 0) - (pb[i] ?? 0);
91
+ if (diff !== 0) {
92
+ return diff > 0 ? 1 : -1;
93
+ }
94
+ }
95
+ return 0;
96
+ }
97
+ /**
98
+ * Validates the API base URL: HTTPS, except plain HTTP for localhost during development.
99
+ */
100
+ export function normalizeApiUrl(input) {
101
+ let url;
102
+ try {
103
+ url = new URL(input.trim());
104
+ }
105
+ catch {
106
+ throw new CliError('invalid_api_url', `Alamat API tidak valid: ${input}`, 'Contoh yang benar: https://tokocode.co-id.id', EXIT.usage);
107
+ }
108
+ const local = ['localhost', '127.0.0.1', '::1', '[::1]'].includes(url.hostname);
109
+ if (url.protocol !== 'https:' && !(url.protocol === 'http:' && local)) {
110
+ throw new CliError('insecure_api_url', 'Alamat API wajib memakai HTTPS.', 'Gunakan alamat yang diawali https://', EXIT.usage);
111
+ }
112
+ if (url.username || url.password) {
113
+ throw new CliError('invalid_api_url', 'Alamat API tidak boleh berisi nama pengguna atau password.', '', EXIT.usage);
114
+ }
115
+ url.hash = '';
116
+ url.search = '';
117
+ return url.toString().replace(/\/+$/, '');
118
+ }
119
+ export function isSha256(value) {
120
+ return /^[a-f0-9]{64}$/i.test(value);
121
+ }
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
2
  "name": "tokocode",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Pasang source code yang sudah Anda beli di TokoCode langsung dari terminal: npx tokocode",
5
+ "keywords": [
6
+ "tokocode",
7
+ "installer",
8
+ "cli",
9
+ "source-code",
10
+ "template"
11
+ ],
12
+ "homepage": "https://tokocode.co-id.id",
13
+ "license": "MIT",
14
+ "author": "TokoCode",
15
+ "type": "module",
16
+ "bin": {
17
+ "tokocode": "dist/index.js"
18
+ },
19
+ "files": [
20
+ "dist",
21
+ "README.md",
22
+ "LICENSE",
23
+ "CHANGELOG.md"
24
+ ],
25
+ "engines": {
26
+ "node": ">=18.17"
27
+ },
28
+ "scripts": {
29
+ "build": "node scripts/clean.mjs && tsc -p tsconfig.build.json",
30
+ "typecheck": "tsc -p tsconfig.json --noEmit",
31
+ "test": "npm run build && vitest run",
32
+ "test:watch": "vitest",
33
+ "start": "node dist/index.js",
34
+ "pack:check": "npm pack --dry-run",
35
+ "prepublishOnly": "npm run typecheck && npm test && npm run build"
36
+ },
37
+ "dependencies": {
38
+ "chalk": "5.6.2",
39
+ "commander": "12.1.0",
40
+ "ora": "8.2.0",
41
+ "yauzl": "3.4.0"
42
+ },
43
+ "devDependencies": {
44
+ "@types/node": "18.19.130",
45
+ "@types/yauzl": "3.4.0",
46
+ "typescript": "5.9.3",
47
+ "vitest": "3.2.7"
48
+ }
49
+ }