@iconify/tools 2.2.6 → 3.0.0-beta.2

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 (85) 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/css/parser/export.cjs +1 -10
  12. package/lib/css/parser/export.mjs +1 -10
  13. package/lib/css/parser/text.cjs +6 -25
  14. package/lib/css/parser/text.mjs +6 -25
  15. package/lib/css/parser/types.d.ts +2 -3
  16. package/lib/download/api/download.cjs +4 -4
  17. package/lib/download/api/download.mjs +4 -4
  18. package/lib/download/api/index.cjs +0 -1
  19. package/lib/download/api/index.mjs +0 -1
  20. package/lib/download/git/index.cjs +1 -1
  21. package/lib/download/git/index.mjs +1 -1
  22. package/lib/download/git/reset.cjs +1 -1
  23. package/lib/download/git/reset.mjs +1 -1
  24. package/lib/download/github/hash.cjs +0 -1
  25. package/lib/download/github/hash.mjs +0 -1
  26. package/lib/download/github/index.cjs +1 -1
  27. package/lib/download/github/index.mjs +1 -1
  28. package/lib/download/gitlab/hash.cjs +0 -1
  29. package/lib/download/gitlab/hash.mjs +0 -1
  30. package/lib/download/gitlab/index.cjs +1 -1
  31. package/lib/download/gitlab/index.mjs +1 -1
  32. package/lib/download/helpers/unzip.cjs +5 -1
  33. package/lib/download/index.cjs +1 -1
  34. package/lib/download/index.mjs +1 -1
  35. package/lib/download/npm/index.cjs +1 -1
  36. package/lib/download/npm/index.mjs +1 -1
  37. package/lib/icon-set/index.cjs +18 -0
  38. package/lib/icon-set/index.d.ts +7 -1
  39. package/lib/icon-set/index.mjs +18 -0
  40. package/lib/icon-set/tags.cjs +2 -2
  41. package/lib/icon-set/tags.d.ts +1 -1
  42. package/lib/icon-set/tags.mjs +2 -2
  43. package/lib/icon-set/types.d.ts +5 -4
  44. package/lib/import/directory.cjs +101 -28
  45. package/lib/import/directory.d.ts +12 -5
  46. package/lib/import/directory.mjs +103 -31
  47. package/lib/import/figma/index.cjs +1 -2
  48. package/lib/import/figma/index.mjs +1 -2
  49. package/lib/import/figma/query.cjs +0 -1
  50. package/lib/import/figma/query.mjs +0 -1
  51. package/lib/index.cjs +7 -1
  52. package/lib/index.d.ts +6 -6
  53. package/lib/index.mjs +7 -7
  54. package/lib/misc/scan.cjs +61 -8
  55. package/lib/misc/scan.d.ts +12 -6
  56. package/lib/misc/scan.mjs +62 -10
  57. package/lib/svg/analyse.cjs +2 -2
  58. package/lib/svg/analyse.d.ts +1 -1
  59. package/lib/svg/analyse.mjs +4 -4
  60. package/lib/svg/cleanup/attribs.cjs +2 -2
  61. package/lib/svg/cleanup/attribs.d.ts +1 -1
  62. package/lib/svg/cleanup/attribs.mjs +3 -3
  63. package/lib/svg/cleanup/bad-tags.cjs +2 -2
  64. package/lib/svg/cleanup/bad-tags.d.ts +1 -1
  65. package/lib/svg/cleanup/bad-tags.mjs +3 -3
  66. package/lib/svg/cleanup/inline-style.cjs +2 -2
  67. package/lib/svg/cleanup/inline-style.d.ts +1 -1
  68. package/lib/svg/cleanup/inline-style.mjs +3 -3
  69. package/lib/svg/cleanup/root-style.cjs +19 -8
  70. package/lib/svg/cleanup/root-style.d.ts +5 -5
  71. package/lib/svg/cleanup/root-style.mjs +20 -9
  72. package/lib/svg/cleanup/svgo-style.cjs +5 -2
  73. package/lib/svg/cleanup/svgo-style.d.ts +1 -1
  74. package/lib/svg/cleanup/svgo-style.mjs +6 -3
  75. package/lib/svg/cleanup.cjs +6 -6
  76. package/lib/svg/cleanup.d.ts +1 -1
  77. package/lib/svg/cleanup.mjs +6 -6
  78. package/lib/svg/index.cjs +5 -1
  79. package/lib/svg/parse-style.cjs +266 -103
  80. package/lib/svg/parse-style.d.ts +21 -3
  81. package/lib/svg/parse-style.mjs +267 -105
  82. package/lib/svg/parse.cjs +56 -18
  83. package/lib/svg/parse.d.ts +8 -2
  84. package/lib/svg/parse.mjs +56 -19
  85. package/package.json +15 -17
