@codeqlick/cli 0.1.0 → 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 +127 -12
- package/dist/api.js +128 -15
- package/dist/cli.js +1235 -14
- package/dist/config.js +56 -1
- package/dist/download.js +63 -0
- package/dist/version.js +2 -0
- package/package.json +7 -2
package/dist/config.js
CHANGED
|
@@ -32,7 +32,62 @@ export async function readProjectFile(cwd = process.cwd()) {
|
|
|
32
32
|
return {};
|
|
33
33
|
}
|
|
34
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
|
+
*/
|
|
35
88
|
export async function writeProjectFile(file, cwd = process.cwd()) {
|
|
36
|
-
await
|
|
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`);
|
|
37
92
|
}
|
|
38
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/version.js
ADDED
package/package.json
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeqlick/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Deploy static sites to Codeqlick Cloud from your terminal or CI",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"homepage": "https://
|
|
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
|
+
},
|
|
8
13
|
"keywords": [
|
|
9
14
|
"codeqlick",
|
|
10
15
|
"deploy",
|