Skip to content

Commit

Permalink
refactor: (strf-8747) move code dealing with .stencil file into a sep…
Browse files Browse the repository at this point in the history
…arate class
  • Loading branch information
MaxGenash committed Oct 28, 2020
1 parent 5d8cd14 commit 7b371e8
Show file tree
Hide file tree
Showing 18 changed files with 342 additions and 400 deletions.
3 changes: 1 addition & 2 deletions bin/stencil-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require('colors');
const inquirer = require('inquirer');
const program = require('../lib/commander');

const { API_HOST, PACKAGE_INFO, DOT_STENCIL_FILE_PATH } = require('../constants');
const { API_HOST, PACKAGE_INFO } = require('../constants');
const stencilDownload = require('../lib/stencil-download');
const { checkNodeVersion } = require('../lib/cliCommon');
const { printCliResultErrorAndExit } = require('../lib/cliCommon');
Expand All @@ -22,7 +22,6 @@ checkNodeVersion();
const cliOptions = program.opts();
const extraExclude = cliOptions.exclude ? [cliOptions.exclude] : [];
const options = {
dotStencilFilePath: DOT_STENCIL_FILE_PATH,
exclude: ['parsed', 'manifest.json', ...extraExclude],
apiHost: cliOptions.host || API_HOST,
channelId: cliOptions.channel_id || 1,
Expand Down
16 changes: 9 additions & 7 deletions bin/stencil-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
const program = require('../lib/commander');

const StencilInit = require('../lib/stencil-init');
const { DOT_STENCIL_FILE_PATH, PACKAGE_INFO } = require('../constants');
const { checkNodeVersion } = require('../lib/cliCommon');
const { PACKAGE_INFO } = require('../constants');
const { checkNodeVersion, printCliResultErrorAndExit } = require('../lib/cliCommon');

program
.version(PACKAGE_INFO.version)
Expand All @@ -17,8 +17,10 @@ checkNodeVersion();

const cliOptions = program.opts();

new StencilInit().run(DOT_STENCIL_FILE_PATH, {
normalStoreUrl: cliOptions.url,
accessToken: cliOptions.token,
port: cliOptions.port,
});
new StencilInit()
.run({
normalStoreUrl: cliOptions.url,
accessToken: cliOptions.token,
port: cliOptions.port,
})
.catch(printCliResultErrorAndExit);
3 changes: 1 addition & 2 deletions bin/stencil-pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require('colors');

const { DOT_STENCIL_FILE_PATH, PACKAGE_INFO, API_HOST } = require('../constants');
const { PACKAGE_INFO, API_HOST } = require('../constants');
const program = require('../lib/commander');
const stencilPull = require('../lib/stencil-pull');
const { checkNodeVersion } = require('../lib/cliCommon');
Expand All @@ -28,7 +28,6 @@ checkNodeVersion();

const cliOptions = program.opts();
const options = {
dotStencilFilePath: DOT_STENCIL_FILE_PATH,
apiHost: cliOptions.host || API_HOST,
saveConfigName: cliOptions.filename,
channelId: cliOptions.channel_id || 1,
Expand Down
3 changes: 1 addition & 2 deletions bin/stencil-push.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

require('colors');
const { DOT_STENCIL_FILE_PATH, PACKAGE_INFO, API_HOST } = require('../constants');
const { PACKAGE_INFO, API_HOST } = require('../constants');
const program = require('../lib/commander');
const stencilPush = require('../lib/stencil-push');
const { checkNodeVersion } = require('../lib/cliCommon');
Expand All @@ -20,7 +20,6 @@ checkNodeVersion();

const cliOptions = program.opts();
const options = {
dotStencilFilePath: DOT_STENCIL_FILE_PATH,
apiHost: cliOptions.host || API_HOST,
bundleZipPath: cliOptions.file,
activate: cliOptions.activate,
Expand Down
6 changes: 2 additions & 4 deletions bin/stencil-start.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

require('colors');
const { PACKAGE_INFO, DOT_STENCIL_FILE_PATH } = require('../constants');
const { PACKAGE_INFO } = require('../constants');
const program = require('../lib/commander');
const StencilStart = require('../lib/stencil-start');
const { printCliResultErrorAndExit } = require('../lib/cliCommon');
Expand All @@ -20,6 +20,4 @@ program
)
.parse(process.argv);

new StencilStart()
.run(program.opts(), DOT_STENCIL_FILE_PATH, PACKAGE_INFO.version)
.catch(printCliResultErrorAndExit);
new StencilStart().run(program.opts(), PACKAGE_INFO.version).catch(printCliResultErrorAndExit);
5 changes: 0 additions & 5 deletions constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const path = require('path');

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

const PACKAGE_INFO = require('./package.json');
Expand All @@ -8,8 +6,6 @@ const PACKAGE_INFO = require('./package.json');

const THEME_PATH = process.cwd();

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

const DEFAULT_CUSTOM_LAYOUTS_CONFIG = {
brand: {},
category: {},
Expand All @@ -24,7 +20,6 @@ const API_HOST = 'https://api.bigcommerce.com';
module.exports = {
PACKAGE_INFO,
THEME_PATH,
DOT_STENCIL_FILE_PATH,
DEFAULT_CUSTOM_LAYOUTS_CONFIG,
API_HOST,
};
8 changes: 5 additions & 3 deletions lib/BuildConfigManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const { fork } = require('child_process');
const path = require('path');
const fsModule = require('fs');

const { THEME_PATH } = require('../constants');

class BuildConfigManager {
constructor({ workDir = process.cwd(), fs = fsModule } = {}) {
this.CONFIG_FILE_NAME = 'stencil.conf.js';
constructor({ workDir = THEME_PATH, fs = fsModule } = {}) {
this.configFileName = 'stencil.conf.js';

this._workDir = workDir;
this._buildConfigPath = path.join(workDir, this.CONFIG_FILE_NAME);
this._buildConfigPath = path.join(workDir, this.configFileName);
this._fs = fs;
this._onReadyCallbacks = [];
this._worker = null;
Expand Down
43 changes: 43 additions & 0 deletions lib/StencilConfigManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require('colors');
const fsModule = require('fs');
const path = require('path');

const fsUtilsModule = require('./utils/fsUtils');
const { THEME_PATH } = require('../constants');

class StencilConfigManager {
constructor({ themePath = THEME_PATH, fs = fsModule, fsUtils = fsUtilsModule } = {}) {
this.configFileName = '.stencil';

this.themePath = themePath;
this.configPath = path.join(themePath, this.configFileName);

this._fs = fs;
this._fsUtils = fsUtils;
}

/**
* @returns {object|null}
* @param {boolean} ignoreFileNotExists
*/
async readStencilConfig(ignoreFileNotExists = false) {
if (this._fs.existsSync(this.configPath)) {
return this._fsUtils.parseJsonFile(this.configPath);
}

if (ignoreFileNotExists) {
return null;
}

throw new Error('Please run'.red + ' $ stencil init'.cyan + ' first.'.red);
}

/**
* @param {object} config
*/
saveStencilConfig(config) {
this._fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2));
}
}

module.exports = StencilConfigManager;
36 changes: 18 additions & 18 deletions lib/release/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,28 @@ const fs = require('fs');
const path = require('path');
const { Octokit } = require('@octokit/rest');
const simpleGit = require('simple-git');

const ThemeConfig = require('../theme-config');
const askQuestions = require('./questions');
const Bundle = require('../stencil-bundle');
const StencilConfigManager = require('../StencilConfigManager');
const { parseJsonFile } = require('../utils/fsUtils');
const { THEME_PATH, DOT_STENCIL_FILE_PATH } = require('../../constants');
const { THEME_PATH } = require('../../constants');

const git = simpleGit(THEME_PATH);
const themeConfig = ThemeConfig.getInstance(THEME_PATH);
const themeConfigManager = ThemeConfig.getInstance(THEME_PATH);
const stencilConfigManager = new StencilConfigManager();

async function saveGithubToken(githubToken) {
let data = {};

if (fs.existsSync(DOT_STENCIL_FILE_PATH)) {
data = await parseJsonFile(DOT_STENCIL_FILE_PATH);
}
const data = (await stencilConfigManager.readStencilConfig(true)) || {};

data.githubToken = githubToken;
await fs.promises.writeFile(DOT_STENCIL_FILE_PATH, `${JSON.stringify(data, null, 2)}\n`);

await this.stencilConfigManager.saveStencilConfig(data);
}

async function getGithubToken() {
let data = {};

if (fs.existsSync(DOT_STENCIL_FILE_PATH)) {
data = await parseJsonFile(DOT_STENCIL_FILE_PATH);
}
const data = (await stencilConfigManager.readStencilConfig(true)) || {};

return data.githubToken;
}
Expand Down Expand Up @@ -58,7 +54,7 @@ async function createGithubRelease(commit, version, changelog, remote, bundlePat
console.log('Creating Github Release...');

const release = await github.repos.createRelease(releaseParams);
const themeName = await themeConfig.getName();
const themeName = await themeConfigManager.getName();

const uploadParams = {
release_id: release.data.id,
Expand Down Expand Up @@ -108,12 +104,12 @@ async function parseChangelog(version, date) {
}

async function bundleTheme() {
const rawConfig = await themeConfig.getRawConfig();
const rawConfig = await themeConfigManager.getRawConfig();
const bundleOptions = {
dest: os.tmpdir(),
name: uuid(),
};
const bundle = new Bundle(THEME_PATH, themeConfig, rawConfig, bundleOptions);
const bundle = new Bundle(THEME_PATH, themeConfigManager, rawConfig, bundleOptions);

return bundle.initBundle();
}
Expand Down Expand Up @@ -206,7 +202,7 @@ async function doRelease(options) {
// Update changelog and get text for release notes
const changelog = await parseChangelog(options.version, options.date);

await bumpJsonFileVersion(themeConfig.configPath, options.version);
await bumpJsonFileVersion(themeConfigManager.configPath, options.version);
await bumpJsonFileVersion(path.join(THEME_PATH, 'package.json'), options.version);

const bundlePath = await bundleTheme();
Expand Down Expand Up @@ -236,7 +232,11 @@ async function run() {
}

try {
const answers = await askQuestions(themeConfig, await getGithubToken(), gitData.remotes);
const answers = await askQuestions(
themeConfigManager,
await getGithubToken(),
gitData.remotes,
);

await saveGithubToken(answers.githubToken);

Expand Down
Loading

0 comments on commit 7b371e8

Please sign in to comment.