Skip to content

Commit

Permalink
Split foundry-config-json package
Browse files Browse the repository at this point in the history
  • Loading branch information
tzyl committed Dec 12, 2024
1 parent a39ef6a commit 780ad87
Show file tree
Hide file tree
Showing 21 changed files with 557 additions and 252 deletions.
7 changes: 7 additions & 0 deletions .changeset/friendly-zoos-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@osdk/foundry-config-json": patch
"@osdk/cli.common": patch
"@osdk/cli": patch
---

Split out @osdk/foundry-config-json package
1 change: 1 addition & 0 deletions .monorepolint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const privatePackages = [
"@osdk/e2e.*",
"@osdk/example-generator",
"@osdk/examples.*",
"@osdk/foundry-config-json",
"@osdk/monorepo.*",
"@osdk/platform-sdk-generator",
"@osdk/shared.test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import { isValidSemver } from "@osdk/cli.common";
import { describe, expect, it } from "vitest";
import { isValidSemver } from "./isValidSemver.js";

describe("isValidSemver", () => {
it("should return true for a valid SemVer version", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"ajv": "^8.13.0",
"archiver": "^7.0.1",
"consola": "^3.2.3",
"find-up": "^7.0.0",
"open": "^10.1.0",
"pretty-bytes": "^6.1.1",
"semver": "^7.6.3",
Expand All @@ -45,6 +44,7 @@
"devDependencies": {
"@osdk/cli.cmd.typescript": "workspace:~",
"@osdk/cli.common": "workspace:~",
"@osdk/foundry-config-json": "workspace:~",
"@osdk/monorepo.api-extractor": "workspace:~",
"@osdk/monorepo.tsconfig": "workspace:~",
"@osdk/monorepo.tsup": "workspace:~",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/site/deploy/SiteDeployArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { AutoVersionConfigType } from "../../../util/config.js";
import type { AutoVersionConfigType } from "@osdk/foundry-config-json";
import type { CommonSiteArgs } from "../CommonSiteArgs.js";

export interface SiteDeployArgs extends CommonSiteArgs {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/site/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/

import { isValidSemver, YargsCheckError } from "@osdk/cli.common";
import type { CommandModule } from "yargs";
import type {
AutoVersionConfigType,
LoadedFoundryConfig,
SiteConfig,
} from "../../../util/config.js";
} from "@osdk/foundry-config-json";
import type { CommandModule } from "yargs";
import configLoader from "../../../util/configLoader.js";
import type { CommonSiteArgs } from "../CommonSiteArgs.js";
import { logDeployCommandConfigFileOverride } from "./logDeployCommandConfigFileOverride.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

import type { SiteConfig } from "@osdk/foundry-config-json";
import { consola } from "consola";
import type { Arguments } from "yargs";
import type { SiteConfig } from "../../../util/config.js";
import type { SiteDeployArgs } from "./SiteDeployArgs.js";

export async function logDeployCommandConfigFileOverride(
Expand Down
16 changes: 14 additions & 2 deletions packages/cli/src/commands/site/deploy/siteDeployCommand.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { consola } from "consola";

import { createInternalClientContext, thirdPartyApplications } from "#net";
import { ExitProcessError } from "@osdk/cli.common";
import type { AutoVersionConfig } from "@osdk/foundry-config-json";
import { autoVersion, AutoVersionError } from "@osdk/foundry-config-json";
import archiver from "archiver";
import { colorize } from "consola/utils";
import * as fs from "node:fs";
Expand All @@ -26,8 +28,6 @@ import { Readable } from "node:stream";
import prettyBytes from "pretty-bytes";
import type { InternalClientContext } from "../../../net/internalClientContext.mjs";
import type { ThirdPartyAppRid } from "../../../net/ThirdPartyAppRid.js";
import { autoVersion as findAutoVersion } from "../../../util/autoVersion.js";
import type { AutoVersionConfig } from "../../../util/config.js";
import { loadToken } from "../../../util/token.js";
import type { SiteDeployArgs } from "./SiteDeployArgs.js";

Expand Down Expand Up @@ -126,6 +126,18 @@ export default async function siteDeployCommand(
}
}

async function findAutoVersion(config: AutoVersionConfig): Promise<string> {
try {
return await autoVersion(config);
} catch (e) {
throw new ExitProcessError(
2,
e instanceof Error ? e.message : undefined,
e instanceof AutoVersionError ? e.tip : undefined,
);
}
}

async function uploadSnapshot(
clientCtx: InternalClientContext,
application: ThirdPartyAppRid,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/site/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import type { CliCommonArgs } from "@osdk/cli.common";
import { YargsCheckError } from "@osdk/cli.common";
import type { LoadedFoundryConfig } from "@osdk/foundry-config-json";
import type { CommandModule } from "yargs";
import type { ThirdPartyAppRid } from "../../net/ThirdPartyAppRid.js";
import type { LoadedFoundryConfig } from "../../util/config.js";
import configLoader from "../../util/configLoader.js";
import type { CommonSiteArgs } from "./CommonSiteArgs.js";
import deploy from "./deploy/index.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

import type { FoundryConfig } from "@osdk/foundry-config-json";
import { consola } from "consola";
import type { Arguments } from "yargs";
import type { FoundryConfig } from "../../util/config.js";
import type { CommonSiteArgs } from "./CommonSiteArgs.js";

export async function logSiteCommandConfigFileOverride(
Expand Down
11 changes: 8 additions & 3 deletions packages/cli/src/util/configLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@
* limitations under the License.
*/

import type { LoadedFoundryConfig } from "./config.js";
import { loadFoundryConfig } from "./config.js";
import { ExitProcessError } from "@osdk/cli.common";
import type { LoadedFoundryConfig } from "@osdk/foundry-config-json";
import { loadFoundryConfig } from "@osdk/foundry-config-json";

let configPromise: Promise<LoadedFoundryConfig | undefined> | undefined =
undefined;

function getConfig(): Promise<LoadedFoundryConfig | undefined> {
if (configPromise == null) {
configPromise = loadFoundryConfig();
try {
configPromise = loadFoundryConfig();
} catch (e) {
throw new ExitProcessError(2, e instanceof Error ? e.message : undefined);
}
}
return configPromise;
}
Expand Down
60 changes: 60 additions & 0 deletions packages/foundry-config-json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "@osdk/foundry-config-json",
"private": true,
"version": "0.0.0",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/palantir/osdk-ts.git"
},
"exports": {
".": {
"browser": "./build/browser/index.js",
"import": "./build/esm/index.js",
"default": "./build/browser/index.js"
},
"./*": {
"browser": "./build/browser/public/*.js",
"import": "./build/esm/public/*.js",
"default": "./build/browser/public/*.js"
}
},
"scripts": {
"check-attw": "monorepo.tool.attw esm",
"check-spelling": "cspell --quiet .",
"clean": "rm -rf lib dist types build tsconfig.tsbuildinfo",
"fix-lint": "eslint . --fix && dprint fmt --config $(find-up dprint.json)",
"lint": "eslint . && dprint check --config $(find-up dprint.json)",
"test": "vitest run --pool=forks",
"test:watch": "vitest",
"transpile": "monorepo.tool.transpile"
},
"dependencies": {
"ajv": "^8.13.0",
"find-up": "^7.0.0",
"semver": "^7.6.3"
},
"devDependencies": {
"@osdk/monorepo.api-extractor": "workspace:~",
"@osdk/monorepo.tsconfig": "workspace:~",
"@osdk/monorepo.tsup": "workspace:~",
"@types/node": "^18.0.0",
"@types/semver": "^7.5.8",
"typescript": "~5.5.4"
},
"publishConfig": {
"access": "public"
},
"files": [
"build/cjs",
"build/esm",
"build/browser",
"CHANGELOG.md",
"package.json",
"templates",
"*.d.ts"
],
"module": "./build/esm/index.js",
"types": "./build/esm/index.d.ts",
"type": "module"
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@
* limitations under the License.
*/

import { ExitProcessError, isValidSemver } from "@osdk/cli.common";
import { findUp } from "find-up";
import { exec } from "node:child_process";
import { promises as fsPromises } from "node:fs";
import { promisify } from "node:util";
import { valid } from "semver";
import type { AutoVersionConfig } from "./config.js";

export class AutoVersionError extends Error {
constructor(
public readonly msg: string,
public readonly tip?: string,
) {
super(msg);
}
}

/**
* Gets the version string using git describe. If the @param tagPrefix is empty, git describe will return the
* latest tag (without any filtering) and if the tag starts with "v", it will be removed.
Expand Down Expand Up @@ -56,8 +65,7 @@ async function gitDescribeAutoVersion(tagPrefix: string = ""): Promise<string> {
async function packageJsonAutoVersion(): Promise<string> {
const packageJsonPath = await findUp("package.json");
if (!packageJsonPath) {
throw new ExitProcessError(
2,
throw new AutoVersionError(
`Couldn't find package.json file in the current working directory or its parents: ${process.cwd()}`,
);
}
Expand All @@ -67,8 +75,7 @@ async function packageJsonAutoVersion(): Promise<string> {
const fileContent = await fsPromises.readFile(packageJsonPath, "utf-8");
packageJson = JSON.parse(fileContent);
} catch (error) {
throw new ExitProcessError(
2,
throw new AutoVersionError(
`Couldn't read or parse package.json file ${packageJsonPath}. Error: ${error}`,
);
}
Expand Down Expand Up @@ -98,8 +105,7 @@ async function gitDescribe(matchPrefix: string | undefined): Promise<string> {
|| errorMessage.includes("command not found")
|| errorMessage.includes("no such file or directory")
) {
throw new ExitProcessError(
2,
throw new AutoVersionError(
"Unable to determine auto version using git-describe as git is not installed or found in the PATH.",
`You can set up git and try again or supply a --version option to set the version manually`,
);
Expand All @@ -108,8 +114,7 @@ async function gitDescribe(matchPrefix: string | undefined): Promise<string> {
if (
errorMessage.includes("fatal: not a git repository")
) {
throw new ExitProcessError(
2,
throw new AutoVersionError(
`Unable to determine auto version using git-describe as the current directory is not a git repository.`,
`You can run the command in a git repository and try again or supply a --version option to set the version manually`,
);
Expand All @@ -120,16 +125,14 @@ async function gitDescribe(matchPrefix: string | undefined): Promise<string> {
"fatal: no names found, cannot describe anything.",
)
) {
throw new ExitProcessError(
2,
throw new AutoVersionError(
`Unable to determine auto version using git-describe as no matching tags were found.`,
`You can create a tag matching the configured tag prefix and try again or supply a --version option to set the version manually`,
);
}
}

throw new ExitProcessError(
2,
throw new AutoVersionError(
`Unable to determine auto version using git-describe: ${error}.`,
`You can supply a --version option to set the version manually`,
);
Expand All @@ -139,9 +142,8 @@ async function gitDescribe(matchPrefix: string | undefined): Promise<string> {
}

function validateVersion(version: string): void {
if (!isValidSemver(version)) {
throw new ExitProcessError(
2,
if (valid(version) == null) {
throw new AutoVersionError(
`The version string ${version} is not SemVer compliant.`,
);
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import { ExitProcessError } from "@osdk/cli.common";
import type { JSONSchemaType } from "ajv";
import { promises as fsPromises } from "node:fs";
import { extname } from "node:path";
Expand Down Expand Up @@ -109,15 +108,13 @@ export async function loadFoundryConfig(): Promise<
const fileContent = await fsPromises.readFile(configFilePath, "utf-8");
foundryConfig = parseConfigFile(fileContent, configFilePath);
} catch (error) {
throw new ExitProcessError(
2,
throw new Error(
`Couldn't read or parse config file ${configFilePath}. Error: ${error}`,
);
}

if (!validate(foundryConfig)) {
throw new ExitProcessError(
2,
throw new Error(
`The configuration file does not match the expected schema: ${
ajv.errorsText(validate.errors)
}`,
Expand All @@ -139,8 +136,7 @@ function parseConfigFile(
case ".json":
return JSON.parse(fileContent);
default:
throw new ExitProcessError(
2,
throw new Error(
`Unsupported file extension: ${extension} for config file.`,
);
}
Expand Down
27 changes: 27 additions & 0 deletions packages/foundry-config-json/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export { autoVersion, AutoVersionError } from "./autoVersion.js";
export { loadFoundryConfig } from "./config.js";
export type {
AutoVersionConfig,
AutoVersionConfigType,
FoundryConfig,
GitDescribeAutoVersionConfig,
LoadedFoundryConfig,
PackageJsonAutoVersionConfig,
SiteConfig,
} from "./config.js";
11 changes: 11 additions & 0 deletions packages/foundry-config-json/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@osdk/monorepo.tsconfig/base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build/esm"
},
"include": [
"./src/**/*"
],
"references": []
}
Loading

0 comments on commit 780ad87

Please sign in to comment.