Skip to content

Commit

Permalink
Merge pull request #304 from dappnode/pablo/sdk-exports-index
Browse files Browse the repository at this point in the history
Export from main `index.js`
  • Loading branch information
pablomendezroyo authored Apr 10, 2023
2 parents 03f2193 + 1e30e32 commit 4df4f5c
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 178 deletions.
23 changes: 0 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,6 @@
"description": "dappnodesdk is a tool to make the creation of new dappnode packages as simple as possible. It helps to initialize and publish in ethereum blockchain",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/exports.js"
},
"./compose": {
"import": "./dist/files/compose/index.js"
},
"./manifest": {
"import": "./dist/files/manifest/index.js"
},
"./setupWizard": {
"import": "./dist/files/setupWizard/index.js"
},
"./schemaValidation": {
"import": "./dist/schemaValidation/index.js"
},
"./params": {
"import": "./dist/params.js"
},
"./types": {
"import": "./dist/types.js"
}
},
"bin": {
"dappnodesdk": "dist/dappnodesdk.js"
},
Expand Down
3 changes: 0 additions & 3 deletions src/exports.ts

This file was deleted.

21 changes: 6 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { buildHandler } from "./commands/build.js";
import { fromGithubHandler } from "./commands/from_github.js";
import { increaseHandler } from "./commands/increase.js";
import { initHandler } from "./commands/init.js";
import { nextHandler } from "./commands/next.js";
import { publishHanlder } from "./commands/publish.js";

export const dappnodesdk = {
build: buildHandler,
fromGithub: fromGithubHandler,
increase: increaseHandler,
init: initHandler,
next: nextHandler,
publish: publishHanlder
};
export * from "./params.js";
export * from "./types.js";
export { validateManifestSchema } from "./schemaValidation/validateManifestSchema.js";
export { validateDappnodeCompose } from "./files/compose/validateDappnodeCompose.js";
// The setupWizard file is not mandatory and may not be present, rn is used the filesystem
// module, so it cannot be imported in browser side apps
2 changes: 2 additions & 0 deletions src/params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { FileFormat, Architecture, ManifestFormat } from "./types.js";

export * from "./files/compose/params.js";

export class CliError extends Error {}
export class YargsError extends Error {}

Expand Down
12 changes: 12 additions & 0 deletions src/schemaValidation/ajv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Ajv from "ajv";
import ajvErrors from "ajv-errors";

export const ajv = new Ajv({
logger: false,
allErrors: true,
coerceTypes: true,
strictSchema: false,
allowUnionTypes: true
});

ajvErrors.default(ajv);
4 changes: 3 additions & 1 deletion src/schemaValidation/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "././validateSchema.js";
export { validateComposeSchema } from "./validateComposeSchema.js";
export { validateManifestSchema } from "./validateManifestSchema.js";
export { validateSetupWizardSchema } from "./validateSetupWizardSchema.js";
65 changes: 65 additions & 0 deletions src/schemaValidation/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ErrorObject } from "ajv";

// validate.errors = [
// {
// keyword: "pattern",
// dataPath: "/avatar",
// schemaPath: "#/properties/avatar/pattern",
// params: { pattern: "^/(ipfs|bzz)/w+$" },
// message: 'should match pattern "^/(ipfs|bzz)/w+$"'
// },
// {
// keyword: "pattern",
// dataPath: "/image/hash",
// schemaPath: "#/properties/image/properties/hash/pattern",
// params: { pattern: "^/(ipfs|bzz)/w+$" },
// message: 'should match pattern "^/(ipfs|bzz)/w+$"'
// },
// {
// keyword: "type",
// dataPath: "/image/size",
// schemaPath: "#/properties/image/properties/size/type",
// params: { type: "number" },
// message: "should be number"
// },
// {
// keyword: "pattern",
// dataPath: "/image/size",
// schemaPath: "#/properties/image/properties/size/pattern",
// params: { pattern: "^d+$" },
// message: 'should match pattern "^d+$"'
// },
// {
// keyword: "required",
// dataPath: "",
// schemaPath: "#/required",
// params: { missingProperty: "license" },
// message: "should have required property 'license'"
// }
// ];

