Skip to content

Commit

Permalink
chore: update nonbreaking types, minimize lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
hoobdeebla committed Feb 9, 2023
1 parent 1ea2ab4 commit 35a9a12
Show file tree
Hide file tree
Showing 154 changed files with 1,157 additions and 1,270 deletions.
10 changes: 9 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ module.exports = {
`eslint:recommended`,
`plugin:flowtype/recommended`,
`plugin:react/recommended`,
`plugin:you-dont-need-lodash-underscore/compatible`,
`prettier`,
],
plugins: [`flowtype`, `prettier`, `react`, `filenames`, `@babel`],
plugins: [
`flowtype`,
`prettier`,
`react`,
`filenames`,
`@babel`,
`you-dont-need-lodash-underscore`,
],
parserOptions: {
ecmaVersion: 2016,
sourceType: `module`,
Expand Down
23 changes: 11 additions & 12 deletions markdown.config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
const fs = require(`fs-extra`)
const path = require(`path`)
const _ = require(`lodash`)
const { readdirSync, statSync, readFileSync } = require(`fs-extra`)
const { join, basename, dirname } = require(`node:path`)
const makeTemplate = require(`lodash/template`)

module.exports = {
transforms: {
LIST_STARTERS() {
const base = path.join(process.cwd(), `starters`)
const starters = fs
.readdirSync(base)
.filter(dir => fs.statSync(path.join(base, dir)).isDirectory())
const base = join(process.cwd(), `starters`)
const starters = readdirSync(base)
.filter(dir => statSync(join(base, dir)).isDirectory())
// theme starters have their own README so skip those
.filter(dir => !dir.includes(`theme`))
.reduce((merged, dir) => {
merged[dir] = JSON.parse(
fs.readFileSync(path.join(base, dir, `package.json`), `utf8`)
readFileSync(join(base, dir, `package.json`), `utf8`)
)
return merged
}, {})
Expand All @@ -32,12 +31,12 @@ module.exports = {
`.replace(/^[^|]+/gm, ``)
},
STARTER(content, options, { originalPath }) {
const starter = path.basename(path.dirname(originalPath))
const template = fs.readFileSync(
path.join(process.cwd(), `starters`, `README-template.md`),
const starter = basename(dirname(originalPath))
const template = readFileSync(
join(process.cwd(), `starters`, `README-template.md`),
`utf8`
)
return _.template(template)({
return makeTemplate(template)({
name: starter,
})
},
Expand Down
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@
"@babel/node": "^7.20.7",
"@babel/plugin-transform-typescript": "^7.20.13",
"@babel/runtime": "^7.20.13",
"@types/bluebird": "^3.5.38",
"@types/jaeger-client": "^3.18.4",
"@types/jest": "^29.4.0",
"@types/joi": "^14.3.4",
"@types/lodash": "^4.14.191",
"@types/node": "^12.20.55",
"@types/react": "^18.0.27",
"@types/stack-trace": "^0.0.30",
"@types/node": "^18.13.0",
"@typescript-eslint/eslint-plugin": "^5.51.0",
"@typescript-eslint/parser": "^5.51.0",
"babel-jest": "^29.4.2",
Expand All @@ -31,6 +25,7 @@
"eslint-plugin-jsx-a11y": "^6.7.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-you-dont-need-lodash-underscore": "^6.12.0",
"glob": "^8.1.0",
"husky": "^8.0.3",
"ignore": "^5.2.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/create-gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
"@ascorbic/worker-threads-shim": "^1.0.0",
"@lukeed/uuid": "^2.0.0",
"@types/common-tags": "^1.8.1",
"@types/configstore": "^4.0.0",
"@types/configstore": "^5.0.1",
"@types/fs-extra": "^11.0.1",
"@types/node": "^14.18.36",
"ansi-wordwrap": "^1.0.2",
"common-tags": "^1.8.2",
"configstore": "^5.0.1",
"enquirer": "^2.3.6",
"execa": "^5.1.1",
"fs-extra": "^11.1.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@
"@types/common-tags": "^1.8.1",
"@types/fs-extra": "^11.0.1",
"@types/hosted-git-info": "^3.0.2",
"@types/lodash": "^4.14.191",
"@types/node-fetch": "^2.6.2",
"@types/semver": "^7.3.13",
"@types/signal-exit": "^3.0.1",
"@types/stack-trace": "^0.0.30",
"@types/yargs": "^15.0.15",
"babel-plugin-lodash": "^3.3.4",
"babel-preset-gatsby-package": "^3.7.0-next.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/gatsby-cli/src/handlers/plugin-add-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from "fs-extra"
import { writeFile } from "fs-extra"
import execa from "execa"
import _ from "lodash"
import { groupBy, debounce } from "lodash"
import {
readConfigFile,
lock,
Expand Down Expand Up @@ -83,7 +83,7 @@ export const GatsbyPluginCreate = async ({
options,
key,
})
await fs.writeFile(getConfigPath(root), code)
await writeFile(getConfigPath(root), code)
release()
}

Expand Down Expand Up @@ -131,7 +131,7 @@ let installs: Array<{
}> = []
const executeInstalls = async (root: string): Promise<void> => {
// @ts-ignore - fix me
const types = _.groupBy(installs, c => c.resource.dependencyType)
const types = groupBy(installs, c => c.resource.dependencyType)

// Grab the key of the first install & delete off installs these packages
// then run intall
Expand Down Expand Up @@ -184,7 +184,7 @@ const executeInstalls = async (root: string): Promise<void> => {
return undefined
}

const debouncedExecute = _.debounce(executeInstalls, 25)
const debouncedExecute = debounce(executeInstalls, 25)

interface IPackageCreateInput {
root: string
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby-cli/src/reporter/errors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import PrettyError from "pretty-error"
import stackTrace from "stack-trace"
import { StackFrame } from "stack-trace"
import { prepareStackTrace, ErrorWithCodeFrame } from "./prepare-stack-trace"
import { isNodeInternalModulePath } from "gatsby-core-utils"
import { IStructuredStackFrame } from "../structured-errors/types"
import { readFileSync } from "fs-extra"
import { codeFrameColumns } from "@babel/code-frame"

const packagesToSkip = [`core-js`, `bluebird`, `regenerator-runtime`, `graphql`]
const packagesToSkip = [`core-js`, `regenerator-runtime`, `graphql`]

const packagesToSkipTest = new RegExp(
`node_modules[\\/](${packagesToSkip.join(`|`)})`
Expand All @@ -15,7 +15,7 @@ const packagesToSkipTest = new RegExp(
// TO-DO: move this this out of this file (and probably delete this file completely)
// it's here because it re-implements similar thing as `pretty-error` already does
export const sanitizeStructuredStackTrace = (
stack: Array<stackTrace.StackFrame>
stack: Array<StackFrame>
): Array<IStructuredStackFrame> => {
// first filter out not useful call sites
stack = stack.filter(callSite => {
Expand Down
10 changes: 4 additions & 6 deletions packages/gatsby-core-utils/src/create-require-from-path.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Module from "module"
import path from "path"
import Module from "node:module"
import { dirname } from "node:path"

/**
* We need to use private Module methods in this polyfill
Expand All @@ -15,12 +15,10 @@ const fallback = (filename: string): NodeRequire => {
mod.filename = filename
mod.paths = (
Module as typeof Module & IModulePrivateMethods
)._nodeModulePaths(path.dirname(filename))
)._nodeModulePaths(dirname(filename))
mod._compile(`module.exports = require;`, filename)

return mod.exports
}

// Polyfill Node's `Module.createRequireFromPath` if not present (added in Node v10.12.0)
export const createRequireFromPath =
Module.createRequire || Module.createRequireFromPath || fallback
export const createRequireFromPath = Module.createRequire || fallback
44 changes: 19 additions & 25 deletions packages/gatsby-dev-cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

const Configstore = require(`configstore`)
const pkg = require(`../package.json`)
const _ = require(`lodash`)
const path = require(`path`)
const os = require(`os`)
const { merge, isEmpty } = require(`lodash`)
const { join, resolve } = require(`node:path`)
const { homedir } = require(`node:os`)
const { existsSync, readdirSync, readFileSync } = require(`fs-extra`)
const watch = require(`./watch`)
const { getVersionInfo } = require(`./utils/version`)
const argv = require(`yargs`)
Expand Down Expand Up @@ -53,19 +54,17 @@ if (argv.version) {

const conf = new Configstore(pkg.name)

const fs = require(`fs-extra`)

let pathToRepo = argv.setPathToRepo

if (pathToRepo) {
if (pathToRepo.includes(`~`)) {
pathToRepo = path.join(os.homedir(), pathToRepo.split(`~`).pop())
pathToRepo = join(homedir(), pathToRepo.split(`~`).pop())
}
conf.set(`gatsby-location`, path.resolve(pathToRepo))
conf.set(`gatsby-location`, resolve(pathToRepo))
process.exit()
}

const havePackageJsonFile = fs.existsSync(`package.json`)
const havePackageJsonFile = existsSync(`package.json`)

if (!havePackageJsonFile) {
console.error(`Current folder must have a package.json file!`)
Expand All @@ -88,42 +87,37 @@ gatsby-dev --set-path-to-repo /path/to/my/cloned/version/gatsby

// get list of packages from monorepo
const packageNameToPath = new Map()
const monoRepoPackages = fs
.readdirSync(path.join(gatsbyLocation, `packages`))
.map(dirName => {
const monoRepoPackages = readdirSync(join(gatsbyLocation, `packages`)).map(
dirName => {
try {
const localPkg = JSON.parse(
fs.readFileSync(
path.join(gatsbyLocation, `packages`, dirName, `package.json`)
)
readFileSync(join(gatsbyLocation, `packages`, dirName, `package.json`))
)

if (localPkg?.name) {
packageNameToPath.set(
localPkg.name,
path.join(gatsbyLocation, `packages`, dirName)
join(gatsbyLocation, `packages`, dirName)
)
return localPkg.name
}
} catch (error) {
// fallback to generic one
}

packageNameToPath.set(
dirName,
path.join(gatsbyLocation, `packages`, dirName)
)
packageNameToPath.set(dirName, join(gatsbyLocation, `packages`, dirName))
return dirName
})
}
)

const localPkg = JSON.parse(fs.readFileSync(`package.json`))
const localPkg = JSON.parse(readFileSync(`package.json`))
// intersect dependencies with monoRepoPackages to get list of packages that are used
const localPackages = _.intersection(
const localPackages = [
monoRepoPackages,
Object.keys(_.merge({}, localPkg.dependencies, localPkg.devDependencies))
)
Object.keys(merge({}, localPkg.dependencies, localPkg.devDependencies)),
].reduce((a, b) => a.filter(c => b.includes(c)))

if (!argv.packages && _.isEmpty(localPackages)) {
if (!argv.packages && isEmpty(localPackages)) {
console.error(
`
You haven't got any gatsby dependencies into your current package.json
Expand Down
10 changes: 5 additions & 5 deletions packages/gatsby-dev-cli/src/local-npm-registry/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const startVerdaccio = require(`verdaccio`).default

const fs = require(`fs-extra`)
const _ = require(`lodash`)
const { removeSync } = require(`fs-extra`)

let VerdaccioInitPromise = null

Expand All @@ -17,7 +15,7 @@ const startServer = () => {
console.log(`Starting local verdaccio server`)

// clear storage
fs.removeSync(verdaccioConfig.storage)
removeSync(verdaccioConfig.storage)

VerdaccioInitPromise = new Promise(resolve => {
startVerdaccio(
Expand Down Expand Up @@ -68,7 +66,9 @@ exports.publishPackagesLocallyAndInstall = async ({
})
}

const packagesToInstall = _.intersection(packagesToPublish, localPackages)
const packagesToInstall = [packagesToPublish, localPackages].reduce((a, b) =>
a.filter(c => b.includes(c))
)

await installPackages({
packagesToInstall,
Expand Down
29 changes: 11 additions & 18 deletions packages/gatsby-dev-cli/src/utils/check-deps-changes.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const fs = require(`fs-extra`)
const _ = require(`lodash`)
const { readFileSync } = require(`fs-extra`)
const transform = require(`lodash/transform`)
const {
getMonorepoPackageJsonPath,
} = require(`./get-monorepo-package-json-path`)
const got = require(`got`)

function difference(object, base) {
function changes(object, base) {
return _.transform(object, function (result, value, key) {
if (!_.isEqual(value, base[key])) {
return transform(object, function (result, value, key) {
if (!value === base[key]) {
result[key] =
_.isObject(value) && _.isObject(base[key])
value instanceof Object && base?.[key] instanceof Object
? changes(value, base[key])
: value
}
Expand Down Expand Up @@ -38,7 +38,7 @@ exports.checkDepsChanges = async ({
let localPKGjson
let packageNotInstalled = false
try {
localPKGjson = JSON.parse(fs.readFileSync(newPath, `utf-8`))
localPKGjson = JSON.parse(readFileSync(newPath, `utf-8`))
} catch {
packageNotInstalled = true
// there is no local package - so we still need to install deps
Expand Down Expand Up @@ -82,10 +82,7 @@ exports.checkDepsChanges = async ({
packageName,
packageNameToPath,
})
const monorepoPKGjsonString = fs.readFileSync(
monoRepoPackageJsonPath,
`utf-8`
)
const monorepoPKGjsonString = readFileSync(monoRepoPackageJsonPath, `utf-8`)
const monorepoPKGjson = JSON.parse(monorepoPKGjsonString)
if (ignoredPackageJSON.has(packageName)) {
if (ignoredPackageJSON.get(packageName).includes(monorepoPKGjsonString)) {
Expand All @@ -101,10 +98,8 @@ exports.checkDepsChanges = async ({
if (!monorepoPKGjson.dependencies) monorepoPKGjson.dependencies = {}
if (!localPKGjson.dependencies) localPKGjson.dependencies = {}

const areDepsEqual = _.isEqual(
monorepoPKGjson.dependencies,
localPKGjson.dependencies
)
const areDepsEqual =
monorepoPKGjson.dependencies === localPKGjson.dependencies

if (!areDepsEqual) {
const diff = difference(
Expand All @@ -119,7 +114,7 @@ exports.checkDepsChanges = async ({

let needPublishing = false
let isPublishing = false
const depChangeLog = _.uniq(Object.keys({ ...diff, ...diff2 }))
const depChangeLog = Array.from(new Set(Object.keys({ ...diff, ...diff2 })))
.reduce((acc, key) => {
if (monorepoPKGjson.dependencies[key] === `gatsby-dev`) {
// if we are in middle of publishing to local repository - ignore
Expand Down Expand Up @@ -184,9 +179,7 @@ exports.checkDepsChanges = async ({
}

function getPackageVersion(packageName) {
const projectPackageJson = JSON.parse(
fs.readFileSync(`./package.json`, `utf-8`)
)
const projectPackageJson = JSON.parse(readFileSync(`./package.json`, `utf-8`))
const { dependencies = {}, devDependencies = {} } = projectPackageJson
const version = dependencies[packageName] || devDependencies[packageName]
return version || `latest`
Expand Down
Loading

0 comments on commit 35a9a12

Please sign in to comment.