Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(package): generate esm output with exports #3337

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion generator.md
Expand Up @@ -32,7 +32,7 @@ This repository uses [synthtool](https://github.com/googleapis/synthtool/) to re
You can generate a single API based on a discovery URL. Replace the url and API name below to match the API you'd like to generate:
```
npm run build-tools
node build/src/generator/generator.js 'https://apigee.googleapis.com/$discovery/rest?version=v1' \
node build/cjs/src/generator/generator.js 'https://apigee.googleapis.com/$discovery/rest?version=v1' \
--include-private false \
--use-cache false
```
Expand Down
47 changes: 34 additions & 13 deletions package.json
Expand Up @@ -4,46 +4,66 @@
"repository": "googleapis/google-api-nodejs-client",
"license": "Apache-2.0",
"description": "Google APIs Client Library for Node.js",
"main": "./build/src/index.js",
"types": "./build/src/index.d.ts",
"main": "./build/cjs/src/index.js",
"module": "./build/esm/src/index.mjs",
"types": "./build/esm/src/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./build/esm/src/index.d.ts",
"default": "./build/esm/src/index.mjs"
},
"require": {
"types": "./build/cjs/src/index.d.ts",
"default": "./build/cjs/src/index.js"
},
"default": {
"types": "./build/esm/src/index.d.ts",
"default": "./build/esm/src/index.mjs"
}
}
},
"engines": {
"node": ">=14.0.0"
},
"files": [
"build/src",
"!build/src/**/*.map"
"build/cjs/src",
"!build/cjs/src/**/*.map",
"build/esm/src",
"!build/esm/src/**/*.map"
],
"scripts": {
"pretest": "npm run build-test",
"prepare": "npm run compile",
"test": "c8 mocha build/test",
"test": "c8 mocha build/cjs/test",
"predocs": "npm run build-tools",
"docs": "node build/src/generator/docs",
"docs": "node build/cjs/src/generator/docs",
"predocs2": "npm run compile",
"docs-extract": "node --max-old-space-size=8192 ./node_modules/@microsoft/api-extractor/bin/api-extractor run --local --verbose",
"docs-md": "node --max-old-space-size=8192 ./node_modules/@microsoft/api-documenter/bin/api-documenter markdown --input-folder build/docs --output-folder docs",
"docs-md": "node --max-old-space-size=8192 ./node_modules/@microsoft/api-documenter/bin/api-documenter markdown --input-folder build/cjs/docs --output-folder docs",
"docs2": "npm run docs-extract && npm run docs-md",
"presystem-test": "npm run build-test",
"system-test": "mocha build/system-test",
"system-test": "mocha build/cjs/system-test",
"samples-test": "cd samples && npm install && npm link ../ && pwd && npm test",
"lint": "gts check",
"precompile": "rimraf build",
"compile": "cross-env NODE_OPTIONS=--max-old-space-size=8192 tsc -p tsconfig.json",
"compile": "cross-env NODE_OPTIONS=--max-old-space-size=8192 tsc-multi --verbose",
"prebuild-test": "rimraf build",
"build-test": "cross-env NODE_OPTIONS=--max-old-space-size=8192 tsc -p tsconfig.test.json",
"build-tools": "tsc -p tsconfig.tools.json",
"clean": "gts clean",
"fix": "gts fix",
"pregenerate": "npm run build-tools",
"generate": "node build/src/generator/generator.js",
"generate": "node build/cjs/src/generator/generator.js",
"docs-test": "echo 🙈 this was taking too long and timing out CI",
"presubmit-prs": "npm run compile",
"submit-prs": "node --max-old-space-size=8192 build/src/generator/synth.js",
"submit-prs": "node --max-old-space-size=8192 build/cjs/src/generator/synth.js",
"prelint": "cd samples; npm link ../; npm i",
"predownload": "npm run build-tools",
"download": "node build/src/generator/download.js",
"download": "node build/cjs/src/generator/download.js",
"preupdate-disclaimers": "npm run build-tools",
"update-disclaimers": "node build/src/generator/disclaimer.js"
"update-disclaimers": "node build/cjs/src/generator/disclaimer.js"
},
"author": "Google Inc.",
"keywords": [
Expand Down Expand Up @@ -93,6 +113,7 @@
"server-destroy": "^1.0.1",
"sinon": "^15.0.0",
"tmp": "^0.2.0",
"tsc-multi": "^1.1.0",
"typescript": "5.1.6",
"yargs-parser": "^20.2.0"
}
Expand Down
2 changes: 1 addition & 1 deletion samples/test/test.samples.auth.js
Expand Up @@ -37,7 +37,7 @@ describe('Auth samples', () => {
.reply(200, {})
.post('/oauth2/v4/token')
.reply(200, {access_token: 'not-a-token'});
const fakePath = path.resolve('../test/fixtures/service.json');
const fakePath = path.resolve('../../test/fixtures/service.json');
const realPath = path.resolve('jwt.keys.json');
const exists = fs.existsSync(realPath);
if (!exists) {
Expand Down
2 changes: 1 addition & 1 deletion samples/test/test.samples.drive.js
Expand Up @@ -42,7 +42,7 @@ for (const sample of Object.values(samples)) {
});
}

const someFile = path.join(__dirname, '../../test/fixtures/public.pem');
const someFile = path.join(__dirname, '../../../test/fixtures/public.pem');
const baseUrl = 'https://www.googleapis.com';

describe('Drive samples', () => {
Expand Down
2 changes: 1 addition & 1 deletion samples/test/test.samples.youtube.js
Expand Up @@ -37,7 +37,7 @@ for (const sample of Object.values(samples)) {
});
}

const someFile = path.join(__dirname, '../../test/fixtures/public.pem');
const someFile = path.join(__dirname, '../../../test/fixtures/public.pem');

describe('YouTube samples', () => {
afterEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/generator/disclaimer.ts
Expand Up @@ -48,7 +48,7 @@ export const gfs = {
* used during generation to call out improved clients in READMEs for a given
* API.
*
* To use this, run `node build/src/generator/disclaimers`.
* To use this, run `node build/cjs/src/generator/disclaimers`.
*/
export async function main() {
const res = await request<LibraryMetadata[]>({url: libraryListUrl});
Expand Down
6 changes: 3 additions & 3 deletions src/generator/docs.ts
Expand Up @@ -12,17 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import * as execa from 'execa';
import * as fs from 'fs';
import * as nunjucks from 'nunjucks';
import * as path from 'path';
import {promisify} from 'util';
import Q from 'p-queue';
const execa: typeof import('execa') = require('execa');

const srcPath = path.join(__dirname, '../../../src');
const srcPath = path.join(__dirname, '../../../../src');
const apiPath = path.join(srcPath, 'apis');
const templatePath = path.join(srcPath, 'generator/templates/index.html.njk');
const docsPath = path.join(__dirname, '../../../docs');
const docsPath = path.join(__dirname, '../../../../docs');
const indexPath = path.join(docsPath, 'index.html');

export const gfs = {
Expand Down
5 changes: 3 additions & 2 deletions src/generator/download.ts
Expand Up @@ -12,14 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import * as minimist from 'yargs-parser';
import * as path from 'path';
import * as fs from 'fs';
const {mkdir} = require('fs').promises;
import Q from 'p-queue';
import {request, Headers} from 'gaxios';
import * as gapi from 'googleapis-common';

const minimist: typeof import('yargs-parser') = require('yargs-parser');

export type Schema = {[index: string]: {}};
export const DISCOVERY_URL = 'https://www.googleapis.com/discovery/v1/apis/';

Expand Down Expand Up @@ -240,6 +241,6 @@ if (require.main === module) {
const argv = minimist(process.argv.slice(2));
const discoveryUrl = argv['discovery-url'] || DISCOVERY_URL;
const downloadPath =
argv['download-path'] || path.join(__dirname, '../../../discovery');
argv['download-path'] || path.join(__dirname, '../../../../discovery');
downloadDiscoveryDocs({discoveryUrl, downloadPath});
}
22 changes: 12 additions & 10 deletions src/generator/generator.ts
Expand Up @@ -20,24 +20,25 @@
import * as util from 'util';
import Q from 'p-queue';
import * as prettier from 'prettier';
import * as minimist from 'yargs-parser';
import {GaxiosError, request} from 'gaxios';

Check warning on line 23 in src/generator/generator.ts

View workflow job for this annotation

GitHub Actions / lint

'GaxiosError' is defined but never used

Check warning on line 23 in src/generator/generator.ts

View workflow job for this annotation

GitHub Actions / lint

'GaxiosError' is defined but never used
import {DISCOVERY_URL} from './download';
import {downloadDiscoveryDocs, ChangeSet} from './download';
import * as filters from './filters';
import {addFragments} from './samplegen';
import {Disclaimer} from './disclaimer';

const minimist: typeof import('yargs-parser') = require('yargs-parser');

const writeFile = util.promisify(fs.writeFile);
const readDir = util.promisify(fs.readdir);
const readFile = util.promisify(fs.readFile);
const stat = util.promisify(fs.stat);

const srcPath = path.join(__dirname, '../../../src');
const srcPath = path.join(__dirname, '../../../../src');
const TEMPLATES_DIR = path.join(srcPath, 'generator/templates');
const API_TEMPLATE = path.join(TEMPLATES_DIR, 'api-endpoint.njk');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const disclaimers = require('../../../disclaimers.json') as Disclaimer[];
const disclaimers = require('../../../../disclaimers.json') as Disclaimer[];

export interface GeneratorOptions {
debug?: boolean;
Expand Down Expand Up @@ -103,8 +104,8 @@
discoveryUrl: string,
useCache: boolean
): Promise<ChangeSet[]> {
const ignore = require('../../../ignore.json').ignore as string[];
const discoveryPath = path.join(__dirname, '../../../discovery');
const ignore = require('../../../../ignore.json').ignore as string[];
const discoveryPath = path.join(__dirname, '../../../../discovery');
let changes = new Array<ChangeSet>();
if (useCache) {
console.log('Reading from cache...');
Expand Down Expand Up @@ -309,7 +310,7 @@
3. find the delta from 2 - 1
4. fill out bootstrap sha
*/
const disclaimers = require('../../../disclaimers.json') as Array<{
const disclaimers = require('../../../../disclaimers.json') as Array<{
package: string;
api: string;
}>;
Expand All @@ -322,7 +323,7 @@
.filter(e => e.isDirectory() && !excludedAPIs.includes(e.name))
.map(x => x.name);

const rootPath = path.join(__dirname, '../../../');
const rootPath = path.join(__dirname, '../../../../');

// Bootstrap sha is used the first time the releaser runs when it grabs the initial commits
// Afterwards, it uses the most recent release as a starting point
Expand All @@ -337,12 +338,13 @@

for (const api of releasableAPIs) {
releasePleaseConfig.packages[`src/apis/${api}`] = {};
releasePleaseManifest[`src/apis/${api}`] =
require(`../../../src/apis/${api}/package.json`).version;
releasePleaseManifest[`src/apis/${api}`] = require(
`../../../../src/apis/${api}/package.json`
).version;
}

// Include the root library in the config:
releasePleaseManifest['.'] = require('../../../package.json').version;
releasePleaseManifest['.'] = require('../../../../package.json').version;
releasePleaseConfig.packages['.'] = {};

fs.writeFileSync(
Expand Down
4 changes: 2 additions & 2 deletions src/generator/samplegen.ts
Expand Up @@ -28,10 +28,10 @@ const {mkdir} = require('fs').promises;
import * as util from 'util';

const writeFile = util.promisify(fs.writeFile);
const srcPath = path.join(__dirname, '../../../src');
const srcPath = path.join(__dirname, '../../../../src');
const TEMPLATES_DIR = path.join(srcPath, 'generator/templates');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const prettierConfig: prettier.Options = require('../../../node_modules/gts/.prettierrc.json');
const prettierConfig: prettier.Options = require('../../../../node_modules/gts/.prettierrc.json');
prettierConfig.parser = 'babel';

const env = new nunjucks.Environment(
Expand Down
4 changes: 2 additions & 2 deletions src/generator/synth.ts
Expand Up @@ -11,13 +11,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import * as execa from 'execa';
import * as path from 'path';
import * as fs from 'fs';
import * as gaxios from 'gaxios';
import * as minimist from 'yargs-parser';
import {Generator} from './generator';
import {DISCOVERY_URL, ChangeSet} from './download';
const execa: typeof import('execa') = require('execa');
const minimist: typeof import('yargs-parser') = require('yargs-parser');

export enum Semverity {
PATCH = 1,
Expand Down
2 changes: 1 addition & 1 deletion src/generator/templates/api-index.njk
Expand Up @@ -8,7 +8,7 @@ import {
GoogleConfigurable
} from 'googleapis-common';
{% for versionName, version in api %}
import { {{ name }}_{{ version|replace('.','_') }} } from './{{ version }}';
import { {{ name }}_{{ version|replace('.','_') }} } from './{{ version }}.js';
{% endfor %}

export const VERSIONS = {
Expand Down
4 changes: 2 additions & 2 deletions src/generator/templates/index.njk
Expand Up @@ -2,10 +2,10 @@
/*! THIS FILE IS AUTO-GENERATED */

{% for apiName, api in apis %}
import {
import {
VERSIONS as {{apiName}}Versions,
{{apiName}}
} from './{{ apiName }}';
} from './{{ apiName }}/index.js';
{% endfor %}

export interface APIList {
Expand Down
4 changes: 2 additions & 2 deletions src/generator/templates/root-index.njk
@@ -1,13 +1,13 @@
{% include "license.njk" %}
/*! THIS FILE IS AUTO-GENERATED */

import {GoogleApis} from './googleapis';
import {GoogleApis} from './googleapis.js';
const google = new GoogleApis();
export {google, GoogleApis};
export * as Common from 'googleapis-common';
export * as Auth from 'google-auth-library';

{% for apiName, api in apis %}
{% for versionName, version in api %}export { {{ apiName }}_{{ version|replace('.','_') }} } from './apis/{{ apiName }}/{{ version }}';
{% for versionName, version in api %}export { {{ apiName }}_{{ version|replace('.','_') }} } from './apis/{{ apiName }}/{{ version }}.js';
{% endfor %}
{% endfor %}
2 changes: 1 addition & 1 deletion test/test.clients.ts
Expand Up @@ -220,5 +220,5 @@ describe('Clients', () => {

it('should pass eslint for a given client', () => {
execSync('npx eslint --no-ignore src/apis/youtube/*.ts');
});
}).timeout(50000);
});
4 changes: 2 additions & 2 deletions test/test.download.ts
Expand Up @@ -134,7 +134,7 @@ describe(__filename, () => {
});
const indexPath = path.join(
__dirname,
'../../test/fixtures/index.json'
'../../../test/fixtures/index.json'
);
fs.readFile(indexPath, (err, data) => {
if (err) {
Expand All @@ -150,7 +150,7 @@ describe(__filename, () => {
.listen(port);
const downloadPath = 'build/test/temp';
await execa('node', [
'./build/src/generator/download',
'./build/cjs/src/generator/download',
'--download-path',
downloadPath,
'--discovery-url',
Expand Down