Skip to content

Commit

Permalink
Various updates:
Browse files Browse the repository at this point in the history
- Adds .builda.json to gitignore (replacing yml)
- Splits index.ts into a more logical order
- Moves get-substitutions into it's own helper
- Adds some extra user feedback
- Adds a catcher to stop functions running if config exists and 'init' is called
- Adds 'Argv' type definition
  • Loading branch information
foxleigh81 committed Aug 17, 2022
1 parent 4af9332 commit d1c8d88
Show file tree
Hide file tree
Showing 21 changed files with 239 additions and 205 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
npm-debug.log
/components
log.txt
.builda.yml
.builda.json
.buildcomrc
.nyc_output/
coverage/
Expand Down
30 changes: 0 additions & 30 deletions builda/.builda.json

This file was deleted.

48 changes: 24 additions & 24 deletions builda/dist/data/arguments.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
"init": {
"description": "Initialises a new project",
"required": false,
"boolean": true
},
"add": {
"description": "Adds a new builda module",
"required": false,
"type": "string"
},
"migrate": {
"description": "Migrates a project to the latest version",
"required": false,
"boolean": true
},
"manual": {
"description": "Bypasses the need for a config file",
"required": false,
"boolean": true,
"alias": [
"cli",
"m"
]
}
"init": {
"description": "Initialises a new project",
"required": false,
"boolean": true
},
"add": {
"description": "Adds a new builda module",
"required": false,
"type": "string"
},
"migrate": {
"description": "Migrates a project to the latest version",
"required": false,
"boolean": true
},
"manual": {
"description": "Bypasses the need for a config file",
"required": false,
"boolean": true,
"alias": [
"cli",
"m"
]
}
}
3 changes: 2 additions & 1 deletion builda/dist/data/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ exports.default = [
{
type: 'checkbox',
name: 'scaffoldSelection',
message: 'Select zero or more scaffold types you wish to use',
message: 'Select one or more scaffold types you wish to use',
default: ['component'],
choices: [
'component',
'atom',
Expand Down
5 changes: 2 additions & 3 deletions builda/dist/helpers/get-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ const moduleTypes = ['scaffold', 'prefab'];
const getmodule = (name) => {
if (config) {
const moduleList = config.modules;
const moduleCategory = moduleTypes.find((category) => { var _a; return moduleList[category] && ((_a = moduleList === null || moduleList === void 0 ? void 0 : moduleList[category]) === null || _a === void 0 ? void 0 : _a[name]); });
const path = `${globals_1.default.buildaDir}/modules/${moduleCategory}/${name}`;
console.log(path);
const moduleType = moduleTypes.find((type) => { var _a; return moduleList[type] && ((_a = moduleList === null || moduleList === void 0 ? void 0 : moduleList[type]) === null || _a === void 0 ? void 0 : _a[name]); });
const path = `${globals_1.default.buildaDir}/modules/${moduleType}/${name}`;
const registry = JSON.parse(fs_1.default.readFileSync(`${path}/registry.json`, 'utf8'));
const files = registry.files.filter((file) => file !== 'registry.json');
return {
Expand Down
49 changes: 49 additions & 0 deletions builda/dist/helpers/get-substitutions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSubstitutions = void 0;
const throw_error_1 = __importDefault(require("./throw-error"));
const getSubstitutions = (commandList, argv) => {
const substitutions = [];
if (commandList.substitute) {
commandList.substitute.forEach((sub) => {
var _a;
// No substitution was provided but the config requires one
if (sub.required && !argv[sub.string]) {
(0, throw_error_1.default)(`"--${sub.string}" missing in arguments. This is required.\n`);
}
// User has not provided a substitution but the config has a default fallback value
if (sub.default && !argv[sub.string]) {
substitutions.push({
replace: sub.string,
with: sub.default
});
}
// User has provided the substitution argument
if (argv[sub.string]) {
const value = argv[sub.string] === true
? ''
: argv[sub.string];
// User has provided the substitution argument with no value
if (value === '') {
(0, throw_error_1.default)(`"--${sub.string}" requires a value`);
}
if (sub.valid &&
value !== '' &&
!((_a = sub.valid) === null || _a === void 0 ? void 0 : _a.includes(value))) {
(0, throw_error_1.default)(`\n"${value}" is not a valid ${sub.string}. Please use one of the following: \n - ${sub.valid.join(`\n - `)}\n`);
}
// The value provided is valid
substitutions.push({
replace: sub.string,
with: argv[sub.string]
});
}
});
}
return substitutions;
};
exports.getSubstitutions = getSubstitutions;
exports.default = exports.getSubstitutions;
4 changes: 3 additions & 1 deletion builda/dist/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeFile = exports.convertRegistryPathToUrl = exports.getRegistry = exports.randomWordGenerator = exports.createDir = exports.throwError = exports.askQuestion = exports.nameError = exports.printLogo = exports.printMessage = exports.skip = exports.getModule = exports.getConfigFile = exports.detectPathType = void 0;
exports.writeFile = exports.convertRegistryPathToUrl = exports.getRegistry = exports.randomWordGenerator = exports.createDir = exports.throwError = exports.askQuestion = exports.nameError = exports.printLogo = exports.printMessage = exports.skip = exports.getModule = exports.getSubstitutions = exports.getConfigFile = exports.detectPathType = void 0;
var detect_path_type_1 = require("./detect-path-type");
Object.defineProperty(exports, "detectPathType", { enumerable: true, get: function () { return __importDefault(detect_path_type_1).default; } });
var get_config_file_1 = require("./get-config-file");
Object.defineProperty(exports, "getConfigFile", { enumerable: true, get: function () { return __importDefault(get_config_file_1).default; } });
var get_substitutions_1 = require("./get-substitutions");
Object.defineProperty(exports, "getSubstitutions", { enumerable: true, get: function () { return __importDefault(get_substitutions_1).default; } });
var get_module_1 = require("./get-module");
Object.defineProperty(exports, "getModule", { enumerable: true, get: function () { return __importDefault(get_module_1).default; } });
var skip_1 = require("./skip");
Expand Down
85 changes: 29 additions & 56 deletions builda/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,102 +10,75 @@ const helpers_1 = require("yargs/helpers");
const _helpers_1 = require("./helpers/index.js");
// import data
const arguments_json_1 = __importDefault(require("./data/arguments.json"));
const globals_1 = __importDefault(require("./data/globals"));
// import scripts
const init_1 = __importDefault(require("./scripts/init"));
const generate_commands_1 = __importDefault(require("./scripts/generate-commands"));
const build_from_scaffold_1 = __importDefault(require("./scripts/build-from-scaffold"));
const add_module_1 = __importDefault(require("./scripts/add-module"));
const args = (0, helpers_1.hideBin)(process.argv);
const config = (0, _helpers_1.getConfigFile)();
const { configFileName, websiteUrl } = globals_1.default;
const parser = (0, yargs_1.default)(args)
.usage('Usage: $0 [options]')
.options(arguments_json_1.default)
.help('h')
.version()
.alias('h', 'help');
.alias('h', 'help')
.epilogue(`For more information, visit ${websiteUrl}`);
(0, _helpers_1.printLogo)();
const CREATE_CONFIG_QUESTION = {
message: 'Would you like to create a .builda config?',
message: `Would you like to create a ${configFileName} config?`,
name: 'createConfig',
type: 'confirm'
};
(async () => {
const argv = await parser.argv;
if (args.length === 0 && config) {
// No arguments were passed but a config file exists
(0, _helpers_1.printMessage)('No arguments provided.\r', 'danger');
parser.showHelp();
/** UNHAPPY PATHS */
if (config) {
if (args.length === 0) {
// No arguments were passed but a config file exists
(0, _helpers_1.printMessage)('No arguments provided.\r', 'danger');
parser.showHelp();
return process.exit(0);
}
if (argv.init) {
(0, _helpers_1.printMessage)(`A ${configFileName} has been found. Please delete it before continuing.\r`, 'danger');
return process.exit(0);
}
}
if ((args.length === 0 || !argv.manual) && !config) {
(0, _helpers_1.printMessage)('Builda config not detected.\r', 'danger');
(0, _helpers_1.printMessage)(`No ${configFileName} found. Please run the 'init' command.\r`, 'danger');
// No arguments were passed but a config file does not exist
return (0, _helpers_1.askQuestion)(CREATE_CONFIG_QUESTION).then(({ createConfig }) => {
if (createConfig) {
return (0, init_1.default)({});
}
(0, _helpers_1.printMessage)('Process terminated due to user selection', 'error');
return process.exit(1);
return process.exit(0);
});
}
if (argv.init)
(0, init_1.default)({});
if (argv.manual) {
(0, _helpers_1.printMessage)('Manual mode selected.\r', 'notice');
return (0, _helpers_1.printMessage)('🛠 This route does not exist yet.\r', 'notice');
}
if (argv._[0].toString() === 'add') {
const module = argv._[1].toString();
return (0, add_module_1.default)({ path: module });
(0, _helpers_1.printMessage)('🛠 This route does not exist yet.\r', 'notice');
return process.exit(0);
}
if (argv.migrate) {
// The user wants to migrate an old buildcom config file
// Go to migrate function
return (0, _helpers_1.printMessage)('🛠 This route does not exist yet.\r', 'notice');
}
/** HAPPY PATHS */
if (argv.init)
return (0, init_1.default)({});
if (argv._[0].toString() === 'add') {
const module = argv._[1].toString();
return (0, add_module_1.default)({ path: module });
}
const commands = config ? (0, generate_commands_1.default)() : [];
const commandString = process.argv[2].replace('--', '');
const command = commands.find((c) => c.name === commandString);
const getSubstitutions = (commandList) => {
const substitutions = [];
if (commandList.substitute) {
commandList.substitute.forEach((sub) => {
var _a;
// No substitution was provided but the config requires one
if (sub.required && !argv[sub.string]) {
(0, _helpers_1.throwError)(`"--${sub.string}" missing in arguments. This is required.\n`);
}
// User has not provided a substitution but the config has a default fallback value
if (sub.default && !argv[sub.string]) {
substitutions.push({
replace: sub.string,
with: sub.default
});
}
// User has provided the substitution argument
if (argv[sub.string]) {
const value = argv[sub.string] === true
? ''
: argv[sub.string];
// User has provided the substitution argument with no value
if (value === '') {
(0, _helpers_1.throwError)(`"--${sub.string}" requires a value`);
}
if (sub.valid &&
value !== '' &&
!((_a = sub.valid) === null || _a === void 0 ? void 0 : _a.includes(value))) {
(0, _helpers_1.throwError)(`\n"${value}" is not a valid ${sub.string}. Please use one of the following: \n - ${sub.valid.join(`\n - `)}\n`);
}
// The value provided is valid
substitutions.push({
replace: sub.string,
with: argv[sub.string]
});
}
});
}
return substitutions;
};
const substitute = getSubstitutions(command);
const substitute = command ? (0, _helpers_1.getSubstitutions)(command, argv) : [];
if (command) {
const name = argv._[1].toString();
return (0, build_from_scaffold_1.default)({
Expand Down
3 changes: 1 addition & 2 deletions builda/dist/scripts/build-from-scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildFromScaffold = void 0;
const fs_1 = __importDefault(require("fs"));
const prettier_1 = __importDefault(require("prettier"));
// import helpers
const _helpers_1 = require("../helpers/index.js");
const string_functions_1 = require("../helpers/string-functions");
Expand Down Expand Up @@ -37,7 +36,7 @@ const buildFromScaffold = ({ name, command, substitute }) => {
}
};
// Add a component registry file to the output directory
return fs_1.default.writeFileSync(`${outputDirectory}/registry.json`, prettier_1.default.format(JSON.stringify(componentRegistry), { parser: 'json' }));
return fs_1.default.writeFileSync(`${outputDirectory}/registry.json`, JSON.stringify(componentRegistry, null, 2));
}
throw new Error('No config file found');
};
Expand Down
2 changes: 1 addition & 1 deletion builda/dist/scripts/generate-scaffold-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const generateScaffoldRegistry = (scaffoldPath) => {
]
}
`;
fs_1.default.writeFileSync(`${readPath}/${file}/registry.json`, JSON.stringify(page));
fs_1.default.writeFileSync(`${readPath}/${file}/registry.json`, JSON.stringify(page, null, 2));
}
});
};
Expand Down
1 change: 1 addition & 0 deletions builda/dist/scripts/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const checkExistingConfig = (fileName, debug) => {
});
}
(0, _helpers_1.printMessage)('Starting initialisation...\r', 'success');
(0, _helpers_1.printMessage)(`All answers can be changed later by editing the ${configFileName} file`, 'notice');
return resolve('yes');
});
};
Expand Down
3 changes: 2 additions & 1 deletion builda/src/data/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export default [
{
type: 'checkbox',
name: 'scaffoldSelection',
message: 'Select zero or more scaffold types you wish to use',
message: 'Select one or more scaffold types you wish to use',
default: ['component'],
choices: [
'component',
'atom',
Expand Down
7 changes: 3 additions & 4 deletions builda/src/helpers/get-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ const moduleTypes = ['scaffold', 'prefab'] as ModuleType[];
export const getmodule = (name: string) => {
if (config) {
const moduleList = config.modules;
const moduleCategory = moduleTypes.find(
(category) => moduleList[category] && moduleList?.[category]?.[name]
const moduleType = moduleTypes.find(
(type) => moduleList[type] && moduleList?.[type]?.[name]
);

const path = `${globals.buildaDir}/modules/${moduleCategory}/${name}`;
console.log(path);
const path = `${globals.buildaDir}/modules/${moduleType}/${name}`;
const registry = JSON.parse(fs.readFileSync(`${path}/registry.json`, 'utf8'));
const files = registry.files.filter(
(file: string) => file !== 'registry.json'
Expand Down
Loading

0 comments on commit d1c8d88

Please sign in to comment.