Skip to content

Commit

Permalink
feat: added version command
Browse files Browse the repository at this point in the history
  • Loading branch information
acunniffe committed Mar 20, 2022
1 parent 0ebcfda commit 0b3719d
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
addDeleteOperationCommand,
addGetOperationCommand,
addListOperationCommand,
createVersionCommand,
} from "./workflows/commands";

const apiCheckService = newSnykApiCheckService();
Expand All @@ -27,6 +28,8 @@ const workflowCommand = new Command("workflow").description(
);
workflowCommand.addCommand(createResourceCommand());

workflowCommand.addCommand(createVersionCommand());

const operationCommand = new Command("operation").description(
"add common operations to an OpenAPI file",
);
Expand Down
85 changes: 82 additions & 3 deletions src/workflows/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
import fs from "fs-extra";
import path from "path";
import { writeYaml } from "@useoptic/openapi-io";
import { writeYaml, loadYaml } from "@useoptic/openapi-io";
import { applyTemplate } from "@useoptic/openapi-cli";
import { addCreateOperation } from "./templates/operations/create";
import { addListOperation } from "./templates/operations/list";
Expand All @@ -13,7 +13,11 @@ import {
resolveResourcesDirectory,
resolveResourceVersion,
} from "./file-resolvers";
import { LogUpdatingSpecification } from "./cli-ux";
import {
LogNewDateVersionSpecification,
LogUpdatingSpecification,
} from "./cli-ux";
import { OpenAPIV3 } from "@useoptic/openapi-utilities";

export async function createResourceAction(resourceName, pluralResourceName) {
// TODO: the SDK should probably help with the generation of new files
Expand Down Expand Up @@ -50,6 +54,81 @@ export async function createResourceAction(resourceName, pluralResourceName) {
fs.writeFileSync(initialSpecFilePath, specYaml);
}

export async function promoteVersionAction(
resourceName: string,
targetStability: string,
) {
const stabilityProgression = ["wip", "experimental", "beta", "ga"];

if (!stabilityProgression.includes(targetStability))
throw new Error(
`target stability must be one of ${JSON.stringify(stabilityProgression)}`,
);

const specFilePath = await resolveResourceVersion(
process.cwd(),
resourceName,
"latest",
true,
);

const currentDateVersion = (() => {
const components = specFilePath.split("/");
components.pop(); // spec.yaml
return components.pop(); // date string
})();

const openAPI: OpenAPIV3.Document = loadYaml(
(await fs.readFile(specFilePath)).toString(),
) as OpenAPIV3.Document;

const currentStability = openAPI["x-snyk-api-stability"];

if (
stabilityProgression.indexOf(currentStability) >
stabilityProgression.indexOf(targetStability)
)
throw new Error(
`stability can not go backwards from ${currentStability} to ${targetStability}`,
);

if (formatResourceVersion() === currentDateVersion)
throw new Error(
`can not promote stability on the same day as the latest version ${currentDateVersion}`,
);

// advance the stability, while copying everything else over
openAPI["x-snyk-api-stability"] = targetStability;

const resourcesDirectory = await resolveResourcesDirectory();

const newDateVersion = formatResourceVersion();

await fs.mkdirSync(
path.join(resourcesDirectory, resourceName, newDateVersion),
{
recursive: true,
},
);

const initialSpecFilePath = path.join(
resourcesDirectory,
resourceName,
newDateVersion,
"spec.yaml",
);

const specYaml = writeYaml(openAPI);
fs.writeFileSync(initialSpecFilePath, specYaml);

LogNewDateVersionSpecification(
resourceName,
newDateVersion,
targetStability,
initialSpecFilePath,
);
}

export const addCreateOperationAction =
buildOperationAction(addCreateOperation);
export const addDeleteOperationAction =
Expand Down
15 changes: 15 additions & 0 deletions src/workflows/cli-ux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ export function LogUpdatingSpecification(
);
}

export function LogNewDateVersionSpecification(
resource: string,
version: string,
stability: string,
filePath: string,
) {
console.log(
chalk.white(
`new version and OpenAPI description for: ${chalk.blue.bold(
resource,
)} ${chalk.green.bold(version)} ${chalk.green.bold(stability)}`,
) + `\n at ${filePath}:0:0\n`,
);
}

export function AlreadyInSpec(method: string, path: string) {
console.log(
chalk.red(
Expand Down
15 changes: 15 additions & 0 deletions src/workflows/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
addListOperationAction,
addUpdateOperationAction,
createResourceAction,
promoteVersionAction,
} from "./actions";

export function createResourceCommand() {
Expand Down Expand Up @@ -67,3 +68,17 @@ function buildOperationCommand(name: string, description: string, action) {

return command;
}

export function createVersionCommand() {
const command = new Command("version")
.addArgument(new Argument("<resource-name>", "[resource-name]"))
.addArgument(new Argument("<stability>", "stability"))

.action(async (resourceName, stability) => {
return promoteVersionAction(resourceName, stability);
});

command.description("create a new resource");

return command;
}
4 changes: 3 additions & 1 deletion src/workflows/file-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export async function resolveResourceVersion(
workingDirectory: string = process.cwd(),
resourceName: string,
resourceVersion: string = "latest",
silent: boolean = false,
): Promise<string> {
const resources = await resolveResourcesDirectory();
const resourceNameLowerCase = resourceName.toLowerCase();
Expand Down Expand Up @@ -53,7 +54,8 @@ export async function resolveResourceVersion(
"spec.yaml",
);

LogUpdatingSpecification(resourceName, resourceVersion, finalPath);
if (!silent)
LogUpdatingSpecification(resourceName, resourceVersion, finalPath);
return finalPath;
}

Expand Down

0 comments on commit 0b3719d

Please sign in to comment.