@@ -35,45 +35,118 @@ require('../optimise/svgo.cjs');
35
35
  require('svgo');
36
36
  require('@iconify/utils/lib/svg/id');
37
37
 
38
- async function importDirectory(path, options = {}) {
39
- const files = await misc_scan.scanDirectory(path, (ext, file, subdir, path2) => {
40
- if (ext.toLowerCase() === ".svg") {
38
+ function importDir(iconSet, options, getKeyword, files, readFile, done) {
39
+ let i = 0;
40
+ const next = () => {
41
+ if (i >= files.length) {
42
+ return done(iconSet);
43
+ }
44
+ const file = files[i];
45
+ i++;
46
+ const defaultKeyword = misc_keyword.cleanupIconKeyword(file.file);
47
+ getKeyword([file, defaultKeyword, iconSet], (keyword) => {
48
+ if (typeof keyword !== "string" || !keyword.length) {
49
+ return next();
50
+ }
51
+ readFile(
52
+ file.path + file.subdir + file.file + file.ext,
53
+ (content) => {
54
+ try {
55
+ const svg = new svg_index.SVG(content);
56
+ svg_cleanup.cleanupSVG(svg);
57
+ iconSet.fromSVG(keyword, svg);
58
+ } catch (err) {
59
+ if (options.ignoreImportErrors !== false) {
60
+ throw err;
61
+ }
62
+ }
63
+ next();
64
+ }
65
+ );
66
+ });
67
+ };
68
+ next();
69
+ }
70
+ function isValidFile(item) {
71
+ return item.ext.toLowerCase() === ".svg";
72
+ }
73
+ function importDirectory(path, options = {}) {
74
+ return new Promise((fulfill, reject) => {
75
+ misc_scan.scanDirectory(path, (ext, file, subdir, path2) => {
41
76
  const result = {
42
77
  file,
43
78
  ext,
44
79
  subdir,
45
80
  path: path2
46
81
  };
47
- return result;
48
- }
49
- return false;
82
+ return isValidFile(result) ? result : false;
83
+ }).then((files) => {
84
+ const iconSet = iconSet_index.blankIconSet(options.prefix || "");
85
+ try {
86
+ importDir(
87
+ iconSet,
88
+ options,
89
+ (params, done) => {
90
+ if (options.keyword) {
91
+ const result = options.keyword(...params);
92
+ if (result instanceof Promise) {
93
+ result.then(done).catch(reject);
94
+ } else {
95
+ done(result);
96
+ }
97
+ } else {
98
+ done(params[1]);
99
+ }
100
+ },
101
+ files,
102
+ (filename, done) => {
103
+ fs.promises.readFile(filename, "utf8").then(done).catch(reject);
104
+ },
105
+ fulfill
106
+ );
107
+ } catch (err) {
108
+ reject(err);
109
+ }
110
+ }).catch(reject);
111
+ });
112
+ }
113
+ function importDirectorySync(path, options = {}) {
114
+ const files = misc_scan.scanDirectorySync(path, (ext, file, subdir, path2) => {
115
+ const result = {
116
+ file,
117
+ ext,
118
+ subdir,
119
+ path: path2
120
+ };
121
+ return isValidFile(result) ? result : false;
50
122
  });
51
123
  const iconSet = iconSet_index.blankIconSet(options.prefix || "");
52
- for (let i = 0; i < files.length; i++) {
53
- const file = files[i];
54
- const defaultKeyword = misc_keyword.cleanupIconKeyword(file.file);
55
- let keyword = options.keyword ? options.keyword(file, defaultKeyword, iconSet) : defaultKeyword;
56
- if (keyword instanceof Promise) {
57
- keyword = await keyword;
58
- }
59
- if (typeof keyword !== "string" || !keyword.length) {
60
- continue;
61
- }
62
- try {
63
- const content = await fs.promises.readFile(
64
- file.path + file.subdir + file.file + file.ext,
65
- "utf8"
66
- );
67
- const svg = new svg_index.SVG(content);
68
- await svg_cleanup.cleanupSVG(svg);
69
- iconSet.fromSVG(keyword, svg);
70
- } catch (err) {
71
- if (options.ignoreImportErrors !== false) {
72
- throw err;
124
+ let isSync = true;
125
+ importDir(
126
+ iconSet,
127
+ options,
128
+ (params, done) => {
129
+ if (options.keyword) {
130
+ done(options.keyword(...params));
131
+ } else {
132
+ done(params[1]);
133
+ }
134
+ },
135
+ files,
136
+ (filename, done) => {
137
+ done(fs.readFileSync(filename, "utf8"));
138
+ },
139
+ () => {
140
+ if (!isSync) {
141
+ throw new Error(
142
+ "importDirectorySync supposed to be synchronous"
143
+ );
73
144
  }
74
145
  }
75
- }
146
+ );
147
+ isSync = false;
76
148
  return iconSet;
77
149
  }
78
150
 
79
151
  exports.importDirectory = importDirectory;
152
+ exports.importDirectorySync = importDirectorySync;
@@ -24,19 +24,26 @@ interface ImportDirectoryFileEntry {
24
24
  * Callback can be asynchronous
25
25
  */
26
26
  type ImportDirectoryKeywordCallbackResult = string | undefined;
27
- type ImportDirectoryKeywordCallback = (file: ImportDirectoryFileEntry, defaultKeyword: string, iconSet: IconSet) => ImportDirectoryKeywordCallbackResult | Promise<ImportDirectoryKeywordCallbackResult>;
27
+ type Callback<T> = (file: ImportDirectoryFileEntry, defaultKeyword: string, iconSet: IconSet) => T;
28
+ type AsyncCallback<T> = Callback<T | Promise<T>>;
29
+ type ImportDirectoryKeywordCallback = AsyncCallback<ImportDirectoryKeywordCallbackResult>;
30
+ type ImportDirectoryKeywordSyncCallback = Callback<ImportDirectoryKeywordCallbackResult>;
28
31
  /**
29
32
  * Options
30
33
  */
31
- interface ImportDirectoryOptions {
34
+ interface ImportDirectoryOptions<K> {
32
35
  prefix?: string;
33
36
  includeSubDirs?: boolean;
34
- keyword?: ImportDirectoryKeywordCallback;
37
+ keyword?: K;
35
38
  ignoreImportErrors?: boolean;
36
39
  }
37
40
  /**
38
41
  * Import all icons from directory
39
42
  */
40
- declare function importDirectory(path: string, options?: ImportDirectoryOptions): Promise<IconSet>;
43
+ declare function importDirectory(path: string, options?: ImportDirectoryOptions<ImportDirectoryKeywordCallback>): Promise<IconSet>;
44
+ /**
45
+ * Import all icons from directory synchronously
46
+ */
47
+ declare function importDirectorySync(path: string, options?: ImportDirectoryOptions<ImportDirectoryKeywordSyncCallback>): IconSet;
41
48
 
42
- export { ImportDirectoryFileEntry, ImportDirectoryKeywordCallback, importDirectory };
49
+ export { ImportDirectoryFileEntry, ImportDirectoryKeywordCallback, ImportDirectoryKeywordSyncCallback, importDirectory, importDirectorySync };
@@ -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 };