From 8cb8245a19fc1f473e650202b3a8ce0e970beeed Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Sat, 30 Dec 2017 12:59:28 +0100 Subject: [PATCH 1/8] Update to github:assetgraph/assetgraph#da16654af8408e9035e44f019f5503daf77ae563 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 17eb1cdd..30ad731b 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ }, "homepage": "/~https://github.com/Munter/subfont#readme", "dependencies": { - "assetgraph": "^3.13.1", + "assetgraph": "github:assetgraph/assetgraph#da16654af8408e9035e44f019f5503daf77ae563", "optimist": "^0.6.1", "urltools": "^0.3.5" } From e4ec56cc724c2f3e54328cbd262807ac7d4d9c84 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Wed, 3 Jan 2018 00:00:37 +0100 Subject: [PATCH 2/8] es6ify, use async/await --- bin/subfont | 106 ++++++++++++++++++++++++++-------------------------- 1 file changed, 54 insertions(+), 52 deletions(-) diff --git a/bin/subfont b/bin/subfont index 474be502..3dbf905c 100755 --- a/bin/subfont +++ b/bin/subfont @@ -1,8 +1,8 @@ #!/usr/bin/env node -var optimist = require('optimist'); +const optimist = require('optimist'); -var commandLineOptions = optimist +const commandLineOptions = optimist .usage('Create optimal font subsets from your actual font usage.\n$0 [options] ') .options('h', { alias: 'help', @@ -58,11 +58,11 @@ var commandLineOptions = optimist .argv; if (commandLineOptions.h) { - optimist.showHelp(); - process.exit(1); + optimist.showHelp(); + process.exit(1); } -var validFontDisplayValues = [ +const validFontDisplayValues = [ 'auto', 'block', 'swap', @@ -75,15 +75,15 @@ if (validFontDisplayValues.indexOf(commandLineOptions['font-display']) === -1) { process.exit(1); } -var urlTools = require('urltools'); -var rootUrl = commandLineOptions.root && urlTools.urlOrFsPathToUrl(commandLineOptions.root, true); -var outRoot = commandLineOptions.output && urlTools.urlOrFsPathToUrl(commandLineOptions.output, true); -var inPlace = commandLineOptions['in-place']; -var inlineSubsets = commandLineOptions['inline-fonts']; -var inlineCss = commandLineOptions['inline-css']; -var fontDisplay = commandLineOptions['font-display']; -var debug = commandLineOptions.debug; -var inputUrls; +const urlTools = require('urltools'); +let rootUrl = commandLineOptions.root && urlTools.urlOrFsPathToUrl(commandLineOptions.root, true); +const outRoot = commandLineOptions.output && urlTools.urlOrFsPathToUrl(commandLineOptions.output, true); +const inPlace = commandLineOptions['in-place']; +const inlineSubsets = commandLineOptions['inline-fonts']; +const inlineCss = commandLineOptions['inline-css']; +const fontDisplay = commandLineOptions['font-display']; +const debug = commandLineOptions.debug; +let inputUrls; if (commandLineOptions._.length > 0) { inputUrls = commandLineOptions._.map(function (urlOrFsPath) { @@ -119,10 +119,10 @@ if (!inPlace && !outRoot) { process.exit(1); } -var AssetGraph = require('assetgraph'); -var query = AssetGraph.query; +const AssetGraph = require('assetgraph'); +const query = AssetGraph.query; -var assetGraphConfig = { +const assetGraphConfig = { root: rootUrl }; @@ -130,7 +130,7 @@ if (rootUrl.indexOf('file:') === -1) { assetGraphConfig.canonicalRoot = rootUrl.replace(/\/?$/, '/'); // Ensure trailing slash } -var followRelationsQuery = { +const followRelationsQuery = { crossorigin: false }; @@ -139,44 +139,46 @@ if (!commandLineOptions.recursive) { } var assetGraph = new AssetGraph(assetGraphConfig); -assetGraph - .logEvents() - .loadAssets(inputUrls) - .populate({ +(async () => { + await assetGraph.logEvents(); + await assetGraph.loadAssets(inputUrls); + await assetGraph.populate({ followRelations: followRelationsQuery - }) - .subsetFonts({ - debug: debug, - inlineSubsets: inlineSubsets, - inlineCss: inlineCss, - fontDisplay: fontDisplay - }) - .queue(function omitFunctionCalls(assetGraph) { - assetGraph.findRelations({type: 'JavaScriptStaticUrl', to: {isLoaded: true}}).forEach(function (relation) { - relation.omitFunctionCall(); - }); - }) - .if(rootUrl.indexOf('file:') === -1) - .queue(function rootRelativeRelations(assetGraph) { - assetGraph.findRelations().forEach(function (relation) { - if (relation.hrefType === 'protocolRelative' || relation.hrefType === 'absolute') { - relation.hrefType = 'rootRelative'; - } - }) - }) - .moveAssets({ type: 'Html', isLoaded: true, isInline: false, fileName: query.or('', undefined) }, function (asset, assetGraph) { - var nextSuffixToTry = 0; - var url; + }); + await assetGraph.subsetFonts({ + debug, + inlineSubsets, + inlineCss, + fontDisplay + }); + + // Omit function calls: + assetGraph.findRelations({type: 'JavaScriptStaticUrl', to: {isLoaded: true}}).forEach(function (relation) { + relation.omitFunctionCall(); + }); + + if (!rootUrl.startsWith('file:')) { + // Root-relative relations: + + for (const relation of assetGraph.findRelations()) { + if (relation.hrefType === 'protocolRelative' || relation.hrefType === 'absolute') { + relation.hrefType = 'rootRelative'; + } + } + + await assetGraph.moveAssets({ type: 'Html', isLoaded: true, isInline: false, fileName: query.or('', undefined) }, function (asset, assetGraph) { + let nextSuffixToTry = 0; + let url; do { url = asset.url.replace(rootUrl, outRoot).replace(/\/?$/, '/') + 'index' + (nextSuffixToTry ? '-' + nextSuffixToTry : '') + asset.defaultExtension; nextSuffixToTry += 1; - } while (assetGraph.findAssets({ url: url }).length > 0); + } while (assetGraph.findAssets({ url }).length > 0); return url; - }) - .endif() + }); + } - .writeAssetsToDisc({ isLoaded: true, url: AssetGraph.query.createPrefixMatcher(assetGraph.root) }, outRoot, assetGraph.root) - .run(function (assetGraph) { - console.log('Output written to', outRoot); - }); + await assetGraph.writeAssetsToDisc({ isLoaded: true, url: AssetGraph.query.createPrefixMatcher(assetGraph.root) }, outRoot, assetGraph.root); + + console.log('Output written to', outRoot); +})(); From 9f5082060a1401ee948eb12aad953c9a6eabf305 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Wed, 3 Jan 2018 00:29:50 +0100 Subject: [PATCH 3/8] Update to github:assetgraph/assetgraph#8d81b6b49e86aed886a4b415aefe77a968b532b1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 30ad731b..6e8ad02c 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ }, "homepage": "/~https://github.com/Munter/subfont#readme", "dependencies": { - "assetgraph": "github:assetgraph/assetgraph#da16654af8408e9035e44f019f5503daf77ae563", + "assetgraph": "github:assetgraph/assetgraph#8d81b6b49e86aed886a4b415aefe77a968b532b1", "optimist": "^0.6.1", "urltools": "^0.3.5" } From a9cde7f51ac459708e291b7f5c5ed1da849e9e8c Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Wed, 3 Jan 2018 00:31:04 +0100 Subject: [PATCH 4/8] Get the subset info from the fulfilment value of the subsetFonts transform Don't exit the process when --debug is given --- bin/subfont | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/subfont b/bin/subfont index 3dbf905c..caabd44f 100755 --- a/bin/subfont +++ b/bin/subfont @@ -145,8 +145,7 @@ var assetGraph = new AssetGraph(assetGraphConfig); await assetGraph.populate({ followRelations: followRelationsQuery }); - await assetGraph.subsetFonts({ - debug, + const { fontInfo } = await assetGraph.subsetFonts({ inlineSubsets, inlineCss, fontDisplay @@ -180,5 +179,8 @@ var assetGraph = new AssetGraph(assetGraphConfig); await assetGraph.writeAssetsToDisc({ isLoaded: true, url: AssetGraph.query.createPrefixMatcher(assetGraph.root) }, outRoot, assetGraph.root); + if (debug) { + console.log(require('util').inspect(fontInfo, false, 99)); + } console.log('Output written to', outRoot); })(); From e47d3629fe0881a6aa4141555bd88119ab25edd8 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Wed, 7 Mar 2018 22:24:35 +0100 Subject: [PATCH 5/8] Update assetgraph to ^4.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6e8ad02c..efbd5912 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ }, "homepage": "/~https://github.com/Munter/subfont#readme", "dependencies": { - "assetgraph": "github:assetgraph/assetgraph#8d81b6b49e86aed886a4b415aefe77a968b532b1", + "assetgraph": "^4.0.0", "optimist": "^0.6.1", "urltools": "^0.3.5" } From ddd318ad35118a834bbd4c2bdddab2e24018cea0 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Wed, 7 Mar 2018 22:29:39 +0100 Subject: [PATCH 6/8] Update query syntax --- bin/subfont | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/bin/subfont b/bin/subfont index caabd44f..db2a6984 100755 --- a/bin/subfont +++ b/bin/subfont @@ -120,7 +120,6 @@ if (!inPlace && !outRoot) { } const AssetGraph = require('assetgraph'); -const query = AssetGraph.query; const assetGraphConfig = { root: rootUrl @@ -135,7 +134,7 @@ const followRelationsQuery = { }; if (!commandLineOptions.recursive) { - followRelationsQuery.type = AssetGraph.query.not(/Anchor$/); + followRelationsQuery.type = { $not: { $regex: /Anchor$/ } }; } var assetGraph = new AssetGraph(assetGraphConfig); @@ -165,7 +164,7 @@ var assetGraph = new AssetGraph(assetGraphConfig); } } - await assetGraph.moveAssets({ type: 'Html', isLoaded: true, isInline: false, fileName: query.or('', undefined) }, function (asset, assetGraph) { + await assetGraph.moveAssets({ type: 'Html', isLoaded: true, isInline: false, fileName: { $or: ['', undefined] } }, function (asset, assetGraph) { let nextSuffixToTry = 0; let url; do { @@ -177,7 +176,7 @@ var assetGraph = new AssetGraph(assetGraphConfig); }); } - await assetGraph.writeAssetsToDisc({ isLoaded: true, url: AssetGraph.query.createPrefixMatcher(assetGraph.root) }, outRoot, assetGraph.root); + await assetGraph.writeAssetsToDisc({ isLoaded: true, url: url => url.startsWith(assetGraph.root) }, outRoot, assetGraph.root); if (debug) { console.log(require('util').inspect(fontInfo, false, 99)); From 1a9248f0a05f70391ab4ba62eea33fcbe31a6e96 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Wed, 7 Mar 2018 22:38:38 +0100 Subject: [PATCH 7/8] Fix error reporting --- bin/subfont | 73 ++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/bin/subfont b/bin/subfont index db2a6984..d63cbd5b 100755 --- a/bin/subfont +++ b/bin/subfont @@ -139,47 +139,52 @@ if (!commandLineOptions.recursive) { var assetGraph = new AssetGraph(assetGraphConfig); (async () => { - await assetGraph.logEvents(); - await assetGraph.loadAssets(inputUrls); - await assetGraph.populate({ - followRelations: followRelationsQuery - }); - const { fontInfo } = await assetGraph.subsetFonts({ - inlineSubsets, - inlineCss, - fontDisplay - }); + try { + await assetGraph.logEvents(); + await assetGraph.loadAssets(inputUrls); + await assetGraph.populate({ + followRelations: followRelationsQuery + }); + const { fontInfo } = await assetGraph.subsetFonts({ + inlineSubsets, + inlineCss, + fontDisplay + }); - // Omit function calls: - assetGraph.findRelations({type: 'JavaScriptStaticUrl', to: {isLoaded: true}}).forEach(function (relation) { - relation.omitFunctionCall(); - }); + // Omit function calls: + assetGraph.findRelations({type: 'JavaScriptStaticUrl', to: {isLoaded: true}}).forEach(function (relation) { + relation.omitFunctionCall(); + }); - if (!rootUrl.startsWith('file:')) { - // Root-relative relations: + if (!rootUrl.startsWith('file:')) { + // Root-relative relations: - for (const relation of assetGraph.findRelations()) { - if (relation.hrefType === 'protocolRelative' || relation.hrefType === 'absolute') { - relation.hrefType = 'rootRelative'; + for (const relation of assetGraph.findRelations()) { + if (relation.hrefType === 'protocolRelative' || relation.hrefType === 'absolute') { + relation.hrefType = 'rootRelative'; + } } - } - await assetGraph.moveAssets({ type: 'Html', isLoaded: true, isInline: false, fileName: { $or: ['', undefined] } }, function (asset, assetGraph) { - let nextSuffixToTry = 0; - let url; - do { - url = asset.url.replace(rootUrl, outRoot).replace(/\/?$/, '/') + 'index' + (nextSuffixToTry ? '-' + nextSuffixToTry : '') + asset.defaultExtension; - nextSuffixToTry += 1; - } while (assetGraph.findAssets({ url }).length > 0); + await assetGraph.moveAssets({ type: 'Html', isLoaded: true, isInline: false, fileName: { $or: ['', undefined] } }, function (asset, assetGraph) { + let nextSuffixToTry = 0; + let url; + do { + url = asset.url.replace(rootUrl, outRoot).replace(/\/?$/, '/') + 'index' + (nextSuffixToTry ? '-' + nextSuffixToTry : '') + asset.defaultExtension; + nextSuffixToTry += 1; + } while (assetGraph.findAssets({ url }).length > 0); - return url; - }); - } + return url; + }); + } - await assetGraph.writeAssetsToDisc({ isLoaded: true, url: url => url.startsWith(assetGraph.root) }, outRoot, assetGraph.root); + await assetGraph.writeAssetsToDisc({ isLoaded: true, url: url => url.startsWith(assetGraph.root) }, outRoot, assetGraph.root); - if (debug) { - console.log(require('util').inspect(fontInfo, false, 99)); + if (debug) { + console.log(require('util').inspect(fontInfo, false, 99)); + } + console.log('Output written to', outRoot); + } catch (err) { + console.log(err.stack); + process.exit(1); } - console.log('Output written to', outRoot); })(); From af689e12e414bbe04b7fe31fafd2349df3c86d07 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Wed, 7 Mar 2018 23:09:43 +0100 Subject: [PATCH 8/8] Simplify --- bin/subfont | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/bin/subfont b/bin/subfont index d63cbd5b..8b4731db 100755 --- a/bin/subfont +++ b/bin/subfont @@ -166,18 +166,14 @@ var assetGraph = new AssetGraph(assetGraphConfig); } await assetGraph.moveAssets({ type: 'Html', isLoaded: true, isInline: false, fileName: { $or: ['', undefined] } }, function (asset, assetGraph) { - let nextSuffixToTry = 0; - let url; - do { - url = asset.url.replace(rootUrl, outRoot).replace(/\/?$/, '/') + 'index' + (nextSuffixToTry ? '-' + nextSuffixToTry : '') + asset.defaultExtension; - nextSuffixToTry += 1; - } while (assetGraph.findAssets({ url }).length > 0); - - return url; + return asset.url.replace(/\/?$/, '/') + 'index' + asset.defaultExtension; }); } - await assetGraph.writeAssetsToDisc({ isLoaded: true, url: url => url.startsWith(assetGraph.root) }, outRoot, assetGraph.root); + await assetGraph.writeAssetsToDisc({ + isLoaded: true, + url: url => url.startsWith(assetGraph.root) + }, outRoot, assetGraph.root); if (debug) { console.log(require('util').inspect(fontInfo, false, 99));