Skip to content

Commit

Permalink
fix: Fix init crashing and submitting too early.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Dec 17, 2020
1 parent 34a32ca commit 6f9e4a9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
12 changes: 4 additions & 8 deletions src/commands/Init.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Command from './Base';
import Package from '../Package';
import Init from '../components/Init';
import { PackemonPackage, PackemonPackageConfig } from '../types';
import { DEFAULT_FORMAT, DEFAULT_INPUT, DEFAULT_PLATFORM, DEFAULT_SUPPORT } from '../constants';
import { DEFAULT_FORMAT, DEFAULT_INPUT, DEFAULT_SUPPORT } from '../constants';

export interface InitOptions {
force: boolean;
Expand Down Expand Up @@ -39,9 +39,7 @@ export class InitCommand extends Command<InitOptions> {
return (
<Init
packageNames={unconfiguredPackages.map((pkg) => pkg.package.name)}
onComplete={(configs) => {
void this.writeConfigsToPackageJsons(unconfiguredPackages, configs);
}}
onComplete={(configs) => this.writeConfigsToPackageJsons(unconfiguredPackages, configs)}
/>
);
}
Expand Down Expand Up @@ -78,9 +76,7 @@ export class InitCommand extends Command<InitOptions> {

if (platform) {
if (Array.isArray(platform) && platform.length === 1) {
if (platform[0] !== DEFAULT_PLATFORM) {
[config.platform] = platform;
}
[config.platform] = platform;
} else {
config.platform = platform;
}
Expand All @@ -97,7 +93,7 @@ export class InitCommand extends Command<InitOptions> {
packages: WorkspacePackage<PackemonPackage>[],
configs: Record<string, PackemonPackageConfig>,
) {
return Promise.all(
await Promise.all(
packages.map((item) => {
const pkg = new Package(this.packemon.project, new Path(item.metadata.packagePath), {
...item.package,
Expand Down
27 changes: 18 additions & 9 deletions src/components/Init/PackageForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,28 @@ export default function PackageForm({ onSubmit }: PackageFormProps) {
input &&
((hasUMD && namespace) || (!hasUMD && !namespace))
) {
// Delay or focus API crashes
const result = {
format,
inputs: { index: input },
namespace,
platform,
support,
};

// Delay submission or focus API crashes
// https://github.com/vadimdemedes/ink/pull/404
setTimeout(() => {
onSubmit({
format,
inputs: { index: input },
namespace,
platform,
support,
});
onSubmit(result);
}, 100);

// Reset state for the next package
setPlatform([]);
setSupport(DEFAULT_SUPPORT);
setFormat([]);
setInput('');
setNamespace('');
}
});
}, [format, input, namespace, onSubmit, platform, support]);

// PLATFORM

Expand Down
15 changes: 8 additions & 7 deletions src/components/Init/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import React, { useCallback, useEffect, useState } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { Box, Text } from 'ink';
import { Header, Style } from '@boost/cli';
import { Header, Style, useProgram } from '@boost/cli';
import PackageForm from './PackageForm';
import { PackemonPackageConfig } from '../../types';

export type InitPackageConfigs = Record<string, PackemonPackageConfig>;

export interface InitProps {
packageNames: string[];
onComplete: (configs: InitPackageConfigs) => void;
onComplete: (configs: InitPackageConfigs) => Promise<unknown>;
}

export default function Init({ packageNames, onComplete }: InitProps) {
const { exit } = useProgram();
const [pkgsToConfigure, setPkgsToConfigure] = useState(() => packageNames);
const [pkgConfigs, setPkgConfigs] = useState<InitPackageConfigs>({});
const currentPkg = pkgsToConfigure[0];
const currentPkg = useMemo(() => pkgsToConfigure[0], [pkgsToConfigure]);

// Save config and move to next package
const handleSubmit = useCallback(
Expand All @@ -32,9 +33,9 @@ export default function Init({ packageNames, onComplete }: InitProps) {
// Complete once all packages have been configured
useEffect(() => {
if (pkgsToConfigure.length === 0) {
onComplete(pkgConfigs);
void onComplete(pkgConfigs).finally(exit);
}
}, [pkgsToConfigure, pkgConfigs, onComplete]);
}, [pkgsToConfigure, pkgConfigs, onComplete, exit]);

// Exit when theres no packages
if (pkgsToConfigure.length === 0) {
Expand All @@ -54,7 +55,7 @@ export default function Init({ packageNames, onComplete }: InitProps) {
</Box>

<Box marginTop={1} flexDirection="column">
<PackageForm onSubmit={handleSubmit} />
<PackageForm key={currentPkg} onSubmit={handleSubmit} />
</Box>
</Box>
);
Expand Down

0 comments on commit 6f9e4a9

Please sign in to comment.