Skip to content

Commit

Permalink
✨ Babel: Add ajv npm dep fix
Browse files Browse the repository at this point in the history
  • Loading branch information
GuiDevloper committed Jun 8, 2023
1 parent 2fdb4cc commit 641b1cf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions nullstack-adapt-babel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const path = require('path')
const { readdirSync } = require('fs')
const getOptions = require('./utils/getOptions')
const fixDeps = require('./utils/fix-deps')

function runtime(options) {
return {
Expand Down Expand Up @@ -233,6 +234,8 @@ function newConfig(options) {
]
}

fixDeps()

/**
*
* @param {Array<(...[]) => {module: {rules: Array<{}>}}>} configs
Expand Down
2 changes: 1 addition & 1 deletion nullstack-adapt-babel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nullstack-adapt-babel",
"version": "0.0.5",
"version": "0.0.6",
"description": "Adapt Nullstack to Babel",
"author": "GuiDevloper",
"license": "MIT",
Expand Down
30 changes: 30 additions & 0 deletions nullstack-adapt-babel/utils/fix-deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fs = require('fs')
const path = require('path')
const atCWD = fullPath => path.join(process.cwd(), fullPath)

/**
* Try to fix dependencies issues at virtual NPM environments (e.g: ajv)
* @see https://github.com/ajv-validator/ajv-keywords/issues/385
**/
function fixDeps() {
try {
const isVirtualNPM =
process.env.PKG_MANAGER === 'npm' && !process.env.npm_config_registry
if (isVirtualNPM && !process.env.DISABLE_AJV_FIX) {
const ajvJSON = require(atCWD('node_modules/ajv/package.json'))
if (ajvJSON.version !== '7.2.4') {
console.log('Fixing npm deps...')
const cprocess = require('child_process')
const npmCLI = process.platform === 'win32' ? 'npm.cmd' : 'npm'
cprocess.spawnSync(npmCLI, 'install [email protected]'.split(' '))
const pkgJSON = fs.readFileSync(atCWD('package.json'), 'utf8')
fs.writeFileSync(
atCWD('package.json'),
pkgJSON.replace(/[ ]+"ajv": "(\^|)7.2.4"(,|)\n/, '')
)
}
}
} catch {}
}

module.exports = fixDeps

0 comments on commit 641b1cf

Please sign in to comment.