Skip to content

Commit

Permalink
no more need for file resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
acunniffe committed Mar 19, 2022
1 parent 04c405f commit 817b03e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 25 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@useoptic/openapi-utilities": "0.23.4",
"chai": "^4.3.4",
"change-case": "^4.1.2",
"find-parent-dir": "^0.3.1",
"fs-extra": "^10.0.0",
"lodash.isequal": "^4.5.0",
"nice-try": "^3.0.0",
Expand Down
46 changes: 24 additions & 22 deletions src/workflows/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,32 @@ import { addGetOperation } from "./templates/operations/get";
import { addUpdateOperation } from "./templates/operations/update";
import { addDeleteOperation } from "./templates/operations/delete";
import { buildNewResourceSpec } from "./templates/new-resource-spec";
import {
formatResourceVersion,
resolveResourcesDirectory,
resolveResourceVersion,
} from "./file-resolvers";

export async function createResourceAction(resourceName, pluralResourceName) {
// TODO: the SDK should probably help with the generation of new files
// and allow ergonomic use of a SpecTemplate to do so
const titleResourceName = titleCase(resourceName);
const version = getResourceVersion();
const collectionPath = `/${pluralResourceName}`;
if (!fs.existsSync(path.join(".", "resources")))
throw new Error(
"Resource directory does not exist. Are you sure you're in the right directory?",
);
await fs.mkdirSync(path.join(".", "resources", pluralResourceName, version), {
recursive: true,
});
const version = formatResourceVersion();

const resourcesDirectory = await resolveResourcesDirectory();
const lowerCaseResourceName = pluralResourceName.toLowerCase();

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

const spec = buildNewResourceSpec(titleResourceName);
const specYaml = writeYaml(spec);
fs.writeFileSync(
path.join(".", "resources", pluralResourceName, version, "spec.yaml"),
path.join(resourcesDirectory, lowerCaseResourceName, version, "spec.yaml"),
specYaml,
);
}
Expand All @@ -42,11 +50,16 @@ export const addUpdateOperationAction =
function buildOperationAction(template) {
// TODO: consider how workflows can provided with more sophisticated context
return async (
specFilePath: string,
resourceName: string,
pluralResourceName: string,
resourceVersion: string,
) => {
const titleResourceName = titleCase(resourceName);
const specFilePath = await resolveResourceVersion(
process.cwd(),
pluralResourceName,
resourceVersion,
);
// TODO: consider how this impacts performance (round trip to the FS for each call)
// and whether that's something we need to address here
await applyTemplate(template, specFilePath, {
Expand All @@ -59,17 +72,6 @@ function buildOperationAction(template) {

//-----

function getResourceVersion(): string {
const today = new Date();
return `${today.getFullYear()}-${padWithZero(today.getMonth())}-${padWithZero(
today.getUTCDay(),
)}`;
}

function padWithZero(value: number): string {
return ("00" + value).slice(-2);
}

function titleCase(value: string): string {
return value[0].toUpperCase() + value.slice(1);
}
6 changes: 3 additions & 3 deletions src/workflows/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export const addUpdateOperationCommand = buildOperationCommand(

function buildOperationCommand(name: string, description: string, action) {
const command = new Command(name)
.addArgument(new Argument("<openapi>", "path to openapi file"))
.addArgument(new Argument("<resource-name>", "[resource-name]"))
.addArgument(
new Argument("<plural-resource-name>", "[plural-resource-name]"),
)
.action(async (specFilePath, resourceName, pluralResourceName) => {
return action(specFilePath, resourceName, pluralResourceName);
.addArgument(new Argument("[resource-version]", "version"))
.action(async (resourceName, pluralResourceName, resourceVersion) => {
return action(resourceName, pluralResourceName, resourceVersion);
});

command.description(description);
Expand Down
66 changes: 66 additions & 0 deletions src/workflows/file-resolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import findParentDir from "find-parent-dir";
import fs from "fs-extra";
import path from "path";

export async function resolveResourcesDirectory(
workingDirectory: string = process.cwd(),
): Promise<string> {
return new Promise((resolve) => {
findParentDir(workingDirectory, "resources", function (err, dir) {
if (err)
throw new Error(
"A Vervet resources directory does not exist here. Is your working directory correct?",
);
resolve(path.join(dir, "resources"));
});
});
}

export async function resolveResourceVersion(
workingDirectory: string = process.cwd(),
resourceName: string,
resourceVersion: string = "latest",
): Promise<string> {
const resources = await resolveResourcesDirectory();
const resourceNameLowerCase = resourceName.toLowerCase();

const resourceNames = await fs.readdir(resources);

if (!resourceNames.includes(resourceNameLowerCase))
throw new Error(
`No resource ${resourceNameLowerCase} found in directory ${resources}`,
);

const resourceDir = path.join(resources, resourceNameLowerCase);
const versions = await fs.readdir(resourceDir);

const matchingDate =
resourceVersion !== "latest"
? versions.find((date) => date === resourceVersion)
: latestDateOfSet(versions);

if (!matchingDate)
throw new Error(
`No resource version ${resourceVersion} found for ${resourceNameLowerCase}`,
);

return path.join(resources, resourceNameLowerCase, matchingDate, "spec.yaml");
}

function latestDateOfSet(dates: string[]): string | undefined {
return dates.sort().pop();
}

function isDate(dateStr) {
return !isNaN(new Date(dateStr).getDate());
}

export function formatResourceVersion(date: Date = new Date()): string {
return `${date.getFullYear()}-${padWithZero(date.getMonth())}-${padWithZero(
date.getUTCDay(),
)}`;
}

function padWithZero(value: number): string {
return ("00" + value).slice(-2);
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3514,6 +3514,11 @@ fill-range@^7.0.1:
dependencies:
to-regex-range "^5.0.1"

find-parent-dir@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/find-parent-dir/-/find-parent-dir-0.3.1.tgz#c5c385b96858c3351f95d446cab866cbf9f11125"
integrity sha512-o4UcykWV/XN9wm+jMEtWLPlV8RXCZnMhQI6F6OdHeSez7iiJWePw8ijOlskJZMsaQoGR/b7dH6lO02HhaTN7+A==

find-up@^4.0.0, find-up@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-4.1.0.tgz#97afe7d6cdc0bc5928584b7c8d7b16e8a9aa5d19"
Expand Down

0 comments on commit 817b03e

Please sign in to comment.