Skip to content

Commit

Permalink
fix: migrate framework-info to TS and vitest and enable more strictne…
Browse files Browse the repository at this point in the history
…ss (#4819)

* fix: version is not part of read-pkg-up result

* fix: migrate context to TS and ensure we do not access version on undefined

* fix: migrate framework-info to TS and vitest and enable more strictness

* chore: fix tests

* chore: fix order of build commands

* chore: move fixture

* chore: fix build

* chore: move to prebuild
  • Loading branch information
danez committed Feb 3, 2023
1 parent e41f7f1 commit 5acb25b
Show file tree
Hide file tree
Showing 79 changed files with 1,924 additions and 1,046 deletions.
1,301 changes: 1,091 additions & 210 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions packages/build/src/log/messages/compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const getPluginDescription = function (
},
debug,
) {
const versionedPackage = getVersionedPackage(packageName, version)
const versionedPackage = getVersionedPackage(version)
const pluginOrigin = getPluginOrigin(loadedFrom, origin)
const description = `${THEME.highlightWords(packageName)}${versionedPackage} ${pluginOrigin}`
if (!debug) {
Expand Down Expand Up @@ -106,7 +106,7 @@ const getOutdatedPlugin = function ({
loadedFrom,
origin,
}) {
const versionedPackage = getVersionedPackage(packageName, version)
const versionedPackage = getVersionedPackage(version)
const outdatedDescription = getOutdatedDescription({ latestVersion, migrationGuide, loadedFrom, origin })
return `${THEME.warningHighlightWords(packageName)}${versionedPackage}: ${outdatedDescription}`
}
Expand Down Expand Up @@ -165,14 +165,14 @@ const getIncompatiblePlugin = function ({
compatibleVersion,
compatWarning,
}) {
const versionedPackage = getVersionedPackage(packageName, version)
const versionedPackage = getVersionedPackage(version)
return `${THEME.warningHighlightWords(
packageName,
)}${versionedPackage}: version ${compatibleVersion} is the most recent version compatible with ${compatWarning}`
}

// Make sure we handle `package.json` with `version` being either `undefined`
// or an empty string
const getVersionedPackage = function (packageName, version = '') {
const getVersionedPackage = function (version = '') {
return version === '' ? '' : `@${version}`
}
2 changes: 1 addition & 1 deletion packages/build/src/plugins/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const loadPluginFiles = async function ({
// Core plugins can only be included once.
// For example, when testing core plugins, they might be included as local plugins,
// in which case they should not be included twice.
const isNotRedundantCorePlugin = function (pluginOptionsA, index, pluginsOptions) {
const isNotRedundantCorePlugin = function (pluginOptionsA, _index, pluginsOptions) {
return (
pluginOptionsA.loadedFrom !== 'core' ||
pluginsOptions.every(
Expand Down
5 changes: 3 additions & 2 deletions packages/cache-utils/src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promises as fs } from 'fs'
import { promises as fs, Stats } from 'fs'
import { basename, dirname } from 'path'

import cpy from 'cpy'
Expand Down Expand Up @@ -78,10 +78,11 @@ const getSrcGlob = async function (src: string): Promise<GlobOptions> {
return baseOptions
}

const getStat = async (src: string) => {
const getStat = async (src: string): Promise<Stats | undefined> => {
try {
return await fs.stat(src)
} catch {
// continue regardless error
return undefined
}
}
4 changes: 2 additions & 2 deletions packages/config/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const getConfigPath = async function ({ configOpt, cwd, repositoryRoot, c
const configPath = await pLocate(
[
searchConfigOpt(cwd, configOpt),
searchBaseConfigFile(repositoryRoot, configBase),
searchBaseConfigFile(configBase),
searchConfigFile(repositoryRoot),
findUp(FILENAME, { cwd }),
],
Expand All @@ -36,7 +36,7 @@ const searchConfigOpt = function (cwd: string, configOpt?: string) {
/**
* Look for `repositoryRoot/{base}/netlify.*`
*/
const searchBaseConfigFile = function (repositoryRoot, configBase?: string) {
const searchBaseConfigFile = function (configBase?: string) {
if (configBase === undefined) {
return
}
Expand Down
8 changes: 4 additions & 4 deletions packages/framework-info/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ node_modules
yarn-error.log
.vscode
dist
build
src/generated
.DS_Store

# Local Netlify folder
.netlify

# These are mock node_modules folder for testing purposes
!test/fixtures/simple/node_modules
!test/fixtures/multiple/node_modules
!test/fixtures/monorepos/node_modules
!tests/fixtures/simple/node_modules
!tests/fixtures/multiple/node_modules
!tests/fixtures/monorepos/node_modules
23 changes: 10 additions & 13 deletions packages/framework-info/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@
"type": "module",
"main": "./dist/index.umd.cjs",
"exports": {
"node": "./src/main.js",
"node": "./lib/main.js",
"default": "./dist/index.umd.cjs"
},
"files": [
"build/*.js",
"src/**/*.js",
"lib",
"dist/index.umd.cjs"
],
"scripts": {
"prebuild": "node scripts/transform_json.js",
"build": "run-s build:*",
"build:ts": "tsc",
"build:json": "node scripts/transform_json.js",
"build:browser": "run-s build:browser:*",
"build:browser:core": "vite build",
"build:browser:site-root": "cpy index.html ./dist",
"test": "npm run test:dev",
"test:dev": "ava",
"test:ci": "c8 -r lcovonly -r text -r json ava"
"test": "vitest run",
"test:dev": "vitest",
"test:ci": "vitest run --reporter=default"
},
"keywords": [
"dependency-management",
Expand Down Expand Up @@ -58,10 +57,9 @@
"url": "https://github.com/netlify/build/issues"
},
"dependencies": {
"ajv": "^8.0.0",
"ajv": "^8.12.0",
"filter-obj": "^3.0.0",
"find-up": "^6.3.0",
"fs-extra": "^10.1.0",
"is-plain-obj": "^4.0.0",
"locate-path": "^7.0.0",
"p-filter": "^3.0.0",
Expand All @@ -72,19 +70,18 @@
"url": "^0.11.0"
},
"devDependencies": {
"ava": "^4.0.0",
"babel-loader": "^8.2.2",
"c8": "^7.11.0",
"cpy": "^9.0.0",
"cpy-cli": "^4.0.0",
"del": "^6.0.0",
"fast-glob": "^3.2.12",
"npm-run-all": "^4.1.5",
"path-browserify": "^1.0.1",
"rollup-plugin-node-polyfills": "^0.2.1",
"test-each": "^4.0.0",
"tmp-promise": "^3.0.2",
"typescript": "^4.9.4",
"vite": "^3.1.6"
"vite": "^4.0.4",
"vitest": "^0.27.1"
},
"engines": {
"node": "^14.14.0 || >=16.0.0"
Expand Down
20 changes: 11 additions & 9 deletions packages/framework-info/scripts/transform_json.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { promises as fs } from 'fs'
import process from 'process'
import { fileURLToPath } from 'url'

import { FRAMEWORK_NAMES } from '../src/frameworks/main.js'
import glob from 'fast-glob'

const FRAMEWORKS_DIR = new URL('../src/frameworks/', import.meta.url)
const BUILD_DIR = new URL('../build/', import.meta.url)
const FRAMEWORKS_BUILD = new URL('frameworks.js', BUILD_DIR)
const BUILD_DIR = new URL('../src/generated/', import.meta.url)
const FRAMEWORKS_BUILD = new URL('frameworks.ts', BUILD_DIR)

// We enforce frameworks to be written with JSON to ensure they remain logicless
// which is simpler for contributors and avoid adding unnecessary logic.
Expand All @@ -14,7 +15,8 @@ const FRAMEWORKS_BUILD = new URL('frameworks.js', BUILD_DIR)
// Therefore, we transform JSON to JavaScript files using at build time.
const transformFrameworks = async function () {
await fs.mkdir(BUILD_DIR, { recursive: true })
const frameworks = await Promise.all(FRAMEWORK_NAMES.map(transformFramework))
const frameworkFiles = await glob('*.json', { cwd: fileURLToPath(FRAMEWORKS_DIR), absolute: true })
const frameworks = await Promise.all(frameworkFiles.map(transformFramework))
const fileContents = `${FRAMEWORKS_HEADER}${JSON.stringify(frameworks, null, 2)}`
await fs.writeFile(FRAMEWORKS_BUILD, fileContents)
}
Expand All @@ -31,16 +33,16 @@ const updateLogoUrls = function (contents) {
return updatedContents
}

const transformFramework = async function (frameworkName) {
const frameworkUrl = new URL(`${frameworkName}.json`, FRAMEWORKS_DIR)
const jsonContents = await fs.readFile(frameworkUrl)
const transformFramework = async function (frameworkFile) {
const jsonContents = await fs.readFile(frameworkFile)
const contents = JSON.parse(jsonContents)

const updatedContents = updateLogoUrls(contents)
return updatedContents
}

const FRAMEWORKS_HEADER = `// This file is autogenerated at build time
export const FRAMEWORKS = `
const FRAMEWORKS_HEADER = `import type { FrameworkDefinition } from "../types.js"
// This file is autogenerated at build time
export const FRAMEWORKS: FrameworkDefinition[] = `

transformFrameworks()
34 changes: 0 additions & 34 deletions packages/framework-info/src/context.js

This file was deleted.

42 changes: 42 additions & 0 deletions packages/framework-info/src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { cwd, version as nodejsVersion } from 'process'

import { locatePath } from 'locate-path'
import { PackageJson, readPackageUp } from 'read-pkg-up'

interface PackageJsonInfo {
packageJson?: PackageJson
packageJsonPath?: string
}

export type PathExists = (path: string) => Promise<boolean>

export interface Context extends PackageJsonInfo {
pathExists: PathExists
nodeVersion: string
}

export const getPackageJson = async (projectDir: string): Promise<PackageJsonInfo> => {
try {
const result = await readPackageUp({ cwd: projectDir, normalize: false })
if (result === undefined) {
return {}
}

const { packageJson, path: packageJsonPath } = result

return { packageJson, packageJsonPath }
} catch {
return {}
}
}

export const getContext = async (projectDir: string = cwd(), nodeVersion: string = nodejsVersion): Promise<Context> => {
const { packageJson, packageJsonPath = projectDir } = await getPackageJson(projectDir)

return {
pathExists: async (path) => (await locatePath([path], { type: 'file', cwd: projectDir })) !== undefined,
packageJson,
packageJsonPath,
nodeVersion,
}
}

0 comments on commit 5acb25b

Please sign in to comment.