From 39c0fde216f37923f9e2603b84b801a3ffdfafbb Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Sat, 14 Oct 2023 12:06:58 -0700 Subject: [PATCH] refactor(jsdoc-util): remove `@jsdoc/util.fs` It was barely used. We now use `fast-glob` instead. --- package-lock.json | 5 +- packages/jsdoc-template-legacy/publish.js | 35 ++++++---- packages/jsdoc-util/index.js | 5 +- packages/jsdoc-util/lib/fs.js | 34 ---------- packages/jsdoc-util/package.json | 1 - packages/jsdoc-util/test/specs/index.js | 7 -- packages/jsdoc-util/test/specs/lib/fs.js | 83 ----------------------- 7 files changed, 26 insertions(+), 144 deletions(-) delete mode 100644 packages/jsdoc-util/lib/fs.js delete mode 100644 packages/jsdoc-util/test/specs/lib/fs.js diff --git a/package-lock.json b/package-lock.json index 6ee0ed4b..b4fe7d26 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5941,7 +5941,8 @@ "node_modules/graceful-fs": { "version": "4.2.11", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", - "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==" + "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==", + "dev": true }, "node_modules/graphemer": { "version": "1.4.0", @@ -7568,6 +7569,7 @@ "version": "6.0.0", "resolved": "https://registry.npmjs.org/klaw-sync/-/klaw-sync-6.0.0.tgz", "integrity": "sha512-nIeuVSzdCCs6TDPTqI8w1Yre34sSq7AkZ4B3sfOBbI2CgVSB4Du4aLQijFU2+lhAFCwt9+42Hel6lQNIv6AntQ==", + "dev": true, "dependencies": { "graceful-fs": "^4.1.11" } @@ -13534,7 +13536,6 @@ "version": "0.3.0", "license": "Apache-2.0", "dependencies": { - "klaw-sync": "^6.0.0", "lodash": "^4.17.21", "ow": "^1.1.1" }, diff --git a/packages/jsdoc-template-legacy/publish.js b/packages/jsdoc-template-legacy/publish.js index bb82993d..5cb2d589 100644 --- a/packages/jsdoc-template-legacy/publish.js +++ b/packages/jsdoc-template-legacy/publish.js @@ -19,17 +19,15 @@ import path from 'node:path'; import { fileURLToPath } from 'node:url'; import salty from '@jsdoc/salty'; -import { fs as jsdocFs, log } from '@jsdoc/util'; +import { log } from '@jsdoc/util'; import commonPathPrefix from 'common-path-prefix'; -import fastGlob from 'fast-glob'; +import glob from 'fast-glob'; import _ from 'lodash'; import { Template } from './lib/template.js'; import * as helper from './lib/templateHelper.js'; const { htmlsafe, linkto, resolveAuthorLinks } = helper; -const { lsSync } = jsdocFs; -const { sync: glob } = fastGlob; const { resolve } = createRequire(import.meta.url); const { taffy } = salty; @@ -415,6 +413,8 @@ function buildNav(members, dependencies) { function sourceToDestination(parentDir, sourcePath, destDir) { const relativeSource = path.relative(parentDir, sourcePath); + console.log(`sourcePath: ${sourcePath}\nrelativeSource: ${relativeSource}`); + return path.resolve(path.join(destDir, relativeSource)); } @@ -453,6 +453,9 @@ export function publish(docletStore, dependencies) { templateConfig = config.templates || {}; templateConfig.default = templateConfig.default || {}; outdir = path.normalize(opts.destination); + if (!path.isAbsolute(outdir)) { + outdir = path.resolve(process.cwd(), outdir); + } templatePath = __dirname; view = new Template(path.join(templatePath, 'tmpl')); @@ -523,26 +526,30 @@ export function publish(docletStore, dependencies) { // copy the template's static files to outdir fromDir = path.join(templatePath, 'static'); - staticFiles = lsSync(fromDir); + staticFiles = glob.sync('**/*', { + cwd: fromDir, + onlyFiles: true, + }); staticFiles.forEach((fileName) => { - const toPath = sourceToDestination(fromDir, fileName, outdir); + const toPath = path.join(outdir, fileName); mkdirpSync(path.dirname(toPath)); - fs.copyFileSync(fileName, toPath); + fs.copyFileSync(path.join(fromDir, fileName), toPath); }); // copy the fonts used by the template to outdir - staticFiles = lsSync(path.join(resolve('@fontsource-variable/open-sans'), '..', 'files')); + fromDir = path.join(resolve('@fontsource-variable/open-sans'), '..', 'files'); + staticFiles = glob.sync('**/*standard-{normal,italic}*', { + cwd: fromDir, + onlyFiles: true, + }); staticFiles.forEach((fileName) => { const toPath = path.join(outdir, 'fonts', path.basename(fileName)); - const name = path.parse(fileName).name; - if (name.includes('standard-normal') || name.includes('standard-italic')) { - mkdirpSync(path.dirname(toPath)); - fs.copyFileSync(fileName, toPath); - } + mkdirpSync(path.dirname(toPath)); + fs.copyFileSync(path.join(fromDir, fileName), toPath); }); // copy the font CSS to outdir @@ -591,7 +598,7 @@ export function publish(docletStore, dependencies) { // with a bug in JSDoc 3.2.x. staticFilePaths = templateConfig.default.staticFiles.include || templateConfig.default.staticFiles.paths || []; - staticFilePaths = glob(staticFilePaths, { + staticFilePaths = glob.sync(staticFilePaths, { absolute: true, onlyFiles: true, }); diff --git a/packages/jsdoc-util/index.js b/packages/jsdoc-util/index.js index 36e8e327..8cdcf04d 100644 --- a/packages/jsdoc-util/index.js +++ b/packages/jsdoc-util/index.js @@ -20,8 +20,7 @@ */ import EventBus from './lib/bus.js'; import cast from './lib/cast.js'; -import * as fs from './lib/fs.js'; import log from './lib/log.js'; -export { cast, EventBus, fs, log }; -export default { cast, EventBus, fs, log }; +export { cast, EventBus, log }; +export default { cast, EventBus, log }; diff --git a/packages/jsdoc-util/lib/fs.js b/packages/jsdoc-util/lib/fs.js deleted file mode 100644 index ddc84e03..00000000 --- a/packages/jsdoc-util/lib/fs.js +++ /dev/null @@ -1,34 +0,0 @@ -/* - Copyright 2019 the JSDoc Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ -/** - * @alias @jsdoc/util.fs - */ -import path from 'node:path'; - -import klawSync from 'klaw-sync'; -import _ from 'lodash'; - -export function lsSync(dir, opts = {}) { - const depth = _.has(opts, 'depth') ? opts.depth : -1; - - const files = klawSync(dir, { - depthLimit: depth, - filter: (f) => !path.basename(f.path).startsWith('.'), - nodir: true, - }); - - return files.map((f) => f.path); -} diff --git a/packages/jsdoc-util/package.json b/packages/jsdoc-util/package.json index 12546cfb..985b3f63 100644 --- a/packages/jsdoc-util/package.json +++ b/packages/jsdoc-util/package.json @@ -31,7 +31,6 @@ } }, "dependencies": { - "klaw-sync": "^6.0.0", "lodash": "^4.17.21", "ow": "^1.1.1" }, diff --git a/packages/jsdoc-util/test/specs/index.js b/packages/jsdoc-util/test/specs/index.js index 4d06bace..50a85753 100644 --- a/packages/jsdoc-util/test/specs/index.js +++ b/packages/jsdoc-util/test/specs/index.js @@ -16,7 +16,6 @@ import util from '../../index.js'; import bus from '../../lib/bus.js'; import cast from '../../lib/cast.js'; -import * as fs from '../../lib/fs.js'; describe('@jsdoc/util', () => { it('is an object', () => { @@ -34,10 +33,4 @@ describe('@jsdoc/util', () => { expect(util.EventBus).toEqual(bus); }); }); - - describe('fs', () => { - it('is lib/fs', () => { - expect(util.fs).toEqual(fs); - }); - }); }); diff --git a/packages/jsdoc-util/test/specs/lib/fs.js b/packages/jsdoc-util/test/specs/lib/fs.js deleted file mode 100644 index 003fd3dd..00000000 --- a/packages/jsdoc-util/test/specs/lib/fs.js +++ /dev/null @@ -1,83 +0,0 @@ -/* - Copyright 2019 the JSDoc Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ -import path from 'node:path'; - -import mockFs from 'mock-fs'; - -import * as fsUtil from '../../../lib/fs.js'; // eslint-disable-line sort-imports - -describe('@jsdoc/util/lib/fs', () => { - afterEach(() => mockFs.restore()); - - it('has an lsSync method', () => { - expect(fsUtil.lsSync).toBeFunction(); - }); - - describe('lsSync', () => { - beforeEach(() => { - mockFs({ - head: { - eyes: '', - ears: '', - mouth: '', - nose: '', - shoulders: { - knees: { - meniscus: '', - toes: { - phalanx: '', - '.big-toe-phalanx': '', - }, - }, - }, - }, - }); - }); - - const cwd = process.cwd(); - - function resolvePaths(files) { - return files.map((f) => path.join(cwd, f)).sort(); - } - - const allFiles = resolvePaths([ - 'head/eyes', - 'head/ears', - 'head/mouth', - 'head/nose', - 'head/shoulders/knees/meniscus', - 'head/shoulders/knees/toes/phalanx', - ]); - - it('gets all non-hidden files from all levels by default', () => { - const files = fsUtil.lsSync(cwd).sort(); - - expect(files).toEqual(allFiles); - }); - - it('limits recursion depth when asked', () => { - const files = fsUtil.lsSync(cwd, { depth: 1 }).sort(); - - expect(files).toEqual(resolvePaths(['head/eyes', 'head/ears', 'head/mouth', 'head/nose'])); - }); - - it('treats a depth of -1 as infinite', () => { - const files = fsUtil.lsSync('head', { depth: -1 }).sort(); - - expect(files).toEqual(allFiles); - }); - }); -});