Skip to content

Commit dfe3222

Browse files
committed
Fix regression where files passed directly to jsdocApi.render() were sorted unnecessarily #33
1 parent 4d06895 commit dfe3222

File tree

4 files changed

+115
-40
lines changed

4 files changed

+115
-40
lines changed

dist/index.cjs

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ var path = require('path');
66
var fs = require('node:fs');
77
var os = require('os');
88
var crypto = require('crypto');
9-
var FileSet = require('file-set');
9+
var fg = require('fast-glob');
10+
var fs$1 = require('fs');
1011
var assert = require('assert');
1112
var walkBack = require('walk-back');
1213
var currentModulePaths = require('current-module-paths');
@@ -39,6 +40,82 @@ class TempFile {
3940
}
4041
}
4142

43+
class FileSet {
44+
constructor () {
45+
/* validation */
46+
if (arguments.length) {
47+
throw new Error('new Fileset() does not require any arguments')
48+
}
49+
50+
/** • fileSet.files :string[]
51+
≈ The existing files found.
52+
*/
53+
this.files = [];
54+
55+
/** • fileSet.dirs :string[]
56+
≈ The existing directories found. Directory paths will always end with `'/'`.
57+
*/
58+
this.dirs = [];
59+
60+
/** • fileSet.notExisting :string[]
61+
≈ Paths which were not found.
62+
*/
63+
this.notExisting = [];
64+
}
65+
66+
/** ø fileSet.add(patterns)
67+
≈ Add file patterns to the set.
68+
• [patterns] :string|string[] - One or more file paths or glob expressions to inspect.
69+
*/
70+
async add (files, options = {}) {
71+
files = arrayify(files);
72+
for (let file of files) {
73+
/* Force all incoming file paths and glob expressions to use posix separators */
74+
file = os.platform() === 'win32'
75+
? file.replace(/\\/g, path.posix.sep)
76+
: file;
77+
try {
78+
const stat = await fs$1.promises.stat(file);
79+
if (stat.isFile() && !this.files.includes(file)) {
80+
this.files.push(file);
81+
} else if (stat.isDirectory() && !this.dirs.includes(file)) {
82+
this.dirs.push(file.endsWith(path.posix.sep) ? file : `${file}${path.posix.sep}`);
83+
}
84+
} catch (err) {
85+
if (err.code === 'ENOENT') {
86+
if (fg.isDynamicPattern(file)) {
87+
const found = await fg.glob(file, { onlyFiles: false, markDirectories: true });
88+
if (found.length) {
89+
if (options.globResultSortFn) {
90+
found.sort(options.globResultSortFn);
91+
}
92+
for (const match of found) {
93+
if (match.endsWith(path.posix.sep)) {
94+
if (!this.dirs.includes(match)) this.dirs.push(match);
95+
} else {
96+
if (!this.files.includes(match)) this.files.push(match);
97+
}
98+
}
99+
} else {
100+
if (!this.notExisting.includes(file)) this.notExisting.push(file);
101+
}
102+
} else {
103+
if (!this.notExisting.includes(file)) this.notExisting.push(file);
104+
}
105+
} else {
106+
throw err
107+
}
108+
}
109+
}
110+
}
111+
112+
clear () {
113+
this.files = [];
114+
this.dirs = [];
115+
this.notExisting = [];
116+
}
117+
}
118+
42119
const { __dirname: __dirname$1 } = currentModulePaths((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
43120

44121
class JsdocCommand {
@@ -74,10 +151,9 @@ class JsdocCommand {
74151

75152
async execute () {
76153
this.inputFileSet = new FileSet();
77-
await this.inputFileSet.add(this.options.files);
78154
/* node-glob v9+ (used by file-set) no longer sorts the output by default. We will continue to sort the file list, for now, as it's what the user expected by default. The user's system locale is used. */
79155
const collator = new Intl.Collator();
80-
this.inputFileSet.files.sort(collator.compare);
156+
await this.inputFileSet.add(this.options.files, { globResultSortFn: collator.compare });
81157

82158
if (this.options.source.length) {
83159
this.tempFiles = this.options.source.map(source => new TempFile(source));

lib/jsdoc-command.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ class JsdocCommand {
4141

4242
async execute () {
4343
this.inputFileSet = new FileSet()
44-
await this.inputFileSet.add(this.options.files)
4544
/* node-glob v9+ (used by file-set) no longer sorts the output by default. We will continue to sort the file list, for now, as it's what the user expected by default. The user's system locale is used. */
4645
const collator = new Intl.Collator()
47-
this.inputFileSet.files.sort(collator.compare)
46+
await this.inputFileSet.add(this.options.files, { globResultSortFn: collator.compare })
4847

4948
if (this.options.source.length) {
5049
this.tempFiles = this.options.source.map(source => new TempFile(source))

package-lock.json

Lines changed: 33 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
},
3333
"dependencies": {
3434
"array-back": "^6.2.2",
35-
"cache-point": "^3.0.0",
35+
"cache-point": "^3.0.1",
3636
"current-module-paths": "^1.1.2",
37-
"file-set": "^5.2.2",
37+
"file-set": "^5.3.0",
3838
"jsdoc": "^4.0.4",
3939
"object-to-spawn-args": "^2.0.1",
4040
"walk-back": "^5.1.1"

0 commit comments

Comments
 (0)