Skip to content

Commit

Permalink
proper packages generated (#918) (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericanderson authored Oct 24, 2024
1 parent 67aabfc commit a83ab34
Show file tree
Hide file tree
Showing 23 changed files with 578 additions and 91 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-garlics-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@osdk/generator-utils": patch
---

Introduces generator utils
5 changes: 5 additions & 0 deletions .changeset/quiet-windows-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@osdk/foundry-sdk-generator": patch
---

Generated packages should have proper package.json
5 changes: 5 additions & 0 deletions .changeset/three-cooks-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@osdk/foundry-sdk-generator": patch
---

Generated packages now use ^ dependencies on @osdk/api and @osdk/client
1 change: 1 addition & 0 deletions packages/create-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@osdk/create-app.template.tutorial-todo-app.beta": "workspace:*",
"@osdk/create-app.template.vue": "workspace:*",
"@osdk/create-app.template.vue.v2": "workspace:*",
"@osdk/generator-utils": "workspace:*",
"@osdk/monorepo.api-extractor": "workspace:~",
"@osdk/monorepo.tsconfig": "workspace:~",
"@osdk/monorepo.tsup": "workspace:~",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async function runTest(
// it should be, so that if the create-app code were to change to different behavior
// it would be caught.
expect(packageJson.dependencies["@osdk/client"]).toBe(
`~${createAppVersion}`,
`^${createAppVersion}`,
);
} else {
expect(packageJson.dependencies["@osdk/client"]).toBe(
Expand Down
9 changes: 7 additions & 2 deletions packages/create-app/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { changeVersionPrefix } from "@osdk/generator-utils";
import { findUpSync } from "find-up";
import Handlebars from "handlebars";
import fs from "node:fs";
Expand Down Expand Up @@ -109,19 +110,23 @@ export async function run(
cwd: fileURLToPath(import.meta.url),
});

const ourPackageJsonVersion = ourPackageJsonPath
const ourPackageJsonVersion: string | undefined = ourPackageJsonPath
? JSON.parse(fs.readFileSync(ourPackageJsonPath, "utf-8")).version
: undefined;

const clientVersion = process.env.PACKAGE_CLIENT_VERSION
?? ourPackageJsonVersion;

if (clientVersion === undefined) {
throw new Error("Could not determine current @osdk/client version");
}

const templateContext: TemplateContext = {
project,
foundryUrl,
osdkPackage,
corsProxy,
clientVersion: `~${clientVersion}`,
clientVersion: changeVersionPrefix(clientVersion, "^"),
};
const processFiles = function(dir: string) {
fs.readdirSync(dir).forEach(function(file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe("Generate Package Command", () => {
expect(scriptsExport).toMatchInlineSnapshot(`
{
"default": "./dist/bundle/index.esm.js",
"types": "./dist/bundle/index.d.ts",
}
`);

Expand Down
1 change: 1 addition & 0 deletions packages/foundry-sdk-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"devDependencies": {
"@osdk/client.unstable": "workspace:~",
"@osdk/client.unstable.tpsa": "workspace:~",
"@osdk/generator-utils": "workspace:*",
"@osdk/monorepo.api-extractor": "workspace:~",
"@osdk/monorepo.tsconfig": "workspace:~",
"@osdk/monorepo.tsup": "workspace:~",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* limitations under the License.
*/

import type { MinimalFs, WireOntologyDefinition } from "@osdk/generator";
import type { MinimalFs } from "@osdk/generator";
import { generateClientSdkVersionTwoPointZero } from "@osdk/generator";
import { mkdir, readdir, readFile, writeFile } from "fs/promises";
import { resolveDependenciesFromFindUp } from "@osdk/generator-utils";
import { mkdir, readdir, writeFile } from "fs/promises";
import { fileURLToPath } from "node:url";
import { dirname, isAbsolute, join, normalize } from "path";
import type { OntologyInfo } from "../../ontologyMetadata/ontologyMetadataResolver.js";
Expand All @@ -26,18 +27,11 @@ import { bundleDependencies } from "./bundleDependencies.js";
import { compileInMemory } from "./compileInMemory.js";
import { generatePackageJson } from "./generatePackageJson.js";

declare const __OSDK_API_VERSION__: string | undefined;
declare const __OSDK_CLIENT_VERSION__: string | undefined;

const betaDependencies: { [key: string]: string | undefined } = {
"@osdk/api": typeof __OSDK_API_VERSION__ !== "undefined"
? __OSDK_API_VERSION__
: undefined,
"@osdk/api": undefined,
};
const betaPeerDependencies: { [key: string]: string | undefined } = {
"@osdk/client": typeof __OSDK_CLIENT_VERSION__ !== "undefined"
? __OSDK_CLIENT_VERSION__
: undefined,
"@osdk/client": undefined,
};

export async function generatePackage(
Expand All @@ -52,9 +46,13 @@ export async function generatePackage(
const { consola } = await import("consola");

const packagePath = join(options.outputDir, options.packageName);
const resolvedDependencies = await resolveDependencies(betaDependencies);
const resolvedPeerDependencies = await resolveDependencies(
const resolvedDependencies = await resolveDependenciesFromFindUp(
betaDependencies,
dirname(fileURLToPath(import.meta.url)),
);
const resolvedPeerDependencies = await resolveDependenciesFromFindUp(
betaPeerDependencies,
dirname(fileURLToPath(import.meta.url)),
);

await mkdir(packagePath, { recursive: true });
Expand Down Expand Up @@ -142,38 +140,3 @@ export async function generatePackage(
consola.error(e);
}
}

async function getDependencyVersion(dependency: string): Promise<string> {
const { findUp } = await import("find-up");
const result = await findUp("package.json", {
cwd: dirname(fileURLToPath(import.meta.url)),
});
const packageJson = await readFile(result!, {
encoding: "utf-8",
});
if (!packageJson) {
throw new Error(
`Could not find package.json in current working directory: ${process.cwd()}`,
);
}
const parsedPackageJson = JSON.parse(packageJson);
return parsedPackageJson.dependencies[dependency];
}

async function resolveDependencies(
deps: { [key: string]: string | undefined },
): Promise<{
dependencyName: string;
dependencyVersion: string;
}[]> {
return await Promise.all(
Object.keys(deps).map(
async dependency => {
return {
dependencyName: dependency,
dependencyVersion: await getDependencyVersion(dependency),
};
},
),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,24 @@ export async function generatePackageJson(options: {
const packagePeerDeps = constructDependencies(options.peerDependencies);

// Note that any "default" conditions _must_ be last in their block otherwise it will crash at runtime
const packageJson = options.beta
? {
name: options.packageName,
version: options.packageVersion,
main: "./index.js",
types: "./index.d.ts",
exports: {
".": {
types: "./index.d.ts",
script: {
default: "./dist/bundle/index.esm.js",
},
default: "./index.js",
const packageJson = {
name: options.packageName,
version: options.packageVersion,
main: "./index.js",
types: "./index.d.ts",
exports: {
".": {
types: "./index.d.ts",
script: {
types: "./dist/bundle/index.d.ts",
default: "./dist/bundle/index.esm.js",
},
default: "./index.js",
},
dependencies: packageDeps,
peerDependencies: packagePeerDeps,
}
: {
name: options.packageName,
version: options.packageVersion,
main: "./index.js",
types: "./index.d.ts",
exports: {
".": {
types: "./index.d.ts",
script: {
types: "./dist/bundle/index.d.ts",
default: "./dist/bundle/index.esm.js",
},
default: "./index.js",
},
"./ontology/objects": {
types: "./ontology/objects/index.d.ts",
default: "./ontology/objects/index.js",
},
},
dependencies: packageDeps,
peerDependencies: packagePeerDeps,
};
},
dependencies: packageDeps,
peerDependencies: packagePeerDeps,
};

await writeFile(
join(options.packagePath, "package.json"),
Expand Down
5 changes: 3 additions & 2 deletions packages/foundry-sdk-generator/src/generate/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Palantir Technologies, Inc. All rights reserved.
* 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.
Expand All @@ -14,4 +14,5 @@
* limitations under the License.
*/

export * from "./GeneratePackageCommand.js";
export { GeneratePackageCommand } from "./GeneratePackageCommand.js";
export type { generatePackageCommandArgs } from "./GeneratePackageCommand.js";
55 changes: 55 additions & 0 deletions packages/generator-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "@osdk/generator-utils",
"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"
},
"./*": {
"browser": "./build/browser/public/*.js",
"import": "./build/esm/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)",
"transpile": "monorepo.tool.transpile"
},
"dependencies": {
"find-up": "^7.0.0",
"tiny-invariant": "^1.3.3"
},
"devDependencies": {
"@osdk/monorepo.api-extractor": "workspace:~",
"@osdk/monorepo.tsconfig": "workspace:~",
"@osdk/monorepo.tsup": "workspace:~",
"@types/node": "^18.0.0",
"@types/tmp": "^0.2.6",
"tmp": "^0.2.3",
"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"
}
45 changes: 45 additions & 0 deletions packages/generator-utils/src/changeVersionPrefix.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.
*/

import { describe, expect, it } from "vitest";
import { changeVersionPrefix } from "./changeVersionPrefix.js";

describe(changeVersionPrefix, () => {
const expectedPrefix = "^";
it.each(["1.2.3", "~1.2.3", "^1.2.3"])(
`replaces "%s" with "${expectedPrefix}1.2.3"`,
async (version) => {
const result = await changeVersionPrefix(version, expectedPrefix);
expect(result).toEqual(`${expectedPrefix}1.2.3`);
},
);

it.each(["workspace:1.2.3", "workspace:~1.2.3", "workspace:^1.2.3"])(
`replaces "%s" with "${expectedPrefix}1.2.3"`,
async (version) => {
const result = await changeVersionPrefix(version, expectedPrefix);
expect(result).toEqual(`workspace:${expectedPrefix}1.2.3`);
},
);

it.each(["workspace:*", "workspace:^", "workspace:~"])(
`replaces "%s" with "workspace:${expectedPrefix}"`,
async (version) => {
const result = await changeVersionPrefix(version, expectedPrefix);
expect(result).toEqual(`workspace:${expectedPrefix}`);
},
);
});
30 changes: 30 additions & 0 deletions packages/generator-utils/src/changeVersionPrefix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 function changeVersionPrefix(
version: string,
prefix: "^" | "~" | "",
): string {
const isWorkspaceProtocol = version.startsWith("workspace:");
if (isWorkspaceProtocol) {
version = version.slice("workspace:".length);
}
if (version[0] === "^" || version[0] === "~" || version[0] === "*") {
version = version.slice(1);
}

return `${isWorkspaceProtocol ? "workspace:" : ""}${prefix}${version}`;
}
Loading

0 comments on commit a83ab34

Please sign in to comment.