-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'rename-to-auto-imports' into master
- Loading branch information
Showing
65 changed files
with
2,626 additions
and
2,060 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()." | ||
)}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
)}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
Oops, something went wrong.