Skip to content

Commit

Permalink
Merge pull request #1301 from polywrap/pileks/cli-manifest-migrate-fo…
Browse files Browse the repository at this point in the history
…rmat-option

CLI `manifest migrate --format` option
  • Loading branch information
dOrgJelli authored Oct 6, 2022
2 parents 3a7e6d4 + aed1e6c commit a1a45c5
Show file tree
Hide file tree
Showing 22 changed files with 203 additions and 127 deletions.
3 changes: 3 additions & 0 deletions packages/cli/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,18 @@
"commands_polywrap_helpPrompt": "Type {command} to view common commands",
"commands_manifest_description": "Inspect & Migrade Polywrap Manifests",
"commands_manifest_options_m": "Path to the manifest file (default: {default})",
"commands_manifest_options_f": "Target format to migrate to (defaults to latest)",
"commands_manifest_options_t": "Type of manifest file to migrate (default: {default})",
"commands_manifest_options_m_path": "path",
"commands_manifest_options_m_format": "format",
"commands_manifest_options_t_type": "type",
"commands_manifest_command_s": "Prints out the schema for the current manifest format.",
"commands_manifest_command_m": "Migrates the polywrap project manifest to the latest version.",
"commands_manifest_command_s_option_r": "Output raw JSON Schema",
"commands_manifest_command_m_preserveManifestMessage": "Saved previous version of manifest to {preservedFilePath}",
"commands_manifest_command_m_migrateManifestMessage": "Migrating {manifestFile} to version {version}",
"commands_manifest_formatError": "Unsupported manifest format. Please make sure that you have the 'format' field present in {fileName} with one of the following values: {values}",
"commands_manifest_migrate_targetFormatError": "Unsupported target format. Supported formats: {formats}",
"commands_manifest_projectTypeError": "Unsupported project type.",
"lib_codeGenerator_genCodeError": "Failed to generate types",
"lib_codeGenerator_genCodeText": "Generate types",
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/lang/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,18 @@
"commands_polywrap_helpPrompt": "Type {command} to view common commands",
"commands_manifest_description": "Inspect & Migrade Polywrap Manifests",
"commands_manifest_options_m": "Path to the manifest file (default: {default})",
"commands_manifest_options_f": "Target format to migrate to (defaults to latest)",
"commands_manifest_options_t": "Type of manifest file to migrate (default: {default})",
"commands_manifest_options_m_path": "path",
"commands_manifest_options_m_format": "format",
"commands_manifest_options_t_type": "type",
"commands_manifest_command_s": "Prints out the schema for the current manifest format.",
"commands_manifest_command_m": "Migrates the polywrap project manifest to the latest version.",
"commands_manifest_command_s_option_r": "Output raw JSON Schema",
"commands_manifest_command_m_preserveManifestMessage": "Saved previous version of manifest to {preservedFilePath}",
"commands_manifest_command_m_migrateManifestMessage": "Migrating {manifestFile} to version {version}",
"commands_manifest_formatError": "Could not detect manifest format. Please make sure that you have the 'format' field present in {fileName} with one of the following values: {values}",
"commands_manifest_formatError": "Unsupported manifest format. Please make sure that you have the 'format' field present in {fileName} with one of the following values: {values}",
"commands_manifest_migrate_targetFormatError": "Unsupported target format. Supported formats: {formats}",
"commands_manifest_projectTypeError": "Unsupported project type.",
"lib_codeGenerator_genCodeError": "Failed to generate types",
"lib_codeGenerator_genCodeText": "Generate types",
Expand Down
37 changes: 35 additions & 2 deletions packages/cli/src/__tests__/e2e/manifest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Options:
| polywrap.yml | polywrap.app.yaml |
polywrap.app.yml | polywrap.plugin.yaml |
polywrap.plugin.yml)
-f, --format <format> Target format to migrate to (defaults to latest)
-h, --help display help for command
`;

Expand Down Expand Up @@ -135,15 +136,15 @@ describe("e2e tests for manifest command", () => {
app: "polywrap.app.yaml",
plugin: "polywrap.plugin.yaml",
};

const validSampleExtensionManifestFiles: Record<string, string> = {
build: "polywrap.build.yaml",
deploy: "polywrap.deploy.yaml",
infra: "polywrap.infra.yaml",
meta: "polywrap.meta.yaml",
workflow: "polywrap.test.yaml",
};

const tempDir = path.join(testsRoot, "temp");

beforeAll(async () => {
Expand Down Expand Up @@ -193,6 +194,22 @@ describe("e2e tests for manifest command", () => {

expect(oldFileExists).toBeTruthy();
});

test(`Should display error when invalid target format is provided for ${projectType} project manifest`, async () => {
const {
exitCode: code,
stdout: output,
stderr: error,
} = await runCLI({
args: ["manifest", "migrate", "-m", manifestFile, "-f", "INVALID_MANIFEST_FORMAT"],
cwd: tempDir,
cli: polywrapCli,
});

expect(output).toBe("");
expect(error).toContain("Unsupported target format. Supported formats:");
expect(code).toBe(1);
});
}

for (const extensionType in validSampleExtensionManifestFiles) {
Expand Down Expand Up @@ -226,6 +243,22 @@ describe("e2e tests for manifest command", () => {

expect(oldFileExists).toBeTruthy();
});

test(`Should display error when invalid target format is provided for ${extensionType} extension manifest`, async () => {
const {
exitCode: code,
stdout: output,
stderr: error,
} = await runCLI({
args: ["manifest", "migrate", extensionType, "-m", manifestFile, "-f", "INVALID_MANIFEST_FORMAT"],
cwd: tempDir,
cli: polywrapCli,
});

expect(output).toBe("");
expect(error).toContain("Unsupported target format. Supported formats:");
expect(code).toBe(1);
});
}
});
});
Expand Down
82 changes: 69 additions & 13 deletions packages/cli/src/commands/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import fs from "fs";
import path from "path";

const pathStr = intlMsg.commands_manifest_options_m_path();
const formatStr = intlMsg.commands_manifest_options_m_format();

const defaultProjectManifestStr = defaultProjectManifestFiles.join(" | ");

Expand All @@ -79,6 +80,7 @@ type ManifestSchemaCommandOptions = {

type ManifestMigrateCommandOptions = {
manifestFile: string;
format: string;
};

export const manifest: Command = {
Expand Down Expand Up @@ -135,6 +137,10 @@ export const manifest: Command = {
default: defaultProjectManifestStr,
})}`
)
.option(
`-f, --format <${formatStr}>`,
`${intlMsg.commands_manifest_options_f()}`
)
.action(async (type, options) => {
await runMigrateCommand(type, options);
});
Expand Down Expand Up @@ -366,22 +372,34 @@ const runMigrateCommand = async (
}

