From a205c5b585bdea32d1bc7bf1d7eb42a2559a315d Mon Sep 17 00:00:00 2001 From: MaxGenash Date: Thu, 17 Sep 2020 23:35:17 +0300 Subject: [PATCH] refactor: moved common constants to constants.js --- bin/stencil-bundle.js | 15 +++------ bin/stencil-download.js | 12 +++---- bin/stencil-init.js | 7 ++-- bin/stencil-pull.js | 13 +++----- bin/stencil-push.js | 13 +++----- bin/stencil-release.js | 4 +-- bin/stencil-start.js | 37 ++++++++++------------ bin/stencil.js | 4 +-- constants.js | 21 ++++++++++-- gulpfile.js | 14 +++++--- lib/release/release.js | 22 ++++++------- lib/stencil-init.js | 2 +- lib/stencil-push.utils.js | 21 ++++++------ lib/version-check.js | 8 ++--- server/plugins/renderer/renderer.module.js | 6 ++-- 15 files changed, 100 insertions(+), 99 deletions(-) diff --git a/bin/stencil-bundle.js b/bin/stencil-bundle.js index 919fb4be..038984e7 100755 --- a/bin/stencil-bundle.js +++ b/bin/stencil-bundle.js @@ -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); @@ -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) { diff --git a/bin/stencil-download.js b/bin/stencil-download.js index ae45d8b6..1a0838ec 100644 --- a/bin/stencil-download.js +++ b/bin/stencil-download.js @@ -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); @@ -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, }; diff --git a/bin/stencil-init.js b/bin/stencil-init.js index 156a84b4..90f26451 100755 --- a/bin/stencil-init.js +++ b/bin/stencil-init.js @@ -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') @@ -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); diff --git a/bin/stencil-pull.js b/bin/stencil-pull.js index efe2b375..8cf289d3 100755 --- a/bin/stencil-pull.js +++ b/bin/stencil-pull.js @@ -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); @@ -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, }; diff --git a/bin/stencil-push.js b/bin/stencil-push.js index 4db77070..054c51d8 100755 --- a/bin/stencil-push.js +++ b/bin/stencil-push.js @@ -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') @@ -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, diff --git a/bin/stencil-release.js b/bin/stencil-release.js index 5339adef..f9f8f436 100755 --- a/bin/stencil-release.js +++ b/bin/stencil-release.js @@ -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()) { diff --git a/bin/stencil-start.js b/bin/stencil-start.js index ecbd9e09..f9cda184 100755 --- a/bin/stencil-start.js +++ b/bin/stencil-start.js @@ -11,7 +11,7 @@ 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'); @@ -19,13 +19,8 @@ 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.') @@ -33,6 +28,8 @@ program .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 @@ -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); } @@ -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); @@ -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); @@ -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; @@ -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, @@ -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(); @@ -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'); } @@ -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, @@ -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) { diff --git a/bin/stencil.js b/bin/stencil.js index f0a6603e..f7eea737 100755 --- a/bin/stencil.js +++ b/bin/stencil.js @@ -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.') diff --git a/constants.js b/constants.js index ac6f9f1b..989f7583 100644 --- a/constants.js +++ b/constants.js @@ -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': {}, @@ -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, }; diff --git a/gulpfile.js b/gulpfile.js index 4fd8af91..22d06f6b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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; diff --git a/lib/release/release.js b/lib/release/release.js index 168ec6e0..f5876a72 100644 --- a/lib/release/release.js +++ b/lib/release/release.js @@ -5,9 +5,9 @@ const uuid = require('uuid4'); const fs = require('fs'); const path = require('path'); const util = require('util'); -const themePath = process.cwd(); -const git = require('simple-git/promise')(themePath); -const themeConfig = require('../theme-config').getInstance(themePath); +const { THEME_PATH, DOT_STENCIL_FILE_PATH } = require('../../constants'); +const git = require('simple-git/promise')(THEME_PATH); +const themeConfig = require('../theme-config').getInstance(THEME_PATH); const askQuestions = require('./questions'); const { Octokit } = require('@octokit/rest'); const Bundle = require('../stencil-bundle'); @@ -36,8 +36,8 @@ async function doRelease(options) { // Update changelog and get text for release notes const changelog = parseChangelog(options.version, options.date); - bumpJsonFileVersion(path.join(themePath, 'config.json'), options.version); - bumpJsonFileVersion(path.join(themePath, 'package.json'), options.version); + bumpJsonFileVersion(themeConfig.configPath, options.version); + bumpJsonFileVersion(path.join(THEME_PATH, 'package.json'), options.version); const bundlePath = await bundleTheme(); @@ -102,7 +102,7 @@ function bumpJsonFileVersion(filePath, version) { } function parseChangelog(version, date) { - const filePath = path.join(themePath, 'CHANGELOG.md'); + const filePath = path.join(THEME_PATH, 'CHANGELOG.md'); let changelog = ''; try { @@ -126,25 +126,23 @@ function parseChangelog(version, date) { } function saveGithubToken(githubToken) { - const dotStencilPath = path.join(themePath, '.stencil'); let data = {}; try { - data = JSON.parse(fs.readFileSync(dotStencilPath)); + data = JSON.parse(fs.readFileSync(DOT_STENCIL_FILE_PATH)); } catch (e) { // .stencil file might not exist } data.githubToken = githubToken; - fs.writeFileSync(dotStencilPath, JSON.stringify(data, null, 2) + '\n'); + fs.writeFileSync(DOT_STENCIL_FILE_PATH, JSON.stringify(data, null, 2) + '\n'); } function getGithubToken() { - const dotStencilPath = path.join(themePath, '.stencil'); let data = {}; try { - data = JSON.parse(fs.readFileSync(dotStencilPath)); + data = JSON.parse(fs.readFileSync(DOT_STENCIL_FILE_PATH)); } catch (e) { // .stencil file might not exist } @@ -153,7 +151,7 @@ function getGithubToken() { } async function bundleTheme() { - const bundle = new Bundle(themePath, themeConfig, themeConfig.getRawConfig(), { + const bundle = new Bundle(THEME_PATH, themeConfig, themeConfig.getRawConfig(), { dest: os.tmpdir(), name: uuid(), }); diff --git a/lib/stencil-init.js b/lib/stencil-init.js index 36143b30..e1476af0 100644 --- a/lib/stencil-init.js +++ b/lib/stencil-init.js @@ -7,7 +7,7 @@ const inquirerModule = require('inquirer'); const serverConfigModule = require('../server/config'); const jsonLintModule = require('./json-lint'); -const { DEFAULT_CUSTOM_LAYOUTS_CONFIG } = require("../constants"); +const { DEFAULT_CUSTOM_LAYOUTS_CONFIG } = require('../constants'); class StencilInit { /** diff --git a/lib/stencil-push.utils.js b/lib/stencil-push.utils.js index 0f44759b..93d62a07 100644 --- a/lib/stencil-push.utils.js +++ b/lib/stencil-push.utils.js @@ -1,17 +1,20 @@ 'use strict'; const _ = require('lodash'); const async = require('async'); -const Bundle = require('./stencil-bundle'); -const fs = require('fs'); const Inquirer = require('inquirer'); -const os = require('os'); const ProgressBar = require('progress'); -const themeApiClient = require('./theme-api-client'); -const themePath = process.cwd(); -const themeConfig = require('./theme-config').getInstance(themePath); const uuid = require('uuid4'); -const utils = {}; const Wreck = require('wreck'); +const fs = require('fs'); +const os = require('os'); + +const { THEME_PATH } = require('../constants'); +const Bundle = require('./stencil-bundle'); +const themeApiClient = require('./theme-api-client'); +const ThemeConfig = require('./theme-config'); + +const themeConfig = ThemeConfig.getInstance(THEME_PATH); +const utils = {}; const bar = new ProgressBar('Processing [:bar] :percent; ETA: :etas', { complete: '=', @@ -102,12 +105,12 @@ utils.generateBundle = (options, callback) => { if (options.saveBundleName) { output = { - dest: themePath, + dest: THEME_PATH, name: options.saveBundleName, }; } - bundle = new Bundle(themePath, themeConfig, themeConfig.getRawConfig(), output); + bundle = new Bundle(THEME_PATH, themeConfig, themeConfig.getRawConfig(), output); bundle.initBundle((err, bundleZipPath) => { if (err) { diff --git a/lib/version-check.js b/lib/version-check.js index dbdb6d47..eb347a5a 100644 --- a/lib/version-check.js +++ b/lib/version-check.js @@ -1,11 +1,11 @@ -var pkg = require('../package.json'); -var semver = require('semver'); +const semver = require('semver'); +const { PACKAGE_INFO } = require('../constants'); module.exports = function () { - var satisfies = semver.satisfies(process.versions.node, pkg.engines.node); + const satisfies = semver.satisfies(process.versions.node, PACKAGE_INFO.engines.node); if (!satisfies) { - console.error('You are running an older version of node. Please upgrade to ' + pkg.engines.node); + console.error('You are running an older version of node. Please upgrade to ' + PACKAGE_INFO.engines.node); } return satisfies; diff --git a/server/plugins/renderer/renderer.module.js b/server/plugins/renderer/renderer.module.js index b6279c56..6bce7459 100644 --- a/server/plugins/renderer/renderer.module.js +++ b/server/plugins/renderer/renderer.module.js @@ -11,7 +11,7 @@ const { promisify } = require('util'); const Url = require('url'); const LangAssembler = require('../../../lib/lang-assembler'); -const Pkg = require('../../../package.json'); +const { PACKAGE_INFO } = require('../../../constants'); const Responses = require('./responses/responses'); const TemplateAssembler = require('../../../lib/template-assembler'); const Utils = require('../../lib/utils'); @@ -408,8 +408,8 @@ internals.getHeaders = function (request, options, config) { } const headers = { - 'stencil-cli': Pkg.version, - 'stencil-version': Pkg.config.stencil_version, + 'stencil-cli': PACKAGE_INFO.version, + 'stencil-version': PACKAGE_INFO.config.stencil_version, 'stencil-options': JSON.stringify(_.defaultsDeep(currentOptions, options)), 'accept-encoding': 'identity', };