Skip to content

Commit

Permalink
Migrate CommonJS to ESM (github#20301)
Browse files Browse the repository at this point in the history
* First run of script

* Get the app running --- ish

* Get NextJS working

* Remove `node:`

* Get more tests passing in unit directory

* Update FailBot test to use nock

* Update test.yml

* Update Dockerfile

* tests/content fixes

* Update page.js

* Update build-changelog.js

* updating tests/routing

* Update orphan-tests.js

* updating tests/rendering

* Update .eslintrc.js

* Update .eslintrc.js

* Install jest/globals

* "linting" tests

* staging update to server.mjs

* Change '.github/allowed-actions.js' to a ESM export

* Lint

* Fixes for the main package.json

* Move Jest to be last in the npm test command so we can pass args

* Just use 'npm run lint' in the npm test command

* update algolia label script

* update openapi script

* update require on openapi

* Update enterprise-algolia-label.js

* forgot JSON.parse

* Update lunr-search-index.js

* Always explicitly include process.cwd() for JSON file reads pathed from project root

* update graphql/update-files.js script

* Update other npm scripts using jest to pass ESM NODE_OPTIONS

* Update check-for-enterprise-issues-by-label.js for ESM

* Update create-enterprise-issue.js for ESM

* Import jest global for browser tests

* Convert 'script/deploy' to ESM

Co-authored-by: Grace Park <gracepark@github.com>
Co-authored-by: James M. Greene <jamesmgreene@github.com>
  • Loading branch information
3 people authored Jul 14, 2021
1 parent b6f8278 commit 42e785b
Show file tree
Hide file tree
Showing 351 changed files with 6,472 additions and 8,225 deletions.
6 changes: 4 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ module.exports = {
'prettier'
],
parserOptions: {
ecmaVersion: 11
ecmaVersion: 11,
requireConfigFile: 'false',
babelOptions: { configFile: './.babelrc' }
},
rules: {
'import/no-extraneous-dependencies': ['error'],
'import/no-extraneous-dependencies': ['error', { packageDir: '.' }],
'node/global-require': ['error'],
'import/no-dynamic-require': ['error']
},
Expand Down
12 changes: 6 additions & 6 deletions .github/actions-scripts/check-for-enterprise-issues-by-label.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#!/usr/bin/env node

const github = require('@actions/github')
const core = require('@actions/core')
import { getOctokit } from '@actions/github'
import { setOutput } from '@actions/core'

async function run () {
const token = process.env.GITHUB_TOKEN
const octokit = github.getOctokit(token)
const octokit = getOctokit(token)
const query = encodeURIComponent('is:open repo:github/docs-internal is:issue')

const deprecationIssues = await octokit.request(`GET /search/issues?q=${query}+label:"enterprise%20deprecation"`)
const releaseIssues = await octokit.request(`GET /search/issues?q=${query}+label:"enterprise%20release"`)
const isDeprecationIssue = deprecationIssues.data.items.length === 0 ? 'false' : 'true'
const isReleaseIssue = releaseIssues.data.items.length === 0 ? 'false' : 'true'
core.setOutput('deprecationIssue', isDeprecationIssue)
core.setOutput('releaseIssue', isReleaseIssue)
setOutput('deprecationIssue', isDeprecationIssue)
setOutput('releaseIssue', isReleaseIssue)
return `Set outputs deprecationIssue: ${isDeprecationIssue}, releaseIssue: ${isReleaseIssue}`
}

Expand Down
14 changes: 7 additions & 7 deletions .github/actions-scripts/create-enterprise-issue.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const github = require('@actions/github')
const enterpriseDates = require('../../lib/enterprise-dates')
const { latest, oldestSupported } = require('../../lib/enterprise-server-releases')
import fs from 'fs'
import path from 'path'
import { getOctokit } from '@actions/github'
import enterpriseDates from '../../lib/enterprise-dates.js'
import { latest, oldestSupported } from '../../lib/enterprise-server-releases.js'

const acceptedMilestones = ['release', 'deprecation']
const teamsToCC = '/cc @github/docs-content @github/docs-engineering'

Expand All @@ -31,7 +32,6 @@ const numberOfdaysBeforeDeprecationToOpenIssue = 15
run()

async function run () {


const milestone = process.argv[2]
if (!acceptedMilestones.includes(milestone)) {
Expand Down Expand Up @@ -81,7 +81,7 @@ async function run () {
const token = process.env.GITHUB_TOKEN

// Create the milestone issue
const octokit = github.getOctokit(token)
const octokit = getOctokit(token)
try {
issue = await octokit.request('POST /repos/{owner}/{repo}/issues', {
owner: 'github',
Expand Down
9 changes: 5 additions & 4 deletions .github/actions-scripts/enterprise-algolia-label.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env node

const fs = require('fs')
const core = require('@actions/core')
import fs from 'fs'
import { setOutput } from '@actions/core'

const eventPayload = JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'))

// This workflow-run script does the following:
Expand Down Expand Up @@ -32,5 +33,5 @@ if (!algoliaLabel) {
const versionToSync = algoliaLabel.split(labelText)[1]

// Store the version so we can access it later in the workflow
core.setOutput('versionToSync', versionToSync)
process.exit(0)
setOutput('versionToSync', versionToSync)
process.exit(0)
11 changes: 6 additions & 5 deletions .github/actions-scripts/openapi-schema-branch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const { execSync } = require('child_process')
const semver = require('semver')
import fs from 'fs'
import path from 'path'
import { execSync } from 'child_process'
import semver from 'semver'

/*
* This script performs two checks to prevent shipping development mode OpenAPI schemas:
Expand All @@ -19,8 +19,9 @@ const semver = require('semver')
// Check that the `info.version` property is a semantic version
const dereferencedDir = path.join(process.cwd(), 'lib/rest/static/dereferenced')
const schemas = fs.readdirSync(dereferencedDir)

schemas.forEach(filename => {
const schema = require(path.join(dereferencedDir, filename))
const schema = JSON.parse(fs.readFileSync(path.join(dereferencedDir, filename)))
if (!semver.valid(schema.info.version)) {
console.log(`🚧⚠️ Your branch contains a development mode OpenAPI schema: ${schema.info.version}. This check is a reminder to not 🚢 OpenAPI files in development mode. 🛑`)
process.exit(1)
Expand Down
8 changes: 4 additions & 4 deletions .github/allowed-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// CI will fail and the action will need to be audited by the docs engineering team before it
// can be added it this list.

module.exports = [
export default [
"actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f", // v2.3.4
"actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d", // v4.0.2
"actions/labeler@5f867a63be70efff62b767459b009290364495eb", // v2.2.0
Expand All @@ -17,6 +17,7 @@ module.exports = [
"cschleiden/actions-linter@0ff16d6ac5103cca6c92e6cbc922b646baaea5be",
"dawidd6/action-delete-branch@47743101a121ad657031e6704086271ca81b1911",
"docker://chinthakagodawita/autoupdate-action:v1",
"dorny/paths-filter@eb75a1edc117d3756a18ef89958ee59f9500ba58",
"github/codeql-action/analyze@v1",
"github/codeql-action/init@v1",
"juliangruber/approve-pull-request-action@c530832d4d346c597332e20e03605aa94fa150a8",
Expand All @@ -34,6 +35,5 @@ module.exports = [
"repo-sync/pull-request@33777245b1aace1a58c87a29c90321aa7a74bd7d",
"someimportantcompany/github-actions-slack-message@0b470c14b39da4260ed9e3f9a4f1298a74ccdefd",
"tjenkinson/gh-action-auto-merge-dependency-updates@4d7756c04d9d999c5968697a621b81c47f533d61",
"EndBug/add-and-commit@b3c7c1e078a023d75fb0bd326e02962575ce0519",
"dorny/paths-filter@eb75a1edc117d3756a18ef89958ee59f9500ba58",
];
"EndBug/add-and-commit@b3c7c1e078a023d75fb0bd326e02962575ce0519"
]
1 change: 1 addition & 0 deletions .github/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"module"}
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ jobs:
- name: Run tests
run: npx jest tests/${{ matrix.test-group }}/
env:
NODE_OPTIONS: '--max_old_space_size=8192'
NODE_OPTIONS: '--max_old_space_size=8192 --experimental-vm-modules'
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ COPY --chown=node:node layouts ./layouts
COPY --chown=node:node lib ./lib
COPY --chown=node:node middleware ./middleware
COPY --chown=node:node translations ./translations
COPY --chown=node:node server.js ./server.js
COPY --chown=node:node server.mjs ./server.mjs
COPY --chown=node:node package*.json ./
COPY --chown=node:node feature-flags.json ./

EXPOSE 80
EXPOSE 443
EXPOSE 4000
CMD ["node", "server.js"]
CMD ["node", "server.mjs"]
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
web: NODE_ENV=production node server.js
web: NODE_ENV=production node server.mjs

release: NODE_ENV=production script/release-heroku
2 changes: 1 addition & 1 deletion data/allowed-topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// content model, add the entry to this list, and ensure you loop in the
// content and/or content strategy team for review.

module.exports = [
export default [
'2FA',
'Access management',
'Accounts',
Expand Down
1 change: 1 addition & 0 deletions data/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"module"}
16 changes: 8 additions & 8 deletions lib/all-products.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const fs = require('fs')
const path = require('path')
const frontmatter = require('./read-frontmatter')
const getApplicableVersions = require('./get-applicable-versions')
const removeFPTFromPath = require('./remove-fpt-from-path')
import fs from 'fs'
import path from 'path'
import frontmatter from './read-frontmatter.js'
import getApplicableVersions from './get-applicable-versions.js'
import removeFPTFromPath from './remove-fpt-from-path.js'

// Both internal and external products are specified in content/index.md
const homepage = path.posix.join(process.cwd(), 'content/index.md')
const { data } = frontmatter(fs.readFileSync(homepage, 'utf8'))
const productIds = data.children
export const productIds = data.children
const externalProducts = data.externalProducts

const internalProducts = {}
Expand Down Expand Up @@ -37,9 +37,9 @@ productIds.forEach(productId => {
internalProducts[productId].versions = applicableVersions
})

const productMap = Object.assign({}, internalProducts, externalProducts)
export const productMap = Object.assign({}, internalProducts, externalProducts)

module.exports = {
export default {
productIds,
productMap
}
4 changes: 2 additions & 2 deletions lib/all-versions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const enterpriseServerReleases = require('./enterprise-server-releases')
import enterpriseServerReleases from './enterprise-server-releases.js'

// version = "plan"@"release"
// example: enterprise-server@2.21
Expand Down Expand Up @@ -60,4 +60,4 @@ plans.forEach(planObj => {
})
})

module.exports = allVersions
export default allVersions
6 changes: 3 additions & 3 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const express = require('express')
const middleware = require('../middleware')
import express from 'express'
import middleware from '../middleware/index.js'

function createApp () {
const app = express()
middleware(app)
return app
}

module.exports = createApp
export default createApp
8 changes: 4 additions & 4 deletions lib/built-asset-urls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
import fs from 'fs'
import path from 'path'
import crypto from 'crypto'

// Get an MD4 Digest Hex content hash, loosely based on Webpack `[contenthash]`
function getContentHash (absFilePath) {
Expand All @@ -15,7 +15,7 @@ function getUrl (relFilePath) {
return `/${relFilePath}?hash=${getContentHash(absFilePath)}`
}

module.exports = {
export default {
main: {
js: getUrl('dist/index.js'),
css: getUrl('dist/index.css')
Expand Down
8 changes: 4 additions & 4 deletions lib/changelog.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Parser = require('rss-parser')
import Parser from 'rss-parser'

async function getRssFeed (url) {
export async function getRssFeed (url) {
const parser = new Parser({ timeout: 5000 })
const feedUrl = `${url}/feed`
let feed
Expand All @@ -15,7 +15,7 @@ async function getRssFeed (url) {
return feed
}

async function getChangelogItems (prefix, feed) {
export async function getChangelogItems (prefix, feed) {
if (!feed || !feed.items) {
console.log(feed)
console.error('feed is not valid or has no items')
Expand All @@ -39,4 +39,4 @@ async function getChangelogItems (prefix, feed) {
return changelog
}

module.exports = { getRssFeed, getChangelogItems }
export default { getRssFeed, getChangelogItems }
6 changes: 3 additions & 3 deletions lib/check-if-next-version-only.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { next, latest } = require('./enterprise-server-releases')
const versionSatisfiesRange = require('./version-satisfies-range')
import { next, latest } from './enterprise-server-releases.js'
import versionSatisfiesRange from './version-satisfies-range.js'

// Special handling for frontmatter that evalues to the next GHES release number or a hardcoded `next`:
// we don't want to return it as an applicable version or it will become a permalink,
// but we also don't want to throw an error if no other versions are found.
module.exports = function checkIfNextVersionOnly (value) {
export default function checkIfNextVersionOnly (value) {
if (value === '*') return false

const ghesNextVersionOnly = versionSatisfiesRange(next, value) && !versionSatisfiesRange(latest, value)
Expand Down
8 changes: 6 additions & 2 deletions lib/check-node-version.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const semver = require('semver')
const { engines } = require('../package.json')
import semver from 'semver'
import fs from 'fs'
import path from 'path'

const packageFile = JSON.parse(fs.readFileSync(path.join(process.cwd(), './package.json')))
const { engines } = packageFile

/* istanbul ignore next */
if (!semver.satisfies(process.version, engines.node)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/cookie-settings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
httpOnly: true, // can't access these cookies through browser JavaScript
secure: !['test', 'development'].includes(process.env.NODE_ENV),
// requires https protocol
Expand Down
13 changes: 8 additions & 5 deletions lib/create-tree.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const fs = require('fs').promises
const path = require('path')
const Page = require('./page')

module.exports = async function createTree (originalPath, langObj) {
import { fileURLToPath } from 'url'
import path from 'path'
import xFs from 'fs'
import Page from './page.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const fs = xFs.promises

export default async function createTree (originalPath, langObj) {
// This basePath definition is needed both here and in lib/page-data.js because this
// function runs recursively, and the value for originalPath changes on recursive runs.
const basePath = path.posix.join(__dirname, '..', langObj.dir, 'content')
Expand Down
16 changes: 8 additions & 8 deletions lib/data-directory.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const walk = require('walk-sync')
const yaml = require('js-yaml')
const { isRegExp, set } = require('lodash')
const filenameToKey = require('./filename-to-key')
import assert from 'assert'
import fs from 'fs'
import path from 'path'
import walk from 'walk-sync'
import yaml from 'js-yaml'
import { isRegExp, set } from 'lodash-es'
import filenameToKey from './filename-to-key.js'

module.exports = function dataDirectory (dir, opts = {}) {
export default function dataDirectory (dir, opts = {}) {
const defaultOpts = {
preprocess: (content) => { return content },
ignorePatterns: [/README\.md$/i],
Expand Down
2 changes: 1 addition & 1 deletion lib/encode-bracketed-parentheses.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// prevent `[foo] (bar)` strings with a space between from being interpreted as markdown links
// by encoding the space character

module.exports = function encodeBracketedParentheses (input) {
export default function encodeBracketedParentheses (input) {
return input.replace(/] \(/g, ']&nbsp;(')
}
Loading

0 comments on commit 42e785b

Please sign in to comment.