if (isPolywrapManifestLanguage(language)) {
maybeFailOnUnsupportedTargetFormat(
options.format,
Object.values(PolywrapManifestFormats)
);
return migrateManifestFile(
manifestFile,
migratePolywrapProjectManifest,
latestPolywrapManifestFormat
options.format ?? latestPolywrapManifestFormat
);
} else if (isAppManifestLanguage(language)) {
maybeFailOnUnsupportedTargetFormat(
options.format,
Object.values(AppManifestFormats)
);
return migrateManifestFile(
manifestFile,
migrateAppProjectManifest,
latestPolywrapManifestFormat
options.format ?? latestPolywrapManifestFormat
);
} else if (isPluginManifestLanguage(language)) {
maybeFailOnUnsupportedTargetFormat(
options.format,
Object.values(PluginManifestFormats)
);
return migrateManifestFile(
manifestFile,
migratePluginProjectManifest,
latestPolywrapManifestFormat
options.format ?? latestPolywrapManifestFormat
);
}

Expand All @@ -390,67 +408,87 @@ const runMigrateCommand = async (
break;

case "build":
maybeFailOnUnsupportedTargetFormat(
options.format,
Object.values(BuildManifestFormats)
);
migrateManifestFile(
parseManifestFileOption(options.manifestFile, defaultBuildManifest),
migrateBuildExtensionManifest,
latestBuildManifestFormat
options.format ?? latestBuildManifestFormat
);
break;

case "meta":
maybeFailOnUnsupportedTargetFormat(
options.format,
Object.values(MetaManifestFormats)
);
migrateManifestFile(
parseManifestFileOption(options.manifestFile, defaultMetaManifest),
migrateMetaExtensionManifest,
latestMetaManifestFormat
options.format ?? latestMetaManifestFormat
);
break;

case "deploy":
maybeFailOnUnsupportedTargetFormat(
options.format,
Object.values(DeployManifestFormats)
);
migrateManifestFile(
parseManifestFileOption(options.manifestFile, defaultDeployManifest),
migrateDeployExtensionManifest,
latestDeployManifestFormat
options.format ?? latestDeployManifestFormat
);
break;

case "infra":
maybeFailOnUnsupportedTargetFormat(
options.format,
Object.values(InfraManifestFormats)
);
migrateManifestFile(
parseManifestFileOption(options.manifestFile, defaultInfraManifest),
migrateInfraExtensionManifest,
latestInfraManifestFormat
options.format ?? latestInfraManifestFormat
);
break;

case "workflow":
maybeFailOnUnsupportedTargetFormat(
options.format,
Object.values(PolywrapWorkflowFormats)
);
migrateManifestFile(
parseManifestFileOption(options.manifestFile, defaultWorkflowManifest),
migrateWorkflow,
latestPolywrapWorkflowFormat
options.format ?? latestPolywrapWorkflowFormat
);
break;
}
};

