From a05b26fa93b735e6c95a6309583d0193fed63bfb Mon Sep 17 00:00:00 2001 From: Monica Kozbial Date: Tue, 13 Apr 2021 09:42:31 -0700 Subject: [PATCH] Add exclude paths as config for typings (#4721) --- scripts/gulpfiles/typings.js | 67 ++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/scripts/gulpfiles/typings.js b/scripts/gulpfiles/typings.js index 70ec96ba943..67b94f59c52 100644 --- a/scripts/gulpfiles/typings.js +++ b/scripts/gulpfiles/typings.js @@ -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} excludePaths The paths to exclude from search. + * @return {Array} 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)