Skip to content

Commit

Permalink
feat: detect config type for dev
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Jun 23, 2024
1 parent caad404 commit 82a3b43
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/configs/juno.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
junoConfigExist as junoConfigExistTools,
junoConfigFile as junoConfigFileTools,
readJunoConfig as readJunoConfigTools,
type ConfigFile,
type ConfigFilename,
type ConfigType
type PartialConfigFile
} from '@junobuild/config-loader';
import type {ConfigFile} from '@junobuild/config-loader/dist/types/types/config';
import {nonNullish} from '@junobuild/utils';
import {writeFile} from 'node:fs/promises';
import {
Expand Down Expand Up @@ -36,9 +36,7 @@ export const writeJunoConfig = async ({
configPath
}: {
config: JunoConfigWithSatelliteId;
configType: ConfigType;
configPath: string | undefined;
}): Promise<void> => {
} & PartialConfigFile): Promise<void> => {
switch (configType) {
case 'ts':
case 'js': {
Expand Down
7 changes: 5 additions & 2 deletions src/configs/juno.dev.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type {ConfigFilename} from '@junobuild/config-loader';
import type {ConfigFile, ConfigFilename} from '@junobuild/config-loader';
import {
detectJunoConfigType as detectJunoConfigTypeTools,
junoConfigExist as junoConfigExistTools,
junoConfigFile as junoConfigFileTools
} from '@junobuild/config-loader';
import type {ConfigFile} from '@junobuild/config-loader/dist/types/types/config';
import {JUNO_DEV_CONFIG_FILENAME} from '../constants/constants';

const JUNO_DEV_CONFIG_FILE: {filename: ConfigFilename} = {filename: JUNO_DEV_CONFIG_FILENAME};
Expand All @@ -12,4 +12,7 @@ export const junoDevConfigExist = async (): Promise<boolean> => {
return await junoConfigExistTools(JUNO_DEV_CONFIG_FILE);
};

export const detectJunoDevConfigType = (): ConfigFile | undefined =>
detectJunoConfigTypeTools(JUNO_DEV_CONFIG_FILE);

export const junoDevConfigFile = (): ConfigFile => junoConfigFileTools(JUNO_DEV_CONFIG_FILE);
26 changes: 23 additions & 3 deletions src/services/docker.services.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import {execute} from '@junobuild/cli-tools';
import type {PartialConfigFile} from '@junobuild/config-loader';
import {nonNullish} from '@junobuild/utils';
import {magenta} from 'kleur';
import {existsSync} from 'node:fs';
import {writeFile} from 'node:fs/promises';
import {join} from 'node:path';
import {junoDevConfigExist, junoDevConfigFile} from '../configs/juno.dev.config';
import {
detectJunoDevConfigType,
junoDevConfigExist,
junoDevConfigFile
} from '../configs/juno.dev.config';
import {JUNO_DEV_CONFIG_FILENAME} from '../constants/constants';
import {assertDockerRunning, checkDockerVersion} from '../utils/env.utils';
import {copyTemplateFile, readTemplateFile} from '../utils/fs.utils';
Expand Down Expand Up @@ -55,15 +61,29 @@ const assertJunoDevConfig = async () => {
`A config file is required for development. Would you like the CLI to create one for you?`
);

const configType = await promptConfigType();
const {configType, configPath} = await buildConfigType();

await copyTemplateFile({
template: `${JUNO_DEV_CONFIG_FILENAME}.${configType}`,
sourceFolder: TEMPLATE_PATH,
destinationFolder: DESTINATION_PATH
destinationFolder: DESTINATION_PATH,
...(nonNullish(configPath) && {destinationFilename: configPath})
});
};

const buildConfigType = async (): Promise<PartialConfigFile> => {
// We try to automatically detect if we should create a TypeScript or JavaScript (mjs) configuration.
const detectedConfig = detectJunoDevConfigType();

if (nonNullish(detectedConfig)) {
return detectedConfig;
}

const configType = await promptConfigType();

return {configType};
};

const assertDockerCompose = async () => {
if (existsSync('docker-compose.yml')) {
return;
Expand Down
4 changes: 3 additions & 1 deletion src/utils/fs.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ const __dirname = dirname(__filename);
export const copyTemplateFile = async ({
sourceFolder,
destinationFolder,
destinationFilename,
template,
overwrite = false
}: {
sourceFolder: string;
destinationFolder: string;
destinationFilename?: string;
template: string;
overwrite?: boolean;
}) => {
const destination = join(destinationFolder, template);
const destination = destinationFilename ?? join(destinationFolder, template);

if (!overwrite && existsSync(destination)) {
const answer = await confirm(
Expand Down

0 comments on commit 82a3b43

Please sign in to comment.