Skip to content

Commit

Permalink
Add assets and templates watch to resource builder and refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Rechkunov committed Mar 21, 2014
1 parent a0d4114 commit 201d920
Showing 1 changed file with 85 additions and 15 deletions.
100 changes: 85 additions & 15 deletions lib/server/ResourceBuilder.js
Expand Up @@ -59,14 +59,15 @@ var STYLE_FILENAME = 'style.css',
TEMPLATE_EXTENSION = '.dust',
SCRIPT_EXTENSION = '.js',
PLACEHOLDER_FULL_NAME_FORMAT = '%s_%s',
INFO_COMPILED_CLEAN = 'Clean compiled placeholders: %s',
INFO_PUBLIC_CLEAN = 'Clean public folder: %s',
INFO_TEMPORARY_CLEAN = 'Clean temporary folder: %s',
INFO_COMPILE_PLACEHOLDERS = 'Compile placeholders of module %s',
INFO_PROCESSING_STYLE = 'Process styles of module %s',
INFO_PUBLISH_SCRIPTS = 'Publish scripts of module %s',
INFO_JOIN_STYLES = 'Join all styles to %s',
INFO_PUBLISH_IMAGES = 'Publish %s images of module %s';
INFO_COMPILED_CLEAN = 'Cleaning compiled placeholders: %s',
INFO_PUBLIC_CLEAN = 'Cleaning public folder: %s',
INFO_TEMPORARY_CLEAN = 'Cleaning temporary folder: %s',
INFO_COMPILE_PLACEHOLDERS = 'Compiling placeholders of module %s',
INFO_PROCESSING_STYLE = 'Processing styles of module %s',
INFO_PUBLISH_SCRIPTS = 'Publishing scripts of module %s',
INFO_JOIN_STYLES = 'Publishing joined style to %s',
INFO_REGISTER_WATCH = 'Registering watch on all templates and assets',
INFO_PUBLISH_IMAGES = 'Publishing %s images of module %s';

var PATHS = {
STYLES: path.join('**', '*.css'),
Expand All @@ -88,11 +89,12 @@ var TASKS = {
BUILD: 'Build',
CLEAN: 'Clean',
PROCESS_STYLES: 'Process all module styles',
JOIN_STYLES: 'Join all styles',
PUBLISH_JOINED_STYLES: 'Join all styles and publish',
PUBLISH_IMAGES: 'Publish all images',
PUBLISH_SCRIPTS: 'Publish all client-side scripts',
BUILD_CLIENT_BUNDLE: 'Build client-side part of catberry',
REGISTER_TEMPLATES: 'Register all templates'
REGISTER_TEMPLATES: 'Register all templates',
REGISTER_WATCH: 'Register watch on all templates and assets'
};

util.inherits(ResourceBuilder, events.EventEmitter);
Expand Down Expand Up @@ -215,7 +217,7 @@ ResourceBuilder.prototype._registerBuildTasks = function () {
return self._getStyleProcessingTasks();
});
// join all style into one file when public directory is clean
gulp.task(TASKS.JOIN_STYLES, [TASKS.PROCESS_STYLES],
gulp.task(TASKS.PUBLISH_JOINED_STYLES, [TASKS.PROCESS_STYLES],
function () {
return self._getStyleJoinTask();
});
Expand Down Expand Up @@ -249,25 +251,93 @@ ResourceBuilder.prototype._registerBuildTasks = function () {
});

// remove temporary directory after style processing is over
gulp.task(TASKS.REMOVE_TEMPORARY, [TASKS.JOIN_STYLES],
gulp.task(TASKS.REMOVE_TEMPORARY, [TASKS.PUBLISH_JOINED_STYLES],
function () {
var toRemove = path.join(process.cwd(), DIRECTORY_NAMES.TEMPORARY);
return self._getCleanTaskForPath(toRemove, INFO_TEMPORARY_CLEAN);
});

gulp.task(TASKS.REGISTER_WATCH, [
TASKS.COMPILE_TEMPLATES,
TASKS.PUBLISH_JOINED_STYLES,
TASKS.PUBLISH_SCRIPTS,
TASKS.PUBLISH_IMAGES,
], function () {
if (self._isRelease) {
return;
}
self._registerWatch();
});

gulp.task(TASKS.BUILD, [
TASKS.COMPILE_TEMPLATES,
TASKS.PROCESS_STYLES,
TASKS.JOIN_STYLES,
TASKS.PUBLISH_JOINED_STYLES,
TASKS.PUBLISH_SCRIPTS,
TASKS.PUBLISH_IMAGES,
TASKS.BUILD_CLIENT_BUNDLE,
TASKS.REMOVE_TEMPORARY,
TASKS.PUBLISH_IMAGES
TASKS.REGISTER_WATCH
], function () {
self.emit('built');
});
};

ResourceBuilder.prototype._registerWatch = function () {
this._logger.info(INFO_REGISTER_WATCH);

var modulesPath = path.join(
process.cwd(),
DIRECTORY_NAMES.CATBERRY_MODULES,
PATHS.ALL
);
var templatesPath = path.join(
modulesPath,
DIRECTORY_NAMES.PLACEHOLDERS,
PATHS.TEMPLATES
);
gulp.watch(templatesPath, [TASKS.COMPILE_TEMPLATES]);

var assetsPath = path.join(
modulesPath,
DIRECTORY_NAMES.ASSETS
);

var stylesPath = path.join(
assetsPath,
PATHS.STYLES
);
gulp.watch(stylesPath, [
TASKS.PUBLISH_JOINED_STYLES,
TASKS.REMOVE_TEMPORARY
]);

var imagesPathPng = path.join(
assetsPath,
PATHS.IMAGES_PNG
);
var imagesPathJpg = path.join(
assetsPath,
PATHS.IMAGES_JPG
);
var imagesPathGif = path.join(
assetsPath,
PATHS.IMAGES_GIF
);
gulp.watch([
imagesPathPng,
imagesPathJpg,
imagesPathGif
],
[TASKS.PUBLISH_IMAGES]
);

var scriptsPath = path.join(
assetsPath,
PATHS.SCRIPTS
);
gulp.watch(scriptsPath, [TASKS.PUBLISH_SCRIPTS]);
};

/**
* Gets gulp clean task for specified path.
* @param {string} folderPath Folder path.
Expand Down

0 comments on commit 201d920

Please sign in to comment.