Skip to content

Commit ed18250

Browse files
authored
Fix inferring the package.json in define calls (#98)
1 parent 8e48557 commit ed18250

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { createSandboxFromDefine } from "./define";
2+
3+
it("can infer title and description", async () => {
4+
const payload = [
5+
{
6+
path: "package.json",
7+
content: {
8+
title: "test",
9+
description: "test description",
10+
dependencies: {},
11+
},
12+
},
13+
];
14+
15+
const result = await createSandboxFromDefine(payload);
16+
17+
expect(result.title).toBe("test");
18+
expect(result.description).toBe("test description");
19+
});
20+
21+
it("works with leading slashes", async () => {
22+
const payload = [
23+
{
24+
path: "/package.json",
25+
content: {
26+
title: "test",
27+
description: "test description",
28+
dependencies: {},
29+
},
30+
},
31+
];
32+
33+
const result = await createSandboxFromDefine(payload);
34+
35+
expect(result.title).toBe("test");
36+
expect(result.description).toBe("test description");
37+
});

packages/git-extractor/src/routes/define.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@ import { Context } from "koa";
22
import createSandbox from "codesandbox-import-utils/lib/create-sandbox";
33
import { INormalizedModules, IModule } from "codesandbox-import-util-types";
44

5-
export const define = async (ctx: Context, next: () => Promise<any>) => {
6-
const { files } = ctx.request.body;
7-
5+
export const createSandboxFromDefine = async (files: any) => {
86
const normalizedFiles: INormalizedModules = files
9-
.map((file: IModule) => {
7+
.map((file: IModule & { path: string }) => {
8+
if (file.path[0] === "/") {
9+
// Remove the leading slash
10+
const p = file.path.split("");
11+
p.shift();
12+
file.path = p.join("");
13+
}
14+
1015
if (typeof file.content === "object") {
11-
return { ...file, content: JSON.stringify(file.content, null, 2) };
16+
file.content = JSON.stringify(file.content, null, 2);
1217
}
1318

1419
return file;
1520
})
1621
.reduce(
1722
(total: INormalizedModules, next: IModule & { path: string }) => ({
1823
...total,
19-
[next.path]: next
24+
[next.path]: next,
2025
}),
2126
{}
2227
);
@@ -35,9 +40,15 @@ export const define = async (ctx: Context, next: () => Promise<any>) => {
3540
} catch (e) {
3641
/* nothing */
3742
}
38-
const sandbox = await createSandbox(normalizedFiles);
3943

44+
return createSandbox(normalizedFiles);
45+
};
46+
47+
export const define = async (ctx: Context, next: () => Promise<any>) => {
48+
const { files } = ctx.request.body;
49+
50+
const sandbox = await createSandboxFromDefine(files);
4051
ctx.body = {
41-
sandbox
52+
sandbox,
4253
};
4354
};

packages/import-utils/src/create-sandbox/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default async function createSandbox(
7171
console.log(`Creating sandbox with template '${template}'`);
7272
}
7373

74-
packageJsonPackage = { main: "/index.html" };
74+
packageJsonPackage = packageJsonPackage || { main: "/index.html" };
7575

7676
const mainFileUnix = findMainFile(
7777
directory,

packages/import-utils/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
1414
"declaration": true /* Generates corresponding '.d.ts' file. */,
1515
"sourceMap": true /* Generates corresponding '.map' file. */,
16+
"declarationMap": true,
1617
// "outFile": "./", /* Concatenate and emit output to single file. */
1718
"outDir": "./lib" /* Redirect output structure to the directory. */,
1819
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
@@ -60,4 +61,4 @@
6061
"**/__mocks__",
6162
"**/__tests__"
6263
]
63-
}
64+
}

0 commit comments

Comments
 (0)