Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: generate metadata-function-map #3392

Merged
merged 4 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ test/integration/*/index.html
axe.js
axe.*.js

# generated src file
lib/core/base/metadata-function-map.js

# generated jsdoc api docs
doc/api/*
Expand Down
15 changes: 15 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ module.exports = function(grunt) {
]
}
},
'metadata-function-map': {
core: {
files: [
{
expand: true,
src: [
'lib/checks/**/*-{evaluate,after}.js',
'lib/rules/**/*-matches.js'
],
dest: 'lib/core/base/metadata-function-map.js'
}
]
}
},
'aria-supported': {
data: {
entry: 'lib/commons/aria/index.js',
Expand Down Expand Up @@ -239,6 +253,7 @@ module.exports = function(grunt) {
grunt.registerTask('build', [
'clean:core',
'validate',
'metadata-function-map',
'esbuild',
'configure',
'babel',
Expand Down
49 changes: 49 additions & 0 deletions build/tasks/metadata-function-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const path = require('path').posix;
const glob = require('glob');
const fs = require('fs');

function toTitleCase(str) {
return str.replace(/-\w/g, txt => {
return txt.charAt(1).toUpperCase() + txt.substr(2).toLowerCase();
});
}

module.exports = function(grunt) {
grunt.registerMultiTask(
'metadata-function-map',
'Task to generate the metadata-function-map file',
function() {
const files = grunt.task.current.data.files;

files.forEach(file => {
const src = Array.isArray(file.src) ? file.src : [file.src];
const map = {};
let outFile =
'// This file is automatically generated using build/tasks/metadata-function-map.js\n';

src.forEach(globPath => {
glob.sync(globPath).forEach(filePath => {
const relativePath = path.relative(
path.dirname(file.dest),
filePath
);
const filename = path.basename(filePath, '.js');
const functionName = toTitleCase(filename);

outFile += `import ${functionName} from '${relativePath}';\n`;
map[filename] = functionName;
});
});

outFile += `\nconst metadataFunctionMap = {\n`;
outFile += Object.keys(map)
.sort()
.map(key => ` '${key}': ${map[key]}`)
.join(',\n');
outFile += `\n};\n\nexport default metadataFunctionMap;`;

fs.writeFileSync(file.dest, outFile, 'utf-8');
});
}
);
};
Loading