Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
try {
logger.log('');
logger.info(`Using kyt config at ${kytConfigPath}`);
// eslint-disable-next-line global-require,import/no-dynamic-require
config = require(kytConfigPath);
} catch (error) {
logger.error('Error loading your kyt.config.js:', error);
process.exit();
}
}
config = merge({}, baseConfig, config);
if (typeof config.modifyWebpackConfig === 'function') {
logger.info('What are you doing in your modifyWebpackConfig?');
logger.info('Let us know: https://github.com/NYTimes/kyt/issues/432');
}
// Create default identity functions for modify functions
['modifyWebpackConfig', 'modifyJestConfig'].forEach(m => {
if (typeof config[m] !== 'function') {
config[m] = c => c;
}
});
const validateURL = (name, userURL) => {
// Check to see if the url has the
// required protocol, hostname and port.
if (!userURL.protocol || !userURL.hostname || !userURL.port) {
logger.error(`â Error: ${name} is an invalid url - ${userURL.href}`);
}
};
if (shell.test('-f', kytConfigPath)) {
try {
logger.log('');
logger.info(`Using kyt config at ${kytConfigPath}`);
// eslint-disable-next-line global-require,import/no-dynamic-require
config = require(kytConfigPath);
} catch (error) {
logger.error('Error loading your kyt.config.js:', error);
process.exit();
}
}
config = merge({}, baseConfig, config);
if (typeof config.modifyWebpackConfig === 'function') {
logger.info('What are you doing in your modifyWebpackConfig?');
logger.info('Let us know: https://github.com/NYTimes/kyt/issues/432');
}
// Create default identity functions for modify functions
['modifyWebpackConfig', 'modifyJestConfig'].forEach(m => {
if (typeof config[m] !== 'function') {
config[m] = c => c;
}
});
const validateURL = (name, userURL) => {
// Check to see if the url has the
// required protocol, hostname and port.
if (!userURL.protocol || !userURL.hostname || !userURL.port) {
logger.error(`â Error: ${name} is an invalid url - ${userURL.href}`);
}
// These are the only differences between dev & prod
if (environment === 'production') {
clientConfig = prodClientConfig;
serverConfig = prodServerConfig;
clientOptions = merge(clientOptions, {
publicPath: config.productionPublicPath,
publicDir: 'build/public',
});
}
const serverOptions = merge(clientOptions, { type: 'server' });
const hasBabelrc = shell.test('-f', userBabelrcPath);
if (!hasBabelrc) {
logger.info('No user .babelrc found. Using kyt default babel preset...');
}
// Merge options with static webpack configs
clientConfig = merge.smart(baseConfig(clientOptions), clientConfig(clientOptions));
serverConfig = merge.smart(baseConfig(serverOptions), serverConfig(serverOptions));
// Modify via userland config
try {
clientConfig = config.modifyWebpackConfig(clone(clientConfig), clientOptions);
serverConfig = config.modifyWebpackConfig(clone(serverConfig), serverOptions);
} catch (error) {
logger.error('Error in your kyt.config.js modifyWebpackConfig():', error);
process.exit(1);
}
if (config.debug) {
webpackCompiler.hooks.done.tap('kyt', stats => {
if (stats.hasErrors()) {
logger.error(`${type} build failed\n`, stats.toString());
logger.info('See webpack error above');
} else if (stats.hasWarnings()) {
logger.warn(`${type} build warnings`, stats.toJson().warnings.join('\n'));
} else {
logger.task(`${type} build successful`);
}
// Remove the build type that we set in the "before-run" hook.
delete process.env.KYT_ENV_TYPE;
// Call the callback on successful build
if (cb) {
cb(stats);
}
});
const clientCompiler = webpackCompiler(clientConfig, stats => {
if (stats.hasErrors()) process.exit(1);
logger.info('Assets:');
printAssets(stats, clientConfig);
if (config.hasServer) {
buildServer();
} else {
logger.end('Done building');
}
});
clientCompiler.run(() => undefined);
detect(port, (error, unusedPort) => {
if (error) {
logger.error('error attempting to detect port', error);
process.exit();
}
if (port === unusedPort) {
callback();
} else {
logger.error(`port: ${port} is in use.`);
logger.info('Ports can be configured in kyt.config.js');
process.exit();
}
});
};
const installUserDependencies = () => {
logger.info('Cleaning node modules and reinstalling. This may take a couple of minutes...');
shell.rm('-rf', paths.userNodeModulesPath);
const result = shell.exec(`${ypm} install`);
if (result.code !== 0) {
fs.writeFileSync(paths.userPackageJSONPath, JSON.stringify(oldPackageJSON, null, 2));
logger.error('An error occurred when trying to install node modules', result.stderr);
logger.task('Restored the original package.json and bailing');
logger.info('You may need to reinstall your modules');
bailProcess();
}
logger.task('Installed new modules');
};
const installUserDependencies = () => {
logger.info('Cleaning node modules and reinstalling. This may take a couple of minutes...');
shell.rm('-rf', paths.userNodeModulesPath);
const result = shell.exec(`${ypm} install`);
if (result.code !== 0) {
fs.writeFileSync(paths.userPackageJSONPath, JSON.stringify(oldPackageJSON, null, 2));
logger.error('An error occurred when trying to install node modules', result.stderr);
logger.task('Restored the original package.json and bailing');
logger.info('You may need to reinstall your modules');
bailProcess();
}
logger.task('Installed new modules');
};