Skip to content

Commit

Permalink
fix(repo): reset git index when terminating publishing (#10689)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Jun 10, 2022
1 parent d6e0cb0 commit 049435c
Showing 1 changed file with 41 additions and 43 deletions.
84 changes: 41 additions & 43 deletions scripts/nx-release.ts
Expand Up @@ -8,6 +8,15 @@ import { join } from 'path';
import * as version from '@lerna/version/index';
import * as publish from '@lerna/publish/index';

function hideFromGitIndex(uncommittedFiles: string[]) {
execSync(`git update-index --assume-unchanged ${uncommittedFiles.join(' ')}`);

return () =>
execSync(
`git update-index --no-assume-unchanged ${uncommittedFiles.join(' ')}`
);
}

(async () => {
const options = parseArgs();
if (!options.local && !options.force) {
Expand Down Expand Up @@ -55,49 +64,38 @@ import * as publish from '@lerna/publish/index';

const lernaJsonPath = join(__dirname, '../lerna.json');
let originalLernaJson: Buffer;
let uncommittedFiles: string[];

try {
if (options.local || options.tag === 'next') {
originalLernaJson = readFileSync(lernaJsonPath);
}
if (options.local) {
/**
* Hide changes from Lerna
*/
uncommittedFiles = execSync('git diff --name-only --relative HEAD .')
.toString()
.split('\n')
.filter((i) => i.length > 0);
execSync(
`git update-index --assume-unchanged ${uncommittedFiles.join(' ')}`
);
}

const publishOptions = {
gitReset: false,
distTag: options.tag,
};

if (!options.skipPublish) {
await publish({ ...versionOptions, ...publishOptions });
} else {
await version(versionOptions);
console.warn('Not Publishing because --dryRun was passed');
}

if (options.local || options.tag === 'next') {
writeFileSync(lernaJsonPath, originalLernaJson);
}
} finally {
if (options.local) {
/**
* Unhide changes from Lerna
*/
execSync(
`git update-index --no-assume-unchanged ${uncommittedFiles.join(' ')}`
);
}

if (options.local || options.tag === 'next') {
originalLernaJson = readFileSync(lernaJsonPath);
}
if (options.local) {
/**
* Hide changes from Lerna
*/
const uncommittedFiles = execSync('git diff --name-only --relative HEAD .')
.toString()
.split('\n')
.filter((i) => i.length > 0);
const unhideFromGitIndex = hideFromGitIndex(uncommittedFiles);

process.on('exit', unhideFromGitIndex);
process.on('SIGTERM', unhideFromGitIndex);
}

const publishOptions = {
gitReset: false,
distTag: options.tag,
};

if (!options.skipPublish) {
await publish({ ...versionOptions, ...publishOptions });
} else {
await version(versionOptions);
console.warn('Not Publishing because --dryRun was passed');
}

if (options.local || options.tag === 'next') {
writeFileSync(lernaJsonPath, originalLernaJson);
}
})();

Expand Down

0 comments on commit 049435c

Please sign in to comment.