UNPKG

ffbt

Version:

Build a Typescript app without pain

88 lines (87 loc) 3.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseWebpackCommand = void 0; const tslib_1 = require("tslib"); const command_1 = require("@oclif/command"); const base_command_1 = require("./base-command"); const project_config_1 = require("../project-config"); const lodash_1 = require("lodash"); const path = require("path"); const config_1 = require("../services/webpack/config"); var Arguments; (function (Arguments) { Arguments["sourcesDirectory"] = "sources_directory"; })(Arguments || (Arguments = {})); /** * Parent class to run any webpack command. Just extend from it and enjoy */ class BaseWebpackCommand extends base_command_1.BaseCommand { constructor() { super(...arguments); this.isFalseOrNil = (value) => { return value === null || value === undefined || value === false; }; } run() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const flags = this.getFlags(); const sourcesDirectory = this.getAbsoluteSourcesDirectory(this.getSourcesDirectory()); const environment = flags.env || this.getDefaultEnvironment(); const projectConfig = this.createProjectConfig(sourcesDirectory, environment, flags); const webpackConfig = config_1.createWebpackConfig(projectConfig, sourcesDirectory); this.printBannerWithBuildInfo(projectConfig, flags.verbose); this.getWebpackRunner(webpackConfig).run(); }); } getSourcesDirectory() { const args = this.getArguments(); return args[Arguments.sourcesDirectory]; } createProjectConfig(sourcesDir, environmentName, flags) { const projectConfig = project_config_1.ProjectConfig.loadFromFile(sourcesDir); projectConfig.setCurrentEnvironmentName(environmentName); const flagsMappedToEnvFields = { outputPath: flags.output, analyzeBundle: flags.analyze, buildVersion: flags.buildVersion, verboseMode: flags.verbose, }; const optionsWithValue = lodash_1.omitBy(flagsMappedToEnvFields, this.isFalseOrNil); projectConfig.overrideEnvironmentSettings(optionsWithValue); return projectConfig; } getAbsoluteSourcesDirectory(workdirPath) { return workdirPath ? path.resolve(process.cwd(), workdirPath) : process.cwd(); } printBannerWithBuildInfo(projectConfig, verbose) { console.log("Environment: " + projectConfig.env._name); console.log("Source Directory: " + projectConfig.paths.projectWorkingDirectory); if (verbose) { console.log("Current environment", projectConfig.env, "\n"); console.log("Paths", projectConfig.paths.getAll(), "\n"); } } } exports.BaseWebpackCommand = BaseWebpackCommand; BaseWebpackCommand.args = [ { name: Arguments.sourcesDirectory, description: "directory with sources of the application", required: false, } ]; BaseWebpackCommand.flags = Object.assign({ env: command_1.flags.string({ default: undefined, description: "environment name", }), output: command_1.flags.string({ default: undefined, description: "a directory where to put the bundled app", }), buildVersion: command_1.flags.string({ default: undefined, description: "string represents version of the build. Can be used for CI needs", }), analyze: command_1.flags.boolean({ default: false, description: "run bundle analyze tools", }) }, base_command_1.BaseCommand.flags);