From ef3d233d4e1b4ed38f78c584b98b4a736205cd0f Mon Sep 17 00:00:00 2001 From: Thijs Louisse Date: Thu, 25 Jun 2020 12:09:45 +0200 Subject: [PATCH] feat(providence-analytics): extend-docs cli command Co-authored-by: Joren Broekema --- .../src/cli/cli-helpers.js | 12 ++++- .../src/cli/generate-extend-docs-data.js | 43 ++++++++++++++++++ .../providence-analytics/src/cli/index.js | 44 +++++++++++++++++-- .../program/services/InputDataService.test.js | 6 ++- 4 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 packages/providence-analytics/src/cli/generate-extend-docs-data.js diff --git a/packages/providence-analytics/src/cli/cli-helpers.js b/packages/providence-analytics/src/cli/cli-helpers.js index 30dcea40b..1c5453a06 100644 --- a/packages/providence-analytics/src/cli/cli-helpers.js +++ b/packages/providence-analytics/src/cli/cli-helpers.js @@ -1,6 +1,7 @@ /* eslint-disable no-shadow */ const pathLib = require('path'); const child_process = require('child_process'); // eslint-disable-line camelcase +const glob = require('glob'); const readPackageTree = require('../program/utils/read-package-tree-with-bower-support.js'); const { InputDataService } = require('../program/services/InputDataService.js'); const { LogService } = require('../program/services/LogService.js'); @@ -28,7 +29,16 @@ function setQueryMethod(m) { * @returns {string[]} */ function pathsArrayFromCs(t) { - return t.split(',').map(t => pathLib.resolve(process.cwd(), t.trim())); + return t + .split(',') + .map(t => { + const isGlob = t.includes('*'); + if (isGlob) { + return glob.sync(t); + } + return pathLib.resolve(process.cwd(), t.trim()); + }) + .flat(); } /** diff --git a/packages/providence-analytics/src/cli/generate-extend-docs-data.js b/packages/providence-analytics/src/cli/generate-extend-docs-data.js new file mode 100644 index 000000000..974ea1078 --- /dev/null +++ b/packages/providence-analytics/src/cli/generate-extend-docs-data.js @@ -0,0 +1,43 @@ +/* eslint-disable import/no-extraneous-dependencies */ +const fs = require('fs'); +const pathLib = require('path'); +const { performance } = require('perf_hooks'); + +const { providence } = require('../program/providence.js'); +const { QueryService } = require('../program/services/QueryService.js'); +const { LogService } = require('../program/services/LogService.js'); + +async function launchProvidenceWithExtendDocs(referencePaths, prefixObj, outputFolder) { + const t0 = performance.now(); + + const results = await providence( + QueryService.getQueryConfigFromAnalyzer('match-paths', { prefix: prefixObj }), + { + gatherFilesConfig: { + extensions: ['.js', '.html'], + excludeFolders: ['coverage', 'test'], + }, + queryMethod: 'ast', + report: false, + targetProjectPaths: [pathLib.resolve(process.cwd())], + referenceProjectPaths: referencePaths, + }, + ); + + const outputFilePath = pathLib.join(outputFolder, 'providence-extend-docs-data.json'); + const queryOutputs = results.map(result => result.queryOutput).flat(); + if (fs.existsSync(outputFilePath)) { + fs.unlinkSync(outputFilePath); + } + fs.writeFile(outputFilePath, JSON.stringify(queryOutputs), err => { + if (err) { + throw err; + } + }); + const t1 = performance.now(); + LogService.info(`"extend-docs" completed in ${Math.round((t1 - t0) / 1000)} seconds`); +} + +module.exports = { + launchProvidenceWithExtendDocs, +}; diff --git a/packages/providence-analytics/src/cli/index.js b/packages/providence-analytics/src/cli/index.js index dbd58cd50..461190dc6 100755 --- a/packages/providence-analytics/src/cli/index.js +++ b/packages/providence-analytics/src/cli/index.js @@ -20,6 +20,7 @@ const { pathsArrayFromCollectionName, pathsArrayFromCs, } = require('./cli-helpers.js'); +const { launchProvidenceWithExtendDocs } = require('./generate-extend-docs-data.js'); // @ts-ignore-next-line const { version } = require('../../package.json'); @@ -153,12 +154,12 @@ commander ) .option( '-w, --whitelist [whitelist]', - `whitelisted paths, like './src, ./packages'`, + `whitelisted paths, like './src, ./packages/*'`, pathsArrayFromCs, ) .option( '--whitelist-reference [whitelist-reference]', - `whitelisted paths fro reference, like './src, ./packages'`, + `whitelisted paths for reference, like './src, ./packages/*'`, pathsArrayFromCs, ) .option( @@ -211,7 +212,7 @@ commander `by default, only required configuration options are asked for. When this flag is provided, optional configuration options are shown as well`, ) - .option('-c, --config [config]', 'configration object for analyzer', c => JSON.parse(c)) + .option('-c, --config [config]', 'configuration object for analyzer', c => JSON.parse(c)) .action((analyzerName, options) => { searchMode = 'analyzer-query'; analyzerOptions = options; @@ -219,6 +220,43 @@ commander launchProvidence(); }); +commander + .command('extend-docs') + .alias('e') + .description( + `Generates data for "babel-extend-docs" plugin. These data are generated by the "match-paths" + plugin, which automatically resolves import paths from reference projects + (say [@lion/input, @lion/textarea, ...etc]) to a target project (say "wolf-ui").`, + ) + .option( + '--prefix-from [prefix-from]', + `Prefix for components of reference layer. By default "lion"`, + a => a, + 'lion', + ) + .option( + '--prefix-to [prefix-to]', + `Prefix for components of reference layer. For instance "wolf"`, + ) + .option( + '--output-folder [output-folder]', + `This is the file path where the result file "providence-extend-docs-data.json" will be written to`, + p => pathLib.resolve(process.cwd(), p.trim()), + process.cwd(), + ) + .action(options => { + if (!options.prefixTo) { + LogService.error(`Please provide a "prefix to" like '--prefix-to "myprefix"'`); + process.exit(1); + } + if (!commander.referencePaths) { + LogService.error(`Please provide referencePaths path like '-r "node_modules/@lion/*"'`); + process.exit(1); + } + const prefixCfg = { from: options.prefixFrom, to: options.prefixTo }; + launchProvidenceWithExtendDocs(commander.referencePaths, prefixCfg, options.outputFolder); + }); + commander .command('manage-projects') .description( diff --git a/packages/providence-analytics/test-node/program/services/InputDataService.test.js b/packages/providence-analytics/test-node/program/services/InputDataService.test.js index b1ac61884..00699ddbc 100644 --- a/packages/providence-analytics/test-node/program/services/InputDataService.test.js +++ b/packages/providence-analytics/test-node/program/services/InputDataService.test.js @@ -77,6 +77,7 @@ describe('InputDataService', () => { './index.js': '', './internal.js': '', './nested/index.js': '', + './nested/nested-two/index.test.js': '', './something.test.js': '', './index.html': '', './something.test.html': '', @@ -89,6 +90,7 @@ describe('InputDataService', () => { '/fictional/project/index.js', '/fictional/project/internal.js', '/fictional/project/nested/index.js', + '/fictional/project/nested/nested-two/index.test.js', '/fictional/project/something.test.js', ]); }); @@ -111,6 +113,7 @@ describe('InputDataService', () => { '/fictional/project/index.js', '/fictional/project/internal.js', '/fictional/project/nested/index.js', + '/fictional/project/nested/nested-two/index.test.js', '/fictional/project/something.test.html', '/fictional/project/something.test.js', ]); @@ -138,12 +141,13 @@ describe('InputDataService', () => { expect(globOutput).to.eql([ '/fictional/project/index.html', '/fictional/project/internal.js', + '/fictional/project/nested/nested-two/index.test.js', '/fictional/project/something.test.html', '/fictional/project/something.test.js', ]); }); - it('allows passing multiple exclude globs', async () => { + it('allows passing exclude globs', async () => { const globOutput = InputDataService.gatherFilesFromDir('/fictional/project', { extensions: ['.html', '.js'], exclude: '**/*.test.{html,js}',