Skip to content

Commit

Permalink
refactor: add web framework plugin types and script to fetch plugin t…
Browse files Browse the repository at this point in the history
…ypes
  • Loading branch information
kjin committed Apr 2, 2018
1 parent 53d2b96 commit 7d46ab6
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ npm-debug.log
.vscode/
build/
*.tgz
src/plugins/types/*
!src/plugins/types/index.d.ts
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
"system-test": "npm run script decrypt-service-account-credentials run-system-tests",
"changelog": "./bin/run-changelog.sh",
"check-install": "npm run script check-install",
"get-plugin-types": "npm run script get-plugin-types",
"coverage": "npm run script run-unit-tests-with-coverage report-coverage",
"bump": "./bin/run-bump.sh",
"check": "gts check",
"clean": "rimraf build/src",
"compile-all": "npm run script compile-auto",
"compile-strict": "npm run script compile-auto-strict",
"compile": "npm run script compile-auto compile-auto-strict",
"compile": "npm run script get-plugin-types compile-auto compile-auto-strict",
"fix": "gts fix",
"prepare": "npm run script npm-clean compile-es2015 compile-es2015-strict",
"script": "ts-node -P ./scripts/tsconfig.json ./scripts",
Expand Down
40 changes: 40 additions & 0 deletions scripts/get-plugin-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { flatten, globP, mkdirP, ncpP, readFileP, spawnP, tmpDirP, writeFileP } from './utils';

const TYPES_DIRECTORY = 'src/plugins/types';

async function mkdirSafeP(dir: string) {
try {
await mkdirP(dir);
return true;
} catch (e) {
if (e.code !== 'EEXIST') {
throw new Error(`Error creating directory ${dir}`);
}
return false;
}
}

export async function getPluginTypes() {
await mkdirSafeP(TYPES_DIRECTORY);

const indexTs = (await readFileP(`${TYPES_DIRECTORY}/index.d.ts`, 'utf8') as string)
.split('\n');
for (const line of indexTs) {
const matches = line.match(/^import \* as .* from '\.\/(.*)';\s*\/\/\s*(.*)@(.*)$/);
if (!matches) {
continue;
}
const [_0, packageName, name, version] = matches;
const installDir = `${TYPES_DIRECTORY}/${packageName}`;
if (await mkdirSafeP(installDir)) {
await spawnP('npm', ['init', '-y'], {
cwd: installDir
});
await spawnP('npm', ['install', `@types/${name}@${version}`], {
cwd: installDir
});
await writeFileP(`${installDir}/index.ts`,
`import * as _ from '${name}'; export = _;\n`, 'utf8');
}
}
}
4 changes: 4 additions & 0 deletions scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
import { checkInstall } from './check-install';
import { compile } from './compile';
import { encryptCredentials, decryptCredentials } from './credentials';
import { getPluginTypes } from './get-plugin-types';
import { initTestFixtures } from './init-test-fixtures';
import { reportCoverage } from './report-coverage';
import { runTests } from './run-tests';
Expand Down Expand Up @@ -81,6 +82,9 @@ async function run(steps: string[]) {

await decryptCredentials({ key, iv }, `node-team-test-${keyID}.json`);
break;
case 'get-plugin-types':
await getPluginTypes();
break;
case 'init-test-fixtures':
await initTestFixtures(!TRACE_TEST_EXCLUDE_INTEGRATION);
break;
Expand Down
43 changes: 37 additions & 6 deletions src/plugins/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
import * as connect_3 from 'connect'; // connect@3
import * as express_4 from 'express'; // express@4
import * as hapi_16 from 'hapi'; // hapi@16
import * as koa_2 from 'koa'; // koa@2
import * as pg_7 from 'pg'; // pg@7
import * as restify_5 from 'restify'; // restify@5
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//---module type imports---//

/**
* NOTE: The lines in this section are parsed by scripts/get-plugin-types.ts
* and therefore must have a specific format
*
* import * as X_Y from './X_Y'; // X@Y
*
* where X is the module name and Y is the module version string.
* Ideally, Y is just the module's major version, since variable names cannot
* contain dots.
*/

import * as connect_3 from './connect_3'; // connect@3
import * as express_4 from './express_4'; // express@4
import * as hapi_16 from './hapi_16'; // hapi@16
import * as koa_2 from './koa_2'; // koa@2
import * as pg_7 from './pg_7'; // pg@7
import * as restify_5 from './restify_5'; // restify@5

//---other imports---//

import { EventEmitter } from 'events';
import { Server } from 'http';
Expand Down

0 comments on commit 7d46ab6

Please sign in to comment.