|
|
@@ -0,0 +1,36 @@ |
|
|
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'); |
|
|
}); |