Last active
November 7, 2019 14:46
-
-
Save smeijer/d205b9a86537fbad6186781423165d5c to your computer and use it in GitHub Desktop.
Revisions
-
smeijer revised this gist
Nov 7, 2019 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,10 @@ const getModules = projectDir => return `${projectDir}/node_modules/${ns}/package.json`; }) .reduce( (acc, path) => (Array.isArray(path) ? [...acc, ...path] : [...acc, path]), [], ) .filter(path => fs.existsSync(path)) .map(path => ({ pkg: JSON.parse(fs.readFileSync(path, { encoding: 'utf8' })), -
smeijer created this gist
Nov 7, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,40 @@ const fs = require('fs'); const getModules = projectDir => fs .readdirSync(`${projectDir}/node_modules`) .map(ns => { if (ns[0] === '@') { return fs .readdirSync(`${projectDir}/node_modules/${ns}`) .map(name => `${projectDir}/node_modules/${ns}/${name}/package.json`); } return `${projectDir}/node_modules/${ns}/package.json`; }) .flat() .filter(path => fs.existsSync(path)) .map(path => ({ pkg: JSON.parse(fs.readFileSync(path, { encoding: 'utf8' })), path, })); const fixPackages = projectDir => { const modules = getModules(projectDir); let count = 0; modules.forEach(({ path, pkg }) => { if (pkg.module && pkg.main && !pkg.browser) { pkg.browser = pkg.main; fs.writeFileSync(path, JSON.stringify(pkg, '', ' '), { encoding: 'utf8', }); count++; } }); console.log(`modified ${count} packages to prefer 'main' over 'module'`); }; // the directory that holds your 'package.json' fixPackages('./');