Add exclude paths as config for typings (#4721)

This commit is contained in:
Monica Kozbial
2021-04-13 09:42:31 -07:00
committed by GitHub
parent 978b1b83c4
commit a05b26fa93

View File

@@ -17,6 +17,34 @@ var fs = require('fs');
var rimraf = require('rimraf');
var execSync = require('child_process').execSync;
/**
* Recursively generates a list of file paths with the specified extension
* contained within the specified basePath.
* @param {string} basePath The base path to use.
* @param {string} filter The extension name to filter for.
* @param {Array<string>} excludePaths The paths to exclude from search.
* @return {Array<string>} The generated file paths.
*/
function getFilePath(basePath, filter, excludePaths) {
const files = [];
const dirContents = fs.readdirSync(basePath);
dirContents.forEach((fn) => {
const filePath = path.join(basePath, fn);
const excluded =
!excludePaths.every((exPath) => !filePath.startsWith(exPath));
if (excluded) {
return;
}
const stat = fs.lstatSync(filePath);
if (stat.isDirectory()) {
files.push(...getFilePath(filePath, filter, excludePaths));
} else if (filePath.endsWith(filter)) {
files.push(filePath);
}
});
return files;
}
// Generates the TypeScript definition file (d.ts) for Blockly.
// As well as generating the typings of each of the files under core/ and msg/,
// the script also pulls in a number of part files from typings/parts.
@@ -24,30 +52,27 @@ var execSync = require('child_process').execSync;
// including Blockly Options and Google Closure typings.
function typings() {
const tmpDir = './typings/tmp';
const blocklySrcs = [
"core/",
"core/events",
"core/keyboard_nav",
"core/renderers/common",
"core/renderers/measurables",
"core/theme",
"core/toolbox",
"core/interfaces",
"core/utils",
"msg/"
];
// Clean directory if exists.
if (fs.existsSync(tmpDir)) {
rimraf.sync(tmpDir);
}
fs.mkdirSync(tmpDir);
const excludePaths = [
"core/renderers/geras",
"core/renderers/minimalist",
"core/renderers/thrasos",
"core/renderers/zelos",
];
const blocklySrcs = [
'core',
'msg'
]
// Find all files that will be included in the typings file.
let files = [];
blocklySrcs.forEach((src) => {
files = files.concat(fs.readdirSync(src)
.filter(fn => fn.endsWith('.js'))
.map(fn => path.join(src, fn)));
blocklySrcs.forEach((basePath) => {
files.push(...getFilePath(basePath, '.js', excludePaths));
});
// Generate typings file for each file.
@@ -64,15 +89,7 @@ function typings() {
const srcs = [
'typings/templates/blockly-header.template',
'typings/templates/blockly-interfaces.template',
`${tmpDir}/core/**`,
`${tmpDir}/core/interfaces/**`,
`${tmpDir}/core/events/**`,
`${tmpDir}/core/keyboard_nav/**`,
`${tmpDir}/core/renderers/common/**`,
`${tmpDir}/core/renderers/measurables/**`,
`${tmpDir}/core/utils/**`,
`${tmpDir}/core/toolbox/**`,
`${tmpDir}/core/theme/**`,
`${tmpDir}/core/**/*`,
`${tmpDir}/msg/**`
];
return gulp.src(srcs)