Skip to content
Permalink
Browse files
refactor: use template strings where applicable (#118)
Applied `lebab --replace src --transform template` to the code and made some
minor adjustments.
  • Loading branch information
raphinesse committed Dec 15, 2019
1 parent 95010ef commit 051cb9a23ffb029743f9698641b978e1dc1e8101
Showing 11 changed files with 58 additions and 72 deletions.
@@ -43,7 +43,7 @@ ActionStack.prototype = {
},
// Returns a promise.
process: function (platform) {
events.emit('verbose', 'Beginning processing of action stack for ' + platform + ' project...');
events.emit('verbose', `Beginning processing of action stack for ${platform} project...`);

while (this.stack.length) {
const action = this.stack.shift();
@@ -66,7 +66,7 @@ ActionStack.prototype = {
revert.apply(null, revert_params);
} catch (err) {
events.emit('warn', 'Error during reversion of action! We probably really messed up your project now, sorry! D:');
issue += 'A reversion action failed: ' + err.message + '\n';
issue += `A reversion action failed: ${err.message}\n`;
}
}
e.message = issue + e.message;
@@ -78,7 +78,7 @@ function PlatformMunger_apply_file_munge (file, munge, remove) {
if (remove) config_file.prune_child(selector, munge.parents[selector][xml_child]);
else config_file.graft_child(selector, munge.parents[selector][xml_child]);
} else {
events.emit('warn', 'config file ' + file + ' requested for changes not found at ' + config_file.filepath + ', ignoring');
events.emit('warn', `config file ${file} requested for changes not found at ${config_file.filepath}, ignoring`);
}
}
}
@@ -128,8 +128,7 @@ function add_plugin_changes (pluginInfo, plugin_vars, is_top_level, should_incre
const isConflictingInfo = this._is_conflicting(edit_config_changes, platform_config.config_munge, plugin_force);

if (isConflictingInfo.conflictWithConfigxml) {
throw new Error(pluginInfo.id +
' cannot be added. <edit-config> changes in this plugin conflicts with <edit-config> changes in config.xml. Conflicts must be resolved before plugin can be added.');
throw new Error(`${pluginInfo.id} cannot be added. <edit-config> changes in this plugin conflicts with <edit-config> changes in config.xml. Conflicts must be resolved before plugin can be added.`);
}
if (plugin_force) {
events.emit('warn', '--force is used. edit-config will overwrite conflicts if any. Conflicting plugins may not work as expected.');
@@ -143,9 +142,7 @@ function add_plugin_changes (pluginInfo, plugin_vars, is_top_level, should_incre
// force add new munges
config_munge = this.generate_plugin_config_munge(pluginInfo, plugin_vars, edit_config_changes);
} else if (isConflictingInfo.conflictFound) {
throw new Error('There was a conflict trying to modify attributes with <edit-config> in plugin ' + pluginInfo.id +
'. The conflicting plugin, ' + isConflictingInfo.conflictingPlugin + ', already modified the same attributes. The conflict must be resolved before ' +
pluginInfo.id + ' can be added. You may use --force to add the plugin and overwrite the conflicting attributes.');
throw new Error(`There was a conflict trying to modify attributes with <edit-config> in plugin ${pluginInfo.id}. The conflicting plugin, ${isConflictingInfo.conflictingPlugin}, already modified the same attributes. The conflict must be resolved before ${pluginInfo.id} can be added. You may use --force to add the plugin and overwrite the conflicting attributes.`);
} else {
// no conflicts, will handle edit-config
config_munge = this.generate_plugin_config_munge(pluginInfo, plugin_vars, edit_config_changes);
@@ -301,7 +298,7 @@ function generate_plugin_config_munge (pluginInfo, vars, edit_config_changes) {
// interp vars
if (vars) {
Object.keys(vars).forEach(key => {
const regExp = new RegExp('\\$' + key, 'g');
const regExp = new RegExp(`\\$${key}`, 'g');
stringified = stringified.replace(regExp, vars[key]);
});
}
@@ -112,13 +112,13 @@ ConfigFile.prototype.graft_child = function ConfigFile_graft_child (selector, xm
result = modules.xml_helpers.graftXML(this.data, xml_to_graft, selector, xml_child.after);
}
if (!result) {
throw new Error('Unable to graft xml at selector "' + selector + '" from "' + filepath + '" during config install');
throw new Error(`Unable to graft xml at selector "${selector}" from "${filepath}" during config install`);
}
} else {
// plist file
result = modules.plist_helpers.graftPLIST(this.data, xml_child.xml, selector);
if (!result) {
throw new Error('Unable to graft plist "' + filepath + '" during config install');
throw new Error(`Unable to graft plist "${filepath}" during config install`);
}
}
this.is_changed = true;
@@ -145,8 +145,7 @@ ConfigFile.prototype.prune_child = function ConfigFile_prune_child (selector, xm
result = modules.plist_helpers.prunePLIST(this.data, xml_child.xml, selector);
}
if (!result) {
const err_msg = 'Pruning at selector "' + selector + '" from "' + filepath + '" went bad.';
throw new Error(err_msg);
throw new Error(`Pruning at selector "${selector}" from "${filepath}" went bad.`);
}
this.is_changed = true;
};
@@ -167,7 +166,7 @@ function resolveConfigFilePath (project_dir, platform, file) {

// [CB-5989] multiple Info.plist files may exist. default to $PROJECT_NAME-Info.plist
if (matches.length > 1 && file.includes('-Info.plist')) {
const plistName = getIOSProjectname(project_dir) + '-Info.plist';
const plistName = `${getIOSProjectname(project_dir)}-Info.plist`;
for (let i = 0; i < matches.length; i++) {
if (matches[i].includes(plistName)) {
filepath = matches[i];
@@ -231,13 +230,10 @@ function getIOSProjectname (project_dir) {
if (matches.length === 1) {
iospath = path.basename(matches[0], '.xcodeproj');
} else {
let msg;
if (matches.length === 0) {
msg = 'Does not appear to be an xcode project, no xcode project file in ' + project_dir;
} else {
msg = 'There are multiple *.xcodeproj dirs in ' + project_dir;
}
throw new Error(msg);
const msg = matches.length === 0
? 'Does not appear to be an xcode project, no xcode project file'
: 'There are multiple *.xcodeproj dirs';
throw new Error(`${msg} in ${project_dir}`);
}
return iospath;
}
@@ -31,12 +31,12 @@ function ConfigParser (path) {
this.cdvNamespacePrefix = getCordovaNamespacePrefix(this.doc);
et.register_namespace(this.cdvNamespacePrefix, 'http://cordova.apache.org/ns/1.0');
} catch (e) {
events.emit('error', 'Parsing ' + path + ' failed');
events.emit('error', `Parsing ${path} failed`);
throw e;
}
const r = this.doc.getroot();
if (r.tag !== 'widget') {
throw new CordovaError(path + ' has incorrect root node name (expected "widget", was "' + r.tag + '")');
throw new CordovaError(`${path} has incorrect root node name (expected "widget", was "${r.tag}")`);
}
}

@@ -156,7 +156,7 @@ ConfigParser.prototype = {
return findElementAttributeValue(name, this.doc.findall('preference'));
},
setGlobalPreference: function (name, value) {
let pref = this.doc.find('preference[@name="' + name + '"]');
let pref = this.doc.find(`preference[@name="${name}"]`);
if (!pref) {
pref = new et.Element('preference');
pref.attrib.name = name;
@@ -165,14 +165,14 @@ ConfigParser.prototype = {
pref.attrib.value = value;
},
getPlatformPreference: function (name, platform) {
return findElementAttributeValue(name, this.doc.findall('./platform[@name="' + platform + '"]/preference'));
return findElementAttributeValue(name, this.doc.findall(`./platform[@name="${platform}"]/preference`));
},
setPlatformPreference: function (name, platform, value) {
const platformEl = this.doc.find('./platform[@name="' + platform + '"]');
const platformEl = this.doc.find(`./platform[@name="${platform}"]`);
if (!platformEl) {
throw new CordovaError('platform does not exist (received platform: ' + platform + ')');
throw new CordovaError(`platform does not exist (received platform: ${platform})`);
}
const elems = this.doc.findall('./platform[@name="' + platform + '"]/preference');
const elems = this.doc.findall(`./platform[@name="${platform}"]/preference`);
let pref = elems.filter(elem =>
elem.attrib.name.toLowerCase() === name.toLowerCase()
).pop();
@@ -216,7 +216,7 @@ ConfigParser.prototype = {
const ret = [];
let staticResources = [];
if (platform) { // platform specific icons
this.doc.findall('./platform[@name="' + platform + '"]/' + resourceName).forEach(elt => {
this.doc.findall(`./platform[@name="${platform}"]/${resourceName}`).forEach(elt => {
elt.platform = platform; // mark as platform specific resource
staticResources.push(elt);
});
@@ -228,7 +228,7 @@ ConfigParser.prototype = {
const res = {};
res.src = elt.attrib.src;
res.target = elt.attrib.target || undefined;
res.density = elt.attrib.density || elt.attrib[this.cdvNamespacePrefix + ':density'] || elt.attrib['gap:density'];
res.density = elt.attrib.density || elt.attrib[`${this.cdvNamespacePrefix}:density`] || elt.attrib['gap:density'];
res.platform = elt.platform || null; // null means icon represents default icon (shared between platforms)
res.width = +elt.attrib.width || undefined;
res.height = +elt.attrib.height || undefined;
@@ -302,7 +302,7 @@ ConfigParser.prototype = {
let fileResources = [];

if (platform) { // platform specific resources
fileResources = this.doc.findall('./platform[@name="' + platform + '"]/resource-file').map(tag => ({
fileResources = this.doc.findall(`./platform[@name="${platform}"]/resource-file`).map(tag => ({
platform,
src: tag.attrib.src,
target: tag.attrib.target,
@@ -339,7 +339,7 @@ ConfigParser.prototype = {

if (platforms) {
platforms.forEach(platform => {
scriptElements = scriptElements.concat(this.doc.findall('./platform[@name="' + platform + '"]/hook'));
scriptElements = scriptElements.concat(this.doc.findall(`./platform[@name="${platform}"]/hook`));
});
}

@@ -417,11 +417,11 @@ ConfigParser.prototype = {
if (!id) {
return undefined;
}
const pluginElement = this.doc.find('./plugin/[@name="' + id + '"]');
const pluginElement = this.doc.find(`./plugin/[@name="${id}"]`);
if (pluginElement === null) {
const legacyFeature = this.doc.find('./feature/param[@name="id"][@value="' + id + '"]/..');
const legacyFeature = this.doc.find(`./feature/param[@name="id"][@value="${id}"]/..`);
if (legacyFeature) {
events.emit('log', 'Found deprecated feature entry for ' + id + ' in config.xml.');
events.emit('log', `Found deprecated feature entry for ${id} in config.xml.`);
return featureToPlugin(legacyFeature);
}
return undefined;
@@ -546,7 +546,7 @@ ConfigParser.prototype = {
},
/* Get all edit-config tags */
getEditConfigs: function (platform) {
const platform_edit_configs = this.doc.findall('./platform[@name="' + platform + '"]/edit-config');
const platform_edit_configs = this.doc.findall(`./platform[@name="${platform}"]/edit-config`);
const edit_configs = this.doc.findall('edit-config').concat(platform_edit_configs);

return edit_configs.map(tag => {
@@ -563,7 +563,7 @@ ConfigParser.prototype = {

/* Get all config-file tags */
getConfigFiles: function (platform) {
const platform_config_files = this.doc.findall('./platform[@name="' + platform + '"]/config-file');
const platform_config_files = this.doc.findall(`./platform[@name="${platform}"]/config-file`);
const config_files = this.doc.findall('config-file').concat(platform_config_files);

return config_files.map(tag => {
@@ -67,7 +67,7 @@ function updatePathWithStats (sourcePath, sourceStats, targetPath, targetStats,

if (targetStats && (targetStats.isDirectory() !== sourceStats.isDirectory())) {
// The target exists. But if the directory status doesn't match the source, delete it.
log('delete ' + targetPath);
log(`delete ${targetPath}`);
fs.removeSync(targetFullPath);
targetStats = null;
updated = true;
@@ -76,20 +76,20 @@ function updatePathWithStats (sourcePath, sourceStats, targetPath, targetStats,
if (!targetStats) {
if (sourceStats.isDirectory()) {
// The target directory does not exist, so it should be created.
log('mkdir ' + targetPath);
log(`mkdir ${targetPath}`);
fs.ensureDirSync(targetFullPath);
updated = true;
} else if (sourceStats.isFile()) {
// The target file does not exist, so it should be copied from the source.
log('copy ' + sourcePath + ' ' + targetPath + (copyAll ? '' : ' (new file)'));
log(`copy ${sourcePath} ${targetPath}${copyAll ? '' : ' (new file)'}`);
fs.copySync(sourceFullPath, targetFullPath);
updated = true;
}
} else if (sourceStats.isFile() && targetStats.isFile()) {
// The source and target paths both exist and are files.
if (copyAll) {
// The caller specified all files should be copied.
log('copy ' + sourcePath + ' ' + targetPath);
log(`copy ${sourcePath} ${targetPath}`);
fs.copySync(sourceFullPath, targetFullPath);
updated = true;
} else {
@@ -99,15 +99,15 @@ function updatePathWithStats (sourcePath, sourceStats, targetPath, targetStats,
// for timestamps lacking sub-second precision in some filesystems.
if (sourceStats.mtime.getTime() >= targetStats.mtime.getTime() ||
sourceStats.size !== targetStats.size) {
log('copy ' + sourcePath + ' ' + targetPath + ' (updated file)');
log(`copy ${sourcePath} ${targetPath} (updated file)`);
fs.copySync(sourceFullPath, targetFullPath);
updated = true;
}
}
}
} else if (targetStats) {
// The target exists but the source is null, so the target should be deleted.
log('delete ' + targetPath + (copyAll ? '' : ' (no source)'));
log(`delete ${targetPath}${copyAll ? '' : ' (no source)'}`);
fs.removeSync(targetFullPath);
updated = true;
}
@@ -129,7 +129,7 @@ function updatePathInternal (sourcePath, targetPath, options, log) {
// A non-null source path was specified. It should exist.
const sourceFullPath = path.join(rootDir, sourcePath);
if (!fs.existsSync(sourceFullPath)) {
throw new Error('Source path does not exist: ' + sourcePath);
throw new Error(`Source path does not exist: ${sourcePath}`);
}

sourceStats = fs.statSync(sourceFullPath);
@@ -276,7 +276,7 @@ function mergeAndUpdateDir (sourceDirs, targetDir, options, log) {
path.join(rootDir, sourceDir)
).map(sourcePath => {
if (!fs.existsSync(sourcePath)) {
throw new Error('Source directory does not exist: ' + sourcePath);
throw new Error(`Source directory does not exist: ${sourcePath}`);
}
return mapDirectory(rootDir, path.relative(rootDir, sourcePath), include, exclude);
});
@@ -26,7 +26,7 @@ function PlatformJson (filePath, platform, root) {
}

PlatformJson.load = (plugins_dir, platform) => {
const filePath = path.join(plugins_dir, platform + '.json');
const filePath = path.join(plugins_dir, `${platform}.json`);
let root = null;
if (fs.existsSync(filePath)) {
root = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
@@ -234,7 +234,7 @@ function ModuleMetadata (pluginId, jsModule) {
if (!pluginId) throw new TypeError('pluginId argument must be a valid plugin id');
if (!jsModule.src && !jsModule.name) throw new TypeError('jsModule argument must contain src or/and name properties');

this.id = pluginId + '.' + (jsModule.name || jsModule.src.match(/([^/]+)\.js/)[1]);
this.id = `${pluginId}.${jsModule.name || jsModule.src.match(/([^/]+)\.js/)[1]}`;
this.file = ['plugins', pluginId, jsModule.src].join('/');
this.pluginId = pluginId;

0 comments on commit 051cb9a

Please sign in to comment.