Skip to content

Commit

Permalink
[docs] Document typescript:transpile script (mui#19951)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored and EsoterikStare committed Mar 30, 2020
1 parent ffe2e78 commit 55109f4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
74 changes: 50 additions & 24 deletions docs/scripts/formattedTSDemos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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(), {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -108,33 +106,37 @@ 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);

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(
[
Expand Down Expand Up @@ -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();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 55109f4

Please sign in to comment.