-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add "postinstall" script to workaround
npm install
/npm pack
…
…behaviour with ignorefiles
- Loading branch information
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import fs from 'node:fs'; | ||
import {stub, match, type SinonStub, restore} from 'sinon'; | ||
import anyTest, {type TestFn} from 'ava'; | ||
import {gitIgnore, npmIgnore, postinstall} from './postinstall.js'; | ||
|
||
const test = anyTest as TestFn<{existsSync: SinonStub; renameSync: SinonStub}>; | ||
test.beforeEach((t) => { | ||
t.context.existsSync = stub(fs, 'existsSync'); | ||
t.context.renameSync = stub(fs, 'renameSync'); | ||
}); | ||
test.afterEach.always(() => { | ||
restore(); | ||
}); | ||
|
||
const gitToNpm = '".gitignore" to ".npmignore"'; | ||
test.serial(`renames ${gitToNpm}`, (t) => { | ||
t.context.existsSync.withArgs(gitIgnore).returns(false); | ||
t.context.existsSync.withArgs(npmIgnore).returns(true); | ||
postinstall(); | ||
t.is(t.context.renameSync.callCount, 1); | ||
t.deepEqual(t.context.renameSync.firstCall.args, [npmIgnore, gitIgnore]); | ||
}); | ||
|
||
test.serial(`does not rename ${gitToNpm} when ".gitignore" exists`, (t) => { | ||
t.context.existsSync.withArgs(gitIgnore).returns(true); | ||
t.context.existsSync.withArgs(npmIgnore).returns(true); | ||
postinstall(); | ||
t.is(t.context.renameSync.callCount, 0); | ||
}); | ||
|
||
test.serial(`does not rename ${gitToNpm} without ".npmignore"`, (t) => { | ||
t.context.existsSync.withArgs(gitIgnore).returns(false); | ||
t.context.existsSync.withArgs(npmIgnore).returns(false); | ||
postinstall(); | ||
t.is(t.context.renameSync.callCount, 0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {join} from 'node:path'; | ||
import fs from 'node:fs'; | ||
import {fileURLToPath} from 'node:url'; | ||
import {argv} from 'node:process'; | ||
import rootPath from '../root-path.js'; | ||
|
||
const gitIgnore = join(rootPath, '.gitignore'); | ||
const npmIgnore = join(rootPath, '.npmignore'); | ||
|
||
/** | ||
* Workaround for [Rename `.gitignore` to `.npmignore` in package if no | ||
* `.npmignore` found](https://github.com/npm/npm/issues/1862) and ['npm pack'/ | ||
* `publish` option to not rename or keep a copy of `.gitignore` files]( | ||
* https://github.com/npm/npm/issues/7252) issues. With npm v9 or newer, the | ||
* `npm pack` includes the `.gitignore` in the tarball and `npm install` renames | ||
* the file to `.npmignore`. This script simply reverts that rename if it has | ||
* occurred. | ||
*/ | ||
function postinstall(): void { | ||
if (fs.existsSync(npmIgnore) && !fs.existsSync(gitIgnore)) { | ||
fs.renameSync(npmIgnore, gitIgnore); | ||
} | ||
} | ||
|
||
if ( | ||
import.meta.url.startsWith('file:') && | ||
fileURLToPath(import.meta.url) === argv.at(1) | ||
) { | ||
postinstall(); | ||
} | ||
|
||
export {gitIgnore, npmIgnore, postinstall}; |