Skip to content

Commit 8d0355f

Browse files
committed
refactor: updates after switching from using commonjs to esm
1 parent 8f00e33 commit 8d0355f

File tree

9 files changed

+42
-36
lines changed

9 files changed

+42
-36
lines changed

run/markdown-preview/editor/app.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,28 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
const express = require('express');
16-
const handlebars = require('handlebars');
17-
const {readFile} = require('fs').promises;
18-
const renderRequest = require('./render.js');
15+
import express from 'express';
16+
import handlebars from 'handlebars';
17+
import fs from 'fs';
18+
import renderRequest from './render.js';
1919

2020
const app = express();
21+
2122
app.use(express.json());
2223

2324
let markdownDefault, compiledTemplate, renderedHtml;
2425

2526
// Load the template files and serve them with the Editor service.
26-
const buildRenderedHtml = async () => {
27+
export const buildRenderedHtml = async () => {
28+
const dirname = process.cwd();
29+
2730
try {
28-
markdownDefault = await readFile(__dirname + '/templates/markdown.md');
31+
markdownDefault = await fs.promises.readFile(dirname + '/templates/markdown.md');
2932
compiledTemplate = handlebars.compile(
30-
await readFile(__dirname + '/templates/index.html', 'utf8')
33+
await fs.promises.readFile(dirname + '/templates/index.html', 'utf8')
3134
);
3235
renderedHtml = compiledTemplate({default: markdownDefault});
36+
3337
return renderedHtml;
3438
} catch (err) {
3539
throw Error('Error loading template: ', err);
@@ -64,7 +68,4 @@ app.post('/render', async (req, res) => {
6468
// [END cloudrun_secure_request_do]
6569

6670
// Exports for testing purposes.
67-
module.exports = {
68-
app,
69-
buildRenderedHtml,
70-
};
71+
export default app;

run/markdown-preview/editor/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
const {app} = require('./app');
16-
const pkg = require('./package.json');
15+
import app from './app.js';
16+
import pkg from './package.json' assert { type: "json" };
17+
1718
const PORT = parseInt(process.env.PORT) || 8080;
1819

1920
app.listen(PORT, () => console.log(`${pkg.name} listening on port ${PORT}`));

run/markdown-preview/editor/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"type": "git",
1010
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
1111
},
12+
"type": "module",
1213
"engines": {
1314
"node": ">=16.0.0"
1415
},
@@ -21,8 +22,8 @@
2122
"dependencies": {
2223
"express": "^4.17.1",
2324
"google-auth-library": "^9.0.0",
24-
"got": "^11.8.0",
25-
"handlebars": "^4.7.6"
25+
"got": "^14.0.0",
26+
"handlebars": "^4.7.8"
2627
},
2728
"devDependencies": {
2829
"c8": "^8.0.0",

run/markdown-preview/editor/render.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
// [START cloudrun_secure_request]
1616
// [START run_secure_request]
17-
const {GoogleAuth} = require('google-auth-library');
18-
const got = require('got');
17+
18+
import { GoogleAuth } from 'google-auth-library';
19+
import got from 'got';
20+
1921
const auth = new GoogleAuth();
2022

2123
let client, serviceUrl;
@@ -61,4 +63,4 @@ const renderRequest = async markdown => {
6163
// [END run_secure_request]
6264
// [END cloudrun_secure_request]
6365

64-
module.exports = renderRequest;
66+
export default renderRequest;

run/markdown-preview/editor/test/app.test.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
'use strict';
16-
17-
const assert = require('assert');
18-
const path = require('path');
19-
const supertest = require('supertest');
15+
import assert from 'assert';
16+
import path from 'path';
17+
import supertest from 'supertest';
2018

2119
describe('Editor unit tests', () => {
2220
describe('Initialize app', () => {
2321
it('should successfully load the index page', async () => {
24-
const {app} = require(path.join(__dirname, '..', 'app'));
22+
import { app } from path.join(__dirname, '..', 'app');
2523
const request = supertest(app);
2624
await request.get('/').retry(3).expect(200);
2725
});
@@ -31,7 +29,7 @@ describe('Editor unit tests', () => {
3129
let template;
3230

3331
before(async () => {
34-
const {buildRenderedHtml} = require(path.join(__dirname, '..', 'app'));
32+
import { buildRenderedHtml } from path.join(__dirname, '..', 'app');
3533
template = await buildRenderedHtml();
3634
});
3735

@@ -48,7 +46,7 @@ describe('Integration tests', () => {
4846

4947
before(async () => {
5048
process.env.EDITOR_UPSTREAM_RENDER_URL = 'https://www.example.com/';
51-
const {app} = require(path.join(__dirname, '..', 'app'));
49+
import { app } from path.join(__dirname, '..', 'app');
5250
request = supertest(app);
5351
});
5452

run/markdown-preview/editor/test/system.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
const assert = require('assert');
16-
const got = require('got');
17-
const {execSync} = require('child_process');
18-
const {GoogleAuth} = require('google-auth-library');
15+
import assert from 'assert';
16+
import got from 'got';
17+
import { execSync } from 'child_process';
18+
import { GoogleAuth } from 'google-auth-library';
19+
1920
const auth = new GoogleAuth();
2021

2122
describe('End-to-End Tests', () => {

run/markdown-preview/renderer/app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
const express = require('express');
16-
const MarkdownIt = require('markdown-it');
15+
import express from 'express';
16+
import MarkdownIt from 'markdown-it';
1717

1818
const app = express();
1919
app.use(express.text());
@@ -40,4 +40,4 @@ app.post('/', (req, res) => {
4040
});
4141

4242
// Export for testing purposes.
43-
module.exports = app;
43+
export default app;

run/markdown-preview/renderer/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
const app = require('./app');
16-
const pkg = require('./package.json');
15+
import app from './app.js';
16+
import pkg from './package.json' assert { type: "json" };
17+
1718
const PORT = parseInt(process.env.PORT) || 8080;
1819

1920
app.listen(PORT, () => console.log(`${pkg.name} listening on port ${PORT}`));

run/markdown-preview/renderer/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"type": "git",
99
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
1010
},
11+
"type": "module",
1112
"engines": {
1213
"node": ">=16.0.0"
1314
},
@@ -24,7 +25,7 @@
2425
"devDependencies": {
2526
"c8": "^8.0.0",
2627
"google-auth-library": "^9.0.0",
27-
"got": "^11.5.0",
28+
"got": "^14.0.0",
2829
"mocha": "^10.0.0",
2930
"sinon": "^16.0.0",
3031
"supertest": "^6.0.0"

0 commit comments

Comments
 (0)