@craftland/ggpm 0.1.2 → 0.1.3
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 +17 -6
- package/bin/ggpm.cjs +6 -1
- package/lib/github-registry.cjs +1 -1
- package/lib/list.cjs +60 -0
- package/lib/publish.cjs +1 -29
- package/lib/registry-index.cjs +58 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,17 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
Package Manager for Craftland Packages
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Requirement
|
|
6
|
+
- Nodejs
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
## Install the CLI
|
|
8
9
|
|
|
9
10
|
```powershell
|
|
10
11
|
npm install -g @craftland/ggpm
|
|
11
12
|
```
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
## Update the CLI
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
npm install -g "@craftland/ggpm@latest" --prefer-online
|
|
18
|
+
```
|
|
16
19
|
|
|
17
20
|
## Commands
|
|
18
21
|
|
|
@@ -20,6 +23,15 @@ hosts `registry.json` and the Craftland package ZIPs.
|
|
|
20
23
|
|
|
21
24
|
Print the installed CLI version.
|
|
22
25
|
|
|
26
|
+
### `ggpm list`
|
|
27
|
+
|
|
28
|
+
Show packages in the registry with the latest published version of each, including
|
|
29
|
+
prereleases. Results are sorted by package name. Run this from any directory.
|
|
30
|
+
|
|
31
|
+
A public registry needs no credentials. For a private registry, sign in with
|
|
32
|
+
`gh auth login` or set `GGPM_GITHUB_TOKEN` with **Contents: Read** access to
|
|
33
|
+
`FF-OGC/ggpm-registry`.
|
|
34
|
+
|
|
23
35
|
### `ggpm publish [path]`
|
|
24
36
|
|
|
25
37
|
Run `ggpm publish path/to/package` to publish a specific package folder.
|
|
@@ -80,7 +92,6 @@ publishing.
|
|
|
80
92
|
|
|
81
93
|
## Planned commands
|
|
82
94
|
|
|
83
|
-
- `ggpm list` shows available packages and their latest versions.
|
|
84
95
|
- `ggpm init` creates `ggpackage.json` at the root of a Craftland project.
|
|
85
96
|
- `ggpm install` installs the packages declared in the project manifest.
|
|
86
97
|
- `ggpm install <package-name> [version]` installs a package and records it in
|
package/bin/ggpm.cjs
CHANGED
|
@@ -7,6 +7,11 @@ const [flag, ...extraArgs] = process.argv.slice(2);
|
|
|
7
7
|
|
|
8
8
|
if (extraArgs.length === 0 && (flag === '-v' || flag === '--version')) {
|
|
9
9
|
console.log(version);
|
|
10
|
+
} else if (flag === 'list' && extraArgs.length === 0) {
|
|
11
|
+
require('../lib/list.cjs').listPackages().catch((error) => {
|
|
12
|
+
console.error(`ggpm list: ${error.message}`);
|
|
13
|
+
process.exitCode = 1;
|
|
14
|
+
});
|
|
10
15
|
} else if (flag === 'publish' && extraArgs.length <= 1) {
|
|
11
16
|
const [packagePath = '.'] = extraArgs;
|
|
12
17
|
require('../lib/publish.cjs').publish(resolve(packagePath)).catch((error) => {
|
|
@@ -14,6 +19,6 @@ if (extraArgs.length === 0 && (flag === '-v' || flag === '--version')) {
|
|
|
14
19
|
process.exitCode = 1;
|
|
15
20
|
});
|
|
16
21
|
} else {
|
|
17
|
-
console.error('Usage: ggpm -v | ggpm --version | ggpm publish [path]');
|
|
22
|
+
console.error('Usage: ggpm -v | ggpm --version | ggpm list | ggpm publish [path]');
|
|
18
23
|
process.exitCode = 1;
|
|
19
24
|
}
|
package/lib/github-registry.cjs
CHANGED
|
@@ -7,7 +7,7 @@ function githubRegistry(token) {
|
|
|
7
7
|
method,
|
|
8
8
|
headers: {
|
|
9
9
|
accept: 'application/vnd.github+json',
|
|
10
|
-
authorization: `Bearer ${token}
|
|
10
|
+
...(token ? { authorization: `Bearer ${token}` } : {}),
|
|
11
11
|
'x-github-api-version': '2026-03-10',
|
|
12
12
|
...(json ? { 'content-type': 'application/json' } : {}),
|
|
13
13
|
...(bytes ? { 'content-type': 'application/zip' } : {}),
|
package/lib/list.cjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const { isObject, parseVersion, readCatalog } = require('./registry-index.cjs');
|
|
2
|
+
|
|
3
|
+
function compareText(left, right) {
|
|
4
|
+
return left === right ? 0 : left > right ? 1 : -1;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function compareNumbers(left, right) {
|
|
8
|
+
// Version components can exceed JavaScript's safe integer range.
|
|
9
|
+
return left.length - right.length || compareText(left, right);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function compareVersions(left, right) {
|
|
13
|
+
for (let i = 0; i < left.parts.length; i += 1) {
|
|
14
|
+
const order = compareNumbers(left.parts[i], right.parts[i]);
|
|
15
|
+
if (order !== 0) return order;
|
|
16
|
+
}
|
|
17
|
+
if (left.prerelease !== right.prerelease) return left.prerelease ? -1 : 1;
|
|
18
|
+
|
|
19
|
+
const leftIds = left.prereleaseParts;
|
|
20
|
+
const rightIds = right.prereleaseParts;
|
|
21
|
+
for (let i = 0; i < Math.min(leftIds.length, rightIds.length); i += 1) {
|
|
22
|
+
const leftIsNumeric = /^\d+$/.test(leftIds[i]);
|
|
23
|
+
const rightIsNumeric = /^\d+$/.test(rightIds[i]);
|
|
24
|
+
if (leftIsNumeric !== rightIsNumeric) return leftIsNumeric ? -1 : 1;
|
|
25
|
+
const order = leftIsNumeric
|
|
26
|
+
? compareNumbers(leftIds[i], rightIds[i])
|
|
27
|
+
: compareText(leftIds[i], rightIds[i]);
|
|
28
|
+
if (order !== 0) return order;
|
|
29
|
+
}
|
|
30
|
+
return leftIds.length - rightIds.length;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function latestVersion(name, versions) {
|
|
34
|
+
if (!isObject(versions)) throw new Error(`Invalid versions object for ${name} in registry.json.`);
|
|
35
|
+
|
|
36
|
+
let latest = null;
|
|
37
|
+
for (const version of Object.keys(versions).sort()) {
|
|
38
|
+
const parsed = parseVersion(version);
|
|
39
|
+
if (!parsed) throw new Error(`Invalid version ${JSON.stringify(version)} for ${name} in registry.json.`);
|
|
40
|
+
if (!latest || compareVersions(parsed, latest) > 0) latest = { ...parsed, version };
|
|
41
|
+
}
|
|
42
|
+
return latest?.version ?? '(no releases)';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function listPackages() {
|
|
46
|
+
const { packages } = await readCatalog();
|
|
47
|
+
const names = Object.keys(packages).sort((left, right) => left.localeCompare(right, 'en'));
|
|
48
|
+
if (names.length === 0) {
|
|
49
|
+
console.log('No packages have been published yet.');
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const rows = names.map((name) => [name, latestVersion(name, packages[name]?.versions)]);
|
|
54
|
+
const width = names.reduce((max, name) => Math.max(max, name.length), 'PACKAGE'.length);
|
|
55
|
+
console.log([['PACKAGE', 'LATEST'], ...rows]
|
|
56
|
+
.map(([name, version]) => `${name.padEnd(width)} ${version}`)
|
|
57
|
+
.join('\n'));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
module.exports = { listPackages };
|
package/lib/publish.cjs
CHANGED
|
@@ -3,26 +3,10 @@ const { lstat, readFile, readdir } = require('node:fs/promises');
|
|
|
3
3
|
const { join } = require('node:path');
|
|
4
4
|
const { Zip, ZipDeflate } = require('fflate');
|
|
5
5
|
const { githubRegistry, registryRepo } = require('./github-registry.cjs');
|
|
6
|
+
const { isObject, parseJson, parseVersion, readIndex } = require('./registry-index.cjs');
|
|
6
7
|
|
|
7
8
|
const manifestFile = 'ggpackage.json';
|
|
8
9
|
const packageName = /^[a-z0-9][a-z0-9._-]*\/[a-z0-9][a-z0-9._-]*$/i;
|
|
9
|
-
const versionPattern = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([a-z\d-]+(?:\.[a-z\d-]+)*))?(?:\+[a-z\d-]+(?:\.[a-z\d-]+)*)?$/i;
|
|
10
|
-
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
11
|
-
|
|
12
|
-
function parseVersion(value) {
|
|
13
|
-
if (typeof value !== 'string' || value !== value.trim()) return null;
|
|
14
|
-
const match = versionPattern.exec(value);
|
|
15
|
-
if (!match || match[4]?.split('.').some((part) => /^0\d+$/.test(part))) return null;
|
|
16
|
-
return { prerelease: Boolean(match[4]) };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function parseJson(bytes, label) {
|
|
20
|
-
try {
|
|
21
|
-
return JSON.parse(bytes.toString('utf8').replace(/^\uFEFF/, ''));
|
|
22
|
-
} catch (error) {
|
|
23
|
-
throw new Error(`${label} is not valid JSON: ${error.message}`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
10
|
|
|
27
11
|
async function loadPackage(root) {
|
|
28
12
|
const path = join(root, manifestFile);
|
|
@@ -106,18 +90,6 @@ async function archivePackage(root, manifest) {
|
|
|
106
90
|
return Buffer.concat(chunks);
|
|
107
91
|
}
|
|
108
92
|
|
|
109
|
-
async function readIndex(registry) {
|
|
110
|
-
const file = await registry.getIndex();
|
|
111
|
-
if (!file?.sha || file.encoding !== 'base64' || typeof file.content !== 'string') {
|
|
112
|
-
throw new Error(`Expected registry.json on the default branch of ${registryRepo}.`);
|
|
113
|
-
}
|
|
114
|
-
const index = parseJson(Buffer.from(file.content, 'base64'), 'registry.json');
|
|
115
|
-
if (index?.schemaVersion !== 1 || !isObject(index.packages)) {
|
|
116
|
-
throw new Error('registry.json must have schemaVersion 1 and a packages object.');
|
|
117
|
-
}
|
|
118
|
-
return { sha: file.sha, index };
|
|
119
|
-
}
|
|
120
|
-
|
|
121
93
|
function packageRecord(index, { name, dependencies }) {
|
|
122
94
|
const existingName = Object.keys(index.packages).find((key) => key.toLowerCase() === name.toLowerCase());
|
|
123
95
|
if (existingName && existingName !== name) {
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const { execFile } = require('node:child_process');
|
|
2
|
+
const { promisify } = require('node:util');
|
|
3
|
+
const { githubRegistry, registryRepo } = require('./github-registry.cjs');
|
|
4
|
+
|
|
5
|
+
const execFileAsync = promisify(execFile);
|
|
6
|
+
const versionPattern = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([a-z\d-]+(?:\.[a-z\d-]+)*))?(?:\+[a-z\d-]+(?:\.[a-z\d-]+)*)?$/i;
|
|
7
|
+
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
8
|
+
|
|
9
|
+
function parseVersion(value) {
|
|
10
|
+
if (typeof value !== 'string' || value !== value.trim()) return null;
|
|
11
|
+
const match = versionPattern.exec(value);
|
|
12
|
+
if (!match) return null;
|
|
13
|
+
const prereleaseParts = match[4]?.split('.') ?? [];
|
|
14
|
+
if (prereleaseParts.some((part) => /^0\d+$/.test(part))) return null;
|
|
15
|
+
return { parts: match.slice(1, 4), prerelease: prereleaseParts.length > 0, prereleaseParts };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function parseJson(bytes, label) {
|
|
19
|
+
try {
|
|
20
|
+
return JSON.parse(bytes.toString('utf8').replace(/^\uFEFF/, ''));
|
|
21
|
+
} catch (error) {
|
|
22
|
+
throw new Error(`${label} is not valid JSON: ${error.message}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function readIndex(registry) {
|
|
27
|
+
const file = await registry.getIndex();
|
|
28
|
+
if (!file?.sha || file.encoding !== 'base64' || typeof file.content !== 'string') {
|
|
29
|
+
throw new Error(`Expected registry.json on the default branch of ${registryRepo}.`);
|
|
30
|
+
}
|
|
31
|
+
const index = parseJson(Buffer.from(file.content, 'base64'), 'registry.json');
|
|
32
|
+
if (index?.schemaVersion !== 1 || !isObject(index.packages)) {
|
|
33
|
+
throw new Error('registry.json must have schemaVersion 1 and a packages object.');
|
|
34
|
+
}
|
|
35
|
+
return { sha: file.sha, index };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function readCatalog() {
|
|
39
|
+
const token = process.env.GGPM_GITHUB_TOKEN?.trim();
|
|
40
|
+
try {
|
|
41
|
+
return (await readIndex(githubRegistry(token))).index;
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (token || ![403, 404, 429].includes(error.status)) throw error;
|
|
44
|
+
|
|
45
|
+
// Public reads need no GitHub CLI session. Try it only when GitHub denies anonymous access.
|
|
46
|
+
const login = await execFileAsync('gh', ['auth', 'token', '--hostname', 'github.com'], {
|
|
47
|
+
timeout: 5_000,
|
|
48
|
+
windowsHide: true,
|
|
49
|
+
}).catch(() => null);
|
|
50
|
+
const githubToken = login?.stdout.trim();
|
|
51
|
+
if (!githubToken) {
|
|
52
|
+
throw new Error(`${error.message}. For an authenticated read, run gh auth login or set GGPM_GITHUB_TOKEN with Contents read access.`);
|
|
53
|
+
}
|
|
54
|
+
return (await readIndex(githubRegistry(githubToken))).index;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = { isObject, parseJson, parseVersion, readIndex, readCatalog };
|