Skip to content

Commit b6f0d96

Browse files
MichaelDeBoeychristianalfoni
authored andcommitted
Fix parsedConfigurations type (codesandbox#3109)
* Fix parsedConfigurations type * Fix types * Add @babel/plugin-proposal-optional-chaining * Fix types * Add babel plugins iin the correct place * Resolve discussions
1 parent 3a45ddc commit b6f0d96

File tree

11 files changed

+50
-53
lines changed

11 files changed

+50
-53
lines changed

packages/app/config/babel.dev.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = {
3232
require.resolve('@babel/plugin-transform-destructuring'),
3333
require.resolve('@babel/plugin-proposal-object-rest-spread'),
3434
require.resolve('@babel/plugin-proposal-class-properties'),
35+
require.resolve('@babel/plugin-proposal-optional-chaining'),
3536
require.resolve('@babel/plugin-transform-runtime'),
3637
require.resolve('babel-plugin-lodash'),
3738
require.resolve('@babel/plugin-syntax-dynamic-import'),

packages/app/config/babel.prod.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = {
3030
require.resolve('@babel/plugin-transform-async-to-generator'),
3131
require.resolve('@babel/plugin-proposal-object-rest-spread'),
3232
require.resolve('@babel/plugin-proposal-class-properties'),
33+
require.resolve('@babel/plugin-proposal-optional-chaining'),
3334
require.resolve('@babel/plugin-transform-runtime'),
3435
require.resolve('babel-plugin-lodash'),
3536
require.resolve('@babel/plugin-syntax-dynamic-import'),

packages/app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
"circular-json": "^0.4.0",
100100
"codemirror": "^5.27.4",
101101
"codesandbox-api": "0.0.23",
102-
"codesandbox-import-utils": "2.1.2",
102+
"codesandbox-import-utils": "^2.1.11",
103103
"color": "^0.11.4",
104104
"compare-versions": "^3.1.0",
105105
"console": "^0.7.2",
@@ -223,6 +223,7 @@
223223
"@babel/helper-module-imports": "^7.0.0",
224224
"@babel/plugin-proposal-class-properties": "^7.5.5",
225225
"@babel/plugin-proposal-object-rest-spread": "^7.5.5",
226+
"@babel/plugin-proposal-optional-chaining": "^7.7.4",
226227
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
227228
"@babel/plugin-transform-async-to-generator": "^7.5.0",
228229
"@babel/plugin-transform-react-display-name": "^7.2.0",

packages/app/src/app/overmind/namespaces/editor/internalActions.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import getTemplateDefinition from '@codesandbox/common/lib/templates';
1+
import getTemplateDefinition, {
2+
TemplateType,
3+
} from '@codesandbox/common/lib/templates';
24
import {
35
Module,
46
ModuleTab,
@@ -175,8 +177,8 @@ export const saveCode: AsyncAction<{
175177
};
176178

177179
export const updateCurrentTemplate: AsyncAction = async ({
178-
state,
179180
effects,
181+
state,
180182
}) => {
181183
try {
182184
const currentTemplate = state.editor.currentSandbox.template;
@@ -187,9 +189,9 @@ export const updateCurrentTemplate: AsyncAction = async ({
187189
// in the sandbox configuration.
188190
if (
189191
templateDefinition.isServer ||
190-
state.editor.parsedConfigurations.sandbox.parsed.template
192+
state.editor.parsedConfigurations?.sandbox?.parsed?.template
191193
) {
192-
const { parsed } = state.editor.parsedConfigurations.package;
194+
const { parsed = {} } = state.editor.parsedConfigurations?.package || {};
193195

194196
const modulesByPath = mapValues(state.editor.modulesByPath, module => ({
195197
// No idea why this typing fails!
@@ -199,10 +201,10 @@ export const updateCurrentTemplate: AsyncAction = async ({
199201
isBinary: module.isBinary,
200202
}));
201203

202-
// TODO: What is a templat really? Two different kinds of templates here, need to fix the types
204+
// TODO: What is a template really? Two different kinds of templates here, need to fix the types
203205
// Talk to Ives and Bogdan
204-
const newTemplate =
205-
computeTemplate(parsed, modulesByPath) || ('node' as any);
206+
const newTemplate = (computeTemplate(parsed, modulesByPath) ||
207+
'node') as TemplateType;
206208

207209
if (
208210
newTemplate !== currentTemplate &&

packages/app/src/app/overmind/namespaces/editor/state.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import getTemplate from '@codesandbox/common/lib/templates';
22
import { generateFileFromSandbox } from '@codesandbox/common/lib/templates/configuration/package-json';
33
import { getPreviewTabs } from '@codesandbox/common/lib/templates/devtools';
4-
import { ViewConfig } from '@codesandbox/common/lib/templates/template';
4+
import {
5+
ParsedConfigurationFiles,
6+
ViewConfig,
7+
} from '@codesandbox/common/lib/templates/template';
58
import {
69
DevToolsTabPosition,
710
DiffTab,
@@ -56,7 +59,7 @@ type State = {
5659
mainModule: Derive<State, Module>;
5760
currentPackageJSON: Derive<State, Module>;
5861
currentPackageJSONCode: Derive<State, string>;
59-
parsedConfigurations: Derive<State, any>;
62+
parsedConfigurations: Derive<State, ParsedConfigurationFiles> | null;
6063
currentTab: Derive<State, ModuleTab | DiffTab>;
6164
modulesByPath: SandboxFs;
6265
isAdvancedEditor: Derive<State, boolean>;

packages/app/src/app/pages/Sandbox/Editor/Workspace/Dependencies/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,19 @@ export const Dependencies: FunctionComponent = () => {
3232
resource => !resource.includes('fonts.googleapis.com/css')
3333
);
3434

35-
if (!parsedConfigurations.package) {
35+
if (!parsedConfigurations?.package) {
3636
return <ErrorMessage>Unable to find package.json</ErrorMessage>;
3737
}
3838

39-
const { parsed, error } = parsedConfigurations.package;
39+
const { error, parsed } = parsedConfigurations.package;
4040
if (error) {
4141
return (
4242
<ErrorMessage>We weren{"'"}t able to parse the package.json</ErrorMessage>
4343
);
4444
}
4545

46+
const { dependencies = {} /* devDependencies = {} */ } = parsed;
4647
const { externalResourcesEnabled } = getTemplateDefinition(template);
47-
48-
const dependencies = parsed.dependencies || {};
4948
return (
5049
<div>
5150
<Margin bottom={0}>

packages/app/src/app/pages/Sandbox/Editor/Workspace/items/Server/Tasks.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ const Task = styled.button`
3737
`;
3838

3939
type Props = {
40-
package:
41-
| {
42-
scripts: {
43-
[command: string]: string;
44-
};
45-
}
46-
| undefined;
40+
package?: {
41+
scripts?: {
42+
[command: string]: string;
43+
};
44+
};
4745
};
4846

4947
// These scripts are only supposed to run on the main thread.
@@ -73,7 +71,7 @@ export class Tasks extends React.PureComponent<Props> {
7371
};
7472

7573
render() {
76-
if (!this.props.package || !this.props.package.scripts) {
74+
if (!this.props.package?.scripts) {
7775
return null;
7876
}
7977

packages/app/src/app/pages/Sandbox/Editor/Workspace/items/Server/index.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,7 @@ export const Server: FunctionComponent = () => {
6767
<SubTitle>Run Scripts</SubTitle>
6868
<Margin top={0.5}>
6969
<TasksContainer disconnected={disconnected}>
70-
<Tasks
71-
package={
72-
parsedConfigurations.package &&
73-
parsedConfigurations.package.parsed
74-
}
75-
/>
70+
<Tasks package={parsedConfigurations?.package?.parsed} />
7671
</TasksContainer>
7772
</Margin>
7873
</Margin>

packages/common/src/templates/configuration/types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ export type ConfigurationFile = {
1818
};
1919

2020
export type ParsedConfigurationFile<T> = {
21-
parsed?: T;
2221
code: string;
2322
generated: boolean;
24-
error?: Error;
2523
path: string;
26-
};
24+
} & ({ error: Error; parsed?: undefined } | { error?: undefined; parsed: T });
2725

2826
export type ConfigurationUIProps = {
2927
file: string;

packages/common/src/templates/template.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ export type ParsedConfigurationFiles = {
3333
package?: ParsedConfigurationFile<{
3434
main: string;
3535
dependencies?: Dependencies;
36-
devDependencies: Dependencies;
37-
resolutions?: {
38-
[source: string]: string;
39-
};
36+
devDependencies?: Dependencies;
37+
resolutions?: { [source: string]: string };
38+
scripts?: { [script: string]: string };
4039
[otherProperties: string]: any | undefined;
4140
}>;
4241
[path: string]: ParsedConfigurationFile<any> | undefined;

0 commit comments

Comments
 (0)