Skip to content

Commit 751f88e

Browse files
authored
[docs] Document typescript:transpile script (#19951)
1 parent a866023 commit 751f88e

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

docs/scripts/formattedTSDemos.js

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const path = require('path');
1616
const babel = require('@babel/core');
1717
const prettier = require('prettier');
1818
const typescriptToProptypes = require('typescript-to-proptypes');
19+
const yargs = require('yargs');
1920
const { fixBabelGeneratorIssues, fixLineEndings } = require('./helpers');
2021

2122
const tsConfig = typescriptToProptypes.loadConfig(path.resolve(__dirname, '../tsconfig.json'));
@@ -28,9 +29,6 @@ const babelConfig = {
2829
configFile: false,
2930
};
3031

31-
const watchMode = process.argv.some(arg => arg === '--watch');
32-
const cacheDisabled = process.argv.some(arg => arg === '--disable-cache');
33-
3432
const workspaceRoot = path.join(__dirname, '../../');
3533

3634
const prettierConfig = prettier.resolveConfig.sync(process.cwd(), {
@@ -69,7 +67,7 @@ const TranspileResult = {
6967
async function transpileFile(tsxPath, program, ignoreCache = false) {
7068
const jsPath = tsxPath.replace('.tsx', '.js');
7169
try {
72-
if (!cacheDisabled && !ignoreCache && (await fse.exists(jsPath))) {
70+
if (!ignoreCache && (await fse.exists(jsPath))) {
7371
const [jsStat, tsxStat] = await Promise.all([fse.stat(jsPath), fse.stat(tsxPath)]);
7472
if (jsStat.mtimeMs > tsxStat.mtimeMs) {
7573
// JavaScript version is newer, skip transpiling
@@ -108,33 +106,37 @@ async function transpileFile(tsxPath, program, ignoreCache = false) {
108106
}
109107
}
110108

111-
(async () => {
109+
async function main(argv) {
110+
const { watch: watchMode, disableCache: cacheDisabled } = argv;
111+
112112
const tsxFiles = await getFiles(path.join(workspaceRoot, 'docs/src/pages'));
113113

114114
const program = typescriptToProptypes.createProgram(tsxFiles, tsConfig);
115115

116116
let successful = 0;
117117
let failed = 0;
118118
let skipped = 0;
119-
(await Promise.all(tsxFiles.map(file => transpileFile(file, program)))).forEach(result => {
120-
switch (result) {
121-
case TranspileResult.Success: {
122-
successful += 1;
123-
break;
124-
}
125-
case TranspileResult.Failed: {
126-
failed += 1;
127-
break;
128-
}
129-
case TranspileResult.Skipped: {
130-
skipped += 1;
131-
break;
132-
}
133-
default: {
134-
throw new Error(`No handler for ${result}`);
119+
(await Promise.all(tsxFiles.map(file => transpileFile(file, program, cacheDisabled)))).forEach(
120+
result => {
121+
switch (result) {
122+
case TranspileResult.Success: {
123+
successful += 1;
124+
break;
125+
}
126+
case TranspileResult.Failed: {
127+
failed += 1;
128+
break;
129+
}
130+
case TranspileResult.Skipped: {
131+
skipped += 1;
132+
break;
133+
}
134+
default: {
135+
throw new Error(`No handler for ${result}`);
136+
}
135137
}
136-
}
137-
});
138+
},
139+
);
138140

139141
console.log(
140142
[
@@ -164,4 +166,28 @@ async function transpileFile(tsxPath, program, ignoreCache = false) {
164166
});
165167

166168
console.log('\nWatching for file changes...');
167-
})();
169+
}
170+
171+
yargs
172+
.command({
173+
command: '$0',
174+
description: 'transpile typescript demos',
175+
builder: command => {
176+
return command
177+
.option('watch', {
178+
default: false,
179+
description: 'transpiles demos as soon as they changed',
180+
type: 'boolean',
181+
})
182+
.option('disable-cache', {
183+
default: false,
184+
description: 'transpiles all demos even if they didnt change',
185+
type: 'boolean',
186+
});
187+
},
188+
handler: main,
189+
})
190+
.help()
191+
.strict(true)
192+
.version(false)
193+
.parse();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
"vrtest": "^0.2.0",
142142
"webpack": "^4.41.0",
143143
"webpack-cli": "^3.3.9",
144+
"yargs": "^15.2.0",
144145
"yarn-deduplicate": "^2.0.0"
145146
},
146147
"resolutions": {

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15907,7 +15907,7 @@ yargs@^14.2.2:
1590715907
y18n "^4.0.0"
1590815908
yargs-parser "^15.0.0"
1590915909

15910-
yargs@^15.0.1, yargs@^15.0.2:
15910+
yargs@^15.0.1, yargs@^15.0.2, yargs@^15.2.0:
1591115911
version "15.2.0"
1591215912
resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.2.0.tgz#cb9fc7f7ec429f7e9329b623f5c707a62dae506a"
1591315913
integrity sha512-E+o8C37U+M7N15rBJVxr0MoInp+O7XNhMqveSGWA5uhddqs8qtkZ+uvT9FI32QML0SKidXdDONr40Xe3tDO9FA==

0 commit comments

Comments
 (0)