Skip to content

Commit

Permalink
Merge branch 'rename-to-auto-imports' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Tonon committed Aug 20, 2020
2 parents 717ae7b + 3a01bab commit 5fd8f0b
Show file tree
Hide file tree
Showing 65 changed files with 2,626 additions and 2,060 deletions.
807 changes: 410 additions & 397 deletions README.md

Large diffs are not rendered by default.

34 changes: 16 additions & 18 deletions core/content-generators/generate_content.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
var isString = require("../helpers/isString");
var warn = require("../helpers/warn");

var isString = require('../helpers/isString');
var warn = require('../helpers/warn');
var template_warning = require("../error-messages/warn-template");

var template_warning = require('../error-messages/warn-template');
var format_paths = require("../formatters/format_paths");
var format_template = require("../formatters/format_template");

var format_paths = require('../formatters/format_paths');
var format_template = require('../formatters/format_template');
module.exports = function generate_content({ pathsArray, opt }) {
var formatIsString = isString(opt.format);

module.exports = function generate_content ({ pathsArray, opt }) {
warn(formatIsString && opt.template, template_warning);

var formatIsString = isString(opt.format);
var output = formatIsString
? format_paths(pathsArray, opt.format)
: format_template(pathsArray, opt.format, opt.template);

warn(formatIsString && opt.template, template_warning);
var defaultHeadFoot = formatIsString ? "\n" : "";

var output = formatIsString ?
format_paths(pathsArray, opt.format) :
format_template(pathsArray, opt.format, opt.template);
var header = opt.header ? `${opt.header}\n` : defaultHeadFoot;
var footer = opt.footer ? `\n${opt.footer}` : defaultHeadFoot;

var defaultHeadFoot = formatIsString ? '\n' : '';

var header = opt.header ? `${opt.header}\n` : defaultHeadFoot;
var footer = opt.footer ? `\n${opt.footer}` : defaultHeadFoot;

return header + output + footer;
}
return header + output + footer;
};
107 changes: 55 additions & 52 deletions core/content-generators/get_ordered_paths.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,63 @@

var get_paths_from_string = require('./get_paths_from_string');

module.exports = function get_ordered_paths ({ oldContent, newPaths }) {

var contains = (array, value) => array.indexOf(value) > -1;
var oldOrderedPaths = get_paths_from_string(oldContent);

if (oldOrderedPaths.length < 1) {
return newPaths;
}

var newOrderedPaths = [].concat(oldOrderedPaths);
newPaths.forEach((path) => {
if (!contains(oldOrderedPaths, path)) {
var bestMatch = get_best_match(path, oldOrderedPaths);
if (bestMatch.score < 2) {
var originalIndex = newPaths.indexOf(path);
newOrderedPaths.splice(originalIndex, 0, path);
} else {
newOrderedPaths.splice(bestMatch.index + 1, 0, path);
}
}
});

var finalPaths = newOrderedPaths.filter(path => {
return contains(newPaths, path);
});

return finalPaths;
}
var get_paths_from_string = require("./get_paths_from_string");

module.exports = function get_ordered_paths({ oldContent, newPaths }) {
var contains = (array, value) => array.indexOf(value) > -1;
var oldOrderedPaths = get_paths_from_string(oldContent);

if (oldOrderedPaths.length < 1) {
return newPaths;
}

var newOrderedPaths = [].concat(oldOrderedPaths);
newPaths.forEach((path) => {
if (!contains(oldOrderedPaths, path)) {
var bestMatch = get_best_match(path, oldOrderedPaths);
if (bestMatch.score < 2) {
var originalIndex = newPaths.indexOf(path);
newOrderedPaths.splice(originalIndex, 0, path);
} else {
newOrderedPaths.splice(bestMatch.index + 1, 0, path);
}
}
});

var finalPaths = newOrderedPaths.filter((path) => {
return contains(newPaths, path);
});

return finalPaths;
};

function get_best_match(targetPath, comparisonArray) {
var lineScores = comparisonArray.map(comparisonPath => compare_paths(targetPath, comparisonPath));
var bestMatch = lineScores.reduce((currentBestMatch, score, index) => {
if (currentBestMatch.score <= score) {
currentBestMatch = {
index: index,
score: score,
}
}
return currentBestMatch;
}, {score: 0, index: 0})

return bestMatch;
var lineScores = comparisonArray.map((comparisonPath) =>
compare_paths(targetPath, comparisonPath)
);
var bestMatch = lineScores.reduce(
(currentBestMatch, score, index) => {
if (currentBestMatch.score <= score) {
currentBestMatch = {
index: index,
score: score,
};
}
return currentBestMatch;
},
{ score: 0, index: 0 }
);

return bestMatch;
}