function migrateManifestFile(
manifestFile: string,
migrationFn: (input: string) => string,
version: string
migrationFn: (input: string, to: string) => string,
to: string
): void {
const manifestFileName = path.basename(manifestFile);
const manifestFileDir = path.dirname(manifestFile);

console.log(
intlMsg.commands_manifest_command_m_migrateManifestMessage({
manifestFile: manifestFileName,
version: version,
version: to,
})
);

const manifestString = fs.readFileSync(manifestFile, {
encoding: "utf-8",
});

const outputManifestString = migrationFn(manifestString);
const outputManifestString = migrationFn(manifestString, to);

// Cache the old manifest file
const cache = new CacheDirectory({
Expand Down Expand Up @@ -489,7 +527,25 @@ function maybeFailOnUnsupportedManifestFormat(
console.error(
intlMsg.commands_manifest_formatError({
fileName: path.relative(".", manifestFile),
values: Object.values(PolywrapManifestFormats).join(", "),
values: formats.join(", "),
})
);
process.exit(1);
}
}

function maybeFailOnUnsupportedTargetFormat(
format: string | undefined,
formats: string[]
) {
if (!format) {
return;
}

if (!formats.includes(format)) {
console.error(
intlMsg.commands_manifest_migrate_targetFormatError({
formats: formats.join(", "),
})
);
process.exit(1);
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/lib/manifest/migrate/migrateAnyManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import YAML from "js-yaml";
export function migrateAnyManifest(
manifestString: string,
manifestTypeName: string,
migrateFn: (manifest: unknown) => unknown
migrateFn: (manifest: unknown, to: string) => unknown,
to: string
): string {
let manifest: unknown | undefined;
try {
Expand All @@ -16,7 +17,7 @@ export function migrateAnyManifest(
throw Error(`Unable to parse ${manifestTypeName}: ${manifestString}`);
}

const newManifest = migrateFn(manifest);
const newManifest = migrateFn(manifest, to);

const cleanedManifest = JSON.parse(JSON.stringify(newManifest));
delete cleanedManifest.__type;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { migrateAnyManifest } from "./migrateAnyManifest";

import {
AnyAppManifest,
latestAppManifestFormat,
migrateAppManifest,
} from "@polywrap/polywrap-manifest-types-js";
import { migrateAppManifest } from "@polywrap/polywrap-manifest-types-js";

export function migrateAppProjectManifest(manifestString: string): string {
export function migrateAppProjectManifest(
manifestString: string,
to: string
): string {
return migrateAnyManifest(
manifestString,
"AppManifest",
(manifest: AnyAppManifest) =>
migrateAppManifest(manifest, latestAppManifestFormat)
migrateAppManifest,
to
);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { migrateAnyManifest } from "./migrateAnyManifest";

import {
AnyBuildManifest,
latestBuildManifestFormat,
migrateBuildManifest,
} from "@polywrap/polywrap-manifest-types-js";
import { migrateBuildManifest } from "@polywrap/polywrap-manifest-types-js";

export function migrateBuildExtensionManifest(manifestString: string): string {
export function migrateBuildExtensionManifest(
manifestString: string,
to: string
): string {
return migrateAnyManifest(
manifestString,
"BuildManifest",
(manifest: AnyBuildManifest) =>
migrateBuildManifest(manifest, latestBuildManifestFormat)
migrateBuildManifest,
to
);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { migrateAnyManifest } from "./migrateAnyManifest";

import {
AnyDeployManifest,
latestDeployManifestFormat,
migrateDeployManifest,
} from "@polywrap/polywrap-manifest-types-js";
import { migrateDeployManifest } from "@polywrap/polywrap-manifest-types-js";

export function migrateDeployExtensionManifest(manifestString: string): string {
export function migrateDeployExtensionManifest(
manifestString: string,
to: string
): string {
return migrateAnyManifest(
manifestString,
"DeployManifest",
(manifest: AnyDeployManifest) =>
migrateDeployManifest(manifest, latestDeployManifestFormat)
migrateDeployManifest,
to
);
}
Loading

0 comments on commit a1a45c5

Please sign in to comment.