Skip to content

Commit

Permalink
introduced switch command
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdugne committed Jan 6, 2022
1 parent 6570580 commit 62fc28b
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 63 deletions.
68 changes: 7 additions & 61 deletions cli/export-bundle/index.js → cli/bundler/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ const {spawn} = require('child_process');

const ini = require('./ini');
const updatePreset = require('./update-preset');
const switchBundle = require('./switch');

// -----------------------------------------------------------------------------

const PRESETS_CFG = 'export_presets.cfg';
const OVERRIDE_CFG = 'override.cfg';

const SEMVER = ['patch', 'minor', 'major'];
const ENV = ['debug', 'production'];

Expand All @@ -42,8 +41,7 @@ const extractEnv = (preset) => {

// -----------------------------------------------------------------------------

const inquireParams = async (bundles, _presets) => {
const presets = _presets.preset;
const inquireVersioning = async () => {
const packageJSON = JSON.parse(fs.readFileSync('./package.json', 'utf8'));

const questions = [
Expand All @@ -52,41 +50,16 @@ const inquireParams = async (bundles, _presets) => {
name: 'versionLevel',
type: 'list',
choices: [`${packageJSON.version}`, ...SEMVER]
},
{
message: 'preset',
name: 'presetNum',
type: 'list',
choices: Object.keys(presets).map((num) => ({
name: presets[num].name,
value: num
}))
}
];

const bundleIds = Object.keys(bundles);
const singleBundleId = bundleIds.length > 1 ? null : bundleIds[0];

if (!singleBundleId) {
questions.push({
message: 'bundle',
name: 'bundleId',
type: 'list',
choices: bundleIds
});
}

const answers = await inquirer.prompt(questions);
const {bundleId = singleBundleId, presetNum, versionLevel} = answers;
const preset = presets[presetNum];
const bundle = bundles[bundleId];

return {bundleId, bundle, preset, versionLevel};
const {versionLevel} = answers;
return {versionLevel};
};

// -----------------------------------------------------------------------------

// -- great example for https://github.com/yargs/yargs/issues/1476
const exportBundle = async (coreConfig, bundles) => {
console.log(`⚙️ exporting a ${chalk.blue.bold('bundle')}...`);

Expand All @@ -96,17 +69,8 @@ const exportBundle = async (coreConfig, bundles) => {
return;
}

let presets;

try {
presets = ini.parse(fs.readFileSync(PRESETS_CFG, 'utf8'));
} catch (e) {
console.log(`\nCould not open ${PRESETS_CFG}`);
console.log(chalk.red.bold('🔴 failed'));
return;
}

const {bundleId, bundle, preset, versionLevel} = await inquireParams(bundles, presets);
const {bundleId, preset, presets} = await switchBundle(bundles);
const {versionLevel} = await inquireVersioning();

const env = extractEnv(preset);

Expand All @@ -117,24 +81,6 @@ const exportBundle = async (coreConfig, bundles) => {

// ---------

let override;

try {
override = ini.parse(fs.readFileSync(OVERRIDE_CFG, 'utf8'));
if (!override.bundle) override.bundle = {};
} catch (e) {
shell.touch(OVERRIDE_CFG);
override = {bundle: {}};
console.log(`\nCould not open ${OVERRIDE_CFG}. Created the file.`);
}

override.bundle.id = bundleId;
override.bundle.platform = preset.platform;

fs.writeFileSync(OVERRIDE_CFG, ini.stringify(override));

// ---------

let newVersion = versionLevel;
if (SEMVER.includes(versionLevel)) {
console.log(`⚙️ npm version ${chalk.blue.bold(versionLevel)}`);
Expand All @@ -150,7 +96,7 @@ const exportBundle = async (coreConfig, bundles) => {

console.log(`\n⚙️ Ready to bundle ${bundleInfo}`);

updatePreset(bundleId, env, coreConfig, preset, bundle, newVersion);
updatePreset(bundleId, env, coreConfig, preset, bundles[bundleId], newVersion);
fs.writeFileSync(PRESETS_CFG, ini.stringify(presets));

// ---------
Expand Down
File renamed without changes.
99 changes: 99 additions & 0 deletions cli/bundler/switch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// -----------------------------------------------------------------------------

const chalk = require('chalk');
const fs = require('fs');
const inquirer = require('inquirer');
const shell = require('shelljs');

// -----------------------------------------------------------------------------

const ini = require('./ini');

// -----------------------------------------------------------------------------

const PRESETS_CFG = 'export_presets.cfg';
const OVERRIDE_CFG = 'override.cfg';

// -----------------------------------------------------------------------------

const inquireParams = async (bundles, _presets) => {
const presets = _presets.preset;
const bundleIds = Object.keys(bundles);
const singleBundleId = bundleIds.length > 1 ? null : bundleIds[0];

const questions = [
{
message: 'preset',
name: 'presetNum',
type: 'list',
choices: Object.keys(presets).map((num) => ({
name: presets[num].name,
value: num
}))
}
];

if (!singleBundleId) {
questions.push({
message: 'bundle',
name: 'bundleId',
type: 'list',
choices: bundleIds
});
}

const answers = await inquirer.prompt(questions);
const {bundleId = singleBundleId, presetNum} = answers;
const preset = presets[presetNum];
const bundle = bundles[bundleId];

return {bundleId, bundle, preset};
};

// -----------------------------------------------------------------------------

const switchBundle = async (bundles) => {
console.log(`⚙️ switching to another ${chalk.blue.bold('bundle')}...`);

if (!bundles) {
console.log('\nmissing bundles in fox.config.json');
console.log(chalk.red.bold('🔴 failed'));
return;
}

let presets;

try {
presets = ini.parse(fs.readFileSync(PRESETS_CFG, 'utf8'));
} catch (e) {
console.log(`\nCould not open ${PRESETS_CFG}`);
console.log(chalk.red.bold('🔴 failed'));
return;
}

const {bundleId, preset} = await inquireParams(bundles, presets);

// ---------

let override;

try {
override = ini.parse(fs.readFileSync(OVERRIDE_CFG, 'utf8'));
if (!override.bundle) override.bundle = {};
} catch (e) {
shell.touch(OVERRIDE_CFG);
override = {bundle: {}};
console.log(`\nCould not open ${OVERRIDE_CFG}. Created the file.`);
}

override.bundle.id = bundleId;
override.bundle.platform = preset.platform;

fs.writeFileSync(OVERRIDE_CFG, ini.stringify(override));

return {bundleId, preset, presets};
};

// -----------------------------------------------------------------------------

module.exports = switchBundle;
File renamed without changes.
File renamed without changes.
13 changes: 11 additions & 2 deletions cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ const pkg = require('../package.json');
const generateIcons = require('./generate-icons');
const generateSplashscreens = require('./generate-splashscreens');
const generateScreenshots = require('./generate-screenshots');
const exportBundle = require('./export-bundle');
const exportBundle = require('./bundler/export');
const switchBundle = require('./bundler/switch');
const runGame = require('./run-game');

// -----------------------------------------------------------------------------

const EXPORT = 'export';
const SWITCH = 'switch';

const GENERATE_ICONS = 'generate:icons';
const GENERATE_SPLASHSCREENS = 'generate:splashscreens';
const GENERATE_SCREENSHOTS = 'generate:screenshots';
Expand All @@ -31,6 +34,7 @@ const RUN_GAME = 'run:game';

const commands = [
EXPORT,
SWITCH,
GENERATE_ICONS,
GENERATE_SCREENSHOTS,
GENERATE_SPLASHSCREENS,
Expand Down Expand Up @@ -112,7 +116,7 @@ const verifyConfig = (config, defaultConfig) => {

// -----------------------------------------------------------------------------

const cli = (args) => {
const cli = (argv) => {
const command = argv._[0];
if (!commands.includes(command)) {
yargs.showHelp();
Expand Down Expand Up @@ -143,6 +147,10 @@ const cli = (args) => {
exportBundle(core, bundles);
return;
}
case SWITCH: {
switchBundle(bundles);
return;
}
}

// -------- IO commands
Expand Down Expand Up @@ -178,6 +186,7 @@ const argv = yargs(process.argv.splice(2))
.command(RUN_EDITOR, 'open Godot Editor with your main scene')
.command(RUN_GAME, 'start your game to debug')
.command(EXPORT, 'export a bundle for one of your presets')
.command(SWITCH, 'switch from a bundle to another (write in override.cfg)')
.command(GENERATE_ICONS, 'generate icons, using a base 1200x1200 image')
.command(
GENERATE_SPLASHSCREENS,
Expand Down

0 comments on commit 62fc28b

Please sign in to comment.