Theme
class
new Theme()
The class that locates themes
class Theme {
constructor(options) {
if (options) {
// Check if Doc will be installing a theme
// or will be rendering a template
let resolved = {
theme: options.template.path ? undefined : Theme.findTheme(options)
};
// Set the options
this.options = {
theme: {
name: resolved.theme ?
resolved.theme.name : undefined,
path: resolved.theme ?
resolved.theme.path : undefined
},
output: {
path: options.output.path ?
options.output.path : options.output
},
template: {
name: options.package ?
options.package.name : '',
path: options.template.path,
isEnabled: options.template.isEnabled,
isKit: options.template.isKit
}
};
}
}
showProgress
property
Theme.showProgress
Shows the progress for each command
showProgress: (command, max = 200) => {
var count = 0;
while (count < max) {
logUpdate('Mr. Doc [info]: ' + frame() + ' ' + command);
count++;
}
},
copyAssets
property
Theme.copyAssets
Create necessary paths to destination folder (Async)
copyAssets: () => {
let types = _.keys(config.paths);
let m = when.map(types, function(type) {
let d = when.defer();
let src = Path.join(config.src, config.paths[type].src);
let dest = Path.join(config.dest, config.paths[type].dest);
File.copy(src, dest, {
clobber: true
}, error => {
if (error) d.reject(error);
else {
d.resolve();
}
});
return d.promise;
});
return m;
},
copyAssetsSync
property
Theme.copyAssetsSync
Create necessary paths to destination folder (Sync)
copyAssetsSync: () => {
let types = _.keys(config.paths);
_.forEach(types, type => {
let src = Path.join(config.src, config.paths[type].src);
let dest = Path.join(config.dest, config.paths[type].dest);
File.copySync(src, dest, {
clobber: true
});
});
},
stringifyTemplate
property
Theme.stringifyTemplate
Reads the template from the source and strigifies it. (Async)
stringifyTemplate: () => {
let d = when.defer();
let file = Path.join(config.src, 'template/index.jade');
File.readFile(file, (error, data) => {
if (error) d.reject(error);
else d.resolve({
template: data.toString()
});
});
return d.promise;
},
stringifyTemplateSync
property
Theme.stringifyTemplateSync
Reads the template from the source and strigifies it. (Sync)
stringifyTemplateSync: () => {
let file = Path.join(config.src, 'template/index.jade');
return File.readFileSync(file).toString();
}
};
}
findTheme
method
Theme.findTheme()
Option name | Type | Description |
---|---|---|
theme | String | The theme to find |
return | Object | The theme. |
Find the theme specified
static findTheme(options) {
const DEFAULT_THEME = 'mr-doc-theme-default';
const mrDocPath = Path.resolve(__dirname, '..');
const projectPath = process.cwd();
const name = options.theme.name;
// Plugins may provide a name property
// so just in case check it
const locations = {
// Path to the project's node_modules dir + theme
project: Path.join(projectPath, 'node_modules', name),
// Path to Doc's node_modules dir + theme
mrDoc: Path.join(mrDocPath, 'node_modules', name),
// Path to the theme relative to the calling process for use with a private theme
private: Path.join(projectPath, name),
// Path to the Doc's default theme dir
default: Path.join(mrDocPath, 'node_modules', DEFAULT_THEME)
};
if (options.private && Dir.exists(locations.private)) {
console.log('Mr. Doc [info]: Using private theme [' + name + ']');
return {
name,
path: locations.private
};
} else if (Dir.exists(locations.mrDoc)) {
console.log('Mr. Doc [info]: Using theme [' + name + ']');
return {
name,
path: locations.mrDoc
};
} else if (Dir.exists(locations.project)) {
console.log('Mr. Doc [info]: Using theme [' + name + ']');
return {
name,
path: locations.project
};
} else {
console.log('Mr. Doc [warn]: Theme "' + name + '" not found, reverting to default.');
return {
name: DEFAULT_THEME,
path: locations.default
};
}
}
install
method
Theme.prototype.install()
Copies the theme specified (reverting to default)
over to the output directory. (Async)
install(options = this.options) {
let final = when.defer();
// Check if the template is enabled (legacy)
if (options.template.isEnabled() &&
!options.template.isKit()) {
final.resolve({
template: File.readFileSync(Path.resolve(__dirname, options.template.path)).toString()
});
} else {
let tasks = Theme.tasks(options);
((notify) => {
return tasks.copyAssets()
.tap(() => {
notify('Copying Assets.');
})
.then(tasks.stringifyTemplate)
.tap(() => {
notify('Rendering template.');
})
.then(final.resolve);
})(tasks.showProgress);
}
return final.promise;
}
JSFiddle
installSync
method
Theme.prototype.installSync()
Copies the theme specified (reverting to default)
over to the output directory. (Sync)
installSync(options = this.options) {
var template;
if (options.template.isEnabled() &&
!options.template.isKit()) {
return {
template: File.readFileSync(Path.resolve(__dirname, options.template.path)).toString()
};
} else {
let tasks = Theme.tasks(options);
tasks.copyAssetsSync();
tasks.showProgress('Copying assets.');
template = tasks.stringifyTemplateSync();
tasks.showProgress('Reading template.');
tasks.showProgress('Done.');
return {
template
};
}
}
}
export default Theme;
JSFiddle