feat(providence-analytics): extend-docs cli command

Co-authored-by: Joren Broekema <Joren.Broekema@ing.com>
This commit is contained in:
Thijs Louisse 2020-06-25 12:09:45 +02:00
parent e78aba8192
commit ef3d233d4e
4 changed files with 100 additions and 5 deletions

View file

@ -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();
}
/**

View file

@ -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,
};

View file

@ -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(

View file

@ -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}',