Skip to content

Commit

Permalink
feat: write configuration on init
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Feb 15, 2024
1 parent 885e34c commit 1f98fe5
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const assertSourceDirExists = (source: string) => {
console.log(
`${red(
'Cannot proceed deployment.'
)}\nAre you sure the folder containing your built app (the "source" tag in the juno.json file) files is correctly configured, or have you built your app?`
)}\nAre you sure the folder containing your built app (the "source" tag in the configuration file for Juno) files is correctly configured, or have you built your app?`
);
process.exit(1);
}
Expand Down
60 changes: 38 additions & 22 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isNullish} from '@junobuild/utils';
import {isNullish, nonNullish} from '@junobuild/utils';
import {cyan, red} from 'kleur';
import prompts from 'prompts';
import {
Expand All @@ -8,13 +8,9 @@ import {
type CliOrbiterConfig,
type CliSatelliteConfig
} from '../configs/cli.config';
import {
junoConfigExist,
junoConfigFile,
saveOrbiterConfig,
saveSatelliteConfig
} from '../configs/juno.config';
import {junoConfigExist, junoConfigFile, saveConfig} from '../configs/juno.config';
import type {ConfigType} from '../types/config';
import {confirmAndExit} from '../utils/prompt.utils';

export const init = async () => {
const token = getToken();
Expand All @@ -24,40 +20,60 @@ export const init = async () => {
return;
}

await initSatelliteConfig();
await initOrbiterConfig();
if (await junoConfigExist()) {
await confirmAndExit(
'Your existing configuration will be overwritten. Are you sure you want to continue?'
);
}

await initConfig();
};

const initSatelliteConfig = async () => {
const initConfig = async () => {
const satelliteId = await initSatelliteConfig();
const orbiterId = await initOrbiterConfig();

const source = await promptSource();

const configType = await initConfigType();

await saveConfig({
config: {
satellite: {satelliteId, source},
...(nonNullish(orbiterId) && {orbiter: {orbiterId}})
},
configType
});
};

const initSatelliteConfig = async (): Promise<string> => {
const satellites = getCliSatellites();

let satellite = await (satellites?.length > 0 ? promptSatellites(satellites) : promptSatellite());
const satellite = await (satellites?.length > 0
? promptSatellites(satellites)
: promptSatellite());

if (satellite === '_manual_') {
satellite = await promptSatellite();
return await promptSatellite();
}

const source = await promptSource();

const configType = await initConfigType();

await saveSatelliteConfig({satellite: {satelliteId: satellite, source}, configType});
return satellite;
};

const initOrbiterConfig = async () => {
const initOrbiterConfig = async (): Promise<string | undefined> => {
const authOrbiters = getCliOrbiters();

if (authOrbiters === undefined || authOrbiters.length === 0) {
return;
return undefined;
}

const orbiter = await promptOrbiters(authOrbiters);

if (orbiter === '_none_') {
return;
return undefined;
}

await saveOrbiterConfig({orbiterId: orbiter});
return orbiter;
};

const promptSatellites = async (satellites: CliSatelliteConfig[]): Promise<string> => {
Expand Down Expand Up @@ -94,7 +110,7 @@ const promptConfigType = async (): Promise<ConfigType> => {
message: 'What configuration file format do you prefer?',
choices: [
{title: 'TypeScript', value: 'ts'},
{title: 'JavaScript', value: 'ts'},
{title: 'JavaScript', value: 'js'},
{title: 'JSON', value: 'json'}
],
initial: 0
Expand Down
2 changes: 1 addition & 1 deletion src/commands/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const missionControlVersion = async () => {

const satelliteVersion = async () => {
if (!(await junoConfigExist())) {
console.log(`No ${yellow('juno.json')} configuration file found.`);
console.log(`No ${yellow('config')} file found.`);
return;
}

Expand Down
80 changes: 48 additions & 32 deletions src/configs/juno.config.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,28 @@
import type {
JunoConfig,
JunoConfigFnOrObject,
OrbiterConfig,
SatelliteConfig
} from '@junobuild/config';
import type {JunoConfig, JunoConfigFnOrObject, SatelliteConfig} from '@junobuild/config';

Check failure on line 1 in src/configs/juno.config.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module '@junobuild/config' or its corresponding type declarations.
import {isNullish} from '@junobuild/utils';
import {existsSync} from 'node:fs';
import {access, readFile, writeFile} from 'node:fs/promises';
import {join} from 'node:path';
import {
TEMPLATE_INIT_PATH,
TEMPLATE_SATELLITE_CONFIG_FILENAME
} from '../constants/config.constants';
import {JUNO_CONFIG_FILENAME, JUNO_JSON} from '../constants/constants';
import {DEPLOY_DEFAULT_SOURCE} from '../constants/deploy.constants';
import type {ConfigType} from '../types/config';
import {readTemplateFile} from '../utils/fs.utils';
import {nodeRequire} from '../utils/node.utils';

export const saveSatelliteConfig = async ({
satellite
export const saveConfig = async ({
config,
configType
}: {
satellite: SatelliteConfig;
config: JunoConfig;
configType: ConfigType;
}): Promise<void> => {
if (await junoConfigExist()) {
const existingConfig = await readJunoConfig();
await writeJunoConfig({
...existingConfig,
satellite
});
return;
}

await writeJunoConfig({satellite});
};

export const saveOrbiterConfig = async (orbiter: OrbiterConfig): Promise<void> => {
if (!(await junoConfigExist())) {
throw new Error(`No juno.json configuration file has been initialized yet.`);
}

const existingConfig = await readJunoConfig();
await writeJunoConfig({
...existingConfig,
orbiter
config,
configType
});
};

Expand Down Expand Up @@ -113,9 +98,40 @@ export const junoConfigFile = (): {configPath: string; configType: ConfigType} =
};
};

// TODO
const writeJunoConfig = async (config: JunoConfig): Promise<void> => {
await writeFile(JUNO_CONFIG_FILENAME, JSON.stringify(config, null, 2), 'utf-8');
const writeJunoConfig = async ({
config,
configType
}: {
config: JunoConfig;
configType: ConfigType;
}): Promise<void> => {
switch (configType) {
case 'ts':
case 'js': {
const {
orbiter,
satellite: {satelliteId, source}
} = config;

const template = await readTemplateFile({
template: isNullish(orbiter)
? `${JUNO_CONFIG_FILENAME}.${configType}`
: `${TEMPLATE_SATELLITE_CONFIG_FILENAME}.${configType}`,
sourceFolder: TEMPLATE_INIT_PATH
});

const content = template
.replace('<SATELLITE_ID>', satelliteId)
.replace('<SOURCE>', source ?? DEPLOY_DEFAULT_SOURCE)
.replace('<ORBITER_ID>', orbiter?.orbiterId ?? '');

await writeFile(`${JUNO_CONFIG_FILENAME}.${configType}`, content, 'utf-8');
break;
}
default: {
await writeFile(`${JUNO_CONFIG_FILENAME}.json`, JSON.stringify(config, null, 2), 'utf-8');
}
}
};

const readJunoConfig = async (): Promise<JunoConfig> => {
Expand Down
2 changes: 2 additions & 0 deletions src/constants/config.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const TEMPLATE_INIT_PATH = '../templates/init';
export const TEMPLATE_SATELLITE_CONFIG_FILENAME = `juno.satellite.config`;
4 changes: 3 additions & 1 deletion src/utils/msg.utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {red} from 'kleur';

export const consoleNoConfigFound = () => {
console.log(`Oops! ${red('No juno.json found.')} Try to run the command from your project root.`);
console.log(
`Oops! ${red('No config file found.')} Try to run the command from your project root.`
);
};
12 changes: 12 additions & 0 deletions templates/init/juno.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {defineConfig} from '@junobuild/config';

/** @type {import('@junobuild/config').JunoConfig} */
export default defineConfig({
satellite: {
satelliteId: '<SATELLITE_ID>',
source: '<SOURCE>'
},
orbiter: {
orbiterId: '<ORBITER_ID>'
}
});
11 changes: 11 additions & 0 deletions templates/init/juno.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {defineConfig} from '@junobuild/config';

Check failure on line 1 in templates/init/juno.config.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module '@junobuild/config' or its corresponding type declarations.

export default defineConfig({
satellite: {
satelliteId: '<SATELLITE_ID>',
source: '<SOURCE>'
},
orbiter: {
orbiterId: '<ORBITER_ID>'
}
});
9 changes: 9 additions & 0 deletions templates/init/juno.satellite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {defineConfig} from '@junobuild/config';

/** @type {import('@junobuild/config').JunoConfig} */
export default defineConfig({
satellite: {
satelliteId: '<SATELLITE_ID>',
source: '<SOURCE>'
}
});
8 changes: 8 additions & 0 deletions templates/init/juno.satellite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {defineConfig} from '@junobuild/config';

Check failure on line 1 in templates/init/juno.satellite.config.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module '@junobuild/config' or its corresponding type declarations.

export default defineConfig({
satellite: {
satelliteId: '<SATELLITE_ID>',
source: '<SOURCE>'
}
});

0 comments on commit 1f98fe5

Please sign in to comment.