/**
*
* @param errorObject from AJV:
* {
* keyword: "pattern",
* dataPath: "/avatar",
* schemaPath: "#/properties/avatar/pattern",
* params: { pattern: "^/(ipfs|bzz)/w+$" },
* message: 'should match pattern "^/(ipfs|bzz)/w+$"'
* }
* @returns errorMessage:
* "manifest.avatar should match pattern "^/(ipfs|bzz)/w+$""
*/
export function processError(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
errorObject: ErrorObject<string, Record<string, any>, unknown>,
releaseFileType: "compose" | "manifest" | "setupWizard"
): string {
const { schemaPath, message } = errorObject;
const path = `${releaseFileType}${schemaPath}`.replace(
new RegExp("/", "g"),
"."
);
return `${path} ${message}`;
}
17 changes: 17 additions & 0 deletions src/schemaValidation/validateComposeSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { CliError } from "../params.js";
import { shell } from "../utils/shell.js";
import fs from "fs";

/**
* Validates compose file with docker-compose config
* @param compose
*/
export async function validateComposeSchema(
composeFilePath: string
): Promise<void> {
if (!fs.existsSync(composeFilePath))
throw Error(`Compose file ${composeFilePath} not found`);
await shell(`docker-compose -f ${composeFilePath} config`).catch(e => {
throw new CliError(`Invalid compose:\n${e.stderr}`);
});
}
22 changes: 22 additions & 0 deletions src/schemaValidation/validateManifestSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ajv } from "./ajv.js";
import { CliError } from "../params.js";
import { Manifest } from "../types.js";
import { processError } from "./utils.js";
import manifestSchema from "./schemas/manifest.schema.json" assert { type: "json" };

/**
* Validates manifest file with schema
* @param manifest
*/
export function validateManifestSchema(manifest: Manifest): void {
const validateManifest = ajv.compile(manifestSchema);
const valid = validateManifest(manifest);
if (!valid) {
const errors = validateManifest.errors
? validateManifest.errors.map(e => processError(e, "manifest"))
: [];
throw new CliError(
`Invalid manifest: \n${errors.map(msg => ` - ${msg}`).join("\n")}`
);
}
}
134 changes: 0 additions & 134 deletions src/schemaValidation/validateSchema.ts

This file was deleted.

25 changes: 25 additions & 0 deletions src/schemaValidation/validateSetupWizardSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ajv } from "./ajv.js";
import { readSetupWizardIfExists } from "../files/index.js";
import { CliError } from "../params.js";
import { processError } from "./utils.js";
import setupWizardSchema from "./schemas/setup-wizard.schema.json" assert { type: "json" };

/**
* Validates setupWizard file with schema
* @param setupWizard
*/
export function validateSetupWizardSchema(dir?: string): void {
// The setupwizard file is not mandatory and may not be present
const setupWizard = readSetupWizardIfExists(dir);
if (!setupWizard) return;
const validateSetupWizard = ajv.compile(setupWizardSchema);
const valid = validateSetupWizard(setupWizard);
if (!valid) {
const errors = validateSetupWizard.errors
? validateSetupWizard.errors.map(e => processError(e, "setupWizard"))
: [];
throw new CliError(
`Invalid setupWizard: \n${errors.map(msg => ` - ${msg}`).join("\n")}`
);
}
}
2 changes: 1 addition & 1 deletion src/tasks/buildAndUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
validateComposeSchema,
validateManifestSchema,
validateSetupWizardSchema
} from "../schemaValidation/validateSchema.js";
} from "../schemaValidation/index.js";
import {
getComposePath,
readCompose,
Expand Down
2 changes: 1 addition & 1 deletion test/schemaValidation/validateSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
validateComposeSchema,
validateManifestSchema,
validateSetupWizardSchema
} from "../../src/schemaValidation/validateSchema.js";
} from "../../src/schemaValidation/index.js";
import fs from "fs";
import path from "path";
import { cleanTestDir, testDir } from "../testUtils.js";
Expand Down

0 comments on commit 4df4f5c

Please sign in to comment.