Skip to content

Commit

Permalink
Restructure create app to make it easier to upgrade deps (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericanderson authored Jul 18, 2024
1 parent f5890f2 commit 280e70c
Show file tree
Hide file tree
Showing 189 changed files with 1,500 additions and 147 deletions.
2 changes: 2 additions & 0 deletions .changeset/lazy-tools-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ tsconfig.tsbuildinfo
packages/client/src/generatedNoCheck
packages/client.test.ontology/src/generatedNoCheck
packages/legacy-client/src/generatedNoCheck
packages/create-app.template.*/src/generatedNoCheck

.log
pnpm-publish-summary.json
Expand Down
44 changes: 44 additions & 0 deletions .monorepolint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const nonStandardPackages = [
"@osdk/e2e.sandbox.*", // sandboxes for manual e2e testing
"@osdk/e2e.generated.*", // generated sdks for e2e testing
"@osdk/shared.client",
"@osdk/create-app.template.*",
"@osdk/tests.*",
];

Expand Down Expand Up @@ -72,6 +73,7 @@ const esmOnlyPackages = [
"@osdk/tool.release",
"@osdk/version-updater",
"@osdk/client.test.ontology",
"@osdk/create-app.template-packager",
// "@osdk/examples.*", but they have their own config cause they are nonstandard
];

Expand Down Expand Up @@ -392,6 +394,48 @@ function standardPackageRules(shared, options) {
*/
export default {
rules: [
fileContents({
includePackages: ["@osdk/create-app.template.*"],
options: {
file: "README.md",
generator: (context) => {
return `# ${context.getPackageJson().name}
This package contains templates for \`@osdk/create-app\`.
The dependencies will come from this package's \`package.json\` (excluding \`@osdk/create-app.template-packager\`) and the rest of template is filled out from the \`templates\` directory.
NOTE: DO NOT EDIT THIS README BY HAND. It is generated by monorepolint.
`;
},
},
}),
fileContents({
includePackages: ["@osdk/create-app.template.*"],
options: {
file: "turbo.json",
template: `{
// WARNING: GENERATED FILE. DO NOT EDIT DIRECTLY. See .monorepolint.config.mjs
"extends": ["//"],
"tasks": {
"codegen": {
"inputs": ["templates/**/*"],
"outputs": ["src/generatedNoCheck/**/*"],
"dependsOn": ["@osdk/create-app.template-packager#transpile"]
}
}
}
`,
},
}),
...standardPackageRules({
includePackages: ["@osdk/create-app.template.*"],
}, {
legacy: false,
packageDepth: 2,
type: "example",
esmOnly: true,
}),
...standardPackageRules({
excludePackages: [
...nonStandardPackages,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node
// @ts-check

import { cli } from "../build/esm/index.js";
cli();
62 changes: 62 additions & 0 deletions packages/create-app.template-packager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "@osdk/create-app.template-packager",
"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": "../../scripts/build_common/check-attw.sh esm",
"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": "find . \\( -path build/cjs -or -path build/esm -or -path build/browser \\) -type f \\( -name '*.js' -or -name '*.js.map' -or -name '*.cjs' -or -name '*.cjs.map' \\) -delete && tsup",
"typecheck": "find . \\( -path build/cjs -or -path build/esm -or -path build/browser \\) -type f \\( -name '*.ts' -or -name '*.ts.map' -or -name '*.cts' -or -name '*.cts.map' \\) -delete && ../../scripts/build_common/typecheck.sh esm"
},
"dependencies": {
"consola": "^3.2.3",
"find-up": "^7.0.0",
"serialize-javascript": "^6.0.2"
},
"devDependencies": {
"@osdk/monorepo.api-extractor": "workspace:~",
"@osdk/monorepo.tsconfig": "workspace:~",
"@osdk/monorepo.tsup": "workspace:~",
"@types/node": "^18.0.0",
"@types/serialize-javascript": "^5.0.4",
"@types/tmp": "^0.2.6",
"@types/yargs": "^17.0.29",
"tmp": "^0.2.3",
"typescript": "^5.5.2"
},
"publishConfig": {
"access": "public"
},
"bin": {
"create-app.template-packager": "bin/create-app.template-packager.mjs"
},
"files": [
"build/cjs",
"build/esm",
"build/browser",
"CHANGELOG.md",
"package.json",
"templates",
"*.d.ts"
],
"main": "./build/cjs/index.cjs",
"module": "./build/esm/index.js",
"types": "./build/esm/index.d.ts",
"type": "module"
}
110 changes: 110 additions & 0 deletions packages/create-app.template-packager/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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 { findUp } from "find-up";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import serialize from "serialize-javascript";

export async function cli() {
const extsToString = new Set([".html", ".cjs", ".hbs", ".gitignore", ".tsx"]);

const templatesDir = await findUp("templates", { type: "directory" });
if (!templatesDir) throw new Error("template dir is missing");

const sourcePackageJsonPath = await findUp("package.json");
if (!sourcePackageJsonPath) throw new Error("package.json is missing");

const sourcePackageJson = JSON.parse(
await fs.readFile(sourcePackageJsonPath, "utf-8"),
);

let result =
"export const files = new Map<string, {type: 'base64', body: string} | {type: 'raw', body: string}>([\n";

const processFiles = async function(dir: string, baseDir: string) {
for (const filename of await fs.readdir(dir)) {
const file = dir + "/" + filename;

const stat = await fs.stat(file);
if (stat.isDirectory()) {
await processFiles(file, baseDir);
continue;
}

let destPath = path.relative(baseDir, file);

if (path.basename(destPath) === "_gitignore") {
destPath = path.join(path.dirname(destPath), ".gitignore");
}

const body = await fs.readFile(file);
let output: string;

if (destPath === "package.json.hbs") {
const packageJson = JSON.parse(body.toString("utf-8"));

for (
const d of ["dependencies", "devDependencies", "peerDependencies"]
) {
if (sourcePackageJson[d]) {
if (!packageJson[d]) {
packageJson[d] = {};
}
Object.assign(packageJson[d], sourcePackageJson[d]);
delete packageJson[d]["@osdk/create-app.template-packager"];
for (const key of Object.keys(packageJson[d])) {
if (key.startsWith("@osdk/monorepo.")) {
delete packageJson[d][key];
}
}
}
}

output = safeRaw(JSON.stringify(packageJson, undefined, 2));
} else if (
extsToString.has(path.extname(destPath))
|| path.basename(destPath) === ".gitignore"
) {
output = safeRaw(body.toString("utf-8"));
} else {
output = serialize({
type: "base64",
body: body.toString("base64"),
}, { space: 2 });
}

result += `["${destPath}", ${output}],\n`;
}
};
await processFiles(templatesDir, templatesDir);
result += `]);`;

await fs.mkdir("src/generatedNoCheck", { recursive: true });

await fs.writeFile("src/generatedNoCheck/index.ts", result, {
encoding: "utf-8",
});
}

function safeRaw(q: string): string {
return `{ type: "raw", body: \`${
q
.replace(/\\/g, "\\\\")
.replace(/`/g, "\\`")
.replace(/\$/g, "\\$")
}\`}`;
}
14 changes: 14 additions & 0 deletions packages/create-app.template-packager/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "Node",
"target": "ES6",
"rootDir": "src",
"outDir": "build/cjs"
},
"include": [
"./src/**/*"
],
"references": []
}
11 changes: 11 additions & 0 deletions packages/create-app.template-packager/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": []
}
23 changes: 23 additions & 0 deletions packages/create-app.template-packager/tsup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2023 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 { defineConfig } from "tsup";

export default defineConfig(async (options) =>
(await import("@osdk/monorepo.tsup")).default(options, {
esmOnly: true,
})
);
7 changes: 7 additions & 0 deletions packages/create-app.template.next-static-export/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @osdk/create-app.template.next-static-export

This package contains templates for `@osdk/create-app`.

The dependencies will come from this package's `package.json` (excluding `@osdk/create-app.template-packager`) and the rest of template is filled out from the `templates` directory.

NOTE: DO NOT EDIT THIS README BY HAND. It is generated by monorepolint.
60 changes: 60 additions & 0 deletions packages/create-app.template.next-static-export/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "@osdk/create-app.template.next-static-export",
"version": "0.17.0-beta.2",
"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": "../../scripts/build_common/check-attw.sh esm",
"clean": "rm -rf lib dist types build tsconfig.tsbuildinfo",
"codegen": "create-app.template-packager",
"fix-lint": "eslint . --fix && dprint fmt --config $(find-up dprint.json)",
"lint": "eslint . && dprint check --config $(find-up dprint.json)",
"transpile": "find . \\( -path build/cjs -or -path build/esm -or -path build/browser \\) -type f \\( -name '*.js' -or -name '*.js.map' -or -name '*.cjs' -or -name '*.cjs.map' \\) -delete && tsup",
"typecheck": "find . \\( -path build/cjs -or -path build/esm -or -path build/browser \\) -type f \\( -name '*.ts' -or -name '*.ts.map' -or -name '*.cts' -or -name '*.cts.map' \\) -delete && ../../scripts/build_common/typecheck.sh esm"
},
"dependencies": {
"next": "14.2.3",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@osdk/create-app.template-packager": "workspace:~",
"@osdk/monorepo.api-extractor": "workspace:~",
"@osdk/monorepo.tsconfig": "workspace:~",
"@osdk/monorepo.tsup": "workspace:~",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8.57.0",
"eslint-config-next": "^14.2.3",
"typescript": "^5"
},
"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"
}
17 changes: 17 additions & 0 deletions packages/create-app.template.next-static-export/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 { files } from "./generatedNoCheck/index.js";
Loading

0 comments on commit 280e70c

Please sign in to comment.