- 
      
- 
        Save mliq/21a309e6566dc6304e14b78a30ed1e68 to your computer and use it in GitHub Desktop. 
    Replace semver ranges with exact version numbers in package.json files
  
        
  
    
      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 characters
    
  
  
    
  | const childProcess = require('child_process'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const glob = require('glob'); | |
| const filePaths = glob | |
| .sync('package.json') | |
| .concat(glob.sync('packages/*/package.json')) | |
| .map(filePath => path.resolve(process.cwd(), filePath)); | |
| const reinstall = (filePath, data, key) => { | |
| const dependencyVersions = Object.entries(data[key]).map( | |
| ([name, version]) => `${name}@${version}` | |
| ); | |
| const options = key === 'dependencies' ? ['--save'] : ['--save', '--save-dev']; | |
| childProcess.spawnSync('npm', ['install'].concat(options).concat(dependencyVersions), { | |
| cwd: filePath.replace('/package.json', ''), | |
| encoding: 'utf-8', | |
| env: process.env, | |
| stdio: 'pipe' | |
| }); | |
| }; | |
| filePaths.forEach(filePath => { | |
| console.log(filePath); | |
| const raw = fs.readFileSync(filePath, { encoding: 'utf8' }); | |
| const clone = JSON.parse(raw); | |
| clone.dependencies && (clone.dependencies = {}); | |
| clone.devDependencies && (clone.devDependencies = {}); | |
| fs.writeFileSync(filePath, JSON.stringify(clone, null, 2), { encoding: 'utf8' }); | |
| const data = JSON.parse(raw); | |
| data.dependencies && reinstall(filePath, data, 'dependencies'); | |
| data.devDependencies && reinstall(filePath, data, 'devDependencies'); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment