From 0dd58cb0bb71d39c99cd02a366f6ae6f0804d106 Mon Sep 17 00:00:00 2001 From: Stanca Pop Date: Thu, 18 May 2023 11:53:09 +0300 Subject: [PATCH 1/2] migrated to ES Modules --- .eslintrc.js => .eslintrc.cjs | 3 +- index.js | 59 ++++++++++++++--------------------- package-lock.json | 3 +- package.json | 3 +- 4 files changed, 29 insertions(+), 39 deletions(-) rename .eslintrc.js => .eslintrc.cjs (97%) diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 97% rename from .eslintrc.js rename to .eslintrc.cjs index 4e4e4d1..ce392ad 100644 --- a/.eslintrc.js +++ b/.eslintrc.cjs @@ -11,7 +11,8 @@ module.exports = { 'mocha': true }, parserOptions: { - 'ecmaVersion': 'latest' + 'ecmaVersion': 'latest', + 'sourceType': 'module' }, root: true, extends: ['eslint:recommended'], diff --git a/index.js b/index.js index 56fd19c..f193818 100755 --- a/index.js +++ b/index.js @@ -1,7 +1,5 @@ #!/usr/bin/env node -'use strict'; - /* eslint-env node */ // For `--standalone` the `--path` needs to include the library name or point to `packages`, no // trailing `/`. Defaults to the current working directory. @@ -9,22 +7,22 @@ // TODO: Allow for configuring output and input dirs better // TODO: Consider returning useful values from functions instead of just outputting/saving/etc. -const shelljs = require('shelljs'), - fs = require('fs'), - os = require('os'), - pathModule = require('path'), - ProgressBar = require('progress'), - elasticlunr = require('elasticlunr'), - jsonata = require('jsonata'), - readdirp = require('readdirp'), - mkdirp = require('mkdirp'), - toc = require('markdown-toc'), - jsonfile = require('jsonfile'), - chalk = require('chalk'), - matter = require('gray-matter'), - parseArgs = require('minimist'); -const documentation = import('documentation'); +import chalk from 'chalk'; +import elasticlunr from 'elasticlunr'; +import fs from 'fs'; +import matter from 'gray-matter'; +import jsonata from 'jsonata'; +import jsonfile from 'jsonfile'; +import toc from 'markdown-toc'; +import parseArgs from 'minimist'; +import mkdirp from 'mkdirp'; +import os from 'os'; +import pathModule from 'path'; +import ProgressBar from 'progress'; +import readdirp from 'readdirp'; +import shelljs from 'shelljs'; +const documentation = import('documentation'); let documentationResponse; const generateDocumentationResponse = async () => { documentationResponse = await documentation.then(result => result); @@ -50,7 +48,7 @@ const allowedErrorTags = ['@curried', '@hoc', '@hocconfig', '@omit', '@required' * @param {string} [pattern=*.js] - An optional regex string to be used for filtering files * @returns {string[]} - A list of paths of matching files */ -const getValidFiles = (modules, pattern = '*.js') => { +export const getValidFiles = (modules, pattern = '*.js') => { const files = []; let cmd, moduleFiles; @@ -92,7 +90,7 @@ const getValidFiles = (modules, pattern = '*.js') => { * @param {boolean} noSave - If `true`, no files are written to disk * @returns {Promise[]} - An array of promises that represent the scanning process */ -const getDocumentation = (paths, strict, noSave) => { +export const getDocumentation = (paths, strict, noSave) => { const docOutputPath = pathModule.join('src', 'pages', 'docs', 'modules'); // TODO: Add @module to all files and scan files and combine json const validPaths = paths.reduce((prev, path) => { @@ -273,7 +271,7 @@ async function validate (docs, componentDirectory, strict) { * @param {boolean} ignoreExternal - If `true`, any modules not scanned will be excluded from * validation (i.e. standalone libraries will not warn for referencing core libraries) */ -function postValidate (strict, ignoreExternal) { +export function postValidate (strict, ignoreExternal) { const moduleRegex = /^((\w+\/\w+)(\.\w+)?)/, exceptions = ['spotlight/Spotlight']; @@ -361,7 +359,7 @@ function prependTableOfContents (contents) { * @param {string} [path] - Parent directory of `/docs/config.json` * @returns {object} Configuration object */ -function getDocsConfig (path = process.cwd()) { +export function getDocsConfig (path = process.cwd()) { const configFilename = `${path}/docs/config.json`, // don't parse CLI or eslint-config-enact for source parseSource = (path.indexOf('/cli') + path.indexOf('eslint')) < 0, @@ -394,7 +392,7 @@ function getDocsConfig (path = process.cwd()) { * @param {string} config.source - Path to search for docs directory (parent of docs dir) * @param {string} config.outputTo - Path to copy static docs */ -function copyStaticDocs ({source, outputTo: outputBase, icon}) { +export function copyStaticDocs ({source, outputTo: outputBase, icon}) { let files = []; if (os.platform() === 'win32') { @@ -489,7 +487,7 @@ function copyStaticDocs ({source, outputTo: outputBase, icon}) { * @param {boolean} [strict] - If `true`, set process exit code on warnings * @returns {object} - keys = library names values = object {desc: description, version: version, etc.} */ -function extractLibraryDescription ({path, hasPackageDir, description, ...rest}, strict) { +export function extractLibraryDescription ({path, hasPackageDir, description, ...rest}, strict) { const output = {}; let libraryPaths; @@ -576,7 +574,7 @@ function extractLibraryDescription ({path, hasPackageDir, description, ...rest}, * * @param {string} outputFilename - Filename for the generated index file */ -function generateIndex (docIndexFile) { +export function generateIndex (docIndexFile) { // Note: The $map($string) is needed because spotlight has a literal 'false' in a return type! const expression = `{ "title": name, @@ -655,7 +653,7 @@ function makeDataDir () { mkdirp.mkdirpSync(dataDir); } -function saveLibraryDescriptions (descriptions) { +export function saveLibraryDescriptions (descriptions) { makeDataDir(); // generate a json file that contains the description to the corresponding libraries jsonfile.writeFileSync(libraryDescriptionFile, descriptions); @@ -680,14 +678,3 @@ function init () { } init(); - -module.exports = { - getValidFiles, - getDocumentation, - postValidate, - copyStaticDocs, - generateIndex, - getDocsConfig, - extractLibraryDescription, - saveLibraryDescriptions -}; diff --git a/package-lock.json b/package-lock.json index 03edf2c..101e493 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5706,7 +5706,8 @@ "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "dev": true + "dev": true, + "requires": {} }, "ajv": { "version": "6.12.6", diff --git a/package.json b/package.json index 1d602c9..84859f8 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,12 @@ { "name": "@enact/docs-utils", "description": "Utilities for parsing and validating JSDoc in Enact projects", + "type": "module", "version": "0.4.2", "engines": { "node": ">=14" }, - "main": "index.js", + "exports": "./index.js", "bin": { "validate-docs": "index.js" }, From 7fa988eb00a5c8f80169a0338a59450c1cbe16c0 Mon Sep 17 00:00:00 2001 From: Stanca Pop Date: Thu, 18 May 2023 13:42:27 +0300 Subject: [PATCH 2/2] adjusted import --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f193818..e978725 100755 --- a/index.js +++ b/index.js @@ -15,7 +15,7 @@ import jsonata from 'jsonata'; import jsonfile from 'jsonfile'; import toc from 'markdown-toc'; import parseArgs from 'minimist'; -import mkdirp from 'mkdirp'; +import {mkdirpSync} from 'mkdirp'; import os from 'os'; import pathModule from 'path'; import ProgressBar from 'progress'; @@ -650,7 +650,7 @@ export function generateIndex (docIndexFile) { } function makeDataDir () { - mkdirp.mkdirpSync(dataDir); + mkdirpSync(dataDir); } export function saveLibraryDescriptions (descriptions) {