Skip to content

Commit

Permalink
Convert remaining files to TypeScript (graphql#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrearden authored and jliu670 committed Aug 14, 2020
1 parent 4649a58 commit 758634b
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 351 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,8 @@ overrides:
'@typescript-eslint/no-throw-literal': error
'@typescript-eslint/no-type-alias': off # TODO consider
'@typescript-eslint/no-unnecessary-boolean-literal-compare': error
'@typescript-eslint/no-unnecessary-condition': error
# FIXME: Disabled to enable checking conditions that would be prevented by types anyway
'@typescript-eslint/no-unnecessary-condition': off
'@typescript-eslint/no-unnecessary-qualifier': error
'@typescript-eslint/no-unnecessary-type-arguments': error
'@typescript-eslint/no-unnecessary-type-assertion': error
Expand Down
3 changes: 1 addition & 2 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
throw-deprecation: true
check-leaks: true
require:
- '@babel/register'
- ts-node/register
- './resources/register.js'
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"serializable",
"subcommand",
"charsets",
"downlevel",

// TODO: remove bellow words
"Graphi", // GraphiQL
Expand Down
File renamed without changes.
57 changes: 57 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
"private": true,
"main": "index.js",
"types": "index.d.ts",
"typesVersions": {
"<3.8": {
"*": [
"ts3.4/*"
]
}
},
"sideEffects": false,
"homepage": "https://github.com/graphql/express-graphql",
"bugs": {
Expand Down Expand Up @@ -40,7 +47,7 @@
"check:spelling": "cspell '**/*'",
"check:integrations": "mocha --full-trace integrationTests/*-test.js",
"build": "node resources/build.js",
"start": "node -r @babel/register examples/index.js"
"start": "node -r ./resources/register.js examples/index.ts"
},
"dependencies": {
"accepts": "^1.3.7",
Expand All @@ -53,10 +60,13 @@
"@babel/plugin-transform-flow-strip-types": "7.10.4",
"@babel/preset-env": "7.10.4",
"@babel/register": "7.10.4",
"@types/accepts": "1.3.5",
"@types/body-parser": "1.19.0",
"@types/chai": "4.2.11",
"@types/connect": "3.4.33",
"@types/content-type": "1.1.3",
"@types/express": "4.17.6",
"@types/http-errors": "1.6.3",
"@types/mocha": "7.0.2",
"@types/multer": "1.4.3",
"@types/node": "14.0.14",
Expand All @@ -70,6 +80,7 @@
"chai": "4.2.0",
"connect": "3.7.0",
"cspell": "4.0.63",
"downlevel-dts": "0.5.0",
"eslint": "7.3.1",
"eslint-plugin-flowtype": "5.2.0",
"eslint-plugin-import": "2.22.0",
Expand Down
38 changes: 19 additions & 19 deletions resources/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,33 @@ const fs = require('fs');
const path = require('path');
const assert = require('assert');

const babel = require('@babel/core');
const ts = require('typescript');
const { main: downlevel } = require('downlevel-dts');

const tsConfig = require('../tsconfig.json');

const {
transformLoadFileStaticallyFromNPM,
} = require('./load-statically-from-npm');
const { rmdirRecursive, readdirRecursive, showStats } = require('./utils');

if (require.main === module) {
rmdirRecursive('./dist');
fs.mkdirSync('./dist');

const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ });
for (const filepath of srcFiles) {
const srcPath = path.join('./src', filepath);
const destPath = path.join('./dist', filepath);

fs.mkdirSync(path.dirname(destPath), { recursive: true });
if (filepath.endsWith('.js')) {
fs.copyFileSync(srcPath, destPath + '.flow');

const cjs = babelBuild(srcPath, { envName: 'cjs' });
fs.writeFileSync(destPath, cjs);
} else if (filepath.endsWith('d.ts')) {
fs.copyFileSync(srcPath, destPath);
}
}
const { options } = ts.convertCompilerOptionsFromJson(
tsConfig.compilerOptions,
process.cwd(),
);
const program = ts.createProgram({
rootNames: srcFiles.map((filepath) => path.join('./src', filepath)),
options,
});
program.emit(undefined, undefined, undefined, undefined, {
after: [transformLoadFileStaticallyFromNPM],
});
downlevel('./dist', './dist/ts3.4');

fs.copyFileSync('./LICENSE', './dist/LICENSE');
fs.copyFileSync('./README.md', './dist/README.md');
Expand All @@ -40,10 +44,6 @@ if (require.main === module) {
showStats();
}

function babelBuild(srcPath, options) {
return babel.transformFileSync(srcPath, options).code + '\n';
}

function buildPackageJSON() {
const packageJSON = require('../package.json');
delete packageJSON.private;
Expand Down
36 changes: 16 additions & 20 deletions resources/load-statically-from-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

const fs = require('fs');

const ts = require('typescript');

/**
* Eliminates function call to `invariant` if the condition is met.
*
* Transforms:
*
* loadFileStaticallyFromNPM(<npm path>)
Expand All @@ -15,23 +15,19 @@ const fs = require('fs');
*
* "<file content>"
*/
module.exports = function inlineInvariant(context) {
return {
visitor: {
CallExpression(path) {
const { node } = path;

if (
node.callee.type === 'Identifier' &&
node.callee.name === 'loadFileStaticallyFromNPM'
) {
const npmPath = node.arguments[0].value;
const filePath = require.resolve(npmPath);
const content = fs.readFileSync(filePath, 'utf-8');

path.replaceWith(context.types.stringLiteral(content));
}
},
},
module.exports.transformLoadFileStaticallyFromNPM = function (context) {
return function visit(node) {
if (ts.isCallExpression(node)) {
if (
ts.isIdentifier(node.expression) &&
node.expression.text === 'loadFileStaticallyFromNPM'
) {
const npmPath = node.arguments[0].text;
const filePath = require.resolve(npmPath);
const content = fs.readFileSync(filePath, 'utf-8');
return ts.createStringLiteral(content);
}
}
return ts.visitEachChild(node, visit, context);
};
};
11 changes: 11 additions & 0 deletions resources/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const {
transformLoadFileStaticallyFromNPM,
} = require('./load-statically-from-npm');

require('ts-node').register({
transformers: () => ({
after: [transformLoadFileStaticallyFromNPM],
}),
});
Loading

0 comments on commit 758634b

Please sign in to comment.