diff --git a/docs/scripts/formattedTSDemos.js b/docs/scripts/formattedTSDemos.js index 08ecbd3be20d56..d23dd36f393dd3 100644 --- a/docs/scripts/formattedTSDemos.js +++ b/docs/scripts/formattedTSDemos.js @@ -16,6 +16,7 @@ const path = require('path'); const babel = require('@babel/core'); const prettier = require('prettier'); const typescriptToProptypes = require('typescript-to-proptypes'); +const yargs = require('yargs'); const { fixBabelGeneratorIssues, fixLineEndings } = require('./helpers'); const tsConfig = typescriptToProptypes.loadConfig(path.resolve(__dirname, '../tsconfig.json')); @@ -28,9 +29,6 @@ const babelConfig = { configFile: false, }; -const watchMode = process.argv.some(arg => arg === '--watch'); -const cacheDisabled = process.argv.some(arg => arg === '--disable-cache'); - const workspaceRoot = path.join(__dirname, '../../'); const prettierConfig = prettier.resolveConfig.sync(process.cwd(), { @@ -69,7 +67,7 @@ const TranspileResult = { async function transpileFile(tsxPath, program, ignoreCache = false) { const jsPath = tsxPath.replace('.tsx', '.js'); try { - if (!cacheDisabled && !ignoreCache && (await fse.exists(jsPath))) { + if (!ignoreCache && (await fse.exists(jsPath))) { const [jsStat, tsxStat] = await Promise.all([fse.stat(jsPath), fse.stat(tsxPath)]); if (jsStat.mtimeMs > tsxStat.mtimeMs) { // JavaScript version is newer, skip transpiling @@ -108,7 +106,9 @@ async function transpileFile(tsxPath, program, ignoreCache = false) { } } -(async () => { +async function main(argv) { + const { watch: watchMode, disableCache: cacheDisabled } = argv; + const tsxFiles = await getFiles(path.join(workspaceRoot, 'docs/src/pages')); const program = typescriptToProptypes.createProgram(tsxFiles, tsConfig); @@ -116,25 +116,27 @@ async function transpileFile(tsxPath, program, ignoreCache = false) { let successful = 0; let failed = 0; let skipped = 0; - (await Promise.all(tsxFiles.map(file => transpileFile(file, program)))).forEach(result => { - switch (result) { - case TranspileResult.Success: { - successful += 1; - break; - } - case TranspileResult.Failed: { - failed += 1; - break; - } - case TranspileResult.Skipped: { - skipped += 1; - break; - } - default: { - throw new Error(`No handler for ${result}`); + (await Promise.all(tsxFiles.map(file => transpileFile(file, program, cacheDisabled)))).forEach( + result => { + switch (result) { + case TranspileResult.Success: { + successful += 1; + break; + } + case TranspileResult.Failed: { + failed += 1; + break; + } + case TranspileResult.Skipped: { + skipped += 1; + break; + } + default: { + throw new Error(`No handler for ${result}`); + } } - } - }); + }, + ); console.log( [ @@ -164,4 +166,28 @@ async function transpileFile(tsxPath, program, ignoreCache = false) { }); console.log('\nWatching for file changes...'); -})(); +} + +yargs + .command({ + command: '$0', + description: 'transpile typescript demos', + builder: command => { + return command + .option('watch', { + default: false, + description: 'transpiles demos as soon as they changed', + type: 'boolean', + }) + .option('disable-cache', { + default: false, + description: 'transpiles all demos even if they didnt change', + type: 'boolean', + }); + }, + handler: main, + }) + .help() + .strict(true) + .version(false) + .parse(); diff --git a/package.json b/package.json index 2381df4b84cc61..b6cf2978db9c17 100644 --- a/package.json +++ b/package.json @@ -141,6 +141,7 @@ "vrtest": "^0.2.0", "webpack": "^4.41.0", "webpack-cli": "^3.3.9", + "yargs": "^15.2.0", "yarn-deduplicate": "^2.0.0" }, "resolutions": { diff --git a/yarn.lock b/yarn.lock index ce588b149ccd19..277e57378a6de3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15907,7 +15907,7 @@ yargs@^14.2.2: y18n "^4.0.0" yargs-parser "^15.0.0" -yargs@^15.0.1, yargs@^15.0.2: +yargs@^15.0.1, yargs@^15.0.2, yargs@^15.2.0: version "15.2.0" resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.2.0.tgz#cb9fc7f7ec429f7e9329b623f5c707a62dae506a" integrity sha512-E+o8C37U+M7N15rBJVxr0MoInp+O7XNhMqveSGWA5uhddqs8qtkZ+uvT9FI32QML0SKidXdDONr40Xe3tDO9FA==