Skip to content

Commit 7c2ec98

Browse files
authored
Merge pull request #1482 from polywrap/chore/separate-deploy-from-polywrap-project
chore: separate deploy from polywrap project
2 parents 0df764f + 2366e99 commit 7c2ec98

56 files changed

Lines changed: 988 additions & 870 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/cli/src/__tests__/e2e/deploy.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Deploys Polywrap Projects
1818
1919
Options:
2020
-m, --manifest-file <path> Path to the Polywrap Deploy manifest file
21-
(default: polywrap.yaml | polywrap.yml)
21+
(default: polywrap.deploy.yaml |
22+
polywrap.deploy.yml)
2223
-o, --output-file <path> Output file path for the deploy result
2324
-v, --verbose Verbose output (default: false)
2425
-q, --quiet Suppress output (default: false)

packages/cli/src/commands/deploy.ts

Lines changed: 8 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,25 @@
22
import { Command, Program, BaseCommandOptions } from "./types";
33
import { createLogger } from "./utils/createLogger";
44
import {
5-
defaultPolywrapManifest,
6-
DeployPackage,
75
intlMsg,
86
parseManifestFileOption,
9-
PolywrapProject,
10-
DeployJob,
11-
DeployStep,
127
parseLogFileOption,
8+
Deployer,
9+
defaultDeployManifest,
1310
} from "../lib";
1411

15-
import { DeployManifest } from "@polywrap/polywrap-manifest-types-js";
1612
import fs from "fs";
17-
import nodePath from "path";
13+
import path from "path";
1814
import yaml from "yaml";
19-
import { validate } from "jsonschema";
2015

21-
const defaultManifestStr = defaultPolywrapManifest.join(" | ");
16+
const defaultManifestStr = defaultDeployManifest.join(" | ");
2217
const pathStr = intlMsg.commands_deploy_options_o_path();
2318

2419
export interface DeployCommandOptions extends BaseCommandOptions {
2520
manifestFile: string;
2621
outputFile: string | false;
2722
}
2823

29-
type ManifestJob = DeployManifest["jobs"][number];
30-
type ManifestStep = ManifestJob["steps"][number];
31-
3224
export const deploy: Command = {
3325
setup: (program: Program) => {
3426
program
@@ -55,7 +47,7 @@ export const deploy: Command = {
5547
await run({
5648
manifestFile: parseManifestFileOption(
5749
options.manifestFile,
58-
defaultPolywrapManifest
50+
defaultDeployManifest
5951
),
6052
outputFile: options.outputFile || false,
6153
verbose: options.verbose || false,
@@ -70,81 +62,11 @@ async function run(options: Required<DeployCommandOptions>): Promise<void> {
7062
const { manifestFile, outputFile, verbose, quiet, logFile } = options;
7163
const logger = createLogger({ verbose, quiet, logFile });
7264

73-
const project = new PolywrapProject({
74-
rootDir: nodePath.dirname(manifestFile),
75-
polywrapManifestPath: manifestFile,
76-
logger,
77-
});
78-
await project.validate();
79-
80-
const deployManifest = await project.getDeployManifest();
81-
82-
if (!deployManifest) {
83-
throw new Error("No deploy manifest found.");
84-
}
85-
86-
const allStepsFromAllJobs = Object.entries(deployManifest.jobs).flatMap(
87-
([jobName, job]) => {
88-
return job.steps.map((step) => ({
89-
jobName,
90-
...step,
91-
}));
92-
}
93-
);
94-
95-
const packageNames = [
96-
...new Set(allStepsFromAllJobs.map((step) => step.package)),
97-
];
98-
99-
sanitizePackages(packageNames);
100-
101-
await project.cacheDeployModules(packageNames);
102-
103-
const packageMapEntries = await Promise.all(
104-
packageNames.map(async (packageName) => {
105-
const deployerPackage = await project.getDeployModule(packageName);
106-
return [packageName, deployerPackage];
107-
})
108-
);
109-
110-
const packageMap = Object.fromEntries(packageMapEntries);
111-
112-
const stepToPackageMap: Record<
113-
string,
114-
DeployPackage & { jobName: string }
115-
> = {};
116-
117-
for (const step of allStepsFromAllJobs) {
118-
stepToPackageMap[step.name] = {
119-
...packageMap[step.package],
120-
jobName: step.jobName,
121-
};
122-
}
123-
124-
validateManifestWithExts(deployManifest, stepToPackageMap);
125-
126-
const jobs = Object.entries(deployManifest.jobs).map(([jobName, job]) => {
127-
const steps: DeployStep[] = job.steps.map((step) => {
128-
return new DeployStep({
129-
name: step.name,
130-
uriOrStepResult: step.uri,
131-
deployer: stepToPackageMap[step.name].deployer,
132-
config: step.config ?? {},
133-
});
134-
});
135-
136-
return new DeployJob({
137-
name: jobName,
138-
steps,
139-
config: job.config ?? {},
140-
logger,
141-
});
142-
});
143-
144-
const jobResults = await Promise.all(jobs.map((job) => job.run()));
65+
const deployer = await Deployer.create(manifestFile, logger);
66+
const jobResults = await deployer.run();
14567

14668
if (outputFile) {
147-
const outputFileExt = nodePath.extname(outputFile).substring(1);
69+
const outputFileExt = path.extname(outputFile).substring(1);
14870
if (!outputFileExt) throw new Error("Require output file extension");
14971
switch (outputFileExt) {
15072
case "yaml":
@@ -164,61 +86,3 @@ async function run(options: Required<DeployCommandOptions>): Promise<void> {
16486
}
16587
process.exit(0);
16688
}
167-
168-
function sanitizePackages(packages: string[]) {
169-
const unrecognizedPackages: string[] = [];
170-
171-
const availableDeployers = fs.readdirSync(
172-
nodePath.join(__dirname, "..", "lib", "defaults", "deploy-modules")
173-
);
174-
175-
packages.forEach((p) => {
176-
if (!availableDeployers.includes(p)) {
177-
unrecognizedPackages.push(p);
178-
}
179-
});
180-
181-
if (unrecognizedPackages.length) {
182-
throw new Error(
183-
`Unrecognized packages: ${unrecognizedPackages.join(", ")}`
184-
);
185-
}
186-
}
187-
188-
function validateManifestWithExts(
189-
deployManifest: DeployManifest,
190-
stepToPackageMap: Record<string, DeployPackage & { jobName: string }>
191-
) {
192-
const errors = Object.entries(stepToPackageMap).flatMap(
193-
([stepName, step]) => {
194-
const jobEntry = Object.entries(deployManifest.jobs).find(
195-
([jobName]) => jobName === step.jobName
196-
) as [string, ManifestJob];
197-
198-
const job = jobEntry[1];
199-
200-
const stepToValidate = job.steps.find(
201-
(s) => s.name === stepName
202-
) as ManifestStep;
203-
204-
return step.manifestExt
205-
? validate(
206-
{
207-
...job.config,
208-
...stepToValidate.config,
209-
},
210-
step.manifestExt
211-
).errors
212-
: [];
213-
}
214-
);
215-
216-
if (errors.length) {
217-
throw new Error(
218-
[
219-
`Validation errors encountered while sanitizing DeployManifest format ${deployManifest.format}`,
220-
...errors.map((error) => error.toString()),
221-
].join("\n")
222-
);
223-
}
224-
}

packages/cli/src/lib/defaults/deploy-modules/ens-recursive-name-register/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-require-imports */
22
/* eslint-disable @typescript-eslint/no-var-requires */
3-
import { Deployer } from "../../../deploy";
3+
import { DeployModule } from "../../../deploy";
44

55
import { Wallet } from "@ethersproject/wallet";
66
import { JsonRpcProvider } from "@ethersproject/providers";
@@ -13,7 +13,7 @@ import {
1313
import { embeddedWrappers } from "@polywrap/test-env-js";
1414
import { PolywrapClient } from "@polywrap/client-js";
1515

16-
class ENSRecursiveNameRegisterPublisher implements Deployer {
16+
class ENSRecursiveNameRegisterPublisher implements DeployModule {
1717
async execute(
1818
uri: Uri,
1919
config: {

packages/cli/src/lib/defaults/deploy-modules/ens/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-require-imports */
22
/* eslint-disable @typescript-eslint/no-var-requires */
3-
import { Deployer } from "../../../deploy";
3+
import { DeployModule } from "../../../deploy";
44

55
import { Wallet } from "@ethersproject/wallet";
66
import { JsonRpcProvider } from "@ethersproject/providers";
@@ -15,7 +15,7 @@ import { PolywrapClient } from "@polywrap/client-js";
1515

1616
const contentHash = require("content-hash");
1717

18-
class ENSPublisher implements Deployer {
18+
class ENSPublisher implements DeployModule {
1919
async execute(
2020
uri: Uri,
2121
config: {

packages/cli/src/lib/defaults/deploy-modules/http/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Deployer } from "../../../deploy/deployer";
1+
import { DeployModule } from "../../../deploy";
22

33
import { Uri } from "@polywrap/core-js";
44
import FormData from "form-data";
@@ -33,7 +33,7 @@ const dirToFormData = (baseDirPath: string) => {
3333
return formData;
3434
};
3535

36-
class HTTPDeployer implements Deployer {
36+
class HTTPDeployer implements DeployModule {
3737
async execute(uri: Uri, config?: { postUrl: string }): Promise<Uri> {
3838
if (!isValidUri(uri)) {
3939
throw new Error(

packages/cli/src/lib/defaults/deploy-modules/ipfs-test/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Deployer } from "../../../deploy/deployer";
1+
import { DeployModule } from "../../../deploy";
22

33
import { Uri } from "@polywrap/core-js";
44

5-
class IPFSDeployer implements Deployer {
5+
class IPFSDeployer implements DeployModule {
66
// eslint-disable-next-line @typescript-eslint/naming-convention
77
async execute(_: Uri, __: unknown): Promise<Uri> {
88
return new Uri(`ipfs/Qm`);

packages/cli/src/lib/defaults/deploy-modules/ipfs/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Deployer } from "../../../deploy/deployer";
1+
import { DeployModule } from "../../../deploy";
22

33
import { Uri } from "@polywrap/core-js";
44

@@ -8,7 +8,7 @@ const { globSource } = IPFSClient;
88

99
const isValidUri = (uri: Uri) => uri.authority === "fs";
1010

11-
class IPFSDeployer implements Deployer {
11+
class IPFSDeployer implements DeployModule {
1212
async execute(uri: Uri, config?: { gatewayUri: string }): Promise<Uri> {
1313
if (!isValidUri(uri)) {
1414
throw new Error(

packages/cli/src/lib/defaults/infra-modules/http/server/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"file-saver": "2.0.5",
2020
"jszip": "3.10.1",
2121
"fs-extra": "7.0.1",
22-
"multer": "1.4.5-lts.1"
22+
"multer": "1.4.5-lts.1",
23+
"sanitize-filename": "1.6.3"
2324
},
2425
"devDependencies": {
2526
"@types/express": "4.17.13",

packages/cli/src/lib/defaults/infra-modules/http/server/src/utils/zip.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import JSZip from "jszip";
2+
import sanitize from "sanitize-filename";
23
import fse from "fs-extra";
4+
import path from "path";
35

46
export class Zip {
57
private _zip: JSZip;
@@ -8,7 +10,7 @@ export class Zip {
810
this._zip = new JSZip();
911
}
1012

11-
private generateNodeZip(filePath: string) {
13+
private _generateNodeZip(filePath: string) {
1214
return new Promise<boolean>((res, rej) => {
1315
this._zip.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
1416
.pipe(fse.createWriteStream(filePath))
@@ -25,6 +27,6 @@ export class Zip {
2527
fse.readdirSync(sourceDir).forEach(file => {
2628
this._zip.file(file, fse.readFileSync(`${sourceDir}/${file}`))
2729
})
28-
return this.generateNodeZip(outputPath);
30+
return this._generateNodeZip(outputPath);
2931
}
3032
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { DeployStep, StepName, StepResult, UriOrPrevStepResult } from "./step";
1+
import {
2+
DeployStep,
3+
StepName,
4+
StepResult,
5+
UriOrPrevStepResult,
6+
} from "./DeployStep";
27
import { Logger } from "../logging";
38

49
import { Uri } from "@polywrap/core-js";

0 commit comments

Comments
 (0)