Skip to content

Commit

Permalink
🚨 enforce scripts/ files conventions (#3022)
Browse files Browse the repository at this point in the history
* 🚨 enforce import/no-unresolve on commonjs files

* 🚨 exclude lib folders from scripts

* 🚨 fix lint: rename scripts/lib files in camelCase

* 🚨 add a lint to enforce scripts in scripts folder

* 🚨 fix lint: use runMain in dev-server

* 🚨 fix lint: rearrange scripts/performance

* 🚨 skip no-unresolved for json-schema-to-typescript
  • Loading branch information
BenoitZugmeyer authored Oct 1, 2024
1 parent cd768fe commit f0dea09
Show file tree
Hide file tree
Showing 44 changed files with 116 additions and 77 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ module.exports = {
'import/no-default-export': 'error',
'import/no-duplicates': 'error',
'import/no-extraneous-dependencies': 'error',
'import/no-unresolved': 'error',
'import/no-unresolved': ['error', { commonjs: true }],
'import/no-useless-path-segments': 'error',
'import/order': 'error',

Expand Down Expand Up @@ -200,9 +200,11 @@ module.exports = {
},
{
files: ['scripts/**/*.js', 'packages/*/scripts/**/*.js'],
excludedFiles: ['**/lib/**'],
rules: {
'unicorn/filename-case': ['error', { case: 'kebabCase' }],
'local-rules/secure-command-execution': 'error',
'local-rules/disallow-non-scripts': 'error',
},
},
{
Expand Down
29 changes: 29 additions & 0 deletions eslint-local-rules/disallowNonScripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
meta: {
docs: {
description: 'Disallow JS files that are not used as a "script"',
},
schema: [],
},
create: (context) => ({
Program: (node) => {
if (!node.body.some(isMain)) {
context.report({
node,
message: 'This file should be a script and contain a `runMain()` expression',
})
}
},
}),
}

/**
* Check if the node is like `runMain(fn)`
*/
function isMain(node) {
return (
node.type === 'ExpressionStatement' &&
node.expression.type === 'CallExpression' &&
node.expression.callee.name === 'runMain'
)
}
1 change: 1 addition & 0 deletions eslint-local-rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ module.exports = {
'disallow-zone-js-patched-values': require('./disallowZoneJsPatchedValues'),
'disallow-url-constructor-patched-values': require('./disallowUrlConstructorPatchValues.js'),
'disallow-generic-utils': require('./disallowGenericUtils'),
'disallow-non-scripts': require('./disallowNonScripts'),
'secure-command-execution': require('./secureCommandExecution'),
}
6 changes: 3 additions & 3 deletions scripts/build/replace-build-env.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const glob = require('glob')
const { printLog, runMain } = require('../lib/execution-utils')
const { modifyFile } = require('../lib/files-utils')
const { buildEnvKeys, getBuildEnvValue } = require('../lib/build-env')
const { printLog, runMain } = require('../lib/executionUtils')
const { modifyFile } = require('../lib/filesUtils')
const { buildEnvKeys, getBuildEnvValue } = require('../lib/buildEnv')

/**
* Replace BuildEnv in build files
Expand Down
4 changes: 2 additions & 2 deletions scripts/check-licenses.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
const fs = require('fs')
const path = require('path')
const readline = require('readline')
const { printLog, printError, runMain } = require('./lib/execution-utils')
const { findBrowserSdkPackageJsonFiles } = require('./lib/files-utils')
const { printLog, printError, runMain } = require('./lib/executionUtils')
const { findBrowserSdkPackageJsonFiles } = require('./lib/filesUtils')

const LICENSE_FILE = 'LICENSE-3rdparty.csv'

Expand Down
2 changes: 1 addition & 1 deletion scripts/check-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs')
const path = require('path')
const glob = require('glob')
const minimatch = require('minimatch')
const { printLog, printError, runMain } = require('./lib/execution-utils')
const { printLog, printError, runMain } = require('./lib/executionUtils')
const { command } = require('./lib/command')

runMain(() => {
Expand Down
2 changes: 1 addition & 1 deletion scripts/check-schemas.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { printLog, printError, runMain } = require('./lib/execution-utils')
const { printLog, printError, runMain } = require('./lib/executionUtils')
const { command } = require('./lib/command')

runMain(() => {
Expand Down
4 changes: 2 additions & 2 deletions scripts/deploy/check-monitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Usage:
* node check-monitors.js us1,eu1,...
*/
const { printLog, runMain, fetchHandlingError } = require('../lib/execution-utils')
const { printLog, runMain, fetchHandlingError } = require('../lib/executionUtils')
const { getTelemetryOrgApiKey, getTelemetryOrgApplicationKey } = require('../lib/secrets')
const { siteByDatacenter } = require('../lib/datadog-sites')
const { siteByDatacenter } = require('../lib/datadogSites')

const monitorIdsByDatacenter = {
us1: [72055549, 68975047, 110519972],
Expand Down
2 changes: 1 addition & 1 deletion scripts/deploy/deploy-prod-dc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { printLog, runMain, timeout } = require('../lib/execution-utils')
const { printLog, runMain, timeout } = require('../lib/executionUtils')
const { command } = require('../lib/command')

/**
Expand Down
6 changes: 3 additions & 3 deletions scripts/deploy/deploy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { printLog, runMain } = require('../lib/execution-utils')
const { fetchPR, LOCAL_BRANCH } = require('../lib/git-utils')
const { printLog, runMain } = require('../lib/executionUtils')
const { fetchPR, LOCAL_BRANCH } = require('../lib/gitUtils')
const { command } = require('../lib/command')

const {
Expand All @@ -11,7 +11,7 @@ const {
buildBundleFileName,
buildPullRequestUploadPath,
packages,
} = require('./lib/deployment-utils')
} = require('./lib/deploymentUtils')

const ONE_MINUTE_IN_SECOND = 60
const ONE_HOUR_IN_SECOND = 60 * ONE_MINUTE_IN_SECOND
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion scripts/deploy/publish-developer-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const webstore = (...args) => import('chrome-webstore-upload').then(({ default: webstore }) => webstore(...args))
const fs = require('node:fs')

const { printLog, runMain } = require('../lib/execution-utils')
const { printLog, runMain } = require('../lib/executionUtils')
const { command } = require('../lib/command')
const {
getChromeWebStoreClientId,
Expand Down
2 changes: 1 addition & 1 deletion scripts/deploy/publish-npm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs')
const { printLog, runMain } = require('../lib/execution-utils')
const { printLog, runMain } = require('../lib/executionUtils')
const { command } = require('../lib/command')
const { getNpmToken } = require('../lib/secrets')

Expand Down
8 changes: 4 additions & 4 deletions scripts/deploy/upload-source-maps.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'use strict'

const path = require('path')
const { printLog, runMain } = require('../lib/execution-utils')
const { printLog, runMain } = require('../lib/executionUtils')
const { command } = require('../lib/command')
const { getBuildEnvValue } = require('../lib/build-env')
const { getBuildEnvValue } = require('../lib/buildEnv')
const { getTelemetryOrgApiKey } = require('../lib/secrets')
const { siteByDatacenter } = require('../lib/datadog-sites')
const { siteByDatacenter } = require('../lib/datadogSites')
const {
buildRootUploadPath,
buildDatacenterUploadPath,
buildBundleFolder,
buildBundleFileName,
packages,
} = require('./lib/deployment-utils')
} = require('./lib/deploymentUtils')

/**
* Upload source maps to datadog
Expand Down
12 changes: 7 additions & 5 deletions scripts/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ const rumSlimConfig = require('../packages/rum-slim/webpack.config')
const rumConfig = require('../packages/rum/webpack.config')
const workerConfig = require('../packages/worker/webpack.config')
const webpackBase = require('../webpack.base')
const { printLog } = require('./lib/execution-utils')
const { printLog, runMain } = require('./lib/executionUtils')

const sandboxPath = path.join(__dirname, '../sandbox')
const port = 8080

const app = express()
app.use(createStaticSandboxApp())
app.use('/react-app', createReactApp())
app.listen(port, () => printLog(`Server listening on port ${port}.`))
runMain(() => {
const app = express()
app.use(createStaticSandboxApp())
app.use('/react-app', createReactApp())
app.listen(port, () => printLog(`Server listening on port ${port}.`))
})

function createStaticSandboxApp() {
const app = express()
Expand Down
7 changes: 6 additions & 1 deletion scripts/generate-schema-types.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const fs = require('fs')
const path = require('path')

// The json-schema-to-typescript is built on demand (see scripts/cli build_json2type) and is not
// always available in the node_modules. Skip the import check.
// eslint-disable-next-line import/no-unresolved
const { compileFromFile } = require('json-schema-to-typescript')

const prettier = require('prettier')
const { printLog, runMain } = require('./lib/execution-utils')
const { printLog, runMain } = require('./lib/executionUtils')

const schemasDirectoryPath = path.join(__dirname, '../rum-events-format/schemas')
const prettierConfigPath = path.join(__dirname, '../.prettierrc.yml')
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion scripts/lib/bs-utils.js β†’ scripts/lib/bsUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { fetchHandlingError } = require('../lib/execution-utils')
const { fetchHandlingError } = require('../lib/executionUtils')

async function browserStackRequest(url, options) {
const response = await fetchHandlingError(url, {
Expand Down
2 changes: 1 addition & 1 deletion scripts/lib/build-env.js β†’ scripts/lib/buildEnv.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { readFileSync } = require('fs')
const path = require('path')
const execSync = require('child_process').execSync
const { browserSdkVersion } = require('./browser-sdk-version')
const { browserSdkVersion } = require('./browserSdkVersion')
const { command } = require('./command')

/**
Expand Down
2 changes: 1 addition & 1 deletion scripts/lib/command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const childProcess = require('child_process')

const { printError } = require('./execution-utils')
const { printError } = require('./executionUtils')

/**
* Helper to run executables. This has been introduced to work around Shell injections[0] while
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion scripts/lib/git-utils.js β†’ scripts/lib/gitUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs')

const { command } = require('../lib/command')
const { getGithubDeployKey, getGithubAccessToken } = require('./secrets')
const { fetchHandlingError } = require('./execution-utils')
const { fetchHandlingError } = require('./executionUtils')

const GITHUB_TOKEN = getGithubAccessToken()

Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions scripts/performance/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { runMain } = require('../lib/execution-utils')
const { reportAsPrComment } = require('./report-as-a-pr-comment')
const { reportToDatadog } = require('./report-to-datadog')
const { calculateBundleSizes } = require('./bundle-size/compute-bundle-size')
const { computeCpuPerformance } = require('./cpu-performance/compute-cpu-performance')
const { computeMemoryPerformance } = require('./memory-performance/compute-memory-performance')
const { runMain } = require('../lib/executionUtils')
const { reportAsPrComment } = require('./lib/reportAsAPrComment')
const { reportToDatadog } = require('./lib/reportToDatadog')
const { calculateBundleSizes } = require('./lib/computeBundleSize')
const { computeCpuPerformance } = require('./lib/computeCpuPerformance')
const { computeMemoryPerformance } = require('./lib/computeMemoryPerformance')

runMain(async () => {
const localBundleSizes = calculateBundleSizes()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { fetchHandlingError } = require('../../lib/execution-utils')
const { fetchHandlingError } = require('../../lib/executionUtils')
const { getOrg2ApiKey, getOrg2AppKey } = require('../../lib/secrets')
const { timeout } = require('../../lib/execution-utils')
const { fetchPR, LOCAL_BRANCH } = require('../../lib/git-utils')
const { LOCAL_COMMIT_SHA } = require('../report-as-a-pr-comment')
const { timeout } = require('../../lib/executionUtils')
const { fetchPR, LOCAL_BRANCH } = require('../../lib/gitUtils')
const { LOCAL_COMMIT_SHA } = require('./reportAsAPrComment')
const API_KEY = getOrg2ApiKey()
const APP_KEY = getOrg2AppKey()
const TIMEOUT_IN_MS = 15000
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const puppeteer = require('puppeteer')
const { fetchPR, LOCAL_BRANCH } = require('../../lib/git-utils')
const { fetchPR, LOCAL_BRANCH } = require('../../lib/gitUtils')
const NUMBER_OF_RUNS = 30 // Rule of thumb: this should be enough to get a good average
const BATCH_SIZE = 2
const TESTS = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { getOrg2ApiKey, getOrg2AppKey } = require('../lib/secrets')
const { fetchHandlingError } = require('../lib/execution-utils')
const { getOrg2ApiKey, getOrg2AppKey } = require('../../lib/secrets')
const { fetchHandlingError } = require('../../lib/executionUtils')
const ONE_DAY_IN_SECOND = 24 * 60 * 60

function fetchPerformanceMetrics(type, names, commitId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { command } = require('../lib/command')
const { fetchHandlingError } = require('../lib/execution-utils')
const { LOCAL_BRANCH, GITHUB_TOKEN, getLastCommonCommit, fetchPR } = require('../lib/git-utils')
const { fetchPerformanceMetrics } = require('./fetch-performance-metrics')
const { command } = require('../../lib/command')
const { fetchHandlingError } = require('../../lib/executionUtils')
const { LOCAL_BRANCH, GITHUB_TOKEN, getLastCommonCommit, fetchPR } = require('../../lib/gitUtils')
const { fetchPerformanceMetrics } = require('./fetchPerformanceMetrics')
const PR_COMMENT_HEADER = 'Bundles Sizes Evolution'
const PR_COMMENTER_AUTH_TOKEN = command`authanywhere --raw`.run()
// The value is set to 5% as it's around 10 times the average value for small PRs.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { fetchHandlingError } = require('../lib/execution-utils')
const { getOrg2ApiKey } = require('../lib/secrets')
const { browserSdkVersion } = require('../lib/browser-sdk-version')
const { fetchHandlingError } = require('../../lib/executionUtils')
const { getOrg2ApiKey } = require('../../lib/secrets')
const { browserSdkVersion } = require('../../lib/browserSdkVersion')
const LOG_INTAKE_URL = 'https://http-intake.logs.datadoghq.com/api/v2/logs'
const LOG_INTAKE_REQUEST_HEADERS = {
'DD-API-KEY': getOrg2ApiKey(),
Expand Down
4 changes: 2 additions & 2 deletions scripts/release/check-release.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const { version: releaseVersion } = require('../../lerna.json')
const { printLog, runMain } = require('../lib/execution-utils')
const { printLog, runMain } = require('../lib/executionUtils')
const { command } = require('../lib/command')
const { findBrowserSdkPackageJsonFiles } = require('../lib/files-utils')
const { findBrowserSdkPackageJsonFiles } = require('../lib/filesUtils')

runMain(() => {
checkGitTag()
Expand Down
8 changes: 4 additions & 4 deletions scripts/release/generate-changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const readFile = util.promisify(require('fs').readFile)

const emojiNameMap = require('emoji-name-map')

const { browserSdkVersion } = require('../lib/browser-sdk-version')
const { spawnCommand, printError, runMain } = require('../lib/execution-utils')
const { browserSdkVersion } = require('../lib/browserSdkVersion')
const { spawnCommand, printError, runMain } = require('../lib/executionUtils')
const { command } = require('../lib/command')
const { modifyFile } = require('../lib/files-utils')
const { packagesDirectoryNames } = require('../lib/packages-directory-names')
const { modifyFile } = require('../lib/filesUtils')
const { packagesDirectoryNames } = require('../lib/packagesDirectoryNames')

const CHANGELOG_FILE = 'CHANGELOG.md'
const CONTRIBUTING_FILE = 'CONTRIBUTING.md'
Expand Down
8 changes: 4 additions & 4 deletions scripts/release/update-peer-dependency-versions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { runMain } = require('../lib/execution-utils')
const { modifyFile } = require('../lib/files-utils')
const { runMain } = require('../lib/executionUtils')
const { modifyFile } = require('../lib/filesUtils')
const { command } = require('../lib/command')
const { browserSdkVersion } = require('../lib/browser-sdk-version')
const { packagesDirectoryNames } = require('../lib/packages-directory-names')
const { browserSdkVersion } = require('../lib/browserSdkVersion')
const { packagesDirectoryNames } = require('../lib/packagesDirectoryNames')

const JSON_FILES = packagesDirectoryNames.map((packageName) => `./packages/${packageName}/package.json`)

Expand Down
4 changes: 2 additions & 2 deletions scripts/staging-ci/check-squash-into-staging.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const { printLog, printError, runMain } = require('../lib/execution-utils')
const { printLog, printError, runMain } = require('../lib/executionUtils')
const { command } = require('../lib/command')
const { initGitConfig } = require('../lib/git-utils')
const { initGitConfig } = require('../lib/gitUtils')

const REPOSITORY = process.env.GIT_REPOSITORY
const CI_COMMIT_SHA = process.env.CI_COMMIT_SHA
Expand Down
4 changes: 2 additions & 2 deletions scripts/staging-ci/check-staging-merge.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const { printLog, printError, runMain } = require('../lib/execution-utils')
const { printLog, printError, runMain } = require('../lib/executionUtils')
const { command } = require('../lib/command')
const { initGitConfig } = require('../lib/git-utils')
const { initGitConfig } = require('../lib/gitUtils')

const REPOSITORY = process.env.GIT_REPOSITORY
const CI_COMMIT_SHA = process.env.CI_COMMIT_SHA
Expand Down
6 changes: 3 additions & 3 deletions scripts/staging-ci/staging-reset.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const fs = require('fs')
const { printLog, runMain } = require('../lib/execution-utils')
const { printLog, runMain } = require('../lib/executionUtils')
const { command } = require('../lib/command')
const { CI_FILE, replaceCiFileVariable, readCiFileVariable } = require('../lib/files-utils')
const { initGitConfig } = require('../lib/git-utils')
const { CI_FILE, replaceCiFileVariable, readCiFileVariable } = require('../lib/filesUtils')
const { initGitConfig } = require('../lib/gitUtils')

const REPOSITORY = process.env.GIT_REPOSITORY
const MAIN_BRANCH = process.env.MAIN_BRANCH
Expand Down
4 changes: 2 additions & 2 deletions scripts/test/bs-kill.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* This script stops all BrowserStack workers that are currently running.
* Useful when troubleshooting BrowserStack issues locally.
*/
const { printLog, runMain, timeout } = require('../lib/execution-utils')
const { browserStackRequest } = require('../lib/bs-utils')
const { printLog, runMain, timeout } = require('../lib/executionUtils')
const { browserStackRequest } = require('../lib/bsUtils')

const MINIMUM_WORKER_LIFE = 30_000

Expand Down
Loading

0 comments on commit f0dea09

Please sign in to comment.