Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postupgrade: Fix script for submodules, merge correctly, keep .git #441

Merged
merged 2 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions postupgrade/PostUpgradeHandler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs');
const fsExtra = require('fs-extra');
const path = require('path');
const { mergeJson } = require('./utils');
const { mergeJson, isGitSubmodule } = require('./utils');
const simpleGit = require('simple-git/promise')();

/**
Expand All @@ -15,13 +15,19 @@ class PostUpgradeHandler {
}

async handlePostUpgrade() {
this.removeFromTheme('.git', '.gitignore', 'tests');
if (!isGitSubmodule(this.themeDir)) {
this.removeFromTheme('.git', '.gitignore', 'tests');
}

const userGlobalConfigPath =
path.relative(process.cwd(), path.join(this.configDir, this.globalConfigFile));
const themeGlobalConfigPath =
path.relative(process.cwd(), path.join(this.themeDir, this.globalConfigFile));
if (fsExtra.pathExistsSync(themeGlobalConfigPath)) {
const mergedGlobalConfig = await this.mergeThemeGlobalConfig(themeGlobalConfigPath);
fs.writeFileSync(themeGlobalConfigPath, mergedGlobalConfig);
const mergedGlobalConfig = await this.mergeThemeGlobalConfig(userGlobalConfigPath, themeGlobalConfigPath);
fs.writeFileSync(userGlobalConfigPath, mergedGlobalConfig);
}

this.copyStaticFilesToTopLevel(
'package.json', 'Gruntfile.js', 'webpack-config.js', 'package-lock.json');
}
Expand All @@ -37,10 +43,13 @@ class PostUpgradeHandler {

/**
* Merges the theme's global_config with the user's current global_config.
* @param {string} userGlobalConfigPath The path to the user's current global config
* @param {string} themeGlobalConfigPath The path to the global config in the theme
oshi97 marked this conversation as resolved.
Show resolved Hide resolved
* @return {string} The comment-json merged global config
*/
async mergeThemeGlobalConfig(themeGlobalConfigPath) {
async mergeThemeGlobalConfig(userGlobalConfigPath, themeGlobalConfigPath) {
const updatedCommentJson = fs.readFileSync(themeGlobalConfigPath, 'utf-8');
const originalCommentJson = await simpleGit.show([`HEAD:${themeGlobalConfigPath}`]);
const originalCommentJson = fs.readFileSync(userGlobalConfigPath, 'utf-8');
return mergeJson(updatedCommentJson, originalCommentJson);
}

Expand Down
14 changes: 14 additions & 0 deletions postupgrade/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const { parse, assign, stringify } = require('comment-json');
const path = require('path');
const simpleGit = require('simple-git/promise')();

/**
* Merges two comment-json files, with the original having higher priority.
Expand Down Expand Up @@ -52,3 +53,16 @@ function removeEmptyDirectoriesRecursively(dirpath) {
}
}
exports.removeEmptyDirectoriesRecursively = removeEmptyDirectoriesRecursively;

/**
* Returns whether the given file path is registered as a git submodule.
* @param {string} submodulePath
* @returns {boolean}
*/
async function isGitSubmodule(submodulePath) {
const submodulePaths = await simpleGit.subModule(['foreach', '--quiet', 'echo $sm_path']);
return !!submodulePaths
.split('\n')
.find(p => p === submodulePath)
}
exports.isGitSubmodule = isGitSubmodule;
5 changes: 5 additions & 0 deletions tests/postupgrade/config1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
// Comment1
"one": "hi", // Inline comment
"onetwo": "hello"
}
5 changes: 5 additions & 0 deletions tests/postupgrade/config2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"two": "bye",
"onetwo": "goodbye"
// Comment2
}
8 changes: 8 additions & 0 deletions tests/postupgrade/config3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
// Comment1
"one": "hi", // Inline comment
"onetwo": "goodbye"
// Comment2
,
"two": "bye"
}
17 changes: 17 additions & 0 deletions tests/postupgrade/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { mergeJson } = require('postupgrade/utils.js');
const fs = require('fs');

describe('postupgrade utils', () => {
describe('mergeJson', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love the test! does mergeJson need it's own describe block right now? We can always give it it's own later if need be

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I was copying the format of the current formatters.js tests. I do kind of like keeping it in its own describe block because it feels easier to remember to do now than later

Are there any negatives I'm missing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just don't think it's needed right now, and don't really foresee adding additional tests for mergeJson (it's only 2 lines long). totally up to you!

it('puts together two comment JSON strings', () => {
const config1 = fs.readFileSync('./tests/postupgrade/config1.json', 'utf-8');
const config2 = fs.readFileSync('./tests/postupgrade/config2.json', 'utf-8');
const expected = fs.readFileSync('./tests/postupgrade/config3.json', 'utf-8');

// if this passes, then it is correctly not deleting any fields or comments
// and it is giving precedence to the second parameter config
const mergedConfig = mergeJson(config1, config2);
expect(mergedConfig).toEqual(expected);
});
});
});