function compare_paths(targetPath, comparisonPath) {
var targetArray = targetPath.replace(/\.?\.\//g,'').split('');
var comparisonArray = comparisonPath.replace(/\.?\.\//g,'').split('');
var targetArray = targetPath.replace(/\.?\.\//g, "").split("");
var comparisonArray = comparisonPath.replace(/\.?\.\//g, "").split("");

var i = 0;
var compare = i => targetArray[i] == comparisonArray[i];
var isDefined = i => typeof targetArray[i] !== 'undefined';
while (isDefined(i) && compare(i)){
i++;
}
var i = 0;
var compare = (i) => targetArray[i] == comparisonArray[i];
var isDefined = (i) => typeof targetArray[i] !== "undefined";
while (isDefined(i) && compare(i)) {
i++;
}

return i;
return i;
}
13 changes: 6 additions & 7 deletions core/content-generators/get_paths_from_string.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var remove_dupes = require("../helpers/remove_dupes");

var remove_dupes = require('../helpers/remove_dupes');

module.exports = function get_paths_from_string(string){
var pathRegEx = /\.\.?\/[^\n"?:*<>|]+\.[A-z0-9]+/g;
var paths = string.match(pathRegEx) || [];
return remove_dupes(paths);
}
module.exports = function get_paths_from_string(string) {
var pathRegEx = /\.\.?\/[^\n"?:*<>|]+\.[A-z0-9]+/g;
var paths = string.match(pathRegEx) || [];
return remove_dupes(paths);
};
19 changes: 9 additions & 10 deletions core/content-generators/get_relative_path.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
var path = require("path");

var path = require('path');

var slash = require('../helpers/slash');
var join = require('../helpers/join');
var slash = require("../helpers/slash");
var join = require("../helpers/join");

module.exports = function get_relative_path(file, dest) {
var from = slash( join([file.cwd, dest]) );
var from = slash(join([file.cwd, dest]));
var to = slash(file.history[0]);
var relativePath = slash( path.relative(from, to) );
var relFixDots = relativePath.replace(/^(\.\.?)/, '$1/');
var relDotSlash = relFixDots.replace(/^([^.\/])/, './$1');
var relDupesRemoved = relDotSlash.replace(/\/\//g, '/');
var relativePath = slash(path.relative(from, to));
var relFixDots = relativePath.replace(/^(\.\.?)/, "$1/");
var relDotSlash = relFixDots.replace(/^([^.\/])/, "./$1");
var relDupesRemoved = relDotSlash.replace(/\/\//g, "/");

return relDupesRemoved;
}
};
13 changes: 6 additions & 7 deletions core/content-generators/order_content.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var get_ordered_paths = require("./get_ordered_paths");
var generate_content = require("./generate_content");

var get_ordered_paths = require('./get_ordered_paths');
var generate_content = require('./generate_content');

module.exports = function order_content ({ oldContent, newPaths, opt }) {
var orderedPaths = get_ordered_paths({ oldContent, newPaths });
return generate_content({ pathsArray: orderedPaths, opt});
}
module.exports = function order_content({ oldContent, newPaths, opt }) {
var orderedPaths = get_ordered_paths({ oldContent, newPaths });
return generate_content({ pathsArray: orderedPaths, opt });
};
8 changes: 4 additions & 4 deletions core/error-messages/$path.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var c = require("chalk");

var c = require('chalk');

module.exports =
`"${c.cyan('$path')}" can only be declared once per format rule.`;
module.exports = `"${c.cyan(
"$path"
)}" can only be declared once per format rule.`;
10 changes: 5 additions & 5 deletions core/error-messages/dest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var c = require("chalk");

var c = require('chalk');

module.exports =
`The "${c.yellow('dest')}" setting is required.
${c.grey('The dest setting should be the exact same value as what is provided to gulp.dest().')}`;
module.exports = `The "${c.yellow("dest")}" setting is required.
${c.grey(
"The dest setting should be the exact same value as what is provided to gulp.dest()."
)}`;
10 changes: 5 additions & 5 deletions core/error-messages/fileName.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var c = require("chalk");

var c = require('chalk');

module.exports =
`The "${c.yellow('fileName')}" setting is required.
${c.grey('The fileName setting determines the file name (including the extension) given to the output file.')}`;
module.exports = `The "${c.yellow("fileName")}" setting is required.
${c.grey(
"The fileName setting determines the file name (including the extension) given to the output file."
)}`;
20 changes: 11 additions & 9 deletions core/error-messages/format.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
var c = require("chalk");

var c = require('chalk');
module.exports = `The "${c.yellow("format")}" setting is required.
${c.grey(
"The format setting dictates the format of each import line in the generated file."
)}
module.exports =
`The "${c.yellow('format')}" setting is required.
${c.grey('The format setting dictates the format of each import line in the generated file.')}
${c.bold("Example:")}
${c.yellow("format:")} "import ${c.magenta("$name")} from '${c.cyan("$path")}'",
${c.bold('Example:')}
${c.yellow('format:')} "import ${c.magenta('$name')} from '${c.cyan('$path')}'",
${c.magenta('$name')} = name of the file minus the extension (good for variable names)
${c.cyan('$path')} = relative path to the file`;
${c.magenta(
"$name"
)} = name of the file minus the extension (good for variable names)
${c.cyan("$path")} = relative path to the file`;
28 changes: 16 additions & 12 deletions core/error-messages/template.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
var c = require("chalk");

var c = require('chalk');
module.exports = `The "${c.yellow(
"template"
)}" setting is required if the "${c.yellow("format")}" setting is an object.
${c.grey(
"The template setting dictates the overall structure of the generated file."
)}
module.exports =
`The "${c.yellow('template')}" setting is required if the "${c.yellow('format')}" setting is an object.
${c.grey('The template setting dictates the overall structure of the generated file.')}
${c.bold('Example:')}
${c.yellow('template:')} \`
${c.bold("Example:")}
${c.yellow("template:")} \`
$format[imports]
export default function(){
$format[functions]
}
\`,
${c.yellow('format:')} {
imports: "import ${c.magenta('$name')} from '${c.cyan('$path')}';",
functions: " ${c.magenta('$name')}();",
${c.yellow("format:")} {
imports: "import ${c.magenta("$name")} from '${c.cyan("$path")}';",
functions: " ${c.magenta("$name")}();",
}
${c.magenta('$name')} = name of the file minus the extension (good for variable names)
${c.cyan('$path')} = relative path to the file`;
${c.magenta(
"$name"
)} = name of the file minus the extension (good for variable names)
${c.cyan("$path")} = relative path to the file`;
7 changes: 4 additions & 3 deletions core/error-messages/warn-template.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var c = require("chalk");

var c = require('chalk');

module.exports = `The "${c.yellow('template')}" setting is ignored if the "${c.yellow('format')}" setting is a string.`;
module.exports = `The "${c.yellow(
"template"
)}" setting is ignored if the "${c.yellow("format")}" setting is a string.`;
14 changes: 6 additions & 8 deletions core/file_manipulation/create_file.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
var join = require('../helpers/join')

var join = require('../helpers/join');

module.exports = function create_file (inspirationFile, opt, newContent) {

module.exports = function create_file(inspirationFile, opt, newContent) {
//Creates a new file based on the old one
var newFile = inspirationFile.clone({contents: false});
var newFile = inspirationFile.clone({ contents: false })

//Sets the new file name
newFile.path = join([inspirationFile.base, opt.fileName]);
newFile.path = join([inspirationFile.base, opt.fileName])

newFile.contents = new Buffer(newContent, "utf-8");
newFile.contents = new Buffer.from(newContent, 'utf-8')

return newFile;
return newFile
}
19 changes: 9 additions & 10 deletions core/file_manipulation/read_file.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

var fs = require('fs');
var fs = require("fs");

module.exports = function read_file(generatedFilePath) {
return new Promise((resolve, reject) => {
fs.readFile(generatedFilePath, (error, data) => {
if(error) reject(error);
var oldContent = data.toString();
resolve(oldContent);
})
})
}
return new Promise((resolve, reject) => {
fs.readFile(generatedFilePath, (error, data) => {
if (error) reject(error);
var oldContent = data.toString();
resolve(oldContent);
});
});
};
9 changes: 4 additions & 5 deletions core/formatters/formatName.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var path = require("path");

var path = require('path');

module.exports = function formatName (filePath, format, uniqueSet) {
module.exports = function formatName(filePath, format, uniqueSet) {
var ext = path.extname(filePath);
var name = path.basename(filePath, ext);
var safeName = name.replace(/\W/g,'_');
var safeName = name.replace(/\W/g, "_");
var uniqueName = uniqueSet.add(safeName);
return format.replace(/\$name/g, uniqueName);
}
};
9 changes: 4 additions & 5 deletions core/formatters/formatPath.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var err = require("../helpers/err");
var path_error = require("../error-messages/$path");

var err = require('../helpers/err');
var path_error = require('../error-messages/$path');

module.exports = function formatPath (filePath, format) {
module.exports = function formatPath(filePath, format) {
var regEx = /\$path/g;
var pathCount = (format.match(regEx) || []).length;
err(pathCount > 1, path_error);
return format.replace(regEx, filePath);
}
};
Loading

0 comments on commit 5fd8f0b

Please sign in to comment.