Skip to content

Commit

Permalink
refactor: moved common constants to constants.js
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGenash committed Sep 17, 2020
1 parent dc3bf29 commit a205c5b
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 99 deletions.
15 changes: 4 additions & 11 deletions bin/stencil-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
require('colors');

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

const pkg = require('../package.json');
const { THEME_PATH, PACKAGE_INFO } = require('../constants');
const ThemeConfig = require('../lib/theme-config');
const Bundle = require('../lib/stencil-bundle');
const versionCheck = require('../lib/version-check');

program
.version(pkg.version)
.version(PACKAGE_INFO.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);
const themeConfig = ThemeConfig.getInstance(THEME_PATH);

if (!versionCheck()) {
process.exit(2);
Expand All @@ -40,12 +38,7 @@ if (!themeConfig.configExists()) {
}

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

bundle.initBundle((err, bundlePath) => {
if (err) {
Expand Down
12 changes: 5 additions & 7 deletions bin/stencil-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ const inquirer = require('inquirer');
const program = require('../lib/commander');
const { promisify } = require("util");

const pkg = require('../package.json');
const { API_HOST, PACKAGE_INFO, DOT_STENCIL_FILE_PATH } = require('../constants');
const stencilDownload = require('../lib/stencil-download');
const versionCheck = require('../lib/version-check');
const themeApiClient = require('../lib/theme-api-client');

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

program
.version(pkg.version)
.option('--host [hostname]', 'specify the api host', defaultApiHost)
.version(PACKAGE_INFO.version)
.option('--host [hostname]', 'specify the api host', API_HOST)
.option('--file [filename]', 'specify the filename to download only')
.option('--exclude [exclude]', 'specify a directory to exclude from download')
.parse(process.argv);
Expand All @@ -26,9 +24,9 @@ if (!versionCheck()) {
const cliOptions = program.opts();
const extraExclude = cliOptions.exclude ? [cliOptions.exclude] : [];
const options = {
dotStencilFilePath: './.stencil',
dotStencilFilePath: DOT_STENCIL_FILE_PATH,
exclude: ['parsed', 'manifest.json', ...extraExclude],
apiHost: cliOptions.host || defaultApiHost,
apiHost: cliOptions.host || API_HOST,
file: cliOptions.file,
};

Expand Down
7 changes: 3 additions & 4 deletions bin/stencil-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
const program = require('../lib/commander');

const StencilInit = require('../lib/stencil-init');
const pkg = require('../package.json');
const { DOT_STENCIL_FILE_PATH, PACKAGE_INFO } = require('../constants');
const versionCheck = require('../lib/version-check');

program
.version(pkg.version)
.version(PACKAGE_INFO.version)
.option('-u, --url [url]', 'Store URL')
.option('-t, --token [token]', 'Access Token')
.option('-p, --port [port]', 'Port')
Expand All @@ -17,7 +17,6 @@ if (!versionCheck()) {
process.exit(2);
}

const dotStencilFilePath = './.stencil';
const cliOptions = program.opts();

new StencilInit().run(dotStencilFilePath, cliOptions);
new StencilInit().run(DOT_STENCIL_FILE_PATH, cliOptions);
13 changes: 5 additions & 8 deletions bin/stencil-pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

require('colors');

const pkg = require('../package.json');
const { DOT_STENCIL_FILE_PATH, PACKAGE_INFO, API_HOST } = require('../constants');
const program = require('../lib/commander');
const stencilPull = require('../lib/stencil-pull');
const versionCheck = require('../lib/version-check');
const themeApiClient = require('../lib/theme-api-client');

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

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

Expand All @@ -23,8 +20,8 @@ if (!versionCheck()) {

const cliOptions = program.opts();
const options = {
dotStencilFilePath,
apiHost: cliOptions.host || defaultApiHost,
dotStencilFilePath: DOT_STENCIL_FILE_PATH,
apiHost: cliOptions.host || API_HOST,
saveConfigName: cliOptions.save,
};

Expand Down
13 changes: 5 additions & 8 deletions bin/stencil-push.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#!/usr/bin/env node

require('colors');
const pkg = require('../package.json');
const { DOT_STENCIL_FILE_PATH, PACKAGE_INFO, API_HOST } = require('../constants');
const program = require('../lib/commander');
const stencilPush = require('../lib/stencil-push');
const versionCheck = require('../lib/version-check');
const themeApiClient = require('../lib/theme-api-client');

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

program
.version(pkg.version)
.option('--host [hostname]', 'specify the api host', defaultApiHost)
.version(PACKAGE_INFO.version)
.option('--host [hostname]', 'specify the api host', API_HOST)
.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 @@ -25,8 +22,8 @@ if (!versionCheck()) {

const cliOptions = program.opts();
const options = {
dotStencilFilePath,
apiHost: cliOptions.host || defaultApiHost,
dotStencilFilePath: DOT_STENCIL_FILE_PATH,
apiHost: cliOptions.host || API_HOST,
bundleZipPath: cliOptions.file,
activate: cliOptions.activate,
saveBundleName: cliOptions.save,
Expand Down
4 changes: 2 additions & 2 deletions bin/stencil-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

require('colors');
const release = require('../lib/release/release');
const pkg = require('../package.json');
const { PACKAGE_INFO } = require('../constants');
const program = require('../lib/commander');
const versionCheck = require('../lib/version-check');

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

if (!versionCheck()) {
Expand Down
37 changes: 16 additions & 21 deletions bin/stencil-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,25 @@ const Url = require('url');

const Cycles = require('../lib/cycles');
const templateAssembler = require('../lib/template-assembler');
const Pkg = require('../package.json');
const { PACKAGE_INFO, DOT_STENCIL_FILE_PATH, THEME_PATH } = require('../constants');
const program = require('../lib/commander');
const Server = require('../server');
const ThemeConfig = require('../lib/theme-config');
const BuildConfigManager = require('../lib/BuildConfigManager');
const jsonLint = require('../lib/json-lint');
const versionCheck = require('../lib/version-check');

const themePath = process.cwd();
const templatePath = Path.join(themePath, 'templates');
const dotStencilFilePath = Path.join(themePath, '.stencil');
const themeConfigPath = Path.join(themePath, 'config.json');

program
.version(Pkg.version)
.version(PACKAGE_INFO.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();
const templatePath = Path.join(THEME_PATH, 'templates');
const themeConfig = ThemeConfig.getInstance(THEME_PATH);

// tunnel value should be true/false or a string with name
// https://browsersync.io/docs/options#option-tunnel
Expand All @@ -44,12 +41,12 @@ if (!versionCheck()) {
process.exit(2);
}

if (!Fs.existsSync(dotStencilFilePath)) {
if (!Fs.existsSync(DOT_STENCIL_FILE_PATH)) {
console.error('Error: Please run'.red + ' $ stencil init'.cyan + ' first.'.red);
process.exit(2);
}

if (!Fs.existsSync(Path.join(themePath, 'config.json'))) {
if (!Fs.existsSync(themeConfig.configPath)) {
console.error('Error: You must have a '.red + 'config.json'.cyan + ' file in your top level theme directory.');
process.exit(2);
}
Expand All @@ -60,8 +57,6 @@ if (cliOptions.variation === true) {
process.exit(2);
}

// Instantiate themeConfig
let themeConfig = ThemeConfig.getInstance(themePath);
if (cliOptions.variation) {
try {
themeConfig.setVariationByName(cliOptions.variation);
Expand All @@ -71,9 +66,9 @@ if (cliOptions.variation) {
}
}

let dotStencilFile = Fs.readFileSync(dotStencilFilePath, { encoding: 'utf-8' });
let dotStencilFile = Fs.readFileSync(DOT_STENCIL_FILE_PATH, { encoding: 'utf-8' });
try {
dotStencilFile = jsonLint.parse(dotStencilFile, dotStencilFilePath);
dotStencilFile = jsonLint.parse(dotStencilFile, DOT_STENCIL_FILE_PATH);
} catch (e) {
console.error(e.stack);
process.exit(2);
Expand All @@ -91,7 +86,7 @@ if (!(dotStencilFile.normalStoreUrl) || !(dotStencilFile.customLayouts)) {

let staplerUrl;
const headers = {
'stencil-cli': Pkg.version,
'stencil-cli': PACKAGE_INFO.version,
};
if (dotStencilFile.staplerUrl) {
staplerUrl = dotStencilFile.staplerUrl;
Expand All @@ -101,7 +96,7 @@ if (dotStencilFile.staplerUrl) {
}

Wreck.get(
Url.resolve(staplerUrl, '/stencil-version-check?v=' + Pkg.version),
Url.resolve(staplerUrl, '/stencil-version-check?v=' + PACKAGE_INFO.version),
{
headers: headers,
json: true,
Expand Down Expand Up @@ -137,7 +132,7 @@ async function startServer() {
dotStencilFile: dotStencilFile,
variationIndex: themeConfig.variationIndex || 0,
useCache: cliOptions.cache,
themePath: themePath,
themePath: THEME_PATH,
});

const buildConfigManger = new BuildConfigManager();
Expand All @@ -156,7 +151,7 @@ async function startServer() {
console.log(getStartUpInfo());

// Watch sccs directory and automatically reload all css files if a file changes
Bs.watch(Path.join(themePath, 'assets/scss'), event => {
Bs.watch(Path.join(THEME_PATH, 'assets/scss'), event => {
if (event === 'change') {
Bs.reload('*.css');
}
Expand Down Expand Up @@ -206,10 +201,10 @@ async function startServer() {
Bs.init({
open: !!cliOptions.open,
port: browserSyncPort,
files: watchFiles.map(val => Path.join(themePath, val)),
files: watchFiles.map(val => Path.join(THEME_PATH, val)),
watchOptions: {
ignoreInitial: true,
ignored: watchIgnored.map(val => Path.join(themePath, val)),
ignored: watchIgnored.map(val => Path.join(THEME_PATH, val)),
},
proxy: "localhost:" + stencilServerPort,
tunnel,
Expand Down Expand Up @@ -262,8 +257,8 @@ function getStartUpInfo() {

information += '-----------------Startup Information-------------\n'.gray;
information += '\n';
information += '.stencil location: ' + dotStencilFilePath.cyan + '\n';
information += 'config.json location: ' + themeConfigPath.cyan + '\n';
information += '.stencil location: ' + DOT_STENCIL_FILE_PATH.cyan + '\n';
information += 'config.json location: ' + themeConfig.configPath.cyan + '\n';
information += 'Store URL: ' + dotStencilFile.normalStoreUrl.cyan + '\n';

if (dotStencilFile.staplerUrl) {
Expand Down
4 changes: 2 additions & 2 deletions bin/stencil.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env node

const program = require('../lib/commander');
const pkg = require('../package.json');
const { PACKAGE_INFO } = require('../constants');

program
.version(pkg.version)
.version(PACKAGE_INFO.version)
.command('init', 'Interactively create a .stencil file which configures how to run a BigCommerce store locally.')
.command('start', 'Starts up BigCommerce store using theme files in the current directory.')
.command('bundle', 'Bundles up the theme into a zip file which can be uploaded to BigCommerce.')
Expand Down
21 changes: 18 additions & 3 deletions constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
const path = require('path');
const packagePath = path.join(process.cwd(), 'package.json');
const packageInfo = require(packagePath);

///////////////////////////////////////// Stencil CLI /////////////////////////////////////////

const PACKAGE_INFO = require('./package.json');

/////////////////////////////////////////// Themes ///////////////////////////////////////////

const THEME_PATH = process.cwd();

const DOT_STENCIL_FILE_PATH = path.join(THEME_PATH, '.stencil');

const DEFAULT_CUSTOM_LAYOUTS_CONFIG = {
'brand': {},
Expand All @@ -9,7 +17,14 @@ const DEFAULT_CUSTOM_LAYOUTS_CONFIG = {
'product': {},
};

//////////////////////////////////////////// Other ////////////////////////////////////////////

const API_HOST = 'https://api.bigcommerce.com';

module.exports = {
packageInfo,
PACKAGE_INFO,
THEME_PATH,
DOT_STENCIL_FILE_PATH,
DEFAULT_CUSTOM_LAYOUTS_CONFIG,
API_HOST,
};
14 changes: 10 additions & 4 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
/**
* These gulp tasks are used to release stencil-cli itself
* (in contrast with /bin/stencil-release.js which is used to release themes)
*/

'use strict';
require('colors');
require('path');
const bump = require('gulp-bump');
const changelog = require('./tasks/changelog');
const constants = require('./constants');
const currentVersion = constants.packageInfo.version;
const exec = require('gulp-exec');
const git = require('gulp-git-streamed');
const gulp = require('gulp');
const gulpif = require('gulp-if');
const gutil = require('gulp-util');
const supportedLockFileVersion = [1];
const prompt = require('gulp-prompt');
const semver = require('semver');

const changelog = require('./tasks/changelog');
const { PACKAGE_INFO } = require('./constants');

const currentVersion = PACKAGE_INFO.version;
const supportedLockFileVersion = [1];
let branch;
let remote;
let responses;
Expand Down
Loading

0 comments on commit a205c5b

Please sign in to comment.