Skip to content

Commit

Permalink
Add exclude paths as config for typings (#4721)
Browse files Browse the repository at this point in the history
  • Loading branch information
Monica Kozbial authored Apr 13, 2021
1 parent 978b1b8 commit a05b26f
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions scripts/gulpfiles/typings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,62 @@ 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.
// This includes the header (incl License), additional useful interfaces
// 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.
Expand All @@ -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)
Expand Down

0 comments on commit a05b26f

Please sign in to comment.