Skip to content

Commit

Permalink
Merge 1441c76 into ef40ab2
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Nov 18, 2022
2 parents ef40ab2 + 1441c76 commit a829310
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 300 deletions.
23 changes: 13 additions & 10 deletions bin/svg-sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,18 @@ function addConfigMap(store, path, value) {
*/
function writeFiles(files) {
let written = 0;

for (const file of Object.values(files)) {
if (isObject(file)) {
if (file.constructor === File) {
fs.mkdirSync(path.dirname(file.path), { recursive: true });
fs.writeFileSync(file.path, file.contents);
++written;
} else {
written += writeFiles(file);
}
if (!isObject(file)) {
continue;
}

if (file.constructor === File) {
fs.mkdirSync(path.dirname(file.path), { recursive: true });
fs.writeFileSync(file.path, file.contents);
++written;
} else {
written += writeFiles(file);
}
}

Expand Down Expand Up @@ -269,9 +272,9 @@ const files = argv._.reduce((f, g) => [...f, ...glob.sync(g)], []);

for (let file of files) {
let basename = file;
// TODO: get rid of variable redefinition
file = path.resolve(file);
file = path.resolve(file); // TODO: get rid of variable redefinition
const stat = fs.lstatSync(file);

if (stat.isSymbolicLink()) {
file = fs.readlinkSync(file);
basename = path.basename(file);
Expand Down
7 changes: 2 additions & 5 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ const spriter = new SVGSpriter({
*/
function addFixtureFiles(spriter, files) {
for (const file of files) {
spriter.add(
path.resolve(path.join(cwd, file)),
file,
fs.readFileSync(path.join(cwd, file), 'utf8')
);
const filePath = path.join(cwd, file);
spriter.add(path.resolve(filePath), file, fs.readFileSync(filePath, 'utf8'));
}

return spriter;
Expand Down
114 changes: 59 additions & 55 deletions lib/svg-sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ SVGSpriter.prototype.add = function(file = '', name = '', svg = '') {
// If no vinyl file object has been given
if (!this._isVinylFile(file)) {
file = file.trim();
let error = null;
let errorMessage = null;

// If the name part of the file path is absolute
if (name && path.isAbsolute(name)) {
error = format('SVGSpriter.add: "%s" is not a valid relative file name', name);
errorMessage = format('SVGSpriter.add: "%s" is not a valid relative file name', name);
} else {
name = trimStart(name.trim(), `${path.sep}.`) || path.basename(file);
// TODO: Avoid Buffer -> String -> Buffer conversion of svg.
Expand All @@ -100,23 +100,23 @@ SVGSpriter.prototype.add = function(file = '', name = '', svg = '') {

// Argument validation
if (arguments.length < 3) {
error = 'SVGSpriter.add: You must provide 3 arguments';
errorMessage = 'SVGSpriter.add: You must provide 3 arguments';
} else if (!file.length) {
error = format('SVGSpriter.add: "%s" is not a valid absolute file name', file);
errorMessage = format('SVGSpriter.add: "%s" is not a valid absolute file name', file);
} else if (!name.length) {
error = format('SVGSpriter.add: "%s" is not a valid relative file name', name);
errorMessage = format('SVGSpriter.add: "%s" is not a valid relative file name', name);
} else if (!svg.length) {
error = 'SVGSpriter.add: You must provide SVG contents';
errorMessage = 'SVGSpriter.add: You must provide SVG contents';
} else if (!file.endsWith(name)) {
error = format('SVGSpriter.add: "%s" is not the local part of "%s"', name, file);
errorMessage = format('SVGSpriter.add: "%s" is not the local part of "%s"', name, file);
}
}

// In case of an error: Throw it!
if (error) {
const e = new ArgumentError(error);
this.error(error, e);
throw e;
if (errorMessage) {
const error = new ArgumentError(errorMessage);
this.error(errorMessage, error);
throw error;
}

// Resolve path before splitting it into base and path
Expand Down Expand Up @@ -160,7 +160,6 @@ SVGSpriter.prototype._isVinylFile = function(file) {
* @param {Function} cb Callback
*/
SVGSpriter.prototype._transformShape = function(shape, cb) {
const tasks = [];
const createTransformTask = transform => {
// If it's a custom transformer
if (isFunction(transform[1])) {
Expand All @@ -180,9 +179,11 @@ SVGSpriter.prototype._transformShape = function(shape, cb) {
return null;
};

const tasks = [];

// Run through all configured transforms
for (const t of this.config.shape.transform) {
const task = createTransformTask(t);
for (const transform of this.config.shape.transform) {
const task = createTransformTask(transform);

if (task) {
tasks.push(task);
Expand Down Expand Up @@ -243,58 +244,60 @@ SVGSpriter.prototype.compileAsync = function(config) {
* Run the next compile task
*/
SVGSpriter.prototype._compile = function() {
// If the shape queue is not currently active
if (!this._queue.active && this._compileQueue.length) {
const [config, cb] = this._compileQueue.shift();
// If the shape queue is currently active return early
if (this._queue.active || this._compileQueue.length === 0) {
return;
}

// If this is a modeless run
if (Object.keys(config).length === 0) {
const files = {};
const [config, cb] = this._compileQueue.shift();

// Add intermediate SVG files
if (this.config.shape.dest) {
files.shapes = this._getShapeFiles(this.config.shape.dest);
this.verbose('Returning %d intermediate SVG files', files.shapes.length);
}
// If this is a modeless run
if (Object.keys(config).length === 0) {
const files = {};

this._logStats(files);
cb(null, files, {});
} else {
const masterShapes = this._shapes.filter(shape => !shape.master);
this.info('Compiling %d shapes...', masterShapes.length);
// Add intermediate SVG files
if (this.config.shape.dest) {
files.shapes = this._getShapeFiles(this.config.shape.dest);
this.verbose('Returning %d intermediate SVG files', files.shapes.length);
}

this._logStats(files);
cb(null, files, {});
} else {
const masterShapes = this._shapes.filter(shape => !shape.master);
this.info('Compiling %d shapes...', masterShapes.length);

// Initialize the namespace powers
while (!this._namespacePow.length || (26 ** this._namespacePow.length < masterShapes.length)) {
this._namespacePow.unshift(26 ** this._namespacePow.length);
// Initialize the namespace powers
while (!this._namespacePow.length || (26 ** this._namespacePow.length < masterShapes.length)) {
this._namespacePow.unshift(26 ** this._namespacePow.length);

for (const shape of this._shapes) {
shape.resetNamespace();
}
for (const shape of this._shapes) {
shape.resetNamespace();
}
}

// Sort shapes by ID
this._shapes = this._shapes.sort(this.config.shape.sort);
// Sort shapes by ID
this._shapes = this._shapes.sort(this.config.shape.sort);

// Set the shape namespaces on all master shapes
for (const [index, shape] of masterShapes.entries()) {
shape.setNamespace(this._indexNamespace(index));
}
// Set the shape namespaces on all master shapes
for (const [index, shape] of masterShapes.entries()) {
shape.setNamespace(this._indexNamespace(index));
}

this._layout(config, (error, files, data) => {
// Add intermediate SVG files
if (this.config.shape.dest) {
files.shapes = this._getShapeFiles(this.config.shape.dest);
this.verbose('Returning %d intermediate SVG files', files.shapes.length);
}
this._layout(config, (error, files, data) => {
// Add intermediate SVG files
if (this.config.shape.dest) {
files.shapes = this._getShapeFiles(this.config.shape.dest);
this.verbose('Returning %d intermediate SVG files', files.shapes.length);
}

this.info('Finished %s sprite compilation', Object.keys(data).map(mode => ${mode}»`).join(' + '));
this.info('Finished %s sprite compilation', Object.keys(data).map(mode => ${mode}»`).join(' + '));

this._logStats(files);
this._logStats(files);

cb(error, files, data);
this.emit('compiled');
});
}
cb(error, files, data);
this.emit('compiled');
});
}
};

Expand Down Expand Up @@ -323,13 +326,14 @@ SVGSpriter.prototype._indexNamespace = function(index) {
* @param {Function} cb Callback
*/
SVGSpriter.prototype._layout = function(config, cb) {
const tasks = [];
const files = {};
const layout = new Layouter(this, config);
const createLayoutTask = (k, m) => _cb => {
layout.layout(files, k, m, _cb);
};

const tasks = [];

for (const [mode, value] of Object.entries(config)) {
tasks.push(createLayoutTask(mode, value.mode));
}
Expand Down

0 comments on commit a829310

Please sign in to comment.