@codeqlick/cli 0.0.0-stage → 0.5.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/README.md +153 -2
- package/dist/api.js +196 -0
- package/dist/cli.js +1627 -0
- package/dist/config.js +93 -0
- package/dist/download.js +63 -0
- package/dist/pack.js +124 -0
- package/dist/version.js +2 -0
- package/package.json +41 -4
package/dist/config.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { chmod, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
/** Where the token is kept: ~/.config/codeqlick/config.json (or $XDG_CONFIG_HOME), mode 600. */
|
|
5
|
+
export function configPath(env = process.env) {
|
|
6
|
+
const base = env.XDG_CONFIG_HOME || join(homedir(), '.config');
|
|
7
|
+
return join(base, 'codeqlick', 'config.json');
|
|
8
|
+
}
|
|
9
|
+
export async function readConfig(path = configPath()) {
|
|
10
|
+
try {
|
|
11
|
+
return JSON.parse(await readFile(path, 'utf8'));
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return {};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export async function saveConfig(config, path = configPath()) {
|
|
18
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
19
|
+
await writeFile(path, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 });
|
|
20
|
+
await chmod(path, 0o600);
|
|
21
|
+
}
|
|
22
|
+
export async function forgetConfig(path = configPath()) {
|
|
23
|
+
await rm(path, { force: true });
|
|
24
|
+
}
|
|
25
|
+
export const PROJECT_FILE = 'codeqlick.json';
|
|
26
|
+
export async function readProjectFile(cwd = process.cwd()) {
|
|
27
|
+
try {
|
|
28
|
+
const data = JSON.parse(await readFile(join(cwd, PROJECT_FILE), 'utf8'));
|
|
29
|
+
return typeof data === 'object' && data ? data : {};
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** The keys that make a `codeqlick.json` carry routing rules (architecture 4.13). */
|
|
36
|
+
export const RULE_KEYS = ['cleanUrls', 'trailingSlash', 'redirects', 'rewrites', 'headers'];
|
|
37
|
+
/** A `codeqlick.json` that can't be used as it is (not JSON, or not an object). */
|
|
38
|
+
export class ProjectFileError extends Error {
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Parses the text of a `codeqlick.json`: its object, or a ProjectFileError that says what is wrong.
|
|
42
|
+
*/
|
|
43
|
+
export function parseProjectText(text) {
|
|
44
|
+
let data;
|
|
45
|
+
try {
|
|
46
|
+
data = JSON.parse(text);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
throw new ProjectFileError(`${PROJECT_FILE} isn't valid JSON (${error.message}): fix it first, it was left as it is.`);
|
|
50
|
+
}
|
|
51
|
+
if (typeof data !== 'object' || data === null || Array.isArray(data)) {
|
|
52
|
+
throw new ProjectFileError(`${PROJECT_FILE} must be a JSON object: fix it first, it was left as it is.`);
|
|
53
|
+
}
|
|
54
|
+
return data;
|
|
55
|
+
}
|
|
56
|
+
/** The file with `link`'s keys set and every other key (and the order of keys) as it was. */
|
|
57
|
+
export function mergeProjectFile(existing, link) {
|
|
58
|
+
const merged = { ...existing };
|
|
59
|
+
for (const key of ['team', 'project', 'dir']) {
|
|
60
|
+
if (link[key] !== undefined)
|
|
61
|
+
merged[key] = link[key];
|
|
62
|
+
}
|
|
63
|
+
return merged;
|
|
64
|
+
}
|
|
65
|
+
/** The text of the folder's `codeqlick.json`, or null when there is none. */
|
|
66
|
+
export async function readProjectText(cwd = process.cwd()) {
|
|
67
|
+
try {
|
|
68
|
+
return await readFile(join(cwd, PROJECT_FILE), 'utf8');
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** True when the text sets any routing rule; text that doesn't parse counts (the API says why). */
|
|
75
|
+
export function hasRules(text) {
|
|
76
|
+
try {
|
|
77
|
+
const data = parseProjectText(text);
|
|
78
|
+
return RULE_KEYS.some((key) => data[key] !== undefined);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Writes the project link into `codeqlick.json`, keeping everything else in the file (rules,
|
|
86
|
+
* `$schema`, unknown keys). A file that isn't valid JSON is never overwritten.
|
|
87
|
+
*/
|
|
88
|
+
export async function writeProjectFile(file, cwd = process.cwd()) {
|
|
89
|
+
const text = await readProjectText(cwd);
|
|
90
|
+
const existing = text === null || text.trim() === '' ? {} : parseProjectText(text);
|
|
91
|
+
await writeFile(join(cwd, PROJECT_FILE), `${JSON.stringify(mergeProjectFile(existing, file), null, 2)}\n`);
|
|
92
|
+
}
|
|
93
|
+
export const DEFAULT_API = 'https://api.codeqlick.com/v1';
|
package/dist/download.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { createWriteStream } from 'node:fs';
|
|
2
|
+
import { rename, rm, stat } from 'node:fs/promises';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import { Readable } from 'node:stream';
|
|
5
|
+
import { pipeline } from 'node:stream/promises';
|
|
6
|
+
/** The name the API gives a deployment's archive: `<slug>-<short id>.tar.gz`. */
|
|
7
|
+
export function archiveName(slug, shortId) {
|
|
8
|
+
return `${slug}-${shortId}.tar.gz`;
|
|
9
|
+
}
|
|
10
|
+
/** Where to save it: `--out`, or its own name in the current folder. */
|
|
11
|
+
export function archivePath(out, slug, shortId) {
|
|
12
|
+
return resolve(out || archiveName(slug, shortId));
|
|
13
|
+
}
|
|
14
|
+
/** Where to save a build's log: the `--out` file, resolved from the current folder. */
|
|
15
|
+
export function logPath(out) {
|
|
16
|
+
return resolve(out);
|
|
17
|
+
}
|
|
18
|
+
/** True when something already exists at `path` (a file, a folder or a link). */
|
|
19
|
+
export async function exists(path) {
|
|
20
|
+
return (await stat(path).catch(() => null)) !== null;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Writes a downloaded body to `<file>.part` and renames it to `file` once complete, so a cut
|
|
24
|
+
* download leaves nothing behind. Returns the bytes written.
|
|
25
|
+
*/
|
|
26
|
+
export async function saveArchive(body, file) {
|
|
27
|
+
const part = `${file}.part`;
|
|
28
|
+
const source = body instanceof ReadableStream
|
|
29
|
+
? Readable.fromWeb(body)
|
|
30
|
+
: Readable.from(body);
|
|
31
|
+
const sink = createWriteStream(part, { mode: 0o644 });
|
|
32
|
+
try {
|
|
33
|
+
await pipeline(source, sink);
|
|
34
|
+
await rename(part, file);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
await rm(part, { force: true }).catch(() => undefined);
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
return sink.bytesWritten;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* What to tell the person when saving failed: the file system's own error (no permission, a
|
|
44
|
+
* folder in the way…) as it is, anything else as a cut download. Nothing is left behind either way.
|
|
45
|
+
*/
|
|
46
|
+
export function downloadFailure(error, file) {
|
|
47
|
+
const failure = error;
|
|
48
|
+
const fileSystem = [
|
|
49
|
+
'EACCES',
|
|
50
|
+
'EPERM',
|
|
51
|
+
'EISDIR',
|
|
52
|
+
'ENOTDIR',
|
|
53
|
+
'ENOENT',
|
|
54
|
+
'ENOSPC',
|
|
55
|
+
'EROFS',
|
|
56
|
+
'EEXIST',
|
|
57
|
+
];
|
|
58
|
+
if (failure?.code && fileSystem.includes(failure.code)) {
|
|
59
|
+
return new Error(`Can't save ${file}: ${failure.message}`);
|
|
60
|
+
}
|
|
61
|
+
const detail = failure?.cause?.message ?? failure?.message ?? 'unknown';
|
|
62
|
+
return new Error(`The download was cut (${detail}); nothing was saved.`);
|
|
63
|
+
}
|
package/dist/pack.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { createWriteStream } from 'node:fs';
|
|
2
|
+
import { lstat, readdir, readFile } from 'node:fs/promises';
|
|
3
|
+
import { join, relative, sep } from 'node:path';
|
|
4
|
+
import { pipeline } from 'node:stream/promises';
|
|
5
|
+
import { Readable } from 'node:stream';
|
|
6
|
+
import { createGzip } from 'node:zlib';
|
|
7
|
+
/**
|
|
8
|
+
* `.codeqlickignore` patterns: one per line, `#` comments, `*` matches anything but `/`, `**`
|
|
9
|
+
* anything, a trailing `/` a folder. A pattern without `/` matches a name at any depth.
|
|
10
|
+
*/
|
|
11
|
+
export function ignoreMatcher(text) {
|
|
12
|
+
const rules = text
|
|
13
|
+
.split(/\r?\n/)
|
|
14
|
+
.map((line) => line.trim())
|
|
15
|
+
.filter((line) => line && !line.startsWith('#'))
|
|
16
|
+
.map((line) => {
|
|
17
|
+
const folderOnly = line.endsWith('/');
|
|
18
|
+
const pattern = line.replace(/\/+$/, '').replace(/^\/+/, '');
|
|
19
|
+
const anchored = line.replace(/\/+$/, '').includes('/');
|
|
20
|
+
const source = pattern
|
|
21
|
+
.split('**')
|
|
22
|
+
.map((part) => part
|
|
23
|
+
.split('*')
|
|
24
|
+
.map((piece) => piece.replace(/[.+?^${}()|[\]\\]/g, '\\$&'))
|
|
25
|
+
.join('[^/]*'))
|
|
26
|
+
.join('.*');
|
|
27
|
+
const regex = new RegExp(anchored ? `^${source}$` : `(^|/)${source}$`);
|
|
28
|
+
return { regex, folderOnly };
|
|
29
|
+
});
|
|
30
|
+
return (path, folder) => rules.some((rule) => (!rule.folderOnly || folder) && rule.regex.test(path));
|
|
31
|
+
}
|
|
32
|
+
/** Whether a dotfile or dot-folder is left out (all but `.well-known`). */
|
|
33
|
+
const hidden = (name) => name.startsWith('.') && name !== '.well-known';
|
|
34
|
+
/** The files to deploy from `root`, sorted, without dotfiles, links or ignored paths. */
|
|
35
|
+
export async function collect(root) {
|
|
36
|
+
let ignore = () => false;
|
|
37
|
+
try {
|
|
38
|
+
ignore = ignoreMatcher(await readFile(join(root, '.codeqlickignore'), 'utf8'));
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// No ignore file.
|
|
42
|
+
}
|
|
43
|
+
const files = [];
|
|
44
|
+
const walk = async (folder) => {
|
|
45
|
+
const entries = await readdir(folder, { withFileTypes: true });
|
|
46
|
+
entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
47
|
+
for (const entry of entries) {
|
|
48
|
+
if (hidden(entry.name))
|
|
49
|
+
continue;
|
|
50
|
+
const absolute = join(folder, entry.name);
|
|
51
|
+
const path = relative(root, absolute).split(sep).join('/');
|
|
52
|
+
if (entry.isDirectory()) {
|
|
53
|
+
if (!ignore(path, true))
|
|
54
|
+
await walk(absolute);
|
|
55
|
+
}
|
|
56
|
+
else if (entry.isFile()) {
|
|
57
|
+
if (!ignore(path, false))
|
|
58
|
+
files.push({ path, absolute, size: (await lstat(absolute)).size });
|
|
59
|
+
}
|
|
60
|
+
// Links and anything else are left out: the server refuses them.
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
await walk(root);
|
|
64
|
+
return files;
|
|
65
|
+
}
|
|
66
|
+
const BLOCK = 512;
|
|
67
|
+
function octal(value, length) {
|
|
68
|
+
return value.toString(8).padStart(length - 1, '0') + '\0';
|
|
69
|
+
}
|
|
70
|
+
function write(buffer, offset, text, length) {
|
|
71
|
+
buffer.write(text, offset, Math.min(length, Buffer.byteLength(text)), 'utf8');
|
|
72
|
+
}
|
|
73
|
+
/** A 512-byte ustar header (type `0` file or `x` PAX extended header). */
|
|
74
|
+
function header(name, size, type, mtime) {
|
|
75
|
+
const block = Buffer.alloc(BLOCK);
|
|
76
|
+
write(block, 0, name, 100);
|
|
77
|
+
write(block, 100, octal(0o644, 8), 8);
|
|
78
|
+
write(block, 108, octal(0, 8), 8);
|
|
79
|
+
write(block, 116, octal(0, 8), 8);
|
|
80
|
+
write(block, 124, octal(size, 12), 12);
|
|
81
|
+
write(block, 136, octal(mtime, 12), 12);
|
|
82
|
+
block.fill(' ', 148, 156);
|
|
83
|
+
write(block, 156, type, 1);
|
|
84
|
+
write(block, 257, 'ustar\0', 6);
|
|
85
|
+
write(block, 263, '00', 2);
|
|
86
|
+
let sum = 0;
|
|
87
|
+
for (const byte of block)
|
|
88
|
+
sum += byte;
|
|
89
|
+
write(block, 148, `${sum.toString(8).padStart(6, '0')}\0 `, 8);
|
|
90
|
+
return block;
|
|
91
|
+
}
|
|
92
|
+
/** A PAX record: `<length> path=<value>\n`, the length counting itself. */
|
|
93
|
+
function paxRecord(key, value) {
|
|
94
|
+
const body = ` ${key}=${value}\n`;
|
|
95
|
+
let length = Buffer.byteLength(body) + 1;
|
|
96
|
+
while (String(length).length + Buffer.byteLength(body) !== length) {
|
|
97
|
+
length = String(length).length + Buffer.byteLength(body);
|
|
98
|
+
}
|
|
99
|
+
return `${length}${body}`;
|
|
100
|
+
}
|
|
101
|
+
const padding = (size) => Buffer.alloc((BLOCK - (size % BLOCK)) % BLOCK);
|
|
102
|
+
/** The tar stream of the files: a PAX header before any path that doesn't fit ustar's 100 bytes. */
|
|
103
|
+
async function* tarStream(files) {
|
|
104
|
+
const mtime = Math.floor(Date.now() / 1000);
|
|
105
|
+
for (const file of files) {
|
|
106
|
+
const fits = Buffer.byteLength(file.path) <= 100 && /^[\x20-\x7e]+$/.test(file.path);
|
|
107
|
+
if (!fits) {
|
|
108
|
+
const record = Buffer.from(paxRecord('path', file.path));
|
|
109
|
+
yield header('PaxHeader', record.length, 'x', mtime);
|
|
110
|
+
yield record;
|
|
111
|
+
yield padding(record.length);
|
|
112
|
+
}
|
|
113
|
+
const data = await readFile(file.absolute);
|
|
114
|
+
yield header(fits ? file.path : file.path.slice(0, 99).replace(/[^\x20-\x7e]/g, '_'), data.length, '0', mtime);
|
|
115
|
+
yield data;
|
|
116
|
+
yield padding(data.length);
|
|
117
|
+
}
|
|
118
|
+
yield Buffer.alloc(BLOCK * 2);
|
|
119
|
+
}
|
|
120
|
+
/** Writes the files as a gzipped tar to `target`; returns its size in bytes. */
|
|
121
|
+
export async function pack(files, target) {
|
|
122
|
+
await pipeline(Readable.from(tarStream(files)), createGzip({ level: 9 }), createWriteStream(target));
|
|
123
|
+
return (await lstat(target)).size;
|
|
124
|
+
}
|
package/dist/version.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeqlick/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Deploy static sites to Codeqlick Cloud from your terminal or CI",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"homepage": "https://docs.codeqlick.com",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/Codeqlick/Codeqlick.git",
|
|
11
|
+
"directory": "codeqlick-cli"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"codeqlick",
|
|
15
|
+
"deploy",
|
|
16
|
+
"static",
|
|
17
|
+
"hosting",
|
|
18
|
+
"cli"
|
|
19
|
+
],
|
|
20
|
+
"bin": {
|
|
21
|
+
"codeqlick": "dist/cli.js"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=20"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json && chmod +x dist/cli.js",
|
|
32
|
+
"typecheck": "tsc --noEmit",
|
|
33
|
+
"test": "node --test src/*.test.ts",
|
|
34
|
+
"format": "prettier --write .",
|
|
35
|
+
"format:check": "prettier --check .",
|
|
36
|
+
"prepublishOnly": "npm run typecheck && npm test && npm run build"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "24.19.2",
|
|
40
|
+
"prettier": "3.9.10",
|
|
41
|
+
"typescript": "7.0.2"
|
|
42
|
+
}
|
|
43
|
+
}
|