Skip to content

Commit

Permalink
Create cli
Browse files Browse the repository at this point in the history
Remove initOpts
Add defualt opts to argv
  • Loading branch information
jalal246 committed Apr 2, 2020
1 parent 3fd94bb commit 1ebc69e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 46 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "builderz",
"version": "0.3.0",
"main": "./dist/builderz.cjs",
"bin": "./dist/cli.js",
"repository": "https://github.com/jalal246/builderz.git",
"author": "Jalal Maskoun <jimmy002020@gmail.com>",
"license": "LGPL-3.0",
Expand Down
39 changes: 1 addition & 38 deletions src/builderz.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import { getInput, getOutput } from "./config/index";
import { camelizeOutputBuild, getBundleOpt } from "./utils";
import resolveArgs from "./resolveArgs";

/**
* Invoking resolveArgs inside `start` won't work.
*/
const globalArgs = resolveArgs();

/**
* Write bundle.
*
Expand All @@ -38,39 +33,7 @@ async function build(inputOptions, outputOptions) {
}
}

/**
* Inits global options
*
* @param {Object} opt1 - cli options
* @param {Object} opt2 - api options
* @returns {Object} - final global opts
*/
function initOpts(opt1, opt2) {
const options = {
isSilent: true,
formats: [],
minify: false,
buildName: "dist",
pkgPaths: [],
pkgNames: [],
alias: []
};

Object.keys(options).forEach(option => {
// eslint-disable-next-line no-underscore-dangle
const _default = options[option];

const value = opt1[option] || opt2[option] || _default;

options[option] = value;
});

return options;
}

async function start(params = {}) {
const generalOpts = initOpts(params, globalArgs);

async function start(generalOpts) {
const { buildName, pkgPaths, pkgNames } = generalOpts;

const { json: allPkgJson, pkgInfo: allPkgInfo } =
Expand Down
17 changes: 17 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env node

import error from "@mytools/print";
import builderz from "./builderz";
import resolveArgs from "./resolveArgs";

const globalArgs = resolveArgs();

function start() {
try {
builderz(globalArgs);
} catch (err) {
error(err);
}
}

start();
5 changes: 4 additions & 1 deletion src/config/input/getInputPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ function getPlugins({ IS_SILENT = true, IS_PROD = true, BUILD_FORMAT, alias }) {
* Locates modules using the Node resolution algorithm, for using third
* party modules in node_modules.
*/
resolve({ extensions: [".mjs", ".js", ".jsx", ".json", ".node"] }),
resolve({
preferBuiltins: true,
extensions: [".mjs", ".js", ".jsx", ".json", ".node"]
}),

/**
* Converts .json files to ES6 modules.
Expand Down
17 changes: 10 additions & 7 deletions src/resolveArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,27 @@ function extractAlias(aliasStr) {

function resolveArgs(argv) {
program
.option("-s, --silent <boolean>", "Silent mode, mutes build massages")
.option("-f, --formats <list>", "Specific build format", string2Arr)
.option("-s, --silent <boolean>", "Silent mode, mutes build massages", true)
.option("-f, --formats <list>", "Specific build format", string2Arr, [])
.option(
"-m, --minify <boolean>",
"Minify bundle works only if format is provided"
"Minify bundle works only if format is provided",
false
)
.option("-b, --build-name <string>", "Specific build name")
.option("-b, --build-name <string>", "Specific build name", "dist")
.option(
"-w, --pkg-paths <list>",
"Provide custom paths not in the root/src",
string2Arr
string2Arr,
[]
)
.option(
"-n, --pkg-names <list>",
"Building specific package[s], in workspace",
string2Arr
string2Arr,
[]
)
.option("-a, --alias <list>", "Package Alias", extractAlias, "");
.option("-a, --alias <list>", "Package Alias", extractAlias, []);

if (argv) {
program.allowUnknownOption();
Expand Down

0 comments on commit 1ebc69e

Please sign in to comment.