mirror of
https://github.com/vale981/Vulcan
synced 2025-03-05 09:31:43 -05:00
add script to update package.json
This commit is contained in:
parent
553a0627d4
commit
9cb5a6c1d4
3 changed files with 93 additions and 0 deletions
2
.vulcan/.gitignore
vendored
Normal file
2
.vulcan/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
bkp
|
||||
package.json
|
88
.vulcan/update_package.js
Normal file
88
.vulcan/update_package.js
Normal file
|
@ -0,0 +1,88 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
|
||||
var fs = require('fs');
|
||||
var mergePackages = require('@userfrosting/merge-package-dependencies');
|
||||
var jsdiff = require('diff');
|
||||
require('colors');
|
||||
|
||||
function diffPartReducer(accumulator, part) {
|
||||
// green for additions, red for deletions
|
||||
// grey for common parts
|
||||
var color = part.added ? 'green' : (part.removed ? 'red' : 'grey');
|
||||
|
||||
return {
|
||||
text: (accumulator.text || '') + part.value[color],
|
||||
count: (accumulator.count || 0) + (part.added || part.removed ? 1 : 0),
|
||||
};
|
||||
}
|
||||
|
||||
// copied from sort-object-keys package
|
||||
function sortObjectByKeyNameList(object, sortWith) {
|
||||
var keys;
|
||||
var sortFn;
|
||||
|
||||
if (typeof sortWith === 'function') {
|
||||
sortFn = sortWith;
|
||||
} else {
|
||||
keys = sortWith;
|
||||
}
|
||||
return (keys || []).concat(Object.keys(object).sort(sortFn)).reduce(function(total, key) {
|
||||
total[key] = object[key];
|
||||
return total;
|
||||
}, Object.create({}));
|
||||
}
|
||||
|
||||
|
||||
var appDirPath = './';
|
||||
var vulcanDirPath = './.vulcan/';
|
||||
|
||||
if (!fs.existsSync(vulcanDirPath + 'package.json')) {
|
||||
console.log('Could not find \'' + vulcanDirPath + 'package.json\'');
|
||||
} else if (!fs.existsSync(appDirPath + 'package.json')) {
|
||||
console.log('Could not find \'' + appDirPath + 'package.json\'');
|
||||
} else {
|
||||
var appPackageFile = fs.readFileSync(appDirPath + '/package.json');
|
||||
var appPackageJson = JSON.parse(appPackageFile);
|
||||
var vulcanPackageFile = fs.readFileSync(vulcanDirPath + 'package.json');
|
||||
var vulcanPackageJson = JSON.parse(vulcanPackageFile);
|
||||
|
||||
console.log(appPackageJson.name + '@' + appPackageJson.version + ' \'package.json\' will be updated with Vulcan@' + vulcanPackageJson.version + ' dependencies.');
|
||||
|
||||
var backupDirPath = vulcanDirPath + 'bkp/';
|
||||
if (!fs.existsSync(backupDirPath)) {
|
||||
fs.mkdirSync(backupDirPath);
|
||||
}
|
||||
var backupFilePath = backupDirPath + 'package-' + Date.now() + '.json';
|
||||
console.log('Saving a backup of \'' + appDirPath + 'package.json\' in \'' + backupFilePath + '\'');
|
||||
fs.writeFileSync(backupFilePath, appPackageFile);
|
||||
|
||||
var updatedAppPackageJson = mergePackages.npm(
|
||||
// IMPORTANT: parse again because mergePackages.npm mutates json
|
||||
JSON.parse(appPackageFile),
|
||||
[vulcanDirPath]
|
||||
);
|
||||
|
||||
[
|
||||
'dependencies',
|
||||
'devDependencies',
|
||||
'peerDependencies'
|
||||
].forEach(function(key) {
|
||||
if (updatedAppPackageJson[key]) {
|
||||
updatedAppPackageJson[key] = sortObjectByKeyNameList(updatedAppPackageJson[key]);
|
||||
}
|
||||
|
||||
const diff = jsdiff.diffJson(
|
||||
sortObjectByKeyNameList(appPackageJson[key] || {}),
|
||||
updatedAppPackageJson[key] || {}
|
||||
).reduce(diffPartReducer, {});
|
||||
if (diff.count) {
|
||||
console.log('Changes in "' + key + '":');
|
||||
console.log(diff.text);
|
||||
} else {
|
||||
console.log('No changes in "' + key + '".');
|
||||
}
|
||||
});
|
||||
|
||||
fs.writeFileSync(appDirPath + 'package.json', JSON.stringify(updatedAppPackageJson, null, ' '));
|
||||
}
|
|
@ -96,10 +96,13 @@
|
|||
},
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"@userfrosting/merge-package-dependencies": "^1.2.0",
|
||||
"autoprefixer": "^6.3.6",
|
||||
"babel-eslint": "^7.0.0",
|
||||
"babylon": "^6.18.0",
|
||||
"colors": "^1.3.2",
|
||||
"chromedriver": "^2.40.0",
|
||||
"diff": "^3.5.0",
|
||||
"enzyme": "^3.3.0",
|
||||
"enzyme-adapter-react-16.3": "^1.4.0",
|
||||
"eslint": "^3.19.0",
|
||||
|
|
Loading…
Add table
Reference in a new issue