Skip to content

Commit

Permalink
Remove nodegit dependency (#6471)
Browse files Browse the repository at this point in the history
* Replace `nodegit` usage in `release.js` with CLI exec

* Auto prettier linting

* Refactor i18n token changelog script to not use nodegit

- instead use execSync and git CLI to accomplish the same outcomes

* Uninstall nodegit

+ remove references to nodegit

* Remove `postinstall` script entirely
- it's no longer of use with `nodegit` removed

* Move git tag fetch command before token script

- token script now relies on tags being up to date/correct, so we should fetch/prune them first
  • Loading branch information
Cee authored Dec 8, 2022
1 parent c4aba31 commit 71e3bfb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 382 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
"start-test-server-and-a11y-test": "cross-env WAIT_ON_TIMEOUT=600000 start-server-and-test start-test-server http-get://localhost:9999 test-a11y",
"yo-doc": "yo ./generator-eui/app/documentation.js",
"yo-changelog": "yo ./generator-eui/changelog/index.js",
"release": "node ./scripts/release.js",
"postinstall": "node ./scripts/postinstall.js"
"release": "node ./scripts/release.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -207,7 +206,6 @@
"moment": "^2.27.0",
"moment-timezone": "^0.5.31",
"node-sass": "^6.0.1",
"nodegit": "^0.28.0-alpha.10",
"path": "^0.12.7",
"pegjs": "^0.10.0",
"postcss-cli": "^7.1.2",
Expand Down Expand Up @@ -270,7 +268,6 @@
"lib",
"licenses",
"optimize",
"scripts/postinstall.js",
"src/**/*.scss",
"test-env",
"eui.d.ts",
Expand Down
9 changes: 0 additions & 9 deletions scripts/postinstall.js

This file was deleted.

17 changes: 7 additions & 10 deletions scripts/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const chalk = require('chalk');
const path = require('path');
const prompt = require('prompt');
let { execSync } = require('child_process');
const git = require('nodegit');

const cwd = path.resolve(__dirname, '..');
const stdio = 'inherit';
Expand Down Expand Up @@ -54,6 +53,9 @@ if (args.dry_run) {
let versionTarget;

if (args.steps.indexOf('version') > -1) {
// Fetch latest tags and clear any local ones
execSync('git fetch upstream --tags --prune --prune-tags --force');

const { changelogMap, changelog } = collateChangelogFiles();

// prompt user for what type of version bump to make (major|minor|patch)
Expand All @@ -67,9 +69,6 @@ if (args.dry_run) {
// Update CHANGELOG.md
updateChangelog(changelog, versionTarget);

// Clear any local tags
execSync('git fetch upstream --tags --prune --prune-tags --force');

// update package.json & package-lock.json version, git commit, git tag
execSync(`npm version ${versionTarget}`, execOptions);
}
Expand Down Expand Up @@ -138,12 +137,10 @@ async function ensureMainBranch() {
return;
}

// delay importing nodegit because it can introduce environmental pains in a CI environment
const git = require('nodegit');
const repo = await git.Repository.open(cwd);
const currentBranch = await repo.getCurrentBranch();
const currentBranchName = currentBranch.shorthand();

// Obtain current branch name from git
const currentBranchName = execSync('git rev-parse --abbrev-ref HEAD')
.toString()
.trim();
if (currentBranchName !== 'main') {
console.error(`Unable to release: currently on branch "${currentBranchName}", expected "main"`);
process.exit(1);
Expand Down
96 changes: 28 additions & 68 deletions scripts/update-token-changelog.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs');
const path = require('path');
const git = require('nodegit');
const semver = require('semver');
const { execSync } = require('child_process');

const repoDir = path.resolve(__dirname, '..');
const packagePath = path.resolve(repoDir, 'package.json');
Expand All @@ -19,13 +19,6 @@ if (validVersionTypes.has(versionIncrementType) === false) {
const { version: oldPackageVersion } = require(packagePath);
const newPackageVersion = semver.inc(oldPackageVersion, versionIncrementType);

async function getFileContentsFromCommit(commit, filePath) {
// https://github.com/nodegit/nodegit/blob/master/examples/read-file.js
const entry = await commit.getEntry(filePath);
const entryBlob = await entry.getBlob();
return entryBlob.content().toString();
}

function getTokenMap(tokenInstances) {
// tokenInstances is the total set of tokens across all files
// reduce those down to a mapping of token -> defString
Expand Down Expand Up @@ -66,7 +59,7 @@ function getTokenChanges(oldTokenInstances, newTokenInstances) {
// token has been removed
changes.push({
token: key,
changeType: 'deleted'
changeType: 'deleted',
});
}
});
Expand All @@ -86,70 +79,37 @@ function getTokenChanges(oldTokenInstances, newTokenInstances) {
return changes;
}

async function getDirtyFiles(repo) {
const diff = await git.Diff.indexToWorkdir(repo, null, { FLAGS: git.Diff.OPTION.INCLUDE_UNTRACKED });
const patches = await diff.patches();

return new Set(patches.map(patch => patch.oldFile().path()));
}

async function commitTokenChanges(repo) {
// add i18ntokens.json and i18ntokens_changelog.json if modified, and commit
// https://github.com/nodegit/nodegit/blob/master/examples/add-and-commit.js
const dirtyFiles = await getDirtyFiles(repo);
let createCommit = false;

const index = await repo.refreshIndex();

if (dirtyFiles.has('i18ntokens.json')) {
createCommit = true;
await index.addByPath('i18ntokens.json');
}

if (dirtyFiles.has('i18ntokens_changelog.json')) {
createCommit = true;
await index.addByPath('i18ntokens_changelog.json');
}

if (createCommit) {
await index.write();
const oid = await index.writeTree();
const head = await git.Reference.nameToId(repo, 'HEAD');
const parent = await repo.getCommit(head);

const userSignature = await repo.defaultSignature();

return repo.createCommit('HEAD', userSignature, userSignature, 'update i18ntokens', oid, [parent]);
async function commitTokenChanges() {
try {
execSync('git add ./i18ntokens.json ./i18ntokens_changelog.json');
execSync('git commit -m "update i18ntokens" -n');
console.log('i18n token changes committed');
} catch {
const staging = execSync('git status').toString().trim();
if (staging.includes('i18ntokens')) {
console.error(
'i18n tokens updates changed, but nothing was committed. Staging is still dirty'
);
process.exit(1);
} else {
console.log('No i18n token changes found to commit');
}
}
}

async function getCommitForTagName(repo, tagname) {
const tag = await repo.getTagByName(tagname);
return git.Commit.lookup(repo, tag.targetId());
}

async function getPreviousI18nTokens(previousVersionCommit) {
try {
return JSON.parse(
await getFileContentsFromCommit(previousVersionCommit, 'i18ntokens.json')
);
} catch (err) {
// If failed, try with the previous path where it used to exist
return JSON.parse(
await getFileContentsFromCommit(
previousVersionCommit,
'src-docs/src/i18ntokens.json'
)
);
}
async function getPreviousI18nTokens() {
const commitID = execSync(`git rev-parse v${oldPackageVersion}`)
.toString()
.trim();
const fileContents = execSync(`git cat-file blob ${commitID}:i18ntokens.json`)
.toString()
.trim();
return JSON.parse(fileContents);
}

async function main() {
const repo = await git.Repository.open(repoDir);
const previousVersionCommit = await getCommitForTagName(repo, `v${oldPackageVersion}`);

// check for i18n token differences between the current file & the most recent EUI version
const originalTokens = await getPreviousI18nTokens(previousVersionCommit);
const originalTokens = await getPreviousI18nTokens();
const newTokens = require(tokensPath);

const changes = getTokenChanges(originalTokens, newTokens);
Expand All @@ -165,10 +125,10 @@ async function main() {
}

// commit pending changes to i18ntokens.json or i18ntokens_changelog.json
await commitTokenChanges(repo);
await commitTokenChanges();
}

main().catch(e => {
main().catch((e) => {
console.error(e);
process.exit(1);
});
1 change: 0 additions & 1 deletion wiki/releasing-versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ This provides a walkthrough of the patching & backport release process; examples
* in the EUI git repo, checkout the release tag the patch is intended for - `git checkout v22.3.0`
* create a new branch from the versioned tag, the name is unimportant but I use the target version without a leading `v` - `git checkout -b 22.3.1`
* Run `yarn` to ensure you have the correct dependencies for that point in time installed
* If you run into an error about `nodegit.node` being compiled against a different version of Node.js, try running `rm -rf node_modules && yarn`
* Apply the commit(s) with the desired changes
* GitHub issue references #3369, #3378, #3330, and #3398
* We always use squash merges, so each PR has a single commit hash to include
Expand Down
Loading

0 comments on commit 71e3bfb

Please sign in to comment.