Skip to content

Commit

Permalink
feat(package.json): read name, description and manifest version from …
Browse files Browse the repository at this point in the history
…package.json file

JST-385
  • Loading branch information
pgrzy-golem committed Sep 8, 2023
1 parent 9c990f1 commit 7d35840
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 20 deletions.
16 changes: 13 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"commander": "^11.0.0",
"lodash": "^4.17.21",
"luxon": "^3.4.3",
"new-find-package-json": "^2.0.0",
"node-fetch": "^2.7.0",
"typescript": "^5.2.2"
},
Expand Down
74 changes: 65 additions & 9 deletions src/manifest/manifest-create.action.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ManifestCreateOptions } from "./manifest-create.options";
import { ManifestDto, ManifestVersions } from "./dto";
import { writeFile } from "fs/promises";
import { readFile, writeFile } from "fs/promises";
import { checkFileOverwrite } from "../lib/file";
import fetch from "node-fetch";
import { DateTime } from "luxon";
import { findAsync } from "new-find-package-json";

const repoUrl = "https://registry.golem.network";

Expand Down Expand Up @@ -105,19 +106,79 @@ async function getImage(imageSpec: string, providedHash?: string): Promise<Image
}
}

export async function manifestCreateAction(name: string, image: string, options: ManifestCreateOptions): Promise<void> {
async function findPackageJson(possiblePath?: string): Promise<string | undefined> {
if (possiblePath) {
return possiblePath;
}

// Find first package.json file.
for await (const path of findAsync(process.cwd(), "package.json")) {
return path;
}
}

async function fillOptionsWithPackageJson(options: ManifestCreateOptions): Promise<ManifestCreateOptions> {
if ("manifestVersion" in options && "name" in options && "description" in options) {
// All fields from package.json are provided.
return options;
}

const path = await findPackageJson(options.packageJson);
if (!path) {
console.error(
'Error: Cannot find package.json. Use "--package-json" option to specify its path or make sure --name, --description and --manifest-version are set.',
);
process.exit(1);
}

// Read package.json.
let packageData: string;
try {
packageData = await readFile(path, "utf-8");
} catch (e) {
console.error(`Error: Cannot read ${path}: ${e}`);
process.exit(1);
}

// Parse package.json
let packageJson: { version: string; name: string; description?: string };
try {
packageJson = JSON.parse(packageData);
} catch (e) {
console.error(`Error: Cannot parse ${path}: ${e}`);
process.exit(1);
}

if (!("manifestVersion" in options)) {
options.manifestVersion = packageJson.version;
}

if (!("name" in options)) {
options.name = packageJson.name;
}

if (!("description" in options)) {
options.description = packageJson.description;
}

return options;
}

export async function manifestCreateAction(image: string, options: ManifestCreateOptions): Promise<void> {
const imageData = await getImage(image, options.imageHash);
const now = DateTime.now();
const expires = now.plus({ days: 90 }); // TODO: move that to options?

options = await fillOptionsWithPackageJson(options);

const manifest: ManifestDto = {
version: ManifestVersions.GAP_5,
createdAt: now.toISO() as string,
expiresAt: expires.toISO() as string,
metadata: {
name,
name: options.name!,
description: options.description,
version: options.version,
version: options.manifestVersion!,
},
payload: [
{
Expand All @@ -132,11 +193,6 @@ export async function manifestCreateAction(name: string, image: string, options:
};

// TODO: Add enquirer to ask for missing fields.

manifest.metadata.name = name;
manifest.metadata.description = options.description;
manifest.metadata.version = options.version;

await checkFileOverwrite("Manifest", options.manifest, options.overwrite);
await writeFile(options.manifest, JSON.stringify(manifest, null, 2));

Expand Down
16 changes: 11 additions & 5 deletions src/manifest/manifest-create.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ manifestCreateCommand
.description("Create a new Golem manifest.")
.addOption(createManifestOption())
.option("-w, --overwrite", "Overwrite existing manifest (if present).")
.option("-n, --name <name>", "Name of the manifest/project.")
.option("-d, --description <text>", "Description of the manifest.")
.option("-v, --version <version>", "Version of the manifest.", "1.0.0")
.option("-i, --image-hash <hash>", "Image hash to be used in the manifest (format 'hash-function:hash-base64'")
.argument("<name>", "Name of the manifest.")
.option("-v, --manifest-version <version>", "Version of the manifest.")
.option("-i, --image-hash <hash>", "Image hash to be used in the manifest (format 'hash-function:hash-base64').")
.option("-p, --package-json <file>", "Package.json file to be used as information source.")
.argument("<image>", "Image to be used in the manifest, identified by URL, image tag or image hash.")
.action(async (name: string, image: string, options: ManifestCreateOptions) => {
.addHelpText(
"after",
"\npackage.json can be used to automatically fill manifest name, description and version." +
"\nIf package.json path is not provided, it will be looked for in current directory and all parent directories.",
)
.action(async (image: string, options: ManifestCreateOptions) => {
const action = await import("./manifest-create.action.js");
await action.default.manifestCreateAction(name, image, options);
await action.default.manifestCreateAction(image, options);
});
8 changes: 5 additions & 3 deletions src/manifest/manifest-create.options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export interface ManifestCreateOptions {
version: string;
manifestVersion?: string;
overwrite: boolean;
description: string;
name?: string;
description?: string;
manifest: string;
imageHash: string;
imageHash?: string;
packageJson?: string;
}

0 comments on commit 7d35840

Please sign in to comment.