Skip to content

Commit

Permalink
feat: strf-9332 added timeout parameter for stencil bundle command
Browse files Browse the repository at this point in the history
  • Loading branch information
jairo-bc committed Sep 15, 2021
1 parent 282ff9a commit 9c762fa
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
16 changes: 15 additions & 1 deletion bin/stencil-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const ThemeConfig = require('../lib/theme-config');
const Bundle = require('../lib/stencil-bundle');
const { printCliResultErrorAndExit } = require('../lib/cliCommon');
const { checkNodeVersion } = require('../lib/cliCommon');
const BuildConfigManager = require('../lib/BuildConfigManager');

program
.version(PACKAGE_INFO.version)
Expand All @@ -23,6 +24,11 @@ program
'-m, --marketplace',
'Runs extra bundle validations for partners who can create marketplace themes',
)
.option(
'-t, --timeout [timeout]',
'Set a timeout for the bundle operation. Default is 20 secs',
'60',
)
.parse(process.argv);

const cliOptions = program.opts();
Expand All @@ -49,7 +55,15 @@ async function run() {
}

const rawConfig = await themeConfig.getRawConfig();
const bundle = new Bundle(THEME_PATH, themeConfig, rawConfig, cliOptions);
const timeout = cliOptions.timeout * 1000; // seconds
const buildConfigManager = new BuildConfigManager({ timeout });
const bundle = new Bundle(
THEME_PATH,
themeConfig,
rawConfig,
cliOptions,
buildConfigManager,
);

const bundlePath = await bundle.initBundle();

Expand Down
6 changes: 5 additions & 1 deletion bin/stencil-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { PACKAGE_INFO } = require('../constants');
const program = require('../lib/commander');
const StencilStart = require('../lib/stencil-start');
const { printCliResultErrorAndExit } = require('../lib/cliCommon');
const BuildConfigManager = require('../lib/BuildConfigManager');

program
.version(PACKAGE_INFO.version)
Expand All @@ -20,6 +21,7 @@ program
'-n, --no-cache',
'Turns off caching for API resource data per storefront page. The cache lasts for 5 minutes before automatically refreshing.',
)
.option('-t, --timeout', 'Set a timeout for the bundle operation. Default is 20 secs', '60')
.parse(process.argv);

const cliOptions = program.opts();
Expand All @@ -33,4 +35,6 @@ const options = {
cache: cliOptions.cache,
};

new StencilStart().run(options).catch(printCliResultErrorAndExit);
const timeout = cliOptions.timeout * 1000; // seconds
const buildConfigManager = new BuildConfigManager({ timeout });
new StencilStart({ buildConfigManager }).run(options).catch(printCliResultErrorAndExit);
13 changes: 9 additions & 4 deletions lib/BuildConfigManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fsModule = require('fs');
const { THEME_PATH } = require('../constants');

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

this._workDir = workDir;
Expand All @@ -15,6 +15,7 @@ class BuildConfigManager {
this._onReadyCallbacks = [];
this._worker = null;
this._workerIsReady = false;
this.timeout = timeout;

const config = this._getConfig(this._buildConfigPath);

Expand Down Expand Up @@ -81,13 +82,17 @@ class BuildConfigManager {
return;
}

const timeout = setTimeout(() => {
const timeoutId = setTimeout(() => {
this.stopWorker();
console.log(
'The process was timed out. Try to increase it by providing --timeout [number] option'
.yellow,
);
callback('worker timed out');
}, 20000);
}, this.timeout);

this._onWorkerReady(() => {
clearTimeout(timeout);
clearTimeout(timeoutId);
// send a message to the worker to start bundling js
this._worker.send('production');
this._worker.on('message', (message) => {
Expand Down
6 changes: 3 additions & 3 deletions lib/stencil-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Bundle {
themeConfig,
rawConfig,
options = {},
buildConfigManger = new BuildConfigManager(),
buildConfigManager = new BuildConfigManager(),
) {
const tasks = {};
this.options = options;
Expand All @@ -68,10 +68,10 @@ class Bundle {
tasks.schema = this.assembleSchema.bind(this);
tasks.schemaTranslations = this.assembleSchemaTranslations.bind(this);

if (typeof buildConfigManger.production === 'function') {
if (typeof buildConfigManager.production === 'function') {
tasks.theme = (callback) => {
console.log('Theme task Started...');
buildConfigManger.initWorker().production((err) => {
buildConfigManager.initWorker().production((err) => {
if (err) {
return callback(err);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/stencil-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class StencilStart {
cliCommon = cliCommonModule,
stencilConfigManager = new StencilConfigManager(),
themeConfigManager = ThemeConfig.getInstance(THEME_PATH),
buildConfigManger = new BuildConfigManager(),
buildConfigManager = new BuildConfigManager(),
templateAssembler = templateAssemblerModule,
CyclesDetector = Cycles,
stencilPushUtils = stencilPushUtilsModule,
Expand All @@ -35,7 +35,7 @@ class StencilStart {
this._cliCommon = cliCommon;
this._stencilConfigManager = stencilConfigManager;
this._themeConfigManager = themeConfigManager;
this._buildConfigManger = buildConfigManger;
this._buildConfigManager = buildConfigManager;
this._templateAssembler = templateAssembler;
this._CyclesDetector = CyclesDetector;
this._stencilPushUtils = stencilPushUtils;
Expand Down Expand Up @@ -138,7 +138,7 @@ class StencilStart {
const DEFAULT_WATCH_FILES = ['/assets', '/templates', '/lang', '/.config'];
const DEFAULT_WATCH_IGNORED = ['/assets/scss', '/assets/css'];
const { themePath, configPath } = this._themeConfigManager;
const { watchOptions } = this._buildConfigManger;
const { watchOptions } = this._buildConfigManager;

// Watch sccs directory and automatically reload all css files if a file changes
const stylesPath = path.join(themePath, 'assets/scss');
Expand Down Expand Up @@ -211,8 +211,8 @@ class StencilStart {
}
});

if (this._buildConfigManger.development) {
this._buildConfigManger.initWorker().development(this._browserSync);
if (this._buildConfigManager.development) {
this._buildConfigManager.initWorker().development(this._browserSync);
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/stencil-start.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('StencilStart unit tests', () => {
});
const getThemeConfigManagerStub = () => ({});
const getStencilConfigManagerStub = () => ({});
const getBuildConfigMangerStub = () => ({});
const getBuildConfigManagerStub = () => ({});
const getTemplateAssemblerStub = () => ({});
const getCyclesDetectorConstructorStub = () => jest.fn();
const getStencilPushUtilsStub = () => ({
Expand All @@ -41,7 +41,7 @@ describe('StencilStart unit tests', () => {
cliCommon,
stencilConfigManager,
themeConfigManager,
buildConfigManger,
buildConfigManager,
templateAssembler,
CyclesDetector,
stencilPushUtils,
Expand All @@ -54,7 +54,7 @@ describe('StencilStart unit tests', () => {
cliCommon: cliCommon || getCliCommonStub(),
stencilConfigManager: stencilConfigManager || getStencilConfigManagerStub(),
themeConfigManager: themeConfigManager || getThemeConfigManagerStub(),
buildConfigManger: buildConfigManger || getBuildConfigMangerStub(),
buildConfigManager: buildConfigManager || getBuildConfigManagerStub(),
templateAssembler: templateAssembler || getTemplateAssemblerStub(),
CyclesDetector: CyclesDetector || getCyclesDetectorConstructorStub(),
stencilPushUtils: stencilPushUtils || getStencilPushUtilsStub(),
Expand Down

0 comments on commit 9c762fa

Please sign in to comment.