Skip to content

Commit

Permalink
feat: (strf-8673) update "commander"
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGenash committed Sep 17, 2020
1 parent 90b7605 commit dc3bf29
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 76 deletions.
43 changes: 21 additions & 22 deletions bin/stencil-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,34 @@

require('colors');

var Program = require('commander');
var ThemeConfig = require('../lib/theme-config');
var pkg = require('../package.json');
var themePath = process.cwd();
var configuration;
var bundle;
var Bundle = require('../lib/stencil-bundle');
var themeConfig;
var versionCheck = require('../lib/version-check');

Program
const program = require('../lib/commander');

const pkg = require('../package.json');
const ThemeConfig = require('../lib/theme-config');
const Bundle = require('../lib/stencil-bundle');
const versionCheck = require('../lib/version-check');

program
.version(pkg.version)
.option('-d, --dest [dest]', 'Where to save the zip file. It defaults to the current directory you are in when bundling')
.option('-n, --name [filename]', 'What do you want to call the zip file. It defaults to stencil-bundle.zip')
.option('-m, --marketplace', 'Runs extra bundle validations for partners who can create marketplace themes')
.parse(process.argv);

const cliOptions = program.opts();
const themePath = process.cwd();
const themeConfig = ThemeConfig.getInstance(themePath);

if (!versionCheck()) {
process.exit(2);
}

themeConfig = ThemeConfig.getInstance(themePath);

if (Program.dest === true) {
if (cliOptions.dest === true) {
console.error('Error: You have to specify a value for -d or --dest'.red);
process.exit(2);
}

if (Program.name === true) {
if (cliOptions.name === true) {
console.error('Error: You have to specify a value for -n or --name'.red);
process.exit(2);
}
Expand All @@ -40,13 +39,13 @@ if (!themeConfig.configExists()) {
process.exit(2);
}

configuration = themeConfig.getRawConfig();

bundle = new Bundle(themePath, themeConfig, configuration, {
marketplace: Program.marketplace,
dest: Program.dest,
name: Program.name,
});
const rawConfig = themeConfig.getRawConfig();
const bundleOptions = {
marketplace: cliOptions.marketplace,
dest: cliOptions.dest,
name: cliOptions.name,
};
const bundle = new Bundle(themePath, themeConfig, rawConfig, bundleOptions);

bundle.initBundle((err, bundlePath) => {
if (err) {
Expand Down
15 changes: 8 additions & 7 deletions bin/stencil-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

require('colors');
const inquirer = require('inquirer');
const Program = require('commander');
const program = require('../lib/commander');
const { promisify } = require("util");

const pkg = require('../package.json');
const stencilDownload = require('../lib/stencil-download');
const versionCheck = require('../lib/version-check');
const themeApiClient = require('../lib/theme-api-client');

const apiHost = 'https://api.bigcommerce.com';
const defaultApiHost = 'https://api.bigcommerce.com';

Program
program
.version(pkg.version)
.option('--host [hostname]', 'specify the api host', apiHost)
.option('--host [hostname]', 'specify the api host', defaultApiHost)
.option('--file [filename]', 'specify the filename to download only')
.option('--exclude [exclude]', 'specify a directory to exclude from download')
.parse(process.argv);
Expand All @@ -23,12 +23,13 @@ if (!versionCheck()) {
process.exit(2);
}

const extraExclude = Program.exclude ? [Program.exclude] : [];
const cliOptions = program.opts();
const extraExclude = cliOptions.exclude ? [cliOptions.exclude] : [];
const options = {
dotStencilFilePath: './.stencil',
exclude: ['parsed', 'manifest.json', ...extraExclude],
apiHost: Program.host || apiHost,
file: Program.file,
apiHost: cliOptions.host || defaultApiHost,
file: cliOptions.file,
};

run(options);
Expand Down
8 changes: 3 additions & 5 deletions bin/stencil-init.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#!/usr/bin/env node

require('colors');
const Program = require('commander');
const _ = require('lodash');
const program = require('../lib/commander');

const StencilInit = require('../lib/stencil-init');
const pkg = require('../package.json');
const versionCheck = require('../lib/version-check');

Program
program
.version(pkg.version)
.option('-u, --url [url]', 'Store URL')
.option('-t, --token [token]', 'Access Token')
Expand All @@ -20,6 +18,6 @@ if (!versionCheck()) {
}

const dotStencilFilePath = './.stencil';
const cliOptions = _.pick(Program, ['url', 'token', 'port']);
const cliOptions = program.opts();

new StencilInit().run(dotStencilFilePath, cliOptions);
25 changes: 15 additions & 10 deletions bin/stencil-pull.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
#!/usr/bin/env node

require('colors');
const apiHost = 'https://api.bigcommerce.com';
const dotStencilFilePath = './.stencil';
const options = { dotStencilFilePath };

const pkg = require('../package.json');
const Program = require('commander');
const program = require('../lib/commander');
const stencilPull = require('../lib/stencil-pull');
const versionCheck = require('../lib/version-check');
const themeApiClient = require('../lib/theme-api-client');

Program
const dotStencilFilePath = './.stencil';
const defaultApiHost = 'https://api.bigcommerce.com';

program
.version(pkg.version)
.option('--host [hostname]', 'specify the api host', apiHost)
.option('--host [hostname]', 'specify the api host', defaultApiHost)
.option('--save [filename]', 'specify the filename to save the config as', 'config.json')
.parse(process.argv);

if (!versionCheck()) {
process.exit(2);
}

stencilPull(Object.assign({}, options, {
apiHost: Program.host || apiHost,
saveConfigName: Program.save,
}), (err, result) => {
const cliOptions = program.opts();
const options = {
dotStencilFilePath,
apiHost: cliOptions.host || defaultApiHost,
saveConfigName: cliOptions.save,
};

stencilPull(options, (err, result) => {
if (err) {
console.log("\n\n" + 'not ok'.red + ` -- ${err} see details below:`);
themeApiClient.printErrorMessages(err.messages);
Expand Down
29 changes: 16 additions & 13 deletions bin/stencil-push.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/bin/env node

require('colors');
const apiHost = 'https://api.bigcommerce.com';
const dotStencilFilePath = './.stencil';
const options = { dotStencilFilePath };
const pkg = require('../package.json');
const Program = require('commander');
const program = require('../lib/commander');
const stencilPush = require('../lib/stencil-push');
const versionCheck = require('../lib/version-check');
const themeApiClient = require('../lib/theme-api-client');

Program
const defaultApiHost = 'https://api.bigcommerce.com';
const dotStencilFilePath = './.stencil';

program
.version(pkg.version)
.option('--host [hostname]', 'specify the api host', apiHost)
.option('--host [hostname]', 'specify the api host', defaultApiHost)
.option('-f, --file [filename]', 'specify the filename of the bundle to upload')
.option('-s, --save [filename]', 'specify the filename to save the bundle as')
.option('-a, --activate [variationname]', 'specify the variation of the theme to activate')
Expand All @@ -23,13 +23,16 @@ if (!versionCheck()) {
process.exit(2);
}

stencilPush(Object.assign({}, options, {
apiHost: Program.host || apiHost,
bundleZipPath: Program.file,
activate: Program.activate,
saveBundleName: Program.save,
deleteOldest: Program.delete,
}), (err, result) => {
const cliOptions = program.opts();
const options = {
dotStencilFilePath,
apiHost: cliOptions.host || defaultApiHost,
bundleZipPath: cliOptions.file,
activate: cliOptions.activate,
saveBundleName: cliOptions.save,
deleteOldest: cliOptions.delete,
};
stencilPush(options, (err, result) => {
if (err) {
console.log("\n\n" + 'not ok'.red + ` -- ${err} see details below:`);
themeApiClient.printErrorMessages(err.messages);
Expand Down
4 changes: 2 additions & 2 deletions bin/stencil-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
require('colors');
const release = require('../lib/release/release');
const pkg = require('../package.json');
const Program = require('commander');
const program = require('../lib/commander');
const versionCheck = require('../lib/version-check');

Program
program
.version(pkg.version)
.parse(process.argv);

Expand Down
24 changes: 13 additions & 11 deletions bin/stencil-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Url = require('url');
const Cycles = require('../lib/cycles');
const templateAssembler = require('../lib/template-assembler');
const Pkg = require('../package.json');
const Program = require('commander');
const program = require('../lib/commander');
const Server = require('../server');
const ThemeConfig = require('../lib/theme-config');
const BuildConfigManager = require('../lib/BuildConfigManager');
Expand All @@ -24,19 +24,21 @@ const templatePath = Path.join(themePath, 'templates');
const dotStencilFilePath = Path.join(themePath, '.stencil');
const themeConfigPath = Path.join(themePath, 'config.json');

Program
program
.version(Pkg.version)
.option('-o, --open', 'Automatically open default browser')
.option('-v, --variation [name]', 'Set which theme variation to use while developing')
.option('--tunnel [name]', 'Create a tunnel URL which points to your local server that anyone can use.')
.option('-n, --no-cache', 'Turns off caching for API resource data per storefront page. The cache lasts for 5 minutes before automatically refreshing.')
.parse(process.argv);

const cliOptions = program.opts();

// tunnel value should be true/false or a string with name
// https://browsersync.io/docs/options#option-tunnel
const tunnel = typeof Program.tunnel === 'string'
? Program.tunnel
: Boolean(Program.tunnel); // convert undefined/true -> false/true
const tunnel = typeof cliOptions.tunnel === 'string'
? cliOptions.tunnel
: Boolean(cliOptions.tunnel); // convert undefined/true -> false/true

if (!versionCheck()) {
process.exit(2);
Expand All @@ -53,18 +55,18 @@ if (!Fs.existsSync(Path.join(themePath, 'config.json'))) {
}

// If the value is true it means that no variation was passed in.
if (Program.variation === true) {
if (cliOptions.variation === true) {
console.error('Error: You have to specify a value for -v or --variation'.red);
process.exit(2);
}

// Instantiate themeConfig
let themeConfig = ThemeConfig.getInstance(themePath);
if (Program.variation) {
if (cliOptions.variation) {
try {
themeConfig.setVariationByName(Program.variation);
themeConfig.setVariationByName(cliOptions.variation);
} catch (err) {
console.error('Error: The variation '.red + Program.variation + ' does not exists in your config.json file'.red);
console.error('Error: The variation '.red + cliOptions.variation + ' does not exists in your config.json file'.red);
process.exit(2);
}
}
Expand Down Expand Up @@ -134,7 +136,7 @@ async function startServer() {
await Server.create({
dotStencilFile: dotStencilFile,
variationIndex: themeConfig.variationIndex || 0,
useCache: Program.cache,
useCache: cliOptions.cache,
themePath: themePath,
});

Expand Down Expand Up @@ -202,7 +204,7 @@ async function startServer() {
}

Bs.init({
open: !!Program.open,
open: !!cliOptions.open,
port: browserSyncPort,
files: watchFiles.map(val => Path.join(themePath, val)),
watchOptions: {
Expand Down
4 changes: 2 additions & 2 deletions bin/stencil.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

var program = require('commander'),
pkg = require('../package.json');
const program = require('../lib/commander');
const pkg = require('../package.json');

program
.version(pkg.version)
Expand Down
8 changes: 8 additions & 0 deletions lib/commander.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const program = require('commander');

program
// Avoid option name clashes https://www.npmjs.com/package/commander#avoiding-option-name-clashes
.storeOptionsAsProperties(false)
.passCommandToAction(false);

module.exports = program;
3 changes: 3 additions & 0 deletions lib/stencil-init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
'use strict';

require('colors');

const fsModule = require('fs');
const inquirerModule = require('inquirer');

Expand Down
Loading

0 comments on commit dc3bf29

Please sign in to comment.