Skip to content

Commit

Permalink
Create foundry.config.json in create-app
Browse files Browse the repository at this point in the history
  • Loading branch information
tzyl committed Feb 8, 2024
1 parent c8854e4 commit b79645a
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"foundryUrl": "https://example.palantirfoundry.com",
"site": {
"application": "ri.third-party-applications.main.application.fake",
"directory": "./out",
"autoVersion": {
"type": "git-describe",
"tagPrefix": "tag-prefix"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"foundryUrl": "https://example.palantirfoundry.com",
"site": {
"application": "ri.third-party-applications.main.application.fake",
"directory": "./dist",
"autoVersion": {
"type": "git-describe",
"tagPrefix": "tag-prefix"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"foundryUrl": "https://example.palantirfoundry.com",
"site": {
"application": "ri.third-party-applications.main.application.fake",
"directory": "./dist",
"autoVersion": {
"type": "git-describe",
"tagPrefix": "tag-prefix"
}
}
}
2 changes: 2 additions & 0 deletions packages/create-app/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ for (const template of TEMPLATES) {
"https://example.palantirfoundry.com",
"--application-url",
"https://app.example.palantirfoundry.com",
"--application",
"ri.third-party-applications.main.application.fake",
"--client-id",
"123",
"--osdk-package",
Expand Down
14 changes: 14 additions & 0 deletions packages/create-app/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import {
generateEnvDevelopment,
generateEnvProduction,
} from "./generate/generateEnv.js";
import { generateFoundryConfigJson } from "./generate/generateFoundryConfigJson.js";
import { generateNpmRc } from "./generate/generateNpmRc.js";
import { green } from "./highlight.js";
import { promptApplicationRid } from "./prompts/promptApplicationRid.js";
import { promptApplicationUrl } from "./prompts/promptApplicationUrl.js";
import { promptClientId } from "./prompts/promptClientId.js";
import { promptFoundryUrl } from "./prompts/promptFoundryUrl.js";
Expand All @@ -46,6 +48,7 @@ interface CliArgs {
foundryUrl?: string;
applicationUrl?: string;
skipApplicationUrl?: boolean;
application?: string;
clientId?: string;
osdkPackage?: string;
osdkRegistryUrl?: string;
Expand Down Expand Up @@ -86,6 +89,10 @@ export async function cli(args: string[] = process.argv) {
describe:
"Skip filling in URL the production application will be hosted on",
})
.option("application", {
type: "string",
describe: "Application resource identifier (rid)",
})
.option("client-id", {
type: "string",
describe: "OAuth client ID for application",
Expand All @@ -106,6 +113,7 @@ export async function cli(args: string[] = process.argv) {
const template: Template = await promptTemplate(parsed);
const foundryUrl: string = await promptFoundryUrl(parsed);
const applicationUrl: string | undefined = await promptApplicationUrl(parsed);
const application: string = await promptApplicationRid(parsed);
const clientId: string = await promptClientId(parsed);
const osdkPackage: string = await promptOsdkPackage(parsed);
const osdkRegistryUrl: string = await promptOsdkRegistryUrl(parsed);
Expand Down Expand Up @@ -183,6 +191,12 @@ export async function cli(args: string[] = process.argv) {
clientId,
});
fs.writeFileSync(path.join(root, ".env.production"), envProduction);
const foundryConfigJson = generateFoundryConfigJson({
foundryUrl,
application,
directory: template.buildDirectory,
});
fs.writeFileSync(path.join(root, "foundry.config.json"), foundryConfigJson);

consola.success("Success");

Expand Down
42 changes: 42 additions & 0 deletions packages/create-app/src/generate/generateFoundryConfigJson.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 { expect, test } from "vitest";
import { generateFoundryConfigJson } from "./generateFoundryConfigJson.js";

const expected = `
{
"foundryUrl": "https://example.palantirfoundry.com",
"site": {
"application": "ri.third-party-applications.main.application.fake",
"directory": "./dist",
"autoVersion": {
"type": "git-describe",
"tagPrefix": "tag-prefix"
}
}
}
`.trimStart();

test("it generates foundry.config.json", () => {
expect(
generateFoundryConfigJson({
foundryUrl: "https://example.palantirfoundry.com",
application: "ri.third-party-applications.main.application.fake",
directory: "./dist",
}),
).toEqual(expected);
});
41 changes: 41 additions & 0 deletions packages/create-app/src/generate/generateFoundryConfigJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.
*/

export function generateFoundryConfigJson({
foundryUrl,
application,
directory,
}: {
foundryUrl: string;
application: string;
directory: string;
}): string {
return JSON.stringify(
{
foundryUrl,
site: {
application,
directory,
autoVersion: {
type: "git-describe",
tagPrefix: "tag-prefix",
},
},
},
null,
2,
) + "\n";
}
55 changes: 55 additions & 0 deletions packages/create-app/src/prompts/promptApplicationRid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 { afterEach, expect, test, vi } from "vitest";
import { consola } from "../consola.js";
import { promptApplicationRid } from "./promptApplicationRid.js";

vi.mock("../consola.js");

afterEach(() => {
vi.restoreAllMocks();
});

const valid = "ri.third-party-applications.main.application.fake";

test("it accepts valid application rid from prompt", async () => {
vi.mocked(consola).prompt.mockResolvedValueOnce(valid);
expect(await promptApplicationRid({})).toEqual(valid);
expect(vi.mocked(consola).prompt).toHaveBeenCalledTimes(1);
});

test("it prompts again if answered value is invalid", async () => {
vi.mocked(consola).prompt.mockResolvedValueOnce("ri.something.else.and.fake");
vi.mocked(consola).prompt.mockResolvedValueOnce(valid);
expect(await promptApplicationRid({})).toEqual(valid);
expect(vi.mocked(consola).prompt).toHaveBeenCalledTimes(2);
});

test("it accepts valid initial value without prompt", async () => {
expect(await promptApplicationRid({ application: valid })).toEqual(valid);
expect(vi.mocked(consola).prompt).not.toHaveBeenCalled();
});

test("it prompts if initial value is invalid", async () => {
vi.mocked(consola).prompt.mockResolvedValueOnce(valid);
expect(
await promptApplicationRid({ application: "ri.something.else.and.fake" }),
).toEqual(
valid,
);
expect(vi.mocked(consola).prompt).toHaveBeenCalledTimes(1);
});
44 changes: 44 additions & 0 deletions packages/create-app/src/prompts/promptApplicationRid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 { consola } from "../consola.js";
import { italic } from "../highlight.js";

export async function promptApplicationRid(
{ application }: { application?: string },
): Promise<string> {
while (
application == null
|| !/^ri\.third-party-applications\.[^.]+\.application\.[^.]+$/.test(
application,
)
) {
if (application != null) {
consola.fail(
"Please enter a valid application resource identifier (rid)",
);
}
application = await consola.prompt(
`Enter the application resource identifier (rid) for your application from Developer Console:\n${
italic(
"(Example ri.third-party-applications.main.application.1c66b352-4e00-40d2-995d-061c9d533ace)",
)
}`,
{ type: "text" },
);
}
return application;
}
4 changes: 4 additions & 0 deletions packages/create-app/src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Template {
id: string;
label: string;
envPrefix: string;
buildDirectory: string;
}

export interface TemplateContext {
Expand All @@ -30,15 +31,18 @@ export const TEMPLATES: readonly Template[] = [
id: "template-react",
label: "React",
envPrefix: "VITE_",
buildDirectory: "./dist",
},
{
id: "template-vue",
label: "Vue",
envPrefix: "VITE_",
buildDirectory: "./dist",
},
{
id: "template-next-static-export",
label: "Next (static export)",
envPrefix: "NEXT_PUBLIC_",
buildDirectory: "./out",
},
];

0 comments on commit b79645a

Please sign in to comment.