cmdBuild function

bool cmdBuild (
  1. Map ctx
)

performs build command ctx is zaart context map

Implementation

bool cmdBuild(Map ctx) {
  // look for an intialized site
  bool _isInitializedFlag;
  try {
    _isInitializedFlag = isInitialized(ctx);
  } catch (err) {
    Logger.root.severe(err.toString());
    return false;
  }

  // if no site initialized return
  if (!_isInitializedFlag) {
    return false;
  }

  // read config file
  var isForced = ctx['cmd-build']['force'];
  var cfgFName = ctx['config'];
  var cfg = readConfig(cfgFName);

  // create build dir if not exists
  var buildDir = Directory(BUILD_DIR);
  if (!buildDir.existsSync()) {
    try {
      buildDir.createSync(recursive: false);
    } catch (err) {
      print("oh no, i cannot create or access build directory");
      Logger.root.severe('failed to create build directory');
      Logger.root.severe(err);
      return false;
    }
  }

  // copy theme dir to build dir (only css and js files)
  var srcthemeDir = Directory(THEME_DIR);
  var dstThemeDir = Directory(BUILD_DIR + '/' + THEME_DIR);
  try {
    dstThemeDir.createSync(recursive: false);
    srcthemeDir.listSync(recursive: false).forEach((f) {
      if (f is Directory) {
        return;
      }

      var name = f.uri.toString();
      if (!(name.endsWith('.css') || name.endsWith('.js'))) {
        return;
      }

      var file = File(name);
      file.copySync(BUILD_DIR + '/' + name);
    });
  } catch (err) {
    print("oh no, i cannot copy theme directory to build directory");
    Logger.root.severe('failed to copy them directory to build directory');
    Logger.root.severe(err);
    return false;
  }

  // build pages one by one
  cfg.pages.forEach((p) {
    _buildPage(BUILD_DIR, PAGES_DIR, THEME_DIR, cfg, p, isForced, false, null);
    if (p.children != null && p.children.length > 0) {
      p.children.forEach((c) => _buildPage(
          BUILD_DIR, PAGES_DIR, THEME_DIR, cfg, c, isForced, true, p));
    }
  });

  return true;
}