@iconify/tools 2.2.6 → 3.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +8 -2
  2. package/lib/colors/detect.cjs +4 -4
  3. package/lib/colors/detect.d.ts +1 -1
  4. package/lib/colors/detect.mjs +5 -5
  5. package/lib/colors/parse.cjs +275 -129
  6. package/lib/colors/parse.d.ts +14 -5
  7. package/lib/colors/parse.mjs +278 -133
  8. package/lib/colors/validate.cjs +11 -2
  9. package/lib/colors/validate.d.ts +10 -2
  10. package/lib/colors/validate.mjs +12 -4
  11. package/lib/download/api/download.cjs +4 -4
  12. package/lib/download/api/download.mjs +4 -4
  13. package/lib/download/api/index.cjs +0 -1
  14. package/lib/download/api/index.mjs +0 -1
  15. package/lib/download/git/index.cjs +1 -1
  16. package/lib/download/git/index.mjs +1 -1
  17. package/lib/download/git/reset.cjs +1 -1
  18. package/lib/download/git/reset.mjs +1 -1
  19. package/lib/download/github/hash.cjs +0 -1
  20. package/lib/download/github/hash.mjs +0 -1
  21. package/lib/download/github/index.cjs +1 -1
  22. package/lib/download/github/index.mjs +1 -1
  23. package/lib/download/gitlab/hash.cjs +0 -1
  24. package/lib/download/gitlab/hash.mjs +0 -1
  25. package/lib/download/gitlab/index.cjs +1 -1
  26. package/lib/download/gitlab/index.mjs +1 -1
  27. package/lib/download/helpers/unzip.cjs +5 -1
  28. package/lib/download/index.cjs +1 -1
  29. package/lib/download/index.mjs +1 -1
  30. package/lib/download/npm/index.cjs +1 -1
  31. package/lib/download/npm/index.mjs +1 -1
  32. package/lib/icon-set/index.cjs +18 -0
  33. package/lib/icon-set/index.d.ts +7 -1
  34. package/lib/icon-set/index.mjs +18 -0
  35. package/lib/icon-set/tags.cjs +2 -2
  36. package/lib/icon-set/tags.d.ts +1 -1
  37. package/lib/icon-set/tags.mjs +2 -2
  38. package/lib/icon-set/types.d.ts +5 -4
  39. package/lib/import/directory.cjs +101 -28
  40. package/lib/import/directory.d.ts +12 -5
  41. package/lib/import/directory.mjs +103 -31
  42. package/lib/import/figma/index.cjs +1 -2
  43. package/lib/import/figma/index.mjs +1 -2
  44. package/lib/import/figma/query.cjs +0 -1
  45. package/lib/import/figma/query.mjs +0 -1
  46. package/lib/index.cjs +7 -1
  47. package/lib/index.d.ts +6 -6
  48. package/lib/index.mjs +7 -7
  49. package/lib/misc/scan.cjs +61 -8
  50. package/lib/misc/scan.d.ts +12 -6
  51. package/lib/misc/scan.mjs +62 -10
  52. package/lib/svg/analyse.cjs +2 -2
  53. package/lib/svg/analyse.d.ts +1 -1
  54. package/lib/svg/analyse.mjs +4 -4
  55. package/lib/svg/cleanup/attribs.cjs +2 -2
  56. package/lib/svg/cleanup/attribs.d.ts +1 -1
  57. package/lib/svg/cleanup/attribs.mjs +3 -3
  58. package/lib/svg/cleanup/bad-tags.cjs +2 -2
  59. package/lib/svg/cleanup/bad-tags.d.ts +1 -1
  60. package/lib/svg/cleanup/bad-tags.mjs +3 -3
  61. package/lib/svg/cleanup/inline-style.cjs +2 -2
  62. package/lib/svg/cleanup/inline-style.d.ts +1 -1
  63. package/lib/svg/cleanup/inline-style.mjs +3 -3
  64. package/lib/svg/cleanup/root-style.cjs +1 -1
  65. package/lib/svg/cleanup/root-style.d.ts +1 -5
  66. package/lib/svg/cleanup/root-style.mjs +2 -2
  67. package/lib/svg/cleanup/svgo-style.cjs +2 -2
  68. package/lib/svg/cleanup/svgo-style.d.ts +1 -1
  69. package/lib/svg/cleanup/svgo-style.mjs +3 -3
  70. package/lib/svg/cleanup.cjs +6 -6
  71. package/lib/svg/cleanup.d.ts +1 -1
  72. package/lib/svg/cleanup.mjs +6 -6
  73. package/lib/svg/index.cjs +5 -1
  74. package/lib/svg/parse-style.cjs +149 -89
  75. package/lib/svg/parse-style.d.ts +6 -1
  76. package/lib/svg/parse-style.mjs +150 -91
  77. package/lib/svg/parse.cjs +56 -18
  78. package/lib/svg/parse.d.ts +8 -2
  79. package/lib/svg/parse.mjs +56 -19
  80. package/package.json +15 -17
@@ -1,7 +1,7 @@
1
- import { promises } from 'fs';
1
+ import { promises, readFileSync } from 'fs';
2
2
  import { blankIconSet } from '../icon-set/index.mjs';
3
3
  import { cleanupIconKeyword } from '../misc/keyword.mjs';
4
- import { scanDirectory } from '../misc/scan.mjs';
4
+ import { scanDirectory, scanDirectorySync } from '../misc/scan.mjs';
5
5
  import { SVG } from '../svg/index.mjs';
6
6
  import { cleanupSVG } from '../svg/cleanup.mjs';
7
7
  import '@iconify/utils/lib/icon/defaults';
@@ -33,45 +33,117 @@ import '../optimise/svgo.mjs';
33
33
  import 'svgo';
34
34
  import '@iconify/utils/lib/svg/id';
35
35
 
36
- async function importDirectory(path, options = {}) {
37
- const files = await scanDirectory(path, (ext, file, subdir, path2) => {
38
- if (ext.toLowerCase() === ".svg") {
36
+ function importDir(iconSet, options, getKeyword, files, readFile, done) {
37
+ let i = 0;
38
+ const next = () => {
39
+ if (i >= files.length) {
40
+ return done(iconSet);
41
+ }
42
+ const file = files[i];
43
+ i++;
44
+ const defaultKeyword = cleanupIconKeyword(file.file);
45
+ getKeyword([file, defaultKeyword, iconSet], (keyword) => {
46
+ if (typeof keyword !== "string" || !keyword.length) {
47
+ return next();
48
+ }
49
+ readFile(
50
+ file.path + file.subdir + file.file + file.ext,
51
+ (content) => {
52
+ try {
53
+ const svg = new SVG(content);
54
+ cleanupSVG(svg);
55
+ iconSet.fromSVG(keyword, svg);
56
+ } catch (err) {
57
+ if (options.ignoreImportErrors !== false) {
58
+ throw err;
59
+ }
60
+ }
61
+ next();
62
+ }
63
+ );
64
+ });
65
+ };
66
+ next();
67
+ }
68
+ function isValidFile(item) {
69
+ return item.ext.toLowerCase() === ".svg";
70
+ }
71
+ function importDirectory(path, options = {}) {
72
+ return new Promise((fulfill, reject) => {
73
+ scanDirectory(path, (ext, file, subdir, path2) => {
39
74
  const result = {
40
75
  file,
41
76
  ext,
42
77
  subdir,
43
78
  path: path2
44
79
  };
45
- return result;
46
- }
47
- return false;
80
+ return isValidFile(result) ? result : false;
81
+ }).then((files) => {
82
+ const iconSet = blankIconSet(options.prefix || "");
83
+ try {
84
+ importDir(
85
+ iconSet,
86
+ options,
87
+ (params, done) => {
88
+ if (options.keyword) {
89
+ const result = options.keyword(...params);
90
+ if (result instanceof Promise) {
91
+ result.then(done).catch(reject);
92
+ } else {
93
+ done(result);
94
+ }
95
+ } else {
96
+ done(params[1]);
97
+ }
98
+ },
99
+ files,
100
+ (filename, done) => {
101
+ promises.readFile(filename, "utf8").then(done).catch(reject);
102
+ },
103
+ fulfill
104
+ );
105
+ } catch (err) {
106
+ reject(err);
107
+ }
108
+ }).catch(reject);
109
+ });
110
+ }
111
+ function importDirectorySync(path, options = {}) {
112
+ const files = scanDirectorySync(path, (ext, file, subdir, path2) => {
113
+ const result = {
114
+ file,
115
+ ext,
116
+ subdir,
117
+ path: path2
118
+ };
119
+ return isValidFile(result) ? result : false;
48
120
  });
49
121
  const iconSet = blankIconSet(options.prefix || "");
50
- for (let i = 0; i < files.length; i++) {
51
- const file = files[i];
52
- const defaultKeyword = cleanupIconKeyword(file.file);
53
- let keyword = options.keyword ? options.keyword(file, defaultKeyword, iconSet) : defaultKeyword;
54
- if (keyword instanceof Promise) {
55
- keyword = await keyword;
56
- }
57
- if (typeof keyword !== "string" || !keyword.length) {
58
- continue;
59
- }
60
- try {
61
- const content = await promises.readFile(
62
- file.path + file.subdir + file.file + file.ext,
63
- "utf8"
64
- );
65
- const svg = new SVG(content);
66
- await cleanupSVG(svg);
67
- iconSet.fromSVG(keyword, svg);
68
- } catch (err) {
69
- if (options.ignoreImportErrors !== false) {
70
- throw err;
122
+ let isSync = true;
123
+ importDir(
124
+ iconSet,
125
+ options,
126
+ (params, done) => {
127
+ if (options.keyword) {
128
+ done(options.keyword(...params));
129
+ } else {
130
+ done(params[1]);
131
+ }
132
+ },
133
+ files,
134
+ (filename, done) => {
135
+ done(readFileSync(filename, "utf8"));
136
+ },
137
+ () => {
138
+ if (!isSync) {
139
+ throw new Error(
140
+ "importDirectorySync supposed to be synchronous"
141
+ );
71
142
  }
72
143
  }
73
- }
144
+ );
145
+ isSync = false;
74
146
  return iconSet;
75
147
  }
76
148
 
77
- export { importDirectory };
149
+ export { importDirectory, importDirectorySync };
@@ -34,7 +34,6 @@ require('../../optimise/svgo.cjs');
34
34
  require('svgo');
35
35
  require('@iconify/utils/lib/svg/id');
36
36
  require('../../download/api/index.cjs');
37
- require('node-fetch');
38
37
  require('../../download/api/cache.cjs');
39
38
  require('fs');
40
39
  require('crypto');
@@ -81,7 +80,7 @@ async function importFromFigma(options) {
81
80
  }
82
81
  try {
83
82
  const svg = new svg_index.SVG(item.content);
84
- await svg_cleanup.cleanupSVG(svg);
83
+ svg_cleanup.cleanupSVG(svg);
85
84
  iconSet.fromSVG(item.keyword, svg);
86
85
  } catch (err) {
87
86
  missing.push(item);
@@ -32,7 +32,6 @@ import '../../optimise/svgo.mjs';
32
32
  import 'svgo';
33
33
  import '@iconify/utils/lib/svg/id';
34
34
  import '../../download/api/index.mjs';
35
- import 'node-fetch';
36
35
  import '../../download/api/cache.mjs';
37
36
  import 'fs';
38
37
  import 'crypto';
@@ -79,7 +78,7 @@ async function importFromFigma(options) {
79
78
  }
80
79
  try {
81
80
  const svg = new SVG(item.content);
82
- await cleanupSVG(svg);
81
+ cleanupSVG(svg);
83
82
  iconSet.fromSVG(item.keyword, svg);
84
83
  } catch (err) {
85
84
  missing.push(item);
@@ -2,7 +2,6 @@
2
2
 
3
3
  const download_api_index = require('../../download/api/index.cjs');
4
4
  const download_api_cache = require('../../download/api/cache.cjs');
5
- require('node-fetch');
6
5
  require('fs');
7
6
  require('crypto');
8
7
  require('../../misc/scan.cjs');
@@ -1,6 +1,5 @@
1
1
  import { sendAPIQuery } from '../../download/api/index.mjs';
2
2
  import { apiCacheKey, getAPICache, clearAPICache } from '../../download/api/cache.mjs';
3
- import 'node-fetch';
4
3
  import 'fs';
5
4
  import 'crypto';
6
5
  import '../../misc/scan.mjs';
package/lib/index.cjs CHANGED
@@ -78,11 +78,11 @@ require('./import/figma/query.cjs');
78
78
  require('./download/api/cache.cjs');
79
79
  require('fs');
80
80
  require('crypto');
81
- require('node-fetch');
82
81
  require('pathe');
83
82
  require('child_process');
84
83
  require('util');
85
84
  require('stream');
85
+ require('fs/promises');
86
86
  require('extract-zip');
87
87
  require('tar');
88
88
  require('./download/gitlab/types.cjs');
@@ -92,7 +92,9 @@ require('./export/helpers/custom-files.cjs');
92
92
 
93
93
  exports.SVG = svg_index.SVG;
94
94
  exports.parseSVG = svg_parse.parseSVG;
95
+ exports.parseSVGSync = svg_parse.parseSVGSync;
95
96
  exports.parseSVGStyle = svg_parseStyle.parseSVGStyle;
97
+ exports.parseSVGStyleSync = svg_parseStyle.parseSVGStyleSync;
96
98
  exports.analyseSVGStructure = svg_analyse.analyseSVGStructure;
97
99
  exports.cleanupSVG = svg_cleanup.cleanupSVG;
98
100
  exports.removeBadAttributes = svg_cleanup_attribs.removeBadAttributes;
@@ -107,6 +109,7 @@ exports.mergeIconSets = iconSet_merge.mergeIconSets;
107
109
  exports.addTagsToIconSet = iconSet_tags.addTagsToIconSet;
108
110
  exports.importFromFigma = import_figma_index.importFromFigma;
109
111
  exports.importDirectory = import_directory.importDirectory;
112
+ exports.importDirectorySync = import_directory.importDirectorySync;
110
113
  exports.downloadGitRepo = download_git_index.downloadGitRepo;
111
114
  exports.getGitRepoHash = download_git_hash.getGitRepoHash;
112
115
  exports.getGitRepoBranch = download_git_branch.getGitRepoBranch;
@@ -122,7 +125,9 @@ exports.downloadPackage = download_index.downloadPackage;
122
125
  exports.downloadFile = download_api_download.downloadFile;
123
126
  exports.isEmptyColor = colors_parse.isEmptyColor;
124
127
  exports.parseColors = colors_parse.parseColors;
128
+ exports.parseColorsSync = colors_parse.parseColorsSync;
125
129
  exports.validateColors = colors_validate.validateColors;
130
+ exports.validateColorsSync = colors_validate.validateColorsSync;
126
131
  exports.detectIconSetPalette = colors_detect.detectIconSetPalette;
127
132
  exports.runSVGO = optimise_svgo.runSVGO;
128
133
  exports.deOptimisePaths = optimise_flags.deOptimisePaths;
@@ -135,6 +140,7 @@ exports.exportJSONPackage = export_jsonPackage.exportJSONPackage;
135
140
  exports.writeJSONFile = misc_writeJson.writeJSONFile;
136
141
  exports.prepareDirectoryForExport = export_helpers_prepare.prepareDirectoryForExport;
137
142
  exports.scanDirectory = misc_scan.scanDirectory;
143
+ exports.scanDirectorySync = misc_scan.scanDirectorySync;
138
144
  exports.compareDirectories = misc_compareDirs.compareDirectories;
139
145
  exports.unzip = download_helpers_unzip.unzip;
140
146
  exports.untar = download_helpers_untar.untar;
package/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { SVG } from './svg/index.js';
2
- export { parseSVG } from './svg/parse.js';
3
- export { parseSVGStyle } from './svg/parse-style.js';
2
+ export { parseSVG, parseSVGSync } from './svg/parse.js';
3
+ export { parseSVGStyle, parseSVGStyleSync } from './svg/parse-style.js';
4
4
  export { analyseSVGStructure } from './svg/analyse.js';
5
5
  export { cleanupSVG } from './svg/cleanup.js';
6
6
  export { removeBadAttributes } from './svg/cleanup/attribs.js';
@@ -13,7 +13,7 @@ export { IconSet, blankIconSet } from './icon-set/index.js';
13
13
  export { mergeIconSets } from './icon-set/merge.js';
14
14
  export { addTagsToIconSet } from './icon-set/tags.js';
15
15
  export { importFromFigma } from './import/figma/index.js';
16
- export { importDirectory } from './import/directory.js';
16
+ export { importDirectory, importDirectorySync } from './import/directory.js';
17
17
  export { downloadGitRepo } from './download/git/index.js';
18
18
  export { getGitRepoHash } from './download/git/hash.js';
19
19
  export { getGitRepoBranch } from './download/git/branch.js';
@@ -26,8 +26,8 @@ export { downloadNPMPackage } from './download/npm/index.js';
26
26
  export { getNPMVersion, getPackageVersion } from './download/npm/version.js';
27
27
  export { downloadPackage } from './download/index.js';
28
28
  export { downloadFile } from './download/api/download.js';
29
- export { isEmptyColor, parseColors } from './colors/parse.js';
30
- export { validateColors } from './colors/validate.js';
29
+ export { isEmptyColor, parseColors, parseColorsSync } from './colors/parse.js';
30
+ export { validateColors, validateColorsSync } from './colors/validate.js';
31
31
  export { detectIconSetPalette } from './colors/detect.js';
32
32
  export { runSVGO } from './optimise/svgo.js';
33
33
  export { deOptimisePaths } from './optimise/flags.js';
@@ -39,7 +39,7 @@ export { exportIconPackage } from './export/icon-package.js';
39
39
  export { exportJSONPackage } from './export/json-package.js';
40
40
  export { writeJSONFile } from './misc/write-json.js';
41
41
  export { prepareDirectoryForExport } from './export/helpers/prepare.js';
42
- export { scanDirectory } from './misc/scan.js';
42
+ export { scanDirectory, scanDirectorySync } from './misc/scan.js';
43
43
  export { compareDirectories } from './misc/compare-dirs.js';
44
44
  export { unzip } from './download/helpers/unzip.js';
45
45
  export { untar } from './download/helpers/untar.js';
package/lib/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  export { SVG } from './svg/index.mjs';
2
- export { parseSVG } from './svg/parse.mjs';
3
- export { parseSVGStyle } from './svg/parse-style.mjs';
2
+ export { parseSVG, parseSVGSync } from './svg/parse.mjs';
3
+ export { parseSVGStyle, parseSVGStyleSync } from './svg/parse-style.mjs';
4
4
  export { analyseSVGStructure } from './svg/analyse.mjs';
5
5
  export { cleanupSVG } from './svg/cleanup.mjs';
6
6
  export { removeBadAttributes } from './svg/cleanup/attribs.mjs';
@@ -13,7 +13,7 @@ export { IconSet, blankIconSet } from './icon-set/index.mjs';
13
13
  export { mergeIconSets } from './icon-set/merge.mjs';
14
14
  export { addTagsToIconSet } from './icon-set/tags.mjs';
15
15
  export { importFromFigma } from './import/figma/index.mjs';
16
- export { importDirectory } from './import/directory.mjs';
16
+ export { importDirectory, importDirectorySync } from './import/directory.mjs';
17
17
  export { downloadGitRepo } from './download/git/index.mjs';
18
18
  export { getGitRepoHash } from './download/git/hash.mjs';
19
19
  export { getGitRepoBranch } from './download/git/branch.mjs';
@@ -26,8 +26,8 @@ export { downloadNPMPackage } from './download/npm/index.mjs';
26
26
  export { getNPMVersion, getPackageVersion } from './download/npm/version.mjs';
27
27
  export { downloadPackage } from './download/index.mjs';
28
28
  export { downloadFile } from './download/api/download.mjs';
29
- export { isEmptyColor, parseColors } from './colors/parse.mjs';
30
- export { validateColors } from './colors/validate.mjs';
29
+ export { isEmptyColor, parseColors, parseColorsSync } from './colors/parse.mjs';
30
+ export { validateColors, validateColorsSync } from './colors/validate.mjs';
31
31
  export { detectIconSetPalette } from './colors/detect.mjs';
32
32
  export { runSVGO } from './optimise/svgo.mjs';
33
33
  export { deOptimisePaths } from './optimise/flags.mjs';
@@ -39,7 +39,7 @@ export { exportIconPackage } from './export/icon-package.mjs';
39
39
  export { exportJSONPackage } from './export/json-package.mjs';
40
40
  export { writeJSONFile } from './misc/write-json.mjs';
41
41
  export { prepareDirectoryForExport } from './export/helpers/prepare.mjs';
42
- export { scanDirectory } from './misc/scan.mjs';
42
+ export { scanDirectory, scanDirectorySync } from './misc/scan.mjs';
43
43
  export { compareDirectories } from './misc/compare-dirs.mjs';
44
44
  export { unzip } from './download/helpers/unzip.mjs';
45
45
  export { untar } from './download/helpers/untar.mjs';
@@ -76,11 +76,11 @@ import './import/figma/query.mjs';
76
76
  import './download/api/cache.mjs';
77
77
  import 'fs';
78
78
  import 'crypto';
79
- import 'node-fetch';
80
79
  import 'pathe';
81
80
  import 'child_process';
82
81
  import 'util';
83
82
  import 'stream';
83
+ import 'fs/promises';
84
84
  import 'extract-zip';
85
85
  import 'tar';
86
86
  import './download/gitlab/types.mjs';
package/lib/misc/scan.cjs CHANGED
@@ -2,16 +2,32 @@
2
2
 
3
3
  const fs = require('fs');
4
4
 
5
- async function scanDirectory(path, callback, subdirs = true) {
6
- const results = [];
5
+ function cleanPath(path) {
7
6
  if (path.length && path.slice(-1) !== "/") {
8
- path += "/";
7
+ return path + "/";
9
8
  }
9
+ return path;
10
+ }
11
+ function isHidden(filename) {
12
+ return filename.slice(0, 1) === ".";
13
+ }
14
+ function split(filename) {
15
+ const parts = filename.split(".");
16
+ const ext = parts.length > 1 ? "." + parts.pop() : "";
17
+ const file = parts.join(".");
18
+ return { ext, file };
19
+ }
20
+ function isIgnoredResult(result) {
21
+ return result === void 0 || result === false || result === null;
22
+ }
23
+ async function scanDirectory(path, callback, subdirs = true) {
24
+ const results = [];
25
+ path = cleanPath(path);
10
26
  async function scan(subdir) {
11
27
  const files = await fs.promises.readdir(path + subdir);
12
28
  for (let i = 0; i < files.length; i++) {
13
29
  const filename = files[i];
14
- if (filename.slice(0, 1) === ".") {
30
+ if (isHidden(filename)) {
15
31
  continue;
16
32
  }
17
33
  const stat = await fs.promises.stat(path + subdir + filename);
@@ -24,16 +40,14 @@ async function scanDirectory(path, callback, subdirs = true) {
24
40
  if (!stat.isFile()) {
25
41
  continue;
26
42
  }
27
- const parts = filename.split(".");
28
- const ext = parts.length > 1 ? "." + parts.pop() : "";
29
- const file = parts.join(".");
43
+ const { ext, file } = split(filename);
30
44
  let callbackResult;
31
45
  if (callback) {
32
46
  callbackResult = callback(ext, file, subdir, path, stat);
33
47
  if (callbackResult instanceof Promise) {
34
48
  callbackResult = await callbackResult;
35
49
  }
36
- if (callbackResult === void 0 || callbackResult === false || callbackResult === null) {
50
+ if (isIgnoredResult(callbackResult)) {
37
51
  continue;
38
52
  }
39
53
  } else {
@@ -47,5 +61,44 @@ async function scanDirectory(path, callback, subdirs = true) {
47
61
  await scan("");
48
62
  return results;
49
63
  }
64
+ function scanDirectorySync(path, callback, subdirs = true) {
65
+ const results = [];
66
+ path = cleanPath(path);
67
+ function scan(subdir) {
68
+ const files = fs.readdirSync(path + subdir);
69
+ for (let i = 0; i < files.length; i++) {
70
+ const filename = files[i];
71
+ if (isHidden(filename)) {
72
+ continue;
73
+ }
74
+ const stat = fs.statSync(path + subdir + filename);
75
+ if (stat.isDirectory()) {
76
+ if (subdirs) {
77
+ scan(subdir + filename + "/");
78
+ }
79
+ continue;
80
+ }
81
+ if (!stat.isFile()) {
82
+ continue;
83
+ }
84
+ const { ext, file } = split(filename);
85
+ let callbackResult;
86
+ if (callback) {
87
+ callbackResult = callback(ext, file, subdir, path, stat);
88
+ if (isIgnoredResult(callbackResult)) {
89
+ continue;
90
+ }
91
+ } else {
92
+ callbackResult = true;
93
+ }
94
+ results.push(
95
+ callbackResult === true ? subdir + filename : callbackResult
96
+ );
97
+ }
98
+ }
99
+ scan("");
100
+ return results;
101
+ }
50
102
 
51
103
  exports.scanDirectory = scanDirectory;
104
+ exports.scanDirectorySync = scanDirectorySync;
@@ -15,13 +15,19 @@ import { Stats } from 'fs';
15
15
  */
16
16
  type ScanDirectoryCallbackFalseResult = boolean | null | undefined;
17
17
  type ScanDirectoryCallbackStringResult = ScanDirectoryCallbackFalseResult | string;
18
- type ScanDirectoryCallbackAsString = (ext: string, file: string, subdir: string, path: string, stat: Stats) => ScanDirectoryCallbackStringResult | Promise<ScanDirectoryCallbackStringResult>;
19
- type ScanDirectoryCallbackAsCustom<T> = (ext: string, file: string, subdir: string, path: string, stat: Stats) => T | ScanDirectoryCallbackFalseResult | Promise<T | ScanDirectoryCallbackFalseResult>;
20
- type ScanDirectoryCallback = ScanDirectoryCallbackAsCustom<unknown> | ScanDirectoryCallbackAsString;
18
+ type Callback<T> = (ext: string, file: string, subdir: string, path: string, stat: Stats) => T;
19
+ type AsyncCallback<T> = Callback<T | Promise<T>>;
20
+ type ScanDirectoryCallback = AsyncCallback<ScanDirectoryCallbackStringResult | unknown>;
21
+ type ScanDirectorySyncCallback = Callback<ScanDirectoryCallbackStringResult | unknown>;
21
22
  /**
22
23
  * Find all files in directory
23
24
  */
24
- declare function scanDirectory(path: string, callback?: ScanDirectoryCallbackAsString, subdirs?: boolean): Promise<string[]>;
25
- declare function scanDirectory<T>(path: string, callback: ScanDirectoryCallbackAsCustom<T>, subdirs?: boolean): Promise<T[]>;
25
+ declare function scanDirectory(path: string, callback?: AsyncCallback<ScanDirectoryCallbackStringResult>, subdirs?: boolean): Promise<string[]>;
26
+ declare function scanDirectory<T>(path: string, callback: AsyncCallback<T | ScanDirectoryCallbackFalseResult>, subdirs?: boolean): Promise<T[]>;
27
+ /**
28
+ * Find all files in directory, synchronously
29
+ */
30
+ declare function scanDirectorySync(path: string, callback?: Callback<ScanDirectoryCallbackStringResult>, subdirs?: boolean): string[];
31
+ declare function scanDirectorySync<T>(path: string, callback: Callback<T | ScanDirectoryCallbackFalseResult>, subdirs?: boolean): T[];
26
32
 
27
- export { ScanDirectoryCallback, scanDirectory };
33
+ export { ScanDirectoryCallback, ScanDirectorySyncCallback, scanDirectory, scanDirectorySync };
package/lib/misc/scan.mjs CHANGED
@@ -1,15 +1,31 @@
1
- import { promises } from 'fs';
1
+ import { promises, readdirSync, statSync } from 'fs';
2
2
 
3
- async function scanDirectory(path, callback, subdirs = true) {
4
- const results = [];
3
+ function cleanPath(path) {
5
4
  if (path.length && path.slice(-1) !== "/") {
6
- path += "/";
5
+ return path + "/";
7
6
  }
7
+ return path;
8
+ }
9
+ function isHidden(filename) {
10
+ return filename.slice(0, 1) === ".";
11
+ }
12
+ function split(filename) {
13
+ const parts = filename.split(".");
14
+ const ext = parts.length > 1 ? "." + parts.pop() : "";
15
+ const file = parts.join(".");
16
+ return { ext, file };
17
+ }
18
+ function isIgnoredResult(result) {
19
+ return result === void 0 || result === false || result === null;
20
+ }
21
+ async function scanDirectory(path, callback, subdirs = true) {
22
+ const results = [];
23
+ path = cleanPath(path);
8
24
  async function scan(subdir) {
9
25
  const files = await promises.readdir(path + subdir);
10
26
  for (let i = 0; i < files.length; i++) {
11
27
  const filename = files[i];
12
- if (filename.slice(0, 1) === ".") {
28
+ if (isHidden(filename)) {
13
29
  continue;
14
30
  }
15
31
  const stat = await promises.stat(path + subdir + filename);
@@ -22,16 +38,14 @@ async function scanDirectory(path, callback, subdirs = true) {
22
38
  if (!stat.isFile()) {
23
39
  continue;
24
40
  }
25
- const parts = filename.split(".");
26
- const ext = parts.length > 1 ? "." + parts.pop() : "";
27
- const file = parts.join(".");
41
+ const { ext, file } = split(filename);
28
42
  let callbackResult;
29
43
  if (callback) {
30
44
  callbackResult = callback(ext, file, subdir, path, stat);
31
45
  if (callbackResult instanceof Promise) {
32
46
  callbackResult = await callbackResult;
33
47
  }
34
- if (callbackResult === void 0 || callbackResult === false || callbackResult === null) {
48
+ if (isIgnoredResult(callbackResult)) {
35
49
  continue;
36
50
  }
37
51
  } else {
@@ -45,5 +59,43 @@ async function scanDirectory(path, callback, subdirs = true) {
45
59
  await scan("");
46
60
  return results;
47
61
  }
62
+ function scanDirectorySync(path, callback, subdirs = true) {
63
+ const results = [];
64
+ path = cleanPath(path);
65
+ function scan(subdir) {
66
+ const files = readdirSync(path + subdir);
67
+ for (let i = 0; i < files.length; i++) {
68
+ const filename = files[i];
69
+ if (isHidden(filename)) {
70
+ continue;
71
+ }
72
+ const stat = statSync(path + subdir + filename);
73
+ if (stat.isDirectory()) {
74
+ if (subdirs) {
75
+ scan(subdir + filename + "/");
76
+ }
77
+ continue;
78
+ }
79
+ if (!stat.isFile()) {
80
+ continue;
81
+ }
82
+ const { ext, file } = split(filename);
83
+ let callbackResult;
84
+ if (callback) {
85
+ callbackResult = callback(ext, file, subdir, path, stat);
86
+ if (isIgnoredResult(callbackResult)) {
87
+ continue;
88
+ }
89
+ } else {
90
+ callbackResult = true;
91
+ }
92
+ results.push(
93
+ callbackResult === true ? subdir + filename : callbackResult
94
+ );
95
+ }
96
+ }
97
+ scan("");
98
+ return results;
99
+ }
48
100
 
49
- export { scanDirectory };
101
+ export { scanDirectory, scanDirectorySync };
@@ -5,7 +5,7 @@ const svg_data_attributes = require('./data/attributes.cjs');
5
5
  const svg_data_tags = require('./data/tags.cjs');
6
6
  const svg_analyse_error = require('./analyse/error.cjs');
7
7
 
8
- async function analyseSVGStructure(svg, options = {}) {
8
+ function analyseSVGStructure(svg, options = {}) {
9
9
  const fixErrors = options.fixErrors;
10
10
  let root = svg.$svg(":root").get(0);
11
11
  if (root._parsed) {
@@ -83,7 +83,7 @@ async function analyseSVGStructure(svg, options = {}) {
83
83
  element._linksTo.push(link);
84
84
  }
85
85
  let index = 0;
86
- await svg_parse.parseSVG(svg, (item) => {
86
+ svg_parse.parseSVGSync(svg, (item) => {
87
87
  const { tagName, parents } = item;
88
88
  if (svg_data_tags.styleTag.has(tagName)) {
89
89
  item.testChildren = false;
@@ -8,6 +8,6 @@ import '@iconify/utils/lib/customisations/defaults';
8
8
  *
9
9
  * Before running this function run cleanup functions to change inline style to attributes and fix attributes
10
10
  */
11
- declare function analyseSVGStructure(svg: SVG, options?: AnalyseSVGStructureOptions): Promise<AnalyseSVGStructureResult>;
11
+ declare function analyseSVGStructure(svg: SVG, options?: AnalyseSVGStructureOptions): AnalyseSVGStructureResult;
12
12
 
13
13
  export { analyseSVGStructure };
@@ -1,9 +1,9 @@
1
- import { parseSVG } from './parse.mjs';
1
+ import { parseSVGSync } from './parse.mjs';
2
2
  import { tagSpecificNonPresentationalAttributes, urlPresentationalAttributes, commonColorPresentationalAttributes, markerAttributes } from './data/attributes.mjs';
3
- import { styleTag, maskTags, reusableElementsWithPalette, defsTag, useTag } from './data/tags.mjs';
3
+ import { useTag, styleTag, maskTags, reusableElementsWithPalette, defsTag } from './data/tags.mjs';
4
4
  import { analyseTagError } from './analyse/error.mjs';
5
5
 
6
- async function analyseSVGStructure(svg, options = {}) {
6
+ function analyseSVGStructure(svg, options = {}) {
7
7
  const fixErrors = options.fixErrors;
8
8
  let root = svg.$svg(":root").get(0);
9
9
  if (root._parsed) {
@@ -81,7 +81,7 @@ async function analyseSVGStructure(svg, options = {}) {
81
81
  element._linksTo.push(link);
82
82
  }
83
83
  let index = 0;
84
- await parseSVG(svg, (item) => {
84
+ parseSVGSync(svg, (item) => {
85
85
  const { tagName, parents } = item;
86
86
  if (styleTag.has(tagName)) {
87
87
  item.testChildren = false;
@@ -4,8 +4,8 @@ const svg_data_attributes = require('../data/attributes.cjs');
4
4
  const svg_data_tags = require('../data/tags.cjs');
5
5
  const svg_parse = require('../parse.cjs');
6
6
 
7
- async function removeBadAttributes(svg) {
8
- await svg_parse.parseSVG(svg, (item) => {
7
+ function removeBadAttributes(svg) {
8
+ svg_parse.parseSVGSync(svg, (item) => {
9
9
  const tagName = item.tagName;
10
10
  const attribs = item.element.attribs;
11
11
  const $element = item.$element;
@@ -5,6 +5,6 @@ import '@iconify/utils/lib/customisations/defaults';
5
5
  /**
6
6
  * Remove useless attributes
7
7
  */
8
- declare function removeBadAttributes(svg: SVG): Promise<void>;
8
+ declare function removeBadAttributes(svg: SVG): void;
9
9
 
10
10
  export { removeBadAttributes };
@@ -1,9 +1,9 @@
1
1
  import { badAttributes, badSoftwareAttributes, badAttributePrefixes, tagSpecificPresentationalAttributes } from '../data/attributes.mjs';
2
2
  import { defsTag } from '../data/tags.mjs';
3
- import { parseSVG } from '../parse.mjs';
3
+ import { parseSVGSync } from '../parse.mjs';
4
4
 
5
- async function removeBadAttributes(svg) {
6
- await parseSVG(svg, (item) => {
5
+ function removeBadAttributes(svg) {
6
+ parseSVGSync(svg, (item) => {
7
7
  const tagName = item.tagName;
8
8
  const attribs = item.element.attribs;
9
9
  const $element = item